cd2656cbdaf8021b027b9b122a054168b17f23c6
[vpp.git] / plugins / snat-plugin / snat / snat_test.c
1
2 /*
3  * snat.c - skeleton vpp-api-test plug-in 
4  *
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:
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  */
18 #include <vat/vat.h>
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>
24
25 uword unformat_sw_if_index (unformat_input_t * input, va_list * args);
26
27 /* Declare message IDs */
28 #include <snat/snat_msg_enum.h>
29
30 /* define message structures */
31 #define vl_typedefs
32 #include <snat/snat_all_api_h.h> 
33 #undef vl_typedefs
34
35 /* declare message handlers for each api */
36
37 #define vl_endianfun             /* define message structures */
38 #include <snat/snat_all_api_h.h> 
39 #undef vl_endianfun
40
41 /* instantiate all the print functions we know about */
42 #define vl_print(handle, ...)
43 #define vl_printfun
44 #include <snat/snat_all_api_h.h> 
45 #undef vl_printfun
46
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>
50 #undef vl_api_version
51
52 typedef struct {
53     /* API message ID base */
54     u16 msg_id_base;
55     vat_main_t *vat_main;
56 } snat_test_main_t;
57
58 snat_test_main_t snat_test_main;
59
60 #define foreach_standard_reply_retval_handler   \
61 _(snat_add_address_range_reply)                 \
62 _(snat_interface_add_del_feature_reply)
63
64 #define _(n)                                            \
65     static void vl_api_##n##_t_handler                  \
66     (vl_api_##n##_t * mp)                               \
67     {                                                   \
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);          \
72         } else {                                        \
73             vam->retval = retval;                       \
74             vam->result_ready = 1;                      \
75         }                                               \
76     }
77 foreach_standard_reply_retval_handler;
78 #undef _
79
80 /* 
81  * Table of message reply handlers, must include boilerplate handlers
82  * we just generated
83  */
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)
88
89 /* M: construct, but don't yet send a message */
90 #define M(T,t)                                                  \
91 do {                                                            \
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;                    \
97 } while(0);
98
99 #define M2(T,t,n)                                               \
100 do {                                                            \
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;                    \
106 } while(0);
107
108 /* S: send a message */
109 #define S (vl_msg_api_send_shmem (vam->vl_input_queue, (u8 *)&mp))
110
111 /* W: wait for results, with timeout */
112 #define W                                       \
113 do {                                            \
114     timeout = vat_time_now (vam) + 1.0;         \
115                                                 \
116     while (vat_time_now (vam) < timeout) {      \
117         if (vam->result_ready == 1) {           \
118             return (vam->retval);               \
119         }                                       \
120     }                                           \
121     return -99;                                 \
122 } while(0);
123
124 static int api_snat_add_address_range (vat_main_t * vam)
125 {
126   snat_test_main_t * sm = &snat_test_main;
127   unformat_input_t * i = vam->input;
128   f64 timeout;
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;
132   int count;
133
134   if (unformat (i, "%U - %U", 
135                 unformat_ip4_address, &start_addr,
136                 unformat_ip4_address, &end_addr))
137     ;
138   else if (unformat (i, "%U", unformat_ip4_address, &start_addr))
139     end_addr = start_addr;
140
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);
143   
144   if (end_host_order < start_host_order)
145     {
146       errmsg ("end address less than start address\n");
147       return -99;
148     }
149
150   count = (end_host_order - start_host_order) + 1;
151
152   if (count > 1024)
153     {
154     errmsg ("%U - %U, %d addresses...\n",
155            format_ip4_address, &start_addr,
156            format_ip4_address, &end_addr,
157            count);
158     }
159   
160   M(SNAT_ADD_ADDRESS_RANGE, snat_add_address_range);
161
162   memcpy (mp->first_ip_address, &start_addr, 4);
163   memcpy (mp->last_ip_address, &end_addr, 4);
164   mp->is_ip4 = 1;
165
166   S; W;
167
168   /* NOTREACHED */
169   return 0;
170 }
171
172 static int api_snat_interface_add_del_feature (vat_main_t * vam)
173 {
174   snat_test_main_t * sm = &snat_test_main;
175   unformat_input_t * i = vam->input;
176   f64 timeout;
177   vl_api_snat_interface_add_del_feature_t * mp;
178   u32 sw_if_index;
179   u8 sw_if_index_set = 0;
180   u8 is_inside = 1; 
181   u8 is_add = 1;
182
183   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
184     {
185       if (unformat (i, "%U", unformat_sw_if_index, vam, &sw_if_index))
186         sw_if_index_set = 1;
187       else if (unformat (i, "sw_if_index %d", &sw_if_index))
188         sw_if_index_set = 1;
189       else if (unformat (i, "out"))
190         is_inside = 0;
191       else if (unformat (i, "in"))
192         is_inside = 1;
193       else if (unformat (i, "del"))
194         is_add = 0;
195     }
196
197   if (sw_if_index_set == 0)
198     {
199       errmsg ("interface / sw_if_index required\n");
200       return -99;
201     }
202
203   M(SNAT_INTERFACE_ADD_DEL_FEATURE, snat_interface_add_del_feature);
204   mp->sw_if_index = ntohl(sw_if_index);
205   mp->is_add = is_add;
206   mp->is_inside = is_inside;
207   
208   S; W;
209   /* NOTREACHED */
210   return 0;
211 }
212
213 /* 
214  * List of messages that the api test plugin sends,
215  * and that the data plane plugin processes
216  */
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]")
221
222 void vat_api_hookup (vat_main_t *vam)
223 {
224   snat_test_main_t * sm __attribute__((unused)) = &snat_test_main;
225   /* Hook up handlers for replies from the data plane plug-in */
226 #define _(N,n)                                                  \
227   vl_msg_api_set_handlers((VL_API_##N + sm->msg_id_base),       \
228                           #n,                                   \
229                           vl_api_##n##_t_handler,               \
230                           vl_noop_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;
235 #undef _
236
237   /* API messages we can send */
238 #define _(n,h) hash_set_mem (vam->function_by_name, #n, api_##n);
239   foreach_vpe_api_msg;
240 #undef _    
241     
242   /* Help strings */
243 #define _(n,h) hash_set_mem (vam->help_by_name, #n, h);
244   foreach_vpe_api_msg;
245 #undef _
246 }
247
248 clib_error_t * vat_plugin_register (vat_main_t *vam)
249 {
250   snat_test_main_t * sm = &snat_test_main;
251   u8 * name;
252
253   sm->vat_main = vam;
254
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);
258
259   if (sm->msg_id_base != (u16) ~0)
260     vat_api_hookup (vam);
261   
262   vec_free(name);
263   
264   return 0;
265 }