VPP-453: SNAT delete and dump addresses
[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 _(SNAT_ADDRESS_DETAILS, snat_address_details)
94
95 /* M: construct, but don't yet send a message */
96 #define M(T,t)                                                  \
97 do {                                                            \
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;                    \
103 } while(0);
104
105 #define M2(T,t,n)                                               \
106 do {                                                            \
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;                    \
112 } while(0);
113
114 /* S: send a message */
115 #define S (vl_msg_api_send_shmem (vam->vl_input_queue, (u8 *)&mp))
116
117 /* W: wait for results, with timeout */
118 #define W                                       \
119 do {                                            \
120     timeout = vat_time_now (vam) + 1.0;         \
121                                                 \
122     while (vat_time_now (vam) < timeout) {      \
123         if (vam->result_ready == 1) {           \
124             return (vam->retval);               \
125         }                                       \
126     }                                           \
127     return -99;                                 \
128 } while(0);
129
130 static int api_snat_add_address_range (vat_main_t * vam)
131 {
132   snat_test_main_t * sm = &snat_test_main;
133   unformat_input_t * i = vam->input;
134   f64 timeout;
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;
138   u8 is_add = 1;
139   int count;
140
141   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
142     {
143       if (unformat (i, "%U - %U",
144                     unformat_ip4_address, &start_addr,
145                     unformat_ip4_address, &end_addr))
146         ;
147       else if (unformat (i, "%U", unformat_ip4_address, &start_addr))
148         end_addr = start_addr;
149       else if (unformat (i, "del"))
150         is_add = 0;
151       else
152         {
153           clib_warning("unknown input '%U'", format_unformat_error, i);
154           return -99;
155         }
156     }
157
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);
160   
161   if (end_host_order < start_host_order)
162     {
163       errmsg ("end address less than start address\n");
164       return -99;
165     }
166
167   count = (end_host_order - start_host_order) + 1;
168
169   if (count > 1024)
170     {
171     errmsg ("%U - %U, %d addresses...\n",
172            format_ip4_address, &start_addr,
173            format_ip4_address, &end_addr,
174            count);
175     }
176   
177   M(SNAT_ADD_ADDRESS_RANGE, snat_add_address_range);
178
179   memcpy (mp->first_ip_address, &start_addr, 4);
180   memcpy (mp->last_ip_address, &end_addr, 4);
181   mp->is_ip4 = 1;
182   mp->is_add = is_add;
183
184   S; W;
185
186   /* NOTREACHED */
187   return 0;
188 }
189
190 static int api_snat_interface_add_del_feature (vat_main_t * vam)
191 {
192   snat_test_main_t * sm = &snat_test_main;
193   unformat_input_t * i = vam->input;
194   f64 timeout;
195   vl_api_snat_interface_add_del_feature_t * mp;
196   u32 sw_if_index;
197   u8 sw_if_index_set = 0;
198   u8 is_inside = 1; 
199   u8 is_add = 1;
200
201   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
202     {
203       if (unformat (i, "%U", unformat_sw_if_index, vam, &sw_if_index))
204         sw_if_index_set = 1;
205       else if (unformat (i, "sw_if_index %d", &sw_if_index))
206         sw_if_index_set = 1;
207       else if (unformat (i, "out"))
208         is_inside = 0;
209       else if (unformat (i, "in"))
210         is_inside = 1;
211       else if (unformat (i, "del"))
212         is_add = 0;
213       else
214         {
215           clib_warning("unknown input '%U'", format_unformat_error, i);
216           return -99;
217         }
218     }
219
220   if (sw_if_index_set == 0)
221     {
222       errmsg ("interface / sw_if_index required\n");
223       return -99;
224     }
225
226   M(SNAT_INTERFACE_ADD_DEL_FEATURE, snat_interface_add_del_feature);
227   mp->sw_if_index = ntohl(sw_if_index);
228   mp->is_add = is_add;
229   mp->is_inside = is_inside;
230   
231   S; W;
232   /* NOTREACHED */
233   return 0;
234 }
235
236 static int api_snat_add_static_mapping(vat_main_t * vam)
237 {
238   snat_test_main_t * sm = &snat_test_main;
239   unformat_input_t * i = vam->input;
240   f64 timeout;
241   vl_api_snat_add_static_mapping_t * mp;
242   u8 addr_set_n = 0;
243   u8 is_add = 1;
244   u8 addr_only = 1;
245   ip4_address_t local_addr, external_addr;
246   u32 local_port = 0, external_port = 0, vrf_id = ~0;
247
248   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
249     {
250       if (unformat (i, "local_addr %U", unformat_ip4_address, &local_addr))
251         addr_set_n++;
252       else if (unformat (i, "external_addr %U", unformat_ip4_address,
253                          &external_addr))
254         addr_set_n++;
255       else if (unformat (i, "local_port %u", &local_port))
256         addr_only = 0;
257       else if (unformat (i, "external_port %u", &external_port))
258         addr_only = 0;
259       else if (unformat (i, "vrf %u", &vrf_id))
260         ;
261       else if (unformat (i, "del"))
262         is_add = 0;
263       else
264         {
265           clib_warning("unknown input '%U'", format_unformat_error, i);
266           return -99;
267         }
268     }
269
270   if (addr_set_n != 2)
271     {
272       errmsg ("local_addr and remote_addr required\n");
273       return -99;
274     }
275
276   M(SNAT_ADD_STATIC_MAPPING, snat_add_static_mapping);
277   mp->is_add = is_add;
278   mp->is_ip4 = 1;
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);
285
286   S; W;
287   /* NOTREACHED */
288   return 0;
289 }
290
291 static void vl_api_snat_control_ping_reply_t_handler
292   (vl_api_snat_control_ping_reply_t * mp)
293 {
294   vat_main_t *vam = &vat_main;
295   i32 retval = ntohl (mp->retval);
296   if (vam->async_mode)
297     {
298       vam->async_errors += (retval < 0);
299     }
300   else
301     {
302       vam->retval = retval;
303       vam->result_ready = 1;
304     }
305 }
306
307 static void vl_api_snat_static_mapping_details_t_handler
308   (vl_api_snat_static_mapping_details_t *mp)
309 {
310   snat_test_main_t * sm = &snat_test_main;
311   vat_main_t *vam = sm->vat_main;
312
313   if (mp->addr_only)
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, "",
317                ntohl (mp->vrf_id));
318   else
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),
324                ntohl (mp->vrf_id));
325
326 }
327
328 static int api_snat_static_mapping_dump(vat_main_t * vam)
329 {
330   snat_test_main_t * sm = &snat_test_main;
331   f64 timeout;
332   vl_api_snat_static_mapping_dump_t * mp;
333
334   if (vam->json_output)
335     {
336       clib_warning ("JSON output not supported for snat_static_mapping_dump");
337       return -99;
338     }
339
340   fformat (vam->ofp, "%21s%21s\n", "local", "external");
341   fformat (vam->ofp, "%15s%6s%15s%6s%11s\n", "address", "port", "address",
342            "port", "vrf");
343
344   M(SNAT_STATIC_MAPPING_DUMP, snat_static_mapping_dump);
345   S;
346   /* Use a control ping for synchronization */
347   {
348     vl_api_snat_control_ping_t *mp;
349     M (SNAT_CONTROL_PING, snat_control_ping);
350     S;
351   }
352   W;
353   /* NOTREACHED */
354   return 0;
355 }
356
357 static void vl_api_snat_show_config_reply_t_handler
358   (vl_api_snat_show_config_reply_t *mp)
359 {
360   snat_test_main_t * sm = &snat_test_main;
361   vat_main_t *vam = sm->vat_main;
362   i32 retval = ntohl (mp->retval);
363
364   if (retval >= 0)
365     {
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)
377         {
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");
382         }
383     }
384   vam->retval = retval;
385   vam->result_ready = 1;
386 }
387
388 static int api_snat_show_config(vat_main_t * vam)
389 {
390   snat_test_main_t * sm = &snat_test_main;
391   f64 timeout;
392   vl_api_snat_show_config_t * mp;
393
394   if (vam->json_output)
395     {
396       clib_warning ("JSON output not supported for snat_show_config");
397       return -99;
398     }
399
400   M(SNAT_SHOW_CONFIG, snat_show_config);
401   S; W;
402   /* NOTREACHED */
403   return 0;
404 }
405
406 static void vl_api_snat_address_details_t_handler
407   (vl_api_snat_address_details_t *mp)
408 {
409   snat_test_main_t * sm = &snat_test_main;
410   vat_main_t *vam = sm->vat_main;
411
412   fformat (vam->ofp, "%U\n", format_ip4_address, &mp->ip_address);
413 }
414
415 static int api_snat_address_dump(vat_main_t * vam)
416 {
417   snat_test_main_t * sm = &snat_test_main;
418   f64 timeout;
419   vl_api_snat_address_dump_t * mp;
420
421   if (vam->json_output)
422     {
423       clib_warning ("JSON output not supported for snat_address_dump");
424       return -99;
425     }
426
427   M(SNAT_ADDRESS_DUMP, snat_address_dump);
428   S;
429   /* Use a control ping for synchronization */
430   {
431     vl_api_snat_control_ping_t *mp;
432     M (SNAT_CONTROL_PING, snat_control_ping);
433     S;
434   }
435   W;
436   /* NOTREACHED */
437   return 0;
438 }
439
440 /* 
441  * List of messages that the api test plugin sends,
442  * and that the data plane plugin processes
443  */
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, "")
453
454 void vat_api_hookup (vat_main_t *vam)
455 {
456   snat_test_main_t * sm __attribute__((unused)) = &snat_test_main;
457   /* Hook up handlers for replies from the data plane plug-in */
458 #define _(N,n)                                                  \
459   vl_msg_api_set_handlers((VL_API_##N + sm->msg_id_base),       \
460                           #n,                                   \
461                           vl_api_##n##_t_handler,               \
462                           vl_noop_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;
467 #undef _
468
469   /* API messages we can send */
470 #define _(n,h) hash_set_mem (vam->function_by_name, #n, api_##n);
471   foreach_vpe_api_msg;
472 #undef _    
473     
474   /* Help strings */
475 #define _(n,h) hash_set_mem (vam->help_by_name, #n, h);
476   foreach_vpe_api_msg;
477 #undef _
478 }
479
480 clib_error_t * vat_plugin_register (vat_main_t *vam)
481 {
482   snat_test_main_t * sm = &snat_test_main;
483   u8 * name;
484
485   sm->vat_main = vam;
486
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);
490
491   if (sm->msg_id_base != (u16) ~0)
492     vat_api_hookup (vam);
493   
494   vec_free(name);
495   
496   return 0;
497 }