3 * snat.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>
21 #include <vlibsocket/api.h>
22 #include <vppinfra/error.h>
23 #include <vnet/ip/ip.h>
25 #define __plugin_msg_base snat_test_main.msg_id_base
26 #include <vlibapi/vat_helper_macros.h>
28 uword unformat_sw_if_index (unformat_input_t * input, va_list * args);
30 /* Declare message IDs */
31 #include <snat/snat_msg_enum.h>
33 /* define message structures */
35 #include <snat/snat_all_api_h.h>
38 /* declare message handlers for each api */
40 #define vl_endianfun /* define message structures */
41 #include <snat/snat_all_api_h.h>
44 /* instantiate all the print functions we know about */
45 #define vl_print(handle, ...)
47 #include <snat/snat_all_api_h.h>
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>
56 /* API message ID base */
61 snat_test_main_t snat_test_main;
63 #define foreach_standard_reply_retval_handler \
64 _(snat_add_address_range_reply) \
65 _(snat_interface_add_del_feature_reply) \
66 _(snat_add_static_mapping_reply) \
67 _(snat_set_workers_reply) \
68 _(snat_add_del_interface_addr_reply) \
69 _(snat_ipfix_enable_disable_reply)
72 static void vl_api_##n##_t_handler \
73 (vl_api_##n##_t * mp) \
75 vat_main_t * vam = snat_test_main.vat_main; \
76 i32 retval = ntohl(mp->retval); \
77 if (vam->async_mode) { \
78 vam->async_errors += (retval < 0); \
80 vam->retval = retval; \
81 vam->result_ready = 1; \
84 foreach_standard_reply_retval_handler;
88 * Table of message reply handlers, must include boilerplate handlers
91 #define foreach_vpe_api_reply_msg \
92 _(SNAT_ADD_ADDRESS_RANGE_REPLY, snat_add_address_range_reply) \
93 _(SNAT_INTERFACE_ADD_DEL_FEATURE_REPLY, \
94 snat_interface_add_del_feature_reply) \
95 _(SNAT_ADD_STATIC_MAPPING_REPLY, snat_add_static_mapping_reply) \
96 _(SNAT_CONTROL_PING_REPLY, snat_control_ping_reply) \
97 _(SNAT_STATIC_MAPPING_DETAILS, snat_static_mapping_details) \
98 _(SNAT_SHOW_CONFIG_REPLY, snat_show_config_reply) \
99 _(SNAT_ADDRESS_DETAILS, snat_address_details) \
100 _(SNAT_INTERFACE_DETAILS, snat_interface_details) \
101 _(SNAT_SET_WORKERS_REPLY, snat_set_workers_reply) \
102 _(SNAT_WORKER_DETAILS, snat_worker_details) \
103 _(SNAT_ADD_DEL_INTERFACE_ADDR_REPLY, \
104 snat_add_del_interface_addr_reply) \
105 _(SNAT_INTERFACE_ADDR_DETAILS, snat_interface_addr_details) \
106 _(SNAT_IPFIX_ENABLE_DISABLE_REPLY, \
107 snat_ipfix_enable_disable_reply)
109 static int api_snat_add_address_range (vat_main_t * vam)
111 unformat_input_t * i = vam->input;
112 ip4_address_t start_addr, end_addr;
113 u32 start_host_order, end_host_order;
114 vl_api_snat_add_address_range_t * mp;
119 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
121 if (unformat (i, "%U - %U",
122 unformat_ip4_address, &start_addr,
123 unformat_ip4_address, &end_addr))
125 else if (unformat (i, "%U", unformat_ip4_address, &start_addr))
126 end_addr = start_addr;
127 else if (unformat (i, "del"))
131 clib_warning("unknown input '%U'", format_unformat_error, i);
136 start_host_order = clib_host_to_net_u32 (start_addr.as_u32);
137 end_host_order = clib_host_to_net_u32 (end_addr.as_u32);
139 if (end_host_order < start_host_order)
141 errmsg ("end address less than start address\n");
145 count = (end_host_order - start_host_order) + 1;
149 errmsg ("%U - %U, %d addresses...\n",
150 format_ip4_address, &start_addr,
151 format_ip4_address, &end_addr,
155 M(SNAT_ADD_ADDRESS_RANGE, mp);
157 memcpy (mp->first_ip_address, &start_addr, 4);
158 memcpy (mp->last_ip_address, &end_addr, 4);
167 static int api_snat_interface_add_del_feature (vat_main_t * vam)
169 unformat_input_t * i = vam->input;
170 vl_api_snat_interface_add_del_feature_t * mp;
172 u8 sw_if_index_set = 0;
177 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
179 if (unformat (i, "%U", unformat_sw_if_index, vam, &sw_if_index))
181 else if (unformat (i, "sw_if_index %d", &sw_if_index))
183 else if (unformat (i, "out"))
185 else if (unformat (i, "in"))
187 else if (unformat (i, "del"))
191 clib_warning("unknown input '%U'", format_unformat_error, i);
196 if (sw_if_index_set == 0)
198 errmsg ("interface / sw_if_index required\n");
202 M(SNAT_INTERFACE_ADD_DEL_FEATURE, mp);
203 mp->sw_if_index = ntohl(sw_if_index);
205 mp->is_inside = is_inside;
212 static int api_snat_add_static_mapping(vat_main_t * vam)
214 unformat_input_t * i = vam->input;
215 vl_api_snat_add_static_mapping_t * mp;
216 u8 external_addr_set = 0;
217 u8 local_addr_set = 0;
220 ip4_address_t local_addr, external_addr;
221 u32 local_port = 0, external_port = 0, vrf_id = ~0;
222 u32 sw_if_index = ~0;
223 u8 sw_if_index_set = 0;
228 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
230 if (unformat (i, "local_addr %U", unformat_ip4_address, &local_addr))
232 else if (unformat (i, "external_addr %U", unformat_ip4_address,
234 external_addr_set = 1;
235 else if (unformat (i, "local_port %u", &local_port))
237 else if (unformat (i, "external_port %u", &external_port))
239 else if (unformat (i, "external_if %U", unformat_sw_if_index, vam,
242 else if (unformat (i, "external_sw_if_index %d", &sw_if_index))
244 else if (unformat (i, "vrf %u", &vrf_id))
246 else if (unformat (i, "protocol %u", &proto))
248 else if (unformat (i, "del"))
252 clib_warning("unknown input '%U'", format_unformat_error, i);
257 if (!addr_only && !proto_set)
259 errmsg ("protocol required\n");
265 errmsg ("local addr required\n");
268 if (!external_addr_set && !sw_if_index_set)
270 errmsg ("external addr or interface required\n");
274 M(SNAT_ADD_STATIC_MAPPING, mp);
277 mp->addr_only = addr_only;
278 mp->local_port = ntohs ((u16) local_port);
279 mp->external_port = ntohs ((u16) external_port);
280 mp->external_sw_if_index = ntohl (sw_if_index);
281 mp->vrf_id = ntohl (vrf_id);
282 mp->protocol = (u8) proto;
283 memcpy (mp->local_ip_address, &local_addr, 4);
284 memcpy (mp->external_ip_address, &external_addr, 4);
291 static void vl_api_snat_control_ping_reply_t_handler
292 (vl_api_snat_control_ping_reply_t * mp)
294 vat_main_t *vam = &vat_main;
295 i32 retval = ntohl (mp->retval);
298 vam->async_errors += (retval < 0);
302 vam->retval = retval;
303 vam->result_ready = 1;
307 static void vl_api_snat_static_mapping_details_t_handler
308 (vl_api_snat_static_mapping_details_t *mp)
310 snat_test_main_t * sm = &snat_test_main;
311 vat_main_t *vam = sm->vat_main;
313 if (mp->addr_only && mp->external_sw_if_index != ~0)
314 fformat (vam->ofp, "%15U%6s%15d%6s%11d%6d\n",
315 format_ip4_address, &mp->local_ip_address, "",
316 ntohl (mp->external_sw_if_index), "",
319 else if (mp->addr_only && mp->external_sw_if_index == ~0)
320 fformat (vam->ofp, "%15U%6s%15U%6s%11d%6d\n",
321 format_ip4_address, &mp->local_ip_address, "",
322 format_ip4_address, &mp->external_ip_address, "",
325 else if (!mp->addr_only && mp->external_sw_if_index != ~0)
326 fformat (vam->ofp, "%15U%6d%15d%6d%11d%6d\n",
327 format_ip4_address, &mp->local_ip_address,
328 ntohs (mp->local_port),
329 ntohl (mp->external_sw_if_index),
330 ntohs (mp->external_port),
334 fformat (vam->ofp, "%15U%6d%15U%6d%11d%6d\n",
335 format_ip4_address, &mp->local_ip_address,
336 ntohs (mp->local_port),
337 format_ip4_address, &mp->external_ip_address,
338 ntohs (mp->external_port),
344 static int api_snat_static_mapping_dump(vat_main_t * vam)
346 vl_api_snat_static_mapping_dump_t * mp;
347 vl_api_snat_control_ping_t *mp_ping;
350 if (vam->json_output)
352 clib_warning ("JSON output not supported for snat_static_mapping_dump");
356 fformat (vam->ofp, "%21s%21s\n", "local", "external");
357 fformat (vam->ofp, "%15s%6s%15s%6s%11s%6s\n", "address", "port",
358 "address/if_idx", "port", "vrf", "proto");
360 M(SNAT_STATIC_MAPPING_DUMP, mp);
363 /* Use a control ping for synchronization */
364 M(SNAT_CONTROL_PING, mp_ping);
371 static void vl_api_snat_show_config_reply_t_handler
372 (vl_api_snat_show_config_reply_t *mp)
374 snat_test_main_t * sm = &snat_test_main;
375 vat_main_t *vam = sm->vat_main;
376 i32 retval = ntohl (mp->retval);
380 fformat (vam->ofp, "translation hash buckets %d\n",
381 ntohl (mp->translation_buckets));
382 fformat (vam->ofp, "translation hash memory %d\n",
383 ntohl (mp->translation_memory_size));
384 fformat (vam->ofp, "user hash buckets %d\n", ntohl (mp->user_buckets));
385 fformat (vam->ofp, "user hash memory %d\n", ntohl (mp->user_memory_size));
386 fformat (vam->ofp, "max translations per user %d\n",
387 ntohl (mp->max_translations_per_user));
388 fformat (vam->ofp, "outside VRF id %d\n", ntohl (mp->outside_vrf_id));
389 fformat (vam->ofp, "inside VRF id %d\n", ntohl (mp->inside_vrf_id));
390 if (mp->static_mapping_only)
392 fformat (vam->ofp, "static mapping only");
393 if (mp->static_mapping_connection_tracking)
394 fformat (vam->ofp, " connection tracking");
395 fformat (vam->ofp, "\n");
398 vam->retval = retval;
399 vam->result_ready = 1;
402 static int api_snat_show_config(vat_main_t * vam)
404 vl_api_snat_show_config_t * mp;
407 if (vam->json_output)
409 clib_warning ("JSON output not supported for snat_show_config");
413 M(SNAT_SHOW_CONFIG, mp);
419 static void vl_api_snat_address_details_t_handler
420 (vl_api_snat_address_details_t *mp)
422 snat_test_main_t * sm = &snat_test_main;
423 vat_main_t *vam = sm->vat_main;
425 fformat (vam->ofp, "%U\n", format_ip4_address, &mp->ip_address);
428 static int api_snat_address_dump(vat_main_t * vam)
430 vl_api_snat_address_dump_t * mp;
431 vl_api_snat_control_ping_t *mp_ping;
434 if (vam->json_output)
436 clib_warning ("JSON output not supported for snat_address_dump");
440 M(SNAT_ADDRESS_DUMP, mp);
443 /* Use a control ping for synchronization */
444 M(SNAT_CONTROL_PING, mp_ping);
451 static void vl_api_snat_interface_details_t_handler
452 (vl_api_snat_interface_details_t *mp)
454 snat_test_main_t * sm = &snat_test_main;
455 vat_main_t *vam = sm->vat_main;
457 fformat (vam->ofp, "sw_if_index %d %s\n", ntohl (mp->sw_if_index),
458 mp->is_inside ? "in" : "out");
461 static int api_snat_interface_dump(vat_main_t * vam)
463 vl_api_snat_interface_dump_t * mp;
464 vl_api_snat_control_ping_t *mp_ping;
467 if (vam->json_output)
469 clib_warning ("JSON output not supported for snat_address_dump");
473 M(SNAT_INTERFACE_DUMP, mp);
476 /* Use a control ping for synchronization */
477 M(SNAT_CONTROL_PING, mp_ping);
484 static int api_snat_set_workers (vat_main_t * vam)
486 unformat_input_t * i = vam->input;
487 vl_api_snat_set_workers_t * mp;
491 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
493 if (unformat (i, "%U", unformat_bitmap_list, &bitmap))
497 clib_warning("unknown input '%U'", format_unformat_error, i);
502 M(SNAT_SET_WORKERS, mp);
503 mp->worker_mask = clib_host_to_net_u64 (bitmap[0]);
510 static void vl_api_snat_worker_details_t_handler
511 (vl_api_snat_worker_details_t *mp)
513 snat_test_main_t * sm = &snat_test_main;
514 vat_main_t *vam = sm->vat_main;
516 fformat (vam->ofp, "worker_index %d (%s at lcore %u)\n",
517 ntohl (mp->worker_index), mp->name, ntohl (mp->lcore_id));
520 static int api_snat_worker_dump(vat_main_t * vam)
522 vl_api_snat_worker_dump_t * mp;
523 vl_api_snat_control_ping_t *mp_ping;
526 if (vam->json_output)
528 clib_warning ("JSON output not supported for snat_address_dump");
532 M(SNAT_WORKER_DUMP, mp);
535 /* Use a control ping for synchronization */
536 M(SNAT_CONTROL_PING, mp_ping);
543 static int api_snat_ipfix_enable_disable (vat_main_t * vam)
545 unformat_input_t * i = vam->input;
546 vl_api_snat_add_del_interface_addr_t * mp;
548 u8 sw_if_index_set = 0;
552 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
554 if (unformat (i, "%U", unformat_sw_if_index, vam, &sw_if_index))
556 else if (unformat (i, "sw_if_index %d", &sw_if_index))
558 else if (unformat (i, "del"))
562 clib_warning("unknown input '%U'", format_unformat_error, i);
567 if (sw_if_index_set == 0)
569 errmsg ("interface / sw_if_index required\n");
573 M(SNAT_ADD_DEL_INTERFACE_ADDR, mp);
574 mp->sw_if_index = ntohl(sw_if_index);
582 static void vl_api_snat_interface_addr_details_t_handler
583 (vl_api_snat_interface_addr_details_t *mp)
585 snat_test_main_t * sm = &snat_test_main;
586 vat_main_t *vam = sm->vat_main;
588 fformat (vam->ofp, "sw_if_index %d\n", ntohl (mp->sw_if_index));
591 static int api_snat_interface_addr_dump(vat_main_t * vam)
593 vl_api_snat_interface_addr_dump_t * mp;
594 vl_api_snat_control_ping_t *mp_ping;
597 if (vam->json_output)
599 clib_warning ("JSON output not supported for snat_address_dump");
603 M(SNAT_INTERFACE_ADDR_DUMP, mp);
606 /* Use a control ping for synchronization */
607 M(SNAT_CONTROL_PING, mp_ping);
614 static int api_snat_add_del_interface_addr (vat_main_t * vam)
616 unformat_input_t * i = vam->input;
617 vl_api_snat_ipfix_enable_disable_t * mp;
623 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
625 if (unformat (i, "domain %d", &domain_id))
627 else if (unformat (i, "src_port %d", &src_port))
629 else if (unformat (i, "disable"))
633 clib_warning("unknown input '%U'", format_unformat_error, i);
638 M(SNAT_IPFIX_ENABLE_DISABLE, mp);
639 mp->domain_id = htonl(domain_id);
640 mp->src_port = htons((u16) src_port);
649 * List of messages that the api test plugin sends,
650 * and that the data plane plugin processes
652 #define foreach_vpe_api_msg \
653 _(snat_add_address_range, "<start-addr> [- <end-addr] [del]") \
654 _(snat_interface_add_del_feature, \
655 "<intfc> | sw_if_index <id> [in] [out] [del]") \
656 _(snat_add_static_mapping, "local_addr <ip> (external_addr <ip>" \
657 " | external_if <intfc> | external_sw_if_ndex <id>) " \
658 "[local_port <n>] [external_port <n>] [vrf <table-id>] [del] " \
660 _(snat_set_workers, "<wokrers_bitmap>") \
661 _(snat_static_mapping_dump, "") \
662 _(snat_show_config, "") \
663 _(snat_address_dump, "") \
664 _(snat_interface_dump, "") \
665 _(snat_worker_dump, "") \
666 _(snat_add_del_interface_addr, \
667 "<intfc> | sw_if_index <id> [del]") \
668 _(snat_interface_addr_dump, "") \
669 _(snat_ipfix_enable_disable, "[domain <id>] [src_port <n>] " \
673 snat_vat_api_hookup (vat_main_t *vam)
675 snat_test_main_t * sm __attribute__((unused)) = &snat_test_main;
676 /* Hook up handlers for replies from the data plane plug-in */
678 vl_msg_api_set_handlers((VL_API_##N + sm->msg_id_base), \
680 vl_api_##n##_t_handler, \
682 vl_api_##n##_t_endian, \
683 vl_api_##n##_t_print, \
684 sizeof(vl_api_##n##_t), 1);
685 foreach_vpe_api_reply_msg;
688 /* API messages we can send */
690 hash_set_mem (vam->function_by_name, #n, api_##n); \
691 clib_warning ("vam %llx add '%s' handler %llx", vam, #n, api_##n);
696 #define _(n,h) hash_set_mem (vam->help_by_name, #n, h);
701 clib_error_t * vat_plugin_register (vat_main_t *vam)
703 snat_test_main_t * sm = &snat_test_main;
708 /* Ask the vpp engine for the first assigned message-id */
709 name = format (0, "snat_%08x%c", api_version, 0);
710 sm->msg_id_base = vl_client_get_first_plugin_msg_id ((char *) name);
712 if (sm->msg_id_base != (u16) ~0)
713 snat_vat_api_hookup (vam);