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)
65 static void vl_api_##n##_t_handler \
66 (vl_api_##n##_t * mp) \
68 vat_main_t * vam = snat_test_main.vat_main; \
69 i32 retval = ntohl(mp->retval); \
70 if (vam->async_mode) { \
71 vam->async_errors += (retval < 0); \
73 vam->retval = retval; \
74 vam->result_ready = 1; \
77 foreach_standard_reply_retval_handler;
81 * Table of message reply handlers, must include boilerplate handlers
84 #define foreach_vpe_api_reply_msg \
85 _(SNAT_ADD_ADDRESS_RANGE_REPLY, snat_add_address_range_reply) \
86 _(SNAT_INTERFACE_ADD_DEL_FEATURE_REPLY, \
87 snat_interface_add_del_feature_reply)
89 /* M: construct, but don't yet send a message */
92 vam->result_ready = 0; \
93 mp = vl_msg_api_alloc(sizeof(*mp)); \
94 memset (mp, 0, sizeof (*mp)); \
95 mp->_vl_msg_id = ntohs (VL_API_##T + sm->msg_id_base); \
96 mp->client_index = vam->my_client_index; \
101 vam->result_ready = 0; \
102 mp = vl_msg_api_alloc(sizeof(*mp)+(n)); \
103 memset (mp, 0, sizeof (*mp)); \
104 mp->_vl_msg_id = ntohs (VL_API_##T + sm->msg_id_base); \
105 mp->client_index = vam->my_client_index; \
108 /* S: send a message */
109 #define S (vl_msg_api_send_shmem (vam->vl_input_queue, (u8 *)&mp))
111 /* W: wait for results, with timeout */
114 timeout = vat_time_now (vam) + 1.0; \
116 while (vat_time_now (vam) < timeout) { \
117 if (vam->result_ready == 1) { \
118 return (vam->retval); \
124 static int api_snat_add_address_range (vat_main_t * vam)
126 snat_test_main_t * sm = &snat_test_main;
127 unformat_input_t * i = vam->input;
129 ip4_address_t start_addr, end_addr;
130 u32 start_host_order, end_host_order;
131 vl_api_snat_add_address_range_t * mp;
134 if (unformat (i, "%U - %U",
135 unformat_ip4_address, &start_addr,
136 unformat_ip4_address, &end_addr))
138 else if (unformat (i, "%U", unformat_ip4_address, &start_addr))
139 end_addr = start_addr;
141 start_host_order = clib_host_to_net_u32 (start_addr.as_u32);
142 end_host_order = clib_host_to_net_u32 (end_addr.as_u32);
144 if (end_host_order < start_host_order)
146 errmsg ("end address less than start address\n");
150 count = (end_host_order - start_host_order) + 1;
154 errmsg ("%U - %U, %d addresses...\n",
155 format_ip4_address, &start_addr,
156 format_ip4_address, &end_addr,
160 M(SNAT_ADD_ADDRESS_RANGE, snat_add_address_range);
162 memcpy (mp->first_ip_address, &start_addr, 4);
163 memcpy (mp->last_ip_address, &end_addr, 4);
172 static int api_snat_interface_add_del_feature (vat_main_t * vam)
174 snat_test_main_t * sm = &snat_test_main;
175 unformat_input_t * i = vam->input;
177 vl_api_snat_interface_add_del_feature_t * mp;
179 u8 sw_if_index_set = 0;
183 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
185 if (unformat (i, "%U", unformat_sw_if_index, vam, &sw_if_index))
187 else if (unformat (i, "sw_if_index %d", &sw_if_index))
189 else if (unformat (i, "out"))
191 else if (unformat (i, "in"))
193 else if (unformat (i, "del"))
197 if (sw_if_index_set == 0)
199 errmsg ("interface / sw_if_index required\n");
203 M(SNAT_INTERFACE_ADD_DEL_FEATURE, snat_interface_add_del_feature);
204 mp->sw_if_index = ntohl(sw_if_index);
206 mp->is_inside = is_inside;
214 * List of messages that the api test plugin sends,
215 * and that the data plane plugin processes
217 #define foreach_vpe_api_msg \
218 _(snat_add_address_range, "<start-addr> [- <end-addr]") \
219 _(snat_interface_add_del_feature, \
220 "<intfc> | sw_if_index <id> [in] [out] [del]")
222 void vat_api_hookup (vat_main_t *vam)
224 snat_test_main_t * sm __attribute__((unused)) = &snat_test_main;
225 /* Hook up handlers for replies from the data plane plug-in */
227 vl_msg_api_set_handlers((VL_API_##N + sm->msg_id_base), \
229 vl_api_##n##_t_handler, \
231 vl_api_##n##_t_endian, \
232 vl_api_##n##_t_print, \
233 sizeof(vl_api_##n##_t), 1);
234 foreach_vpe_api_reply_msg;
237 /* API messages we can send */
238 #define _(n,h) hash_set_mem (vam->function_by_name, #n, api_##n);
243 #define _(n,h) hash_set_mem (vam->help_by_name, #n, h);
248 clib_error_t * vat_plugin_register (vat_main_t *vam)
250 snat_test_main_t * sm = &snat_test_main;
255 /* Ask the vpp engine for the first assigned message-id */
256 name = format (0, "snat_%08x%c", api_version, 0);
257 sm->msg_id_base = vl_client_get_first_plugin_msg_id ((char *) name);
259 if (sm->msg_id_base != (u16) ~0)
260 vat_api_hookup (vam);