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 uword unformat_sw_if_index (unformat_input_t * input, va_list * args);
27 /* Declare message IDs */
28 #include <snat/snat_msg_enum.h>
30 /* define message structures */
32 #include <snat/snat_all_api_h.h>
35 /* declare message handlers for each api */
37 #define vl_endianfun /* define message structures */
38 #include <snat/snat_all_api_h.h>
41 /* instantiate all the print functions we know about */
42 #define vl_print(handle, ...)
44 #include <snat/snat_all_api_h.h>
47 /* Get the API version number. */
48 #define vl_api_version(n,v) static u32 api_version=(v);
49 #include <snat/snat_all_api_h.h>
53 /* API message ID base */
58 snat_test_main_t snat_test_main;
60 #define foreach_standard_reply_retval_handler \
61 _(snat_add_address_range_reply) \
62 _(snat_interface_add_del_feature_reply) \
63 _(snat_add_static_mapping_reply)
66 static void vl_api_##n##_t_handler \
67 (vl_api_##n##_t * mp) \
69 vat_main_t * vam = snat_test_main.vat_main; \
70 i32 retval = ntohl(mp->retval); \
71 if (vam->async_mode) { \
72 vam->async_errors += (retval < 0); \
74 vam->retval = retval; \
75 vam->result_ready = 1; \
78 foreach_standard_reply_retval_handler;
82 * Table of message reply handlers, must include boilerplate handlers
85 #define foreach_vpe_api_reply_msg \
86 _(SNAT_ADD_ADDRESS_RANGE_REPLY, snat_add_address_range_reply) \
87 _(SNAT_INTERFACE_ADD_DEL_FEATURE_REPLY, \
88 snat_interface_add_del_feature_reply) \
89 _(SNAT_ADD_STATIC_MAPPING_REPLY, snat_add_static_mapping_reply) \
90 _(SNAT_CONTROL_PING_REPLY, snat_control_ping_reply) \
91 _(SNAT_STATIC_MAPPING_DETAILS, snat_static_mapping_details) \
92 _(SNAT_SHOW_CONFIG_REPLY, snat_show_config_reply) \
93 _(SNAT_ADDRESS_DETAILS, snat_address_details)
95 /* M: construct, but don't yet send a message */
98 vam->result_ready = 0; \
99 mp = vl_msg_api_alloc(sizeof(*mp)); \
100 memset (mp, 0, sizeof (*mp)); \
101 mp->_vl_msg_id = ntohs (VL_API_##T + sm->msg_id_base); \
102 mp->client_index = vam->my_client_index; \
107 vam->result_ready = 0; \
108 mp = vl_msg_api_alloc(sizeof(*mp)+(n)); \
109 memset (mp, 0, sizeof (*mp)); \
110 mp->_vl_msg_id = ntohs (VL_API_##T + sm->msg_id_base); \
111 mp->client_index = vam->my_client_index; \
114 /* S: send a message */
115 #define S (vl_msg_api_send_shmem (vam->vl_input_queue, (u8 *)&mp))
117 /* W: wait for results, with timeout */
120 timeout = vat_time_now (vam) + 1.0; \
122 while (vat_time_now (vam) < timeout) { \
123 if (vam->result_ready == 1) { \
124 return (vam->retval); \
130 static int api_snat_add_address_range (vat_main_t * vam)
132 snat_test_main_t * sm = &snat_test_main;
133 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_snat_add_address_range_t * mp;
141 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
143 if (unformat (i, "%U - %U",
144 unformat_ip4_address, &start_addr,
145 unformat_ip4_address, &end_addr))
147 else if (unformat (i, "%U", unformat_ip4_address, &start_addr))
148 end_addr = start_addr;
149 else if (unformat (i, "del"))
153 clib_warning("unknown input '%U'", format_unformat_error, i);
158 start_host_order = clib_host_to_net_u32 (start_addr.as_u32);
159 end_host_order = clib_host_to_net_u32 (end_addr.as_u32);
161 if (end_host_order < start_host_order)
163 errmsg ("end address less than start address\n");
167 count = (end_host_order - start_host_order) + 1;
171 errmsg ("%U - %U, %d addresses...\n",
172 format_ip4_address, &start_addr,
173 format_ip4_address, &end_addr,
177 M(SNAT_ADD_ADDRESS_RANGE, snat_add_address_range);
179 memcpy (mp->first_ip_address, &start_addr, 4);
180 memcpy (mp->last_ip_address, &end_addr, 4);
190 static int api_snat_interface_add_del_feature (vat_main_t * vam)
192 snat_test_main_t * sm = &snat_test_main;
193 unformat_input_t * i = vam->input;
195 vl_api_snat_interface_add_del_feature_t * mp;
197 u8 sw_if_index_set = 0;
201 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
203 if (unformat (i, "%U", unformat_sw_if_index, vam, &sw_if_index))
205 else if (unformat (i, "sw_if_index %d", &sw_if_index))
207 else if (unformat (i, "out"))
209 else if (unformat (i, "in"))
211 else if (unformat (i, "del"))
215 clib_warning("unknown input '%U'", format_unformat_error, i);
220 if (sw_if_index_set == 0)
222 errmsg ("interface / sw_if_index required\n");
226 M(SNAT_INTERFACE_ADD_DEL_FEATURE, snat_interface_add_del_feature);
227 mp->sw_if_index = ntohl(sw_if_index);
229 mp->is_inside = is_inside;
236 static int api_snat_add_static_mapping(vat_main_t * vam)
238 snat_test_main_t * sm = &snat_test_main;
239 unformat_input_t * i = vam->input;
241 vl_api_snat_add_static_mapping_t * mp;
245 ip4_address_t local_addr, external_addr;
246 u32 local_port = 0, external_port = 0, vrf_id = ~0;
248 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
250 if (unformat (i, "local_addr %U", unformat_ip4_address, &local_addr))
252 else if (unformat (i, "external_addr %U", unformat_ip4_address,
255 else if (unformat (i, "local_port %u", &local_port))
257 else if (unformat (i, "external_port %u", &external_port))
259 else if (unformat (i, "vrf %u", &vrf_id))
261 else if (unformat (i, "del"))
265 clib_warning("unknown input '%U'", format_unformat_error, i);
272 errmsg ("local_addr and remote_addr required\n");
276 M(SNAT_ADD_STATIC_MAPPING, snat_add_static_mapping);
279 mp->addr_only = addr_only;
280 mp->local_port = ntohs ((u16) local_port);
281 mp->external_port = ntohs ((u16) external_port);
282 mp->vrf_id = ntohl (vrf_id);
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;
314 fformat (vam->ofp, "%15U%6s%15U%6s%11d\n",
315 format_ip4_address, &mp->local_ip_address, "",
316 format_ip4_address, &mp->external_ip_address, "",
319 fformat (vam->ofp, "%15U%6d%15U%6d%11d\n",
320 format_ip4_address, &mp->local_ip_address,
321 ntohs (mp->local_port),
322 format_ip4_address, &mp->external_ip_address,
323 ntohs (mp->external_port),
328 static int api_snat_static_mapping_dump(vat_main_t * vam)
330 snat_test_main_t * sm = &snat_test_main;
332 vl_api_snat_static_mapping_dump_t * mp;
334 if (vam->json_output)
336 clib_warning ("JSON output not supported for snat_static_mapping_dump");
340 fformat (vam->ofp, "%21s%21s\n", "local", "external");
341 fformat (vam->ofp, "%15s%6s%15s%6s%11s\n", "address", "port", "address",
344 M(SNAT_STATIC_MAPPING_DUMP, snat_static_mapping_dump);
346 /* Use a control ping for synchronization */
348 vl_api_snat_control_ping_t *mp;
349 M (SNAT_CONTROL_PING, snat_control_ping);
357 static void vl_api_snat_show_config_reply_t_handler
358 (vl_api_snat_show_config_reply_t *mp)
360 snat_test_main_t * sm = &snat_test_main;
361 vat_main_t *vam = sm->vat_main;
362 i32 retval = ntohl (mp->retval);
366 fformat (vam->ofp, "translation hash buckets %d\n",
367 ntohl (mp->translation_buckets));
368 fformat (vam->ofp, "translation hash memory %d\n",
369 ntohl (mp->translation_memory_size));
370 fformat (vam->ofp, "user hash buckets %d\n", ntohl (mp->user_buckets));
371 fformat (vam->ofp, "user hash memory %d\n", ntohl (mp->user_memory_size));
372 fformat (vam->ofp, "max translations per user %d\n",
373 ntohl (mp->max_translations_per_user));
374 fformat (vam->ofp, "outside VRF id %d\n", ntohl (mp->outside_vrf_id));
375 fformat (vam->ofp, "inside VRF id %d\n", ntohl (mp->inside_vrf_id));
376 if (mp->static_mapping_only)
378 fformat (vam->ofp, "static mapping only");
379 if (mp->static_mapping_connection_tracking)
380 fformat (vam->ofp, " connection tracking");
381 fformat (vam->ofp, "\n");
384 vam->retval = retval;
385 vam->result_ready = 1;
388 static int api_snat_show_config(vat_main_t * vam)
390 snat_test_main_t * sm = &snat_test_main;
392 vl_api_snat_show_config_t * mp;
394 if (vam->json_output)
396 clib_warning ("JSON output not supported for snat_show_config");
400 M(SNAT_SHOW_CONFIG, snat_show_config);
406 static void vl_api_snat_address_details_t_handler
407 (vl_api_snat_address_details_t *mp)
409 snat_test_main_t * sm = &snat_test_main;
410 vat_main_t *vam = sm->vat_main;
412 fformat (vam->ofp, "%U\n", format_ip4_address, &mp->ip_address);
415 static int api_snat_address_dump(vat_main_t * vam)
417 snat_test_main_t * sm = &snat_test_main;
419 vl_api_snat_address_dump_t * mp;
421 if (vam->json_output)
423 clib_warning ("JSON output not supported for snat_address_dump");
427 M(SNAT_ADDRESS_DUMP, snat_address_dump);
429 /* Use a control ping for synchronization */
431 vl_api_snat_control_ping_t *mp;
432 M (SNAT_CONTROL_PING, snat_control_ping);
441 * List of messages that the api test plugin sends,
442 * and that the data plane plugin processes
444 #define foreach_vpe_api_msg \
445 _(snat_add_address_range, "<start-addr> [- <end-addr] [del]") \
446 _(snat_interface_add_del_feature, \
447 "<intfc> | sw_if_index <id> [in] [out] [del]") \
448 _(snat_add_static_mapping, "local_addr <ip> external_addr <ip> " \
449 "[local_port <n>] [external_port <n>] [vrf <table-id>] [del]") \
450 _(snat_static_mapping_dump, "") \
451 _(snat_show_config, "") \
452 _(snat_address_dump, "")
454 void vat_api_hookup (vat_main_t *vam)
456 snat_test_main_t * sm __attribute__((unused)) = &snat_test_main;
457 /* Hook up handlers for replies from the data plane plug-in */
459 vl_msg_api_set_handlers((VL_API_##N + sm->msg_id_base), \
461 vl_api_##n##_t_handler, \
463 vl_api_##n##_t_endian, \
464 vl_api_##n##_t_print, \
465 sizeof(vl_api_##n##_t), 1);
466 foreach_vpe_api_reply_msg;
469 /* API messages we can send */
470 #define _(n,h) hash_set_mem (vam->function_by_name, #n, api_##n);
475 #define _(n,h) hash_set_mem (vam->help_by_name, #n, h);
480 clib_error_t * vat_plugin_register (vat_main_t *vam)
482 snat_test_main_t * sm = &snat_test_main;
487 /* Ask the vpp engine for the first assigned message-id */
488 name = format (0, "snat_%08x%c", api_version, 0);
489 sm->msg_id_base = vl_client_get_first_plugin_msg_id ((char *) name);
491 if (sm->msg_id_base != (u16) ~0)
492 vat_api_hookup (vam);