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_interface_add_del_output_feature_reply) \
68 _(nat44_add_del_static_mapping_reply) \
69 _(nat_set_workers_reply) \
70 _(nat44_add_del_interface_addr_reply) \
71 _(nat_ipfix_enable_disable_reply) \
72 _(nat_det_add_del_map_reply) \
73 _(nat_set_timeouts_reply) \
74 _(nat_det_close_session_out_reply) \
75 _(nat_det_close_session_in_reply)
78 static void vl_api_##n##_t_handler \
79 (vl_api_##n##_t * mp) \
81 vat_main_t * vam = snat_test_main.vat_main; \
82 i32 retval = ntohl(mp->retval); \
83 if (vam->async_mode) { \
84 vam->async_errors += (retval < 0); \
86 vam->retval = retval; \
87 vam->result_ready = 1; \
90 foreach_standard_reply_retval_handler;
94 * Table of message reply handlers, must include boilerplate handlers
97 #define foreach_vpe_api_reply_msg \
98 _(NAT44_ADD_DEL_ADDRESS_RANGE_REPLY, \
99 nat44_add_del_address_range_reply) \
100 _(NAT44_INTERFACE_ADD_DEL_FEATURE_REPLY, \
101 nat44_interface_add_del_feature_reply) \
102 _(NAT44_INTERFACE_ADD_DEL_OUTPUT_FEATURE_REPLY, \
103 nat44_interface_add_del_output_feature_reply) \
104 _(NAT44_ADD_DEL_STATIC_MAPPING_REPLY, \
105 nat44_add_del_static_mapping_reply) \
106 _(NAT_CONTROL_PING_REPLY, nat_control_ping_reply) \
107 _(NAT44_STATIC_MAPPING_DETAILS, nat44_static_mapping_details) \
108 _(NAT_SHOW_CONFIG_REPLY, nat_show_config_reply) \
109 _(NAT44_ADDRESS_DETAILS, nat44_address_details) \
110 _(NAT44_INTERFACE_DETAILS, nat44_interface_details) \
111 _(NAT_SET_WORKERS_REPLY, nat_set_workers_reply) \
112 _(NAT_WORKER_DETAILS, nat_worker_details) \
113 _(NAT44_ADD_DEL_INTERFACE_ADDR_REPLY, \
114 nat44_add_del_interface_addr_reply) \
115 _(NAT44_INTERFACE_ADDR_DETAILS, nat44_interface_addr_details) \
116 _(NAT_IPFIX_ENABLE_DISABLE_REPLY, \
117 nat_ipfix_enable_disable_reply) \
118 _(NAT44_USER_DETAILS, nat44_user_details) \
119 _(NAT44_USER_SESSION_DETAILS, nat44_user_session_details) \
120 _(NAT_DET_ADD_DEL_MAP_REPLY, nat_det_add_del_map_reply) \
121 _(NAT_DET_FORWARD_REPLY, nat_det_forward_reply) \
122 _(NAT_DET_REVERSE_REPLY, nat_det_reverse_reply) \
123 _(NAT_DET_MAP_DETAILS, nat_det_map_details) \
124 _(NAT_SET_TIMEOUTS_REPLY, nat_set_timeouts_reply) \
125 _(NAT_GET_TIMEOUTS_REPLY, nat_get_timeouts_reply) \
126 _(NAT_DET_CLOSE_SESSION_OUT_REPLY, \
127 nat_det_close_session_out_reply) \
128 _(NAT_DET_CLOSE_SESSION_IN_REPLY, \
129 nat_det_close_session_in_reply) \
130 _(NAT_DET_SESSION_DETAILS, nat_det_session_details)
132 static int api_nat44_add_del_address_range (vat_main_t * vam)
134 unformat_input_t * i = vam->input;
135 ip4_address_t start_addr, end_addr;
136 u32 start_host_order, end_host_order;
137 vl_api_nat44_add_del_address_range_t * mp;
144 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
146 if (unformat (i, "%U - %U",
147 unformat_ip4_address, &start_addr,
148 unformat_ip4_address, &end_addr))
150 else if (unformat (i, "%U", unformat_ip4_address, &start_addr))
151 end_addr = start_addr;
152 else if (unformat (i, "twice-nat"))
154 else if (unformat (i, "vrf %u", &vrf_id))
156 else if (unformat (i, "del"))
160 clib_warning("unknown input '%U'", format_unformat_error, i);
165 start_host_order = clib_host_to_net_u32 (start_addr.as_u32);
166 end_host_order = clib_host_to_net_u32 (end_addr.as_u32);
168 if (end_host_order < start_host_order)
170 errmsg ("end address less than start address\n");
174 count = (end_host_order - start_host_order) + 1;
178 errmsg ("%U - %U, %d addresses...\n",
179 format_ip4_address, &start_addr,
180 format_ip4_address, &end_addr,
184 M(NAT44_ADD_DEL_ADDRESS_RANGE, mp);
186 memcpy (mp->first_ip_address, &start_addr, 4);
187 memcpy (mp->last_ip_address, &end_addr, 4);
190 mp->flags = (vl_api_nat_config_flags_t)NAT_API_IS_TWICE_NAT;
198 static int api_nat44_interface_add_del_feature (vat_main_t * vam)
200 unformat_input_t * i = vam->input;
201 vl_api_nat44_interface_add_del_feature_t * mp;
203 u8 sw_if_index_set = 0;
208 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
210 if (unformat (i, "%U", unformat_sw_if_index, vam, &sw_if_index))
212 else if (unformat (i, "sw_if_index %d", &sw_if_index))
214 else if (unformat (i, "out"))
216 else if (unformat (i, "in"))
218 else if (unformat (i, "del"))
222 clib_warning("unknown input '%U'", format_unformat_error, i);
227 if (sw_if_index_set == 0)
229 errmsg ("interface / sw_if_index required\n");
233 M(NAT44_INTERFACE_ADD_DEL_FEATURE, mp);
234 mp->sw_if_index = ntohl(sw_if_index);
237 mp->flags |= NAT_API_IS_INSIDE;
244 static int api_nat44_interface_add_del_output_feature (vat_main_t * vam)
246 unformat_input_t * i = vam->input;
247 vl_api_nat44_interface_add_del_output_feature_t * mp;
249 u8 sw_if_index_set = 0;
254 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
256 if (unformat (i, "%U", unformat_sw_if_index, vam, &sw_if_index))
258 else if (unformat (i, "sw_if_index %d", &sw_if_index))
260 else if (unformat (i, "out"))
262 else if (unformat (i, "in"))
264 else if (unformat (i, "del"))
268 clib_warning("unknown input '%U'", format_unformat_error, i);
273 if (sw_if_index_set == 0)
275 errmsg ("interface / sw_if_index required\n");
279 M(NAT44_INTERFACE_ADD_DEL_OUTPUT_FEATURE, mp);
280 mp->sw_if_index = ntohl(sw_if_index);
283 mp->flags |= NAT_API_IS_INSIDE;
290 static int api_nat44_add_del_static_mapping(vat_main_t * vam)
292 unformat_input_t * i = vam->input;
293 vl_api_nat44_add_del_static_mapping_t * mp;
294 u8 external_addr_set = 0;
295 u8 local_addr_set = 0;
298 ip4_address_t local_addr, external_addr;
299 u32 local_port = 0, external_port = 0, vrf_id = ~0;
300 u32 sw_if_index = ~0;
301 u8 sw_if_index_set = 0;
306 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
308 if (unformat (i, "local_addr %U", unformat_ip4_address, &local_addr))
310 else if (unformat (i, "external_addr %U", unformat_ip4_address,
312 external_addr_set = 1;
313 else if (unformat (i, "local_port %u", &local_port))
315 else if (unformat (i, "external_port %u", &external_port))
317 else if (unformat (i, "external_if %U", unformat_sw_if_index, vam,
320 else if (unformat (i, "external_sw_if_index %d", &sw_if_index))
322 else if (unformat (i, "vrf %u", &vrf_id))
324 else if (unformat (i, "protocol %u", &proto))
326 else if (unformat (i, "del"))
330 clib_warning("unknown input '%U'", format_unformat_error, i);
335 if (!addr_only && !proto_set)
337 errmsg ("protocol required\n");
343 errmsg ("local addr required\n");
346 if (!external_addr_set && !sw_if_index_set)
348 errmsg ("external addr or interface required\n");
352 M(NAT44_ADD_DEL_STATIC_MAPPING, mp);
356 mp->flags |= NAT_API_IS_ADDR_ONLY;
358 mp->local_port = ntohs ((u16) local_port);
359 mp->external_port = ntohs ((u16) external_port);
360 mp->external_sw_if_index = ntohl (sw_if_index);
361 mp->vrf_id = ntohl (vrf_id);
362 mp->protocol = (u8) proto;
363 memcpy (mp->local_ip_address, &local_addr, 4);
364 memcpy (mp->external_ip_address, &external_addr, 4);
371 static void vl_api_nat_control_ping_reply_t_handler
372 (vl_api_nat_control_ping_reply_t * mp)
374 vat_main_t *vam = &vat_main;
375 i32 retval = ntohl (mp->retval);
378 vam->async_errors += (retval < 0);
382 vam->retval = retval;
383 vam->result_ready = 1;
387 static void vl_api_nat44_static_mapping_details_t_handler
388 (vl_api_nat44_static_mapping_details_t *mp)
390 snat_test_main_t * sm = &snat_test_main;
391 vat_main_t *vam = sm->vat_main;
392 nat_config_flags_t flags = (nat_config_flags_t) mp->flags;
394 if (flags & NAT_API_IS_ADDR_ONLY && mp->external_sw_if_index != ~0)
395 fformat (vam->ofp, "%15U%6s%15d%6s%11d%6d\n",
396 format_ip4_address, &mp->local_ip_address, "",
397 ntohl (mp->external_sw_if_index), "",
400 else if (flags & NAT_API_IS_ADDR_ONLY && mp->external_sw_if_index == ~0)
401 fformat (vam->ofp, "%15U%6s%15U%6s%11d%6d\n",
402 format_ip4_address, &mp->local_ip_address, "",
403 format_ip4_address, &mp->external_ip_address, "",
406 else if (!(flags & NAT_API_IS_ADDR_ONLY) && mp->external_sw_if_index != ~0)
407 fformat (vam->ofp, "%15U%6d%15d%6d%11d%6d\n",
408 format_ip4_address, &mp->local_ip_address,
409 ntohs (mp->local_port),
410 ntohl (mp->external_sw_if_index),
411 ntohs (mp->external_port),
415 fformat (vam->ofp, "%15U%6d%15U%6d%11d%6d\n",
416 format_ip4_address, &mp->local_ip_address,
417 ntohs (mp->local_port),
418 format_ip4_address, &mp->external_ip_address,
419 ntohs (mp->external_port),
425 static int api_nat44_static_mapping_dump(vat_main_t * vam)
427 vl_api_nat44_static_mapping_dump_t * mp;
428 vl_api_nat_control_ping_t *mp_ping;
431 if (vam->json_output)
433 clib_warning ("JSON output not supported for nat44_static_mapping_dump");
437 fformat (vam->ofp, "%21s%21s\n", "local", "external");
438 fformat (vam->ofp, "%15s%6s%15s%6s%11s%6s\n", "address", "port",
439 "address/if_idx", "port", "vrf", "proto");
441 M(NAT44_STATIC_MAPPING_DUMP, mp);
444 /* Use a control ping for synchronization */
445 M(NAT_CONTROL_PING, mp_ping);
452 static void vl_api_nat_show_config_reply_t_handler
453 (vl_api_nat_show_config_reply_t *mp)
455 snat_test_main_t * sm = &snat_test_main;
456 vat_main_t *vam = sm->vat_main;
457 i32 retval = ntohl (mp->retval);
461 fformat (vam->ofp, "translation hash buckets %d\n",
462 ntohl (mp->translation_buckets));
463 fformat (vam->ofp, "translation hash memory %d\n",
464 ntohl (mp->translation_memory_size));
465 fformat (vam->ofp, "user hash buckets %d\n", ntohl (mp->user_buckets));
466 fformat (vam->ofp, "user hash memory %d\n", ntohl (mp->user_memory_size));
467 fformat (vam->ofp, "max translations per user %d\n",
468 ntohl (mp->max_translations_per_user));
469 fformat (vam->ofp, "outside VRF id %d\n", ntohl (mp->outside_vrf_id));
470 fformat (vam->ofp, "inside VRF id %d\n", ntohl (mp->inside_vrf_id));
471 if (mp->static_mapping_only)
473 fformat (vam->ofp, "static mapping only");
474 if (mp->static_mapping_connection_tracking)
475 fformat (vam->ofp, " connection tracking");
476 fformat (vam->ofp, "\n");
479 vam->retval = retval;
480 vam->result_ready = 1;
483 static int api_nat_show_config(vat_main_t * vam)
485 vl_api_nat_show_config_t * mp;
488 if (vam->json_output)
490 clib_warning ("JSON output not supported for nat_show_config");
494 M(NAT_SHOW_CONFIG, mp);
500 static void vl_api_nat44_address_details_t_handler
501 (vl_api_nat44_address_details_t *mp)
503 snat_test_main_t * sm = &snat_test_main;
504 vat_main_t *vam = sm->vat_main;
506 fformat (vam->ofp, "%U\n", format_ip4_address, &mp->ip_address);
509 static int api_nat44_address_dump(vat_main_t * vam)
511 vl_api_nat44_address_dump_t * mp;
512 vl_api_nat_control_ping_t *mp_ping;
515 if (vam->json_output)
517 clib_warning ("JSON output not supported for nat44_address_dump");
521 M(NAT44_ADDRESS_DUMP, mp);
524 /* Use a control ping for synchronization */
525 M(NAT_CONTROL_PING, mp_ping);
532 static void vl_api_nat44_interface_details_t_handler
533 (vl_api_nat44_interface_details_t *mp)
535 nat_config_flags_t flags = (nat_config_flags_t) mp->flags;
536 snat_test_main_t * sm = &snat_test_main;
537 vat_main_t *vam = sm->vat_main;
539 if ((flags & NAT_API_IS_INSIDE) && (flags & NAT_API_IS_OUTSIDE))
541 fformat (vam->ofp, "sw_if_index %d in & out\n", ntohl (mp->sw_if_index));
545 fformat (vam->ofp, "sw_if_index %d %s\n", ntohl (mp->sw_if_index),
546 flags & NAT_API_IS_INSIDE ? "in" : "out");
550 static int api_nat44_interface_dump(vat_main_t * vam)
552 vl_api_nat44_interface_dump_t * mp;
553 vl_api_nat_control_ping_t *mp_ping;
556 if (vam->json_output)
558 clib_warning ("JSON output not supported for nat44_address_dump");
562 M(NAT44_INTERFACE_DUMP, mp);
565 /* Use a control ping for synchronization */
566 M(NAT_CONTROL_PING, mp_ping);
573 static int api_nat_set_workers (vat_main_t * vam)
575 unformat_input_t * i = vam->input;
576 vl_api_nat_set_workers_t * mp;
580 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
582 if (unformat (i, "%U", unformat_bitmap_list, &bitmap))
586 clib_warning("unknown input '%U'", format_unformat_error, i);
591 M(NAT_SET_WORKERS, mp);
592 mp->worker_mask = clib_host_to_net_u64 (bitmap[0]);
599 static void vl_api_nat_worker_details_t_handler
600 (vl_api_nat_worker_details_t *mp)
602 snat_test_main_t * sm = &snat_test_main;
603 vat_main_t *vam = sm->vat_main;
605 fformat (vam->ofp, "worker_index %d (%s at lcore %u)\n",
606 ntohl (mp->worker_index), mp->name, ntohl (mp->lcore_id));
609 static int api_nat_worker_dump(vat_main_t * vam)
611 vl_api_nat_worker_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 nat_address_dump");
621 M(NAT_WORKER_DUMP, mp);
624 /* Use a control ping for synchronization */
625 M(NAT_CONTROL_PING, mp_ping);
632 static int api_nat44_add_del_interface_addr (vat_main_t * vam)
634 unformat_input_t * i = vam->input;
635 vl_api_nat44_add_del_interface_addr_t * mp;
637 u8 sw_if_index_set = 0;
642 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
644 if (unformat (i, "%U", unformat_sw_if_index, vam, &sw_if_index))
646 else if (unformat (i, "sw_if_index %d", &sw_if_index))
648 else if (unformat (i, "twice-nat"))
650 else if (unformat (i, "del"))
654 clib_warning("unknown input '%U'", format_unformat_error, i);
659 if (sw_if_index_set == 0)
661 errmsg ("interface / sw_if_index required\n");
665 M(NAT44_ADD_DEL_INTERFACE_ADDR, mp);
666 mp->sw_if_index = ntohl(sw_if_index);
668 mp->flags = (vl_api_nat_config_flags_t)NAT_API_IS_TWICE_NAT;
676 static void vl_api_nat44_interface_addr_details_t_handler
677 (vl_api_nat44_interface_addr_details_t *mp)
679 snat_test_main_t * sm = &snat_test_main;
680 vat_main_t *vam = sm->vat_main;
682 fformat (vam->ofp, "sw_if_index %d\n", ntohl (mp->sw_if_index));
685 static int api_nat44_interface_addr_dump(vat_main_t * vam)
687 vl_api_nat44_interface_addr_dump_t * mp;
688 vl_api_nat_control_ping_t *mp_ping;
691 if (vam->json_output)
693 clib_warning ("JSON output not supported for nat44_address_dump");
697 M(NAT44_INTERFACE_ADDR_DUMP, mp);
700 /* Use a control ping for synchronization */
701 M(NAT_CONTROL_PING, mp_ping);
708 static int api_nat_ipfix_enable_disable (vat_main_t * vam)
710 unformat_input_t * i = vam->input;
711 vl_api_nat_ipfix_enable_disable_t * mp;
717 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
719 if (unformat (i, "domain %d", &domain_id))
721 else if (unformat (i, "src_port %d", &src_port))
723 else if (unformat (i, "disable"))
727 clib_warning("unknown input '%U'", format_unformat_error, i);
732 M(NAT_IPFIX_ENABLE_DISABLE, mp);
733 mp->domain_id = htonl(domain_id);
734 mp->src_port = htons((u16) src_port);
742 static void vl_api_nat44_user_session_details_t_handler
743 (vl_api_nat44_user_session_details_t *mp)
745 snat_test_main_t * sm = &snat_test_main;
746 vat_main_t *vam = sm->vat_main;
748 fformat(vam->ofp, "%s session %U:%d to %U:%d protocol id %d "
749 "total packets %d total bytes %lld\n",
750 mp->flags & NAT_API_IS_STATIC ? "static" : "dynamic",
751 format_ip4_address, mp->inside_ip_address, ntohs(mp->inside_port),
752 format_ip4_address, mp->outside_ip_address, ntohs(mp->outside_port),
753 ntohs(mp->protocol), ntohl(mp->total_pkts),
754 clib_net_to_host_u64(mp->total_bytes));
757 static int api_nat44_user_session_dump(vat_main_t * vam)
759 unformat_input_t* i = vam->input;
760 vl_api_nat44_user_session_dump_t * mp;
761 vl_api_nat_control_ping_t *mp_ping;
766 if (vam->json_output)
768 clib_warning ("JSON output not supported for nat44_address_dump");
772 if (unformat (i, "ip_address %U vrf_id %d",
773 unformat_ip4_address, &addr, &vrf_id))
777 clib_warning("unknown input '%U'", format_unformat_error, i);
781 M(NAT44_USER_SESSION_DUMP, mp);
784 /* Use a control ping for synchronization */
785 M(NAT_CONTROL_PING, mp_ping);
786 clib_memset(mp->ip_address, 0, 16);
787 clib_memcpy(mp->ip_address, &addr, 4);
788 mp->vrf_id = htonl(vrf_id);
795 static void vl_api_nat44_user_details_t_handler
796 (vl_api_nat44_user_details_t *mp)
798 snat_test_main_t * sm = &snat_test_main;
799 vat_main_t *vam = sm->vat_main;
801 fformat(vam->ofp, "user with ip %U with vrf_id %d "
802 "with %d sessions and %d static sessions\n",
803 format_ip4_address, mp->ip_address, ntohl(mp->vrf_id),
804 ntohl(mp->nsessions), ntohl(mp->nstaticsessions));
807 static int api_nat44_user_dump(vat_main_t * vam)
809 vl_api_nat44_user_dump_t * mp;
810 vl_api_nat_control_ping_t *mp_ping;
813 if (vam->json_output)
815 clib_warning ("JSON output not supported for nat44_address_dump");
819 M(NAT44_USER_DUMP, mp);
822 /* Use a control ping for synchronization */
823 M(NAT_CONTROL_PING, mp_ping);
830 static int api_nat_det_add_del_map (vat_main_t * vam)
832 unformat_input_t * i = vam->input;
833 vl_api_nat_det_add_del_map_t * mp;
834 ip4_address_t in_addr, out_addr;
835 u32 in_plen, out_plen;
839 if (unformat (i, "in %U/%d out %U/%d",
840 unformat_ip4_address, &in_addr, &in_plen,
841 unformat_ip4_address, &out_addr, &out_plen))
843 else if (unformat (i, "del"))
847 clib_warning("unknown input '%U'", format_unformat_error, i);
851 M(NAT_DET_ADD_DEL_MAP, mp);
852 clib_memcpy(mp->in_addr, &in_addr, 4);
853 mp->in_plen = in_plen;
854 clib_memcpy(mp->out_addr, &out_addr, 4);
855 mp->out_plen = out_plen;
863 static void vl_api_nat_det_forward_reply_t_handler
864 (vl_api_nat_det_forward_reply_t *mp)
866 snat_test_main_t * sm = &snat_test_main;
867 vat_main_t *vam = sm->vat_main;
868 i32 retval = ntohl(mp->retval);
872 fformat (vam->ofp, "outside address %U", format_ip4_address, &mp->out_addr);
873 fformat (vam->ofp, " outside port range start %d", ntohs(mp->out_port_lo));
874 fformat (vam->ofp, " outside port range end %d\n", ntohs(mp->out_port_hi));
877 vam->retval = retval;
878 vam->result_ready = 1;
881 static int api_nat_det_forward (vat_main_t * vam)
883 unformat_input_t * i = vam->input;
884 vl_api_nat_det_forward_t * mp;
885 ip4_address_t in_addr;
888 if (unformat (i, "%U", unformat_ip4_address, &in_addr))
892 clib_warning("unknown input '%U'", format_unformat_error, i);
896 M(NAT_DET_FORWARD, mp);
897 clib_memcpy(mp->in_addr, &in_addr, 4);
904 static void vl_api_nat_det_reverse_reply_t_handler
905 (vl_api_nat_det_reverse_reply_t *mp)
907 snat_test_main_t * sm = &snat_test_main;
908 vat_main_t *vam = sm->vat_main;
909 i32 retval = ntohl(mp->retval);
913 fformat (vam->ofp, "inside address %U\n", format_ip4_address, &mp->in_addr);
916 vam->retval = retval;
917 vam->result_ready = 1;
920 static int api_nat_det_reverse (vat_main_t * vam)
922 unformat_input_t * i = vam->input;
923 vl_api_nat_det_reverse_t * mp;
924 ip4_address_t out_addr;
928 if (unformat (i, "%U %d", unformat_ip4_address, &out_addr, &out_port))
932 clib_warning("unknown input '%U'", format_unformat_error, i);
936 M(NAT_DET_REVERSE, mp);
937 clib_memcpy(mp->out_addr, &out_addr, 4);
938 mp->out_port = htons((u16)out_port);
945 static void vl_api_nat_det_map_details_t_handler
946 (vl_api_nat_det_map_details_t *mp)
948 snat_test_main_t * sm = &snat_test_main;
949 vat_main_t *vam = sm->vat_main;
951 fformat (vam->ofp, "Deterministic S-NAT mapping in %U/%d out %U/%d "
952 "ports per host %d sharing ratio %d "
953 "number of sessions %d",
954 format_ip4_address, mp->in_addr, mp->in_plen,
955 format_ip4_address, mp->out_addr, mp->out_plen,
956 ntohs(mp->ports_per_host), ntohl(mp->sharing_ratio),
960 static int api_nat_det_map_dump(vat_main_t * vam)
962 vl_api_nat_det_map_dump_t * mp;
963 vl_api_nat_control_ping_t *mp_ping;
966 if (vam->json_output)
968 clib_warning ("JSON output not supported for nat_det_map_dump");
972 M(NAT_DET_MAP_DUMP, mp);
975 /* Use a control ping for synchronization */
976 M(NAT_CONTROL_PING, mp_ping);
983 static int api_nat_set_timeouts (vat_main_t * vam)
985 unformat_input_t * i = vam->input;
986 vl_api_nat_set_timeouts_t * mp;
987 u32 udp = SNAT_UDP_TIMEOUT;
988 u32 tcp_established = SNAT_TCP_ESTABLISHED_TIMEOUT;
989 u32 tcp_transitory = SNAT_TCP_TRANSITORY_TIMEOUT;
990 u32 icmp = SNAT_ICMP_TIMEOUT;
993 if (unformat (i, "udp %d", &udp))
995 else if (unformat (i, "tcp_established %d", &tcp_established))
997 else if (unformat (i, "tcp_transitory %d", &tcp_transitory))
999 else if (unformat (i, "icmp %d", &icmp))
1003 clib_warning("unknown input '%U'", format_unformat_error, i);
1007 M(NAT_SET_TIMEOUTS, mp);
1008 mp->udp = htonl(udp);
1009 mp->tcp_established = htonl(tcp_established);
1010 mp->tcp_transitory = htonl(tcp_transitory);
1011 mp->icmp = htonl(icmp);
1018 static void vl_api_nat_get_timeouts_reply_t_handler
1019 (vl_api_nat_get_timeouts_reply_t *mp)
1021 snat_test_main_t * sm = &snat_test_main;
1022 vat_main_t *vam = sm->vat_main;
1023 i32 retval = ntohl (mp->retval);
1027 fformat (vam->ofp, "udp timeout: %dsec\n", ntohl (mp->udp));
1028 fformat (vam->ofp, "tcp-established timeout: %dsec",
1029 ntohl (mp->tcp_established));
1030 fformat (vam->ofp, "tcp-transitory timeout: %dsec",
1031 ntohl (mp->tcp_transitory));
1032 fformat (vam->ofp, "icmp timeout: %dsec", ntohl (mp->icmp));
1034 vam->retval = retval;
1035 vam->result_ready = 1;
1038 static int api_nat_get_timeouts(vat_main_t * vam)
1040 vl_api_nat_get_timeouts_t * mp;
1043 if (vam->json_output)
1045 clib_warning ("JSON output not supported for nat_get_timeouts");
1049 M(NAT_GET_TIMEOUTS, mp);
1055 static int api_nat_det_close_session_out (vat_main_t * vam)
1057 unformat_input_t * i = vam->input;
1058 vl_api_nat_det_close_session_out_t * mp;
1059 ip4_address_t out_addr, ext_addr;
1060 u32 out_port, ext_port;
1063 if (unformat (i, "%U:%d %U:%d",
1064 unformat_ip4_address, &out_addr, &out_port,
1065 unformat_ip4_address, &ext_addr, &ext_port))
1069 clib_warning("unknown input '%U'", format_unformat_error, i);
1073 M(NAT_DET_CLOSE_SESSION_OUT, mp);
1074 clib_memcpy(mp->out_addr, &out_addr, 4);
1075 mp->out_port = ntohs((u16)out_port);
1076 clib_memcpy(mp->ext_addr, &ext_addr, 4);
1077 mp->ext_port = ntohs((u16)ext_port);
1084 static int api_nat_det_close_session_in (vat_main_t * vam)
1086 unformat_input_t * i = vam->input;
1087 vl_api_nat_det_close_session_in_t * mp;
1088 ip4_address_t in_addr, ext_addr;
1089 u32 in_port, ext_port;
1092 if (unformat (i, "%U:%d %U:%d",
1093 unformat_ip4_address, &in_addr, &in_port,
1094 unformat_ip4_address, &ext_addr, &ext_port))
1098 clib_warning("unknown input '%U'", format_unformat_error, i);
1102 M(NAT_DET_CLOSE_SESSION_IN, mp);
1103 clib_memcpy(mp->in_addr, &in_addr, 4);
1104 mp->in_port = ntohs((u16)in_port);
1105 clib_memcpy(mp->ext_addr, &ext_addr, 4);
1106 mp->ext_port = ntohs((u16)ext_port);
1113 static void vl_api_nat_det_session_details_t_handler
1114 (vl_api_nat_det_session_details_t *mp)
1116 snat_test_main_t * sm = &snat_test_main;
1117 vat_main_t *vam = sm->vat_main;
1119 fformat(vam->ofp, "deterministic session, external host address %U, "
1120 "external host port %d, outer port %d, inside port %d",
1121 format_ip4_address, mp->ext_addr, mp->ext_port,
1122 mp->out_port, mp->in_port);
1125 static int api_nat_det_session_dump(vat_main_t * vam)
1127 unformat_input_t* i = vam->input;
1128 vl_api_nat_det_session_dump_t * mp;
1129 vl_api_nat_control_ping_t *mp_ping;
1130 ip4_address_t user_addr;
1133 if (vam->json_output)
1135 clib_warning ("JSON output not supported for nat_det_session_dump");
1139 if (unformat (i, "user_addr %U", unformat_ip4_address, &user_addr))
1143 clib_warning ("unknown input '%U'", format_unformat_error, i);
1147 M(NAT_DET_SESSION_DUMP, mp);
1148 clib_memcpy (&mp->user_addr, &user_addr, 4);
1151 /* Use a control ping for synchronization */
1152 M(NAT_CONTROL_PING, mp_ping);
1160 * List of messages that the api test plugin sends,
1161 * and that the data plane plugin processes
1163 #define foreach_vpe_api_msg \
1164 _(nat44_add_del_address_range, \
1165 "<start-addr> [- <end-addr>] [vrf <table-id>] [twice-nat] [del]") \
1166 _(nat44_interface_add_del_feature, \
1167 "<intfc> | sw_if_index <id> [in] [out] [del]") \
1168 _(nat44_interface_add_del_output_feature, \
1169 "<intfc> | sw_if_index <id> [in] [out] [del]") \
1170 _(nat44_add_del_static_mapping, "local_addr <ip>" \
1171 " (external_addr <ip> | external_if <intfc> |" \
1172 " external_sw_if_ndex <id>) [local_port <n>]" \
1173 " [external_port <n>] [vrf <table-id>] [del] protocol <n>") \
1174 _(nat_set_workers, "<wokrers_bitmap>") \
1175 _(nat44_static_mapping_dump, "") \
1176 _(nat_show_config, "") \
1177 _(nat44_address_dump, "") \
1178 _(nat44_interface_dump, "") \
1179 _(nat_worker_dump, "") \
1180 _(nat44_add_del_interface_addr, \
1181 "<intfc> | sw_if_index <id> [twice-nat] [del]") \
1182 _(nat44_interface_addr_dump, "") \
1183 _(nat_ipfix_enable_disable, "[domain <id>] [src_port <n>] " \
1185 _(nat44_user_dump, "") \
1186 _(nat44_user_session_dump, "ip_address <ip> vrf_id <table-id>") \
1187 _(nat_det_add_del_map, "in <in_addr>/<in_plen> out " \
1188 "<out_addr>/<out_plen> [del]") \
1189 _(nat_det_forward, "<in_addr>") \
1190 _(nat_det_reverse, "<out_addr> <out_port>") \
1191 _(nat_det_map_dump, "") \
1192 _(nat_set_timeouts, "[udp <sec> | tcp_established <sec> | " \
1193 "tcp_transitory <sec> | icmp <sec>]") \
1194 _(nat_get_timeouts, "") \
1195 _(nat_det_close_session_out, "<out_addr>:<out_port> " \
1196 "<ext_addr>:<ext_port>") \
1197 _(nat_det_close_session_in, "<in_addr>:<in_port> " \
1198 "<out_addr>:<out_port>") \
1199 _(nat_det_session_dump, "ip_address <user_addr>")
1202 snat_vat_api_hookup (vat_main_t *vam)
1204 snat_test_main_t * sm __attribute__((unused)) = &snat_test_main;
1205 /* Hook up handlers for replies from the data plane plug-in */
1207 vl_msg_api_set_handlers((VL_API_##N + sm->msg_id_base), \
1209 vl_api_##n##_t_handler, \
1211 vl_api_##n##_t_endian, \
1212 vl_api_##n##_t_print, \
1213 sizeof(vl_api_##n##_t), 1);
1214 foreach_vpe_api_reply_msg;
1217 /* API messages we can send */
1219 hash_set_mem (vam->function_by_name, #n, api_##n);
1220 foreach_vpe_api_msg;
1224 #define _(n,h) hash_set_mem (vam->help_by_name, #n, h);
1225 foreach_vpe_api_msg;
1229 clib_error_t * vat_plugin_register (vat_main_t *vam)
1231 snat_test_main_t * sm = &snat_test_main;
1236 /* Ask the vpp engine for the first assigned message-id */
1237 name = format (0, "nat_%08x%c", api_version, 0);
1238 sm->msg_id_base = vl_client_get_first_plugin_msg_id ((char *) name);
1241 if (sm->msg_id_base != (u16) ~0)
1242 snat_vat_api_hookup (vam);
1244 return clib_error_return (0, "nat plugin not loaded...");