VPP-339: SNAT static mapping
[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 _(snat_add_static_mapping_reply)
64
65 #define _(n)                                            \
66     static void vl_api_##n##_t_handler                  \
67     (vl_api_##n##_t * mp)                               \
68     {                                                   \
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);          \
73         } else {                                        \
74             vam->retval = retval;                       \
75             vam->result_ready = 1;                      \
76         }                                               \
77     }
78 foreach_standard_reply_retval_handler;
79 #undef _
80
81 /* 
82  * Table of message reply handlers, must include boilerplate handlers
83  * we just generated
84  */
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
94 /* M: construct, but don't yet send a message */
95 #define M(T,t)                                                  \
96 do {                                                            \
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;                    \
102 } while(0);
103
104 #define M2(T,t,n)                                               \
105 do {                                                            \
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;                    \
111 } while(0);
112
113 /* S: send a message */
114 #define S (vl_msg_api_send_shmem (vam->vl_input_queue, (u8 *)&mp))
115
116 /* W: wait for results, with timeout */
117 #define W                                       \
118 do {                                            \
119     timeout = vat_time_now (vam) + 1.0;         \
120                                                 \
121     while (vat_time_now (vam) < timeout) {      \
122         if (vam->result_ready == 1) {           \
123             return (vam->retval);               \
124         }                                       \
125     }                                           \
126     return -99;                                 \
127 } while(0);
128
129 static int api_snat_add_address_range (vat_main_t * vam)
130 {
131   snat_test_main_t * sm = &snat_test_main;
132   unformat_input_t * i = vam->input;
133   f64 timeout;
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;
137   int count;
138
139   if (unformat (i, "%U - %U", 
140                 unformat_ip4_address, &start_addr,
141                 unformat_ip4_address, &end_addr))
142     ;
143   else if (unformat (i, "%U", unformat_ip4_address, &start_addr))
144     end_addr = start_addr;
145   else
146     {
147       clib_warning("unknown input '%U'", format_unformat_error, i);
148       return -99;
149     }
150
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);
153   
154   if (end_host_order < start_host_order)
155     {
156       errmsg ("end address less than start address\n");
157       return -99;
158     }
159
160   count = (end_host_order - start_host_order) + 1;
161
162   if (count > 1024)
163     {
164     errmsg ("%U - %U, %d addresses...\n",
165            format_ip4_address, &start_addr,
166            format_ip4_address, &end_addr,
167            count);
168     }
169   
170   M(SNAT_ADD_ADDRESS_RANGE, snat_add_address_range);
171
172   memcpy (mp->first_ip_address, &start_addr, 4);
173   memcpy (mp->last_ip_address, &end_addr, 4);
174   mp->is_ip4 = 1;
175
176   S; W;
177
178   /* NOTREACHED */
179   return 0;
180 }
181
182 static int api_snat_interface_add_del_feature (vat_main_t * vam)
183 {
184   snat_test_main_t * sm = &snat_test_main;
185   unformat_input_t * i = vam->input;
186   f64 timeout;
187   vl_api_snat_interface_add_del_feature_t * mp;
188   u32 sw_if_index;
189   u8 sw_if_index_set = 0;
190   u8 is_inside = 1; 
191   u8 is_add = 1;
192
193   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
194     {
195       if (unformat (i, "%U", unformat_sw_if_index, vam, &sw_if_index))
196         sw_if_index_set = 1;
197       else if (unformat (i, "sw_if_index %d", &sw_if_index))
198         sw_if_index_set = 1;
199       else if (unformat (i, "out"))
200         is_inside = 0;
201       else if (unformat (i, "in"))
202         is_inside = 1;
203       else if (unformat (i, "del"))
204         is_add = 0;
205       else
206         {
207           clib_warning("unknown input '%U'", format_unformat_error, i);
208           return -99;
209         }
210     }
211
212   if (sw_if_index_set == 0)
213     {
214       errmsg ("interface / sw_if_index required\n");
215       return -99;
216     }
217
218   M(SNAT_INTERFACE_ADD_DEL_FEATURE, snat_interface_add_del_feature);
219   mp->sw_if_index = ntohl(sw_if_index);
220   mp->is_add = is_add;
221   mp->is_inside = is_inside;
222   
223   S; W;
224   /* NOTREACHED */
225   return 0;
226 }
227
228 static int api_snat_add_static_mapping(vat_main_t * vam)
229 {
230   snat_test_main_t * sm = &snat_test_main;
231   unformat_input_t * i = vam->input;
232   f64 timeout;
233   vl_api_snat_add_static_mapping_t * mp;
234   u8 addr_set_n = 0;
235   u8 is_add = 1;
236   u8 addr_only = 1;
237   ip4_address_t local_addr, external_addr;
238   u32 local_port = 0, external_port = 0, vrf_id = ~0;
239
240   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
241     {
242       if (unformat (i, "local_addr %U", unformat_ip4_address, &local_addr))
243         addr_set_n++;
244       else if (unformat (i, "external_addr %U", unformat_ip4_address,
245                          &external_addr))
246         addr_set_n++;
247       else if (unformat (i, "local_port %u", &local_port))
248         addr_only = 0;
249       else if (unformat (i, "external_port %u", &external_port))
250         addr_only = 0;
251       else if (unformat (i, "vrf %u", &vrf_id))
252         ;
253       else if (unformat (i, "del"))
254         is_add = 0;
255       else
256         {
257           clib_warning("unknown input '%U'", format_unformat_error, i);
258           return -99;
259         }
260     }
261
262   if (addr_set_n != 2)
263     {
264       errmsg ("local_addr and remote_addr required\n");
265       return -99;
266     }
267
268   M(SNAT_ADD_STATIC_MAPPING, snat_add_static_mapping);
269   mp->is_add = is_add;
270   mp->is_ip4 = 1;
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);
277
278   S; W;
279   /* NOTREACHED */
280   return 0;
281 }
282
283 static void vl_api_snat_control_ping_reply_t_handler
284   (vl_api_snat_control_ping_reply_t * mp)
285 {
286   vat_main_t *vam = &vat_main;
287   i32 retval = ntohl (mp->retval);
288   if (vam->async_mode)
289     {
290       vam->async_errors += (retval < 0);
291     }
292   else
293     {
294       vam->retval = retval;
295       vam->result_ready = 1;
296     }
297 }
298
299 static void vl_api_snat_static_mapping_details_t_handler
300   (vl_api_snat_static_mapping_details_t *mp)
301 {
302   snat_test_main_t * sm = &snat_test_main;
303   vat_main_t *vam = sm->vat_main;
304
305   if (mp->addr_only)
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, "",
309                ntohl (mp->vrf_id));
310   else
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),
316                ntohl (mp->vrf_id));
317
318 }
319
320 static int api_snat_static_mapping_dump(vat_main_t * vam)
321 {
322   snat_test_main_t * sm = &snat_test_main;
323   f64 timeout;
324   vl_api_snat_static_mapping_dump_t * mp;
325
326   if (vam->json_output)
327     {
328       clib_warning ("JSON output not supported for snat_static_mapping_dump");
329       return -99;
330     }
331
332   fformat (vam->ofp, "%21s%21s\n", "local", "external");
333   fformat (vam->ofp, "%15s%6s%15s%6s%11s\n", "address", "port", "address",
334            "port", "vrf");
335
336   M(SNAT_STATIC_MAPPING_DUMP, snat_static_mapping_dump);
337   S;
338   /* Use a control ping for synchronization */
339   {
340     vl_api_snat_control_ping_t *mp;
341     M (SNAT_CONTROL_PING, snat_control_ping);
342     S;
343   }
344   W;
345   /* NOTREACHED */
346   return 0;
347 }
348
349 static void vl_api_snat_show_config_reply_t_handler
350   (vl_api_snat_show_config_reply_t *mp)
351 {
352   snat_test_main_t * sm = &snat_test_main;
353   vat_main_t *vam = sm->vat_main;
354   i32 retval = ntohl (mp->retval);
355
356   if (retval >= 0)
357     {
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)
369         {
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");
374         }
375     }
376   vam->retval = retval;
377   vam->result_ready = 1;
378 }
379
380 static int api_snat_show_config(vat_main_t * vam)
381 {
382   snat_test_main_t * sm = &snat_test_main;
383   f64 timeout;
384   vl_api_snat_show_config_t * mp;
385
386   if (vam->json_output)
387     {
388       clib_warning ("JSON output not supported for snat_show_config");
389       return -99;
390     }
391
392   M(SNAT_SHOW_CONFIG, snat_show_config);
393   S; W;
394   /* NOTREACHED */
395   return 0;
396 }
397
398 /* 
399  * List of messages that the api test plugin sends,
400  * and that the data plane plugin processes
401  */
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, "")
410
411 void vat_api_hookup (vat_main_t *vam)
412 {
413   snat_test_main_t * sm __attribute__((unused)) = &snat_test_main;
414   /* Hook up handlers for replies from the data plane plug-in */
415 #define _(N,n)                                                  \
416   vl_msg_api_set_handlers((VL_API_##N + sm->msg_id_base),       \
417                           #n,                                   \
418                           vl_api_##n##_t_handler,               \
419                           vl_noop_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;
424 #undef _
425
426   /* API messages we can send */
427 #define _(n,h) hash_set_mem (vam->function_by_name, #n, api_##n);
428   foreach_vpe_api_msg;
429 #undef _    
430     
431   /* Help strings */
432 #define _(n,h) hash_set_mem (vam->help_by_name, #n, h);
433   foreach_vpe_api_msg;
434 #undef _
435 }
436
437 clib_error_t * vat_plugin_register (vat_main_t *vam)
438 {
439   snat_test_main_t * sm = &snat_test_main;
440   u8 * name;
441
442   sm->vat_main = vam;
443
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);
447
448   if (sm->msg_id_base != (u16) ~0)
449     vat_api_hookup (vam);
450   
451   vec_free(name);
452   
453   return 0;
454 }