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)
94 /* M: construct, but don't yet send a message */
97 vam->result_ready = 0; \
98 mp = vl_msg_api_alloc(sizeof(*mp)); \
99 memset (mp, 0, sizeof (*mp)); \
100 mp->_vl_msg_id = ntohs (VL_API_##T + sm->msg_id_base); \
101 mp->client_index = vam->my_client_index; \
106 vam->result_ready = 0; \
107 mp = vl_msg_api_alloc(sizeof(*mp)+(n)); \
108 memset (mp, 0, sizeof (*mp)); \
109 mp->_vl_msg_id = ntohs (VL_API_##T + sm->msg_id_base); \
110 mp->client_index = vam->my_client_index; \
113 /* S: send a message */
114 #define S (vl_msg_api_send_shmem (vam->vl_input_queue, (u8 *)&mp))
116 /* W: wait for results, with timeout */
119 timeout = vat_time_now (vam) + 1.0; \
121 while (vat_time_now (vam) < timeout) { \
122 if (vam->result_ready == 1) { \
123 return (vam->retval); \
129 static int api_snat_add_address_range (vat_main_t * vam)
131 snat_test_main_t * sm = &snat_test_main;
132 unformat_input_t * i = vam->input;
134 ip4_address_t start_addr, end_addr;
135 u32 start_host_order, end_host_order;
136 vl_api_snat_add_address_range_t * mp;
139 if (unformat (i, "%U - %U",
140 unformat_ip4_address, &start_addr,
141 unformat_ip4_address, &end_addr))
143 else if (unformat (i, "%U", unformat_ip4_address, &start_addr))
144 end_addr = start_addr;
147 clib_warning("unknown input '%U'", format_unformat_error, i);
151 start_host_order = clib_host_to_net_u32 (start_addr.as_u32);
152 end_host_order = clib_host_to_net_u32 (end_addr.as_u32);
154 if (end_host_order < start_host_order)
156 errmsg ("end address less than start address\n");
160 count = (end_host_order - start_host_order) + 1;
164 errmsg ("%U - %U, %d addresses...\n",
165 format_ip4_address, &start_addr,
166 format_ip4_address, &end_addr,
170 M(SNAT_ADD_ADDRESS_RANGE, snat_add_address_range);
172 memcpy (mp->first_ip_address, &start_addr, 4);
173 memcpy (mp->last_ip_address, &end_addr, 4);
182 static int api_snat_interface_add_del_feature (vat_main_t * vam)
184 snat_test_main_t * sm = &snat_test_main;
185 unformat_input_t * i = vam->input;
187 vl_api_snat_interface_add_del_feature_t * mp;
189 u8 sw_if_index_set = 0;
193 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
195 if (unformat (i, "%U", unformat_sw_if_index, vam, &sw_if_index))
197 else if (unformat (i, "sw_if_index %d", &sw_if_index))
199 else if (unformat (i, "out"))
201 else if (unformat (i, "in"))
203 else if (unformat (i, "del"))
207 clib_warning("unknown input '%U'", format_unformat_error, i);
212 if (sw_if_index_set == 0)
214 errmsg ("interface / sw_if_index required\n");
218 M(SNAT_INTERFACE_ADD_DEL_FEATURE, snat_interface_add_del_feature);
219 mp->sw_if_index = ntohl(sw_if_index);
221 mp->is_inside = is_inside;
228 static int api_snat_add_static_mapping(vat_main_t * vam)
230 snat_test_main_t * sm = &snat_test_main;
231 unformat_input_t * i = vam->input;
233 vl_api_snat_add_static_mapping_t * mp;
237 ip4_address_t local_addr, external_addr;
238 u32 local_port = 0, external_port = 0, vrf_id = ~0;
240 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
242 if (unformat (i, "local_addr %U", unformat_ip4_address, &local_addr))
244 else if (unformat (i, "external_addr %U", unformat_ip4_address,
247 else if (unformat (i, "local_port %u", &local_port))
249 else if (unformat (i, "external_port %u", &external_port))
251 else if (unformat (i, "vrf %u", &vrf_id))
253 else if (unformat (i, "del"))
257 clib_warning("unknown input '%U'", format_unformat_error, i);
264 errmsg ("local_addr and remote_addr required\n");
268 M(SNAT_ADD_STATIC_MAPPING, snat_add_static_mapping);
271 mp->addr_only = addr_only;
272 mp->local_port = ntohs ((u16) local_port);
273 mp->external_port = ntohs ((u16) external_port);
274 mp->vrf_id = ntohl (vrf_id);
275 memcpy (mp->local_ip_address, &local_addr, 4);
276 memcpy (mp->external_ip_address, &external_addr, 4);
283 static void vl_api_snat_control_ping_reply_t_handler
284 (vl_api_snat_control_ping_reply_t * mp)
286 vat_main_t *vam = &vat_main;
287 i32 retval = ntohl (mp->retval);
290 vam->async_errors += (retval < 0);
294 vam->retval = retval;
295 vam->result_ready = 1;
299 static void vl_api_snat_static_mapping_details_t_handler
300 (vl_api_snat_static_mapping_details_t *mp)
302 snat_test_main_t * sm = &snat_test_main;
303 vat_main_t *vam = sm->vat_main;
306 fformat (vam->ofp, "%15U%6s%15U%6s%11d\n",
307 format_ip4_address, &mp->local_ip_address, "",
308 format_ip4_address, &mp->external_ip_address, "",
311 fformat (vam->ofp, "%15U%6d%15U%6d%11d\n",
312 format_ip4_address, &mp->local_ip_address,
313 ntohs (mp->local_port),
314 format_ip4_address, &mp->external_ip_address,
315 ntohs (mp->external_port),
320 static int api_snat_static_mapping_dump(vat_main_t * vam)
322 snat_test_main_t * sm = &snat_test_main;
324 vl_api_snat_static_mapping_dump_t * mp;
326 if (vam->json_output)
328 clib_warning ("JSON output not supported for snat_static_mapping_dump");
332 fformat (vam->ofp, "%21s%21s\n", "local", "external");
333 fformat (vam->ofp, "%15s%6s%15s%6s%11s\n", "address", "port", "address",
336 M(SNAT_STATIC_MAPPING_DUMP, snat_static_mapping_dump);
338 /* Use a control ping for synchronization */
340 vl_api_snat_control_ping_t *mp;
341 M (SNAT_CONTROL_PING, snat_control_ping);
349 static void vl_api_snat_show_config_reply_t_handler
350 (vl_api_snat_show_config_reply_t *mp)
352 snat_test_main_t * sm = &snat_test_main;
353 vat_main_t *vam = sm->vat_main;
354 i32 retval = ntohl (mp->retval);
358 fformat (vam->ofp, "translation hash buckets %d\n",
359 ntohl (mp->translation_buckets));
360 fformat (vam->ofp, "translation hash memory %d\n",
361 ntohl (mp->translation_memory_size));
362 fformat (vam->ofp, "user hash buckets %d\n", ntohl (mp->user_buckets));
363 fformat (vam->ofp, "user hash memory %d\n", ntohl (mp->user_memory_size));
364 fformat (vam->ofp, "max translations per user %d\n",
365 ntohl (mp->max_translations_per_user));
366 fformat (vam->ofp, "outside VRF id %d\n", ntohl (mp->outside_vrf_id));
367 fformat (vam->ofp, "inside VRF id %d\n", ntohl (mp->inside_vrf_id));
368 if (mp->static_mapping_only)
370 fformat (vam->ofp, "static mapping only");
371 if (mp->static_mapping_connection_tracking)
372 fformat (vam->ofp, " connection tracking");
373 fformat (vam->ofp, "\n");
376 vam->retval = retval;
377 vam->result_ready = 1;
380 static int api_snat_show_config(vat_main_t * vam)
382 snat_test_main_t * sm = &snat_test_main;
384 vl_api_snat_show_config_t * mp;
386 if (vam->json_output)
388 clib_warning ("JSON output not supported for snat_show_config");
392 M(SNAT_SHOW_CONFIG, snat_show_config);
399 * List of messages that the api test plugin sends,
400 * and that the data plane plugin processes
402 #define foreach_vpe_api_msg \
403 _(snat_add_address_range, "<start-addr> [- <end-addr]") \
404 _(snat_interface_add_del_feature, \
405 "<intfc> | sw_if_index <id> [in] [out] [del]") \
406 _(snat_add_static_mapping, "local_addr <ip> external_addr <ip> " \
407 "[local_port <n>] [external_port <n>] [vrf <table-id>] [del]") \
408 _(snat_static_mapping_dump, "") \
409 _(snat_show_config, "")
411 void vat_api_hookup (vat_main_t *vam)
413 snat_test_main_t * sm __attribute__((unused)) = &snat_test_main;
414 /* Hook up handlers for replies from the data plane plug-in */
416 vl_msg_api_set_handlers((VL_API_##N + sm->msg_id_base), \
418 vl_api_##n##_t_handler, \
420 vl_api_##n##_t_endian, \
421 vl_api_##n##_t_print, \
422 sizeof(vl_api_##n##_t), 1);
423 foreach_vpe_api_reply_msg;
426 /* API messages we can send */
427 #define _(n,h) hash_set_mem (vam->function_by_name, #n, api_##n);
432 #define _(n,h) hash_set_mem (vam->help_by_name, #n, h);
437 clib_error_t * vat_plugin_register (vat_main_t *vam)
439 snat_test_main_t * sm = &snat_test_main;
444 /* Ask the vpp engine for the first assigned message-id */
445 name = format (0, "snat_%08x%c", api_version, 0);
446 sm->msg_id_base = vl_client_get_first_plugin_msg_id ((char *) name);
448 if (sm->msg_id_base != (u16) ~0)
449 vat_api_hookup (vam);