Revert "Add support for MAP-T CE (VPP-1058)"
[vpp.git] / src / plugins / map / map_test.c
1 /*
2  * Copyright (c) 2018 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 /*
16  *------------------------------------------------------------------
17  * map_test.c - test harness plugin
18  *------------------------------------------------------------------
19  */
20
21 #include <vat/vat.h>
22 #include <vlibapi/api.h>
23 #include <vlibmemory/api.h>
24 #include <vppinfra/error.h>
25 #include <vnet/ip/ip.h>
26
27 #define __plugin_msg_base map_test_main.msg_id_base
28 #include <vlibapi/vat_helper_macros.h>
29
30 /* Declare message IDs */
31 #include <map/map_msg_enum.h>
32
33 /* Get CRC codes of the messages defined outside of this plugin */
34 #define vl_msg_name_crc_list
35 #include <vpp/api/vpe_all_api_h.h>
36 #undef vl_msg_name_crc_list
37
38 /* define message structures */
39 #define vl_typedefs
40 #include <vpp/api/vpe_all_api_h.h>
41 #include <map/map_all_api_h.h> 
42 #undef vl_typedefs
43
44 /* declare message handlers for each api */
45
46 #define vl_endianfun             /* define message structures */
47 #include <map/map_all_api_h.h> 
48 #undef vl_endianfun
49
50 /* instantiate all the print functions we know about */
51 #define vl_print(handle, ...)
52 #define vl_printfun
53 #include <map/map_all_api_h.h> 
54 #undef vl_printfun
55
56 /* Get the API version number. */
57 #define vl_api_version(n,v) static u32 api_version=(v);
58 #include <map/map_all_api_h.h>
59 #undef vl_api_version
60
61 typedef struct {
62   /* API message ID base */
63   u16 msg_id_base;
64   u32 ping_id;
65   vat_main_t *vat_main;
66 } map_test_main_t;
67
68 map_test_main_t map_test_main;
69
70 #define foreach_standard_reply_retval_handler   \
71 _(map_del_domain_reply)                         \
72 _(map_add_del_rule_reply)
73
74 #define _(n)                                                            \
75     static void vl_api_##n##_t_handler                                  \
76     (vl_api_##n##_t * mp)                                               \
77     {                                                                   \
78         vat_main_t * vam = map_test_main.vat_main;                      \
79         i32 retval = ntohl(mp->retval);                                 \
80         if (vam->json_output) {                                         \
81           vat_json_node_t node;                                         \
82           vat_json_init_object (&node);                                 \
83           vat_json_object_add_int (&node, "retval", ntohl (mp->retval)); \
84           vat_json_print (vam->ofp, &node);                             \
85           vat_json_free (&node);                                        \
86           return;                                                       \
87         }                                                               \
88         if (vam->async_mode) {                                          \
89             vam->async_errors += (retval < 0);                          \
90         } else {                                                        \
91             vam->retval = retval;                                       \
92             vam->result_ready = 1;                                      \
93         }                                                               \
94     }
95 foreach_standard_reply_retval_handler;
96 #undef _
97
98 /* 
99  * Table of message reply handlers, must include boilerplate handlers
100  * we just generated
101  */
102 #define foreach_vpe_api_reply_msg                       \
103 _(MAP_ADD_DOMAIN_REPLY, map_add_domain_reply)           \
104 _(MAP_DEL_DOMAIN_REPLY, map_del_domain_reply)           \
105 _(MAP_ADD_DEL_RULE_REPLY, map_add_del_rule_reply)       \
106 _(MAP_DOMAIN_DETAILS, map_domain_details)
107
108 static int
109 api_map_add_domain (vat_main_t * vam)
110 {
111   unformat_input_t *i = vam->input;
112   vl_api_map_add_domain_t *mp;
113
114   ip4_address_t ip4_prefix;
115   ip6_address_t ip6_prefix;
116   ip6_address_t ip6_src;
117   u32 num_m_args = 0;
118   u32 ip6_prefix_len = 0, ip4_prefix_len = 0, ea_bits_len = 0, psid_offset =
119     0, psid_length = 0;
120   u8 is_translation = 0;
121   u32 mtu = 0;
122   u32 ip6_src_len = 128;
123   int ret;
124
125   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
126     {
127       if (unformat (i, "ip4-pfx %U/%d", unformat_ip4_address,
128                    &ip4_prefix, &ip4_prefix_len))
129        num_m_args++;
130       else if (unformat (i, "ip6-pfx %U/%d", unformat_ip6_address,
131                         &ip6_prefix, &ip6_prefix_len))
132        num_m_args++;
133       else
134        if (unformat
135            (i, "ip6-src %U/%d", unformat_ip6_address, &ip6_src,
136             &ip6_src_len))
137        num_m_args++;
138       else if (unformat (i, "ip6-src %U", unformat_ip6_address, &ip6_src))
139        num_m_args++;
140       else if (unformat (i, "ea-bits-len %d", &ea_bits_len))
141        num_m_args++;
142       else if (unformat (i, "psid-offset %d", &psid_offset))
143        num_m_args++;
144       else if (unformat (i, "psid-len %d", &psid_length))
145        num_m_args++;
146       else if (unformat (i, "mtu %d", &mtu))
147        num_m_args++;
148       else if (unformat (i, "map-t"))
149        is_translation = 1;
150       else
151        {
152          clib_warning ("parse error '%U'", format_unformat_error, i);
153          return -99;
154        }
155     }
156
157   if (num_m_args < 3)
158     {
159       errmsg ("mandatory argument(s) missing");
160       return -99;
161     }
162
163   /* Construct the API message */
164   M (MAP_ADD_DOMAIN, mp);
165
166   clib_memcpy (mp->ip4_prefix, &ip4_prefix, sizeof (ip4_prefix));
167   mp->ip4_prefix_len = ip4_prefix_len;
168
169   clib_memcpy (mp->ip6_prefix, &ip6_prefix, sizeof (ip6_prefix));
170   mp->ip6_prefix_len = ip6_prefix_len;
171
172   clib_memcpy (mp->ip6_src, &ip6_src, sizeof (ip6_src));
173   mp->ip6_src_prefix_len = ip6_src_len;
174
175   mp->ea_bits_len = ea_bits_len;
176   mp->psid_offset = psid_offset;
177   mp->psid_length = psid_length;
178   mp->is_translation = is_translation;
179   mp->mtu = htons (mtu);
180
181   /* send it... */
182   S (mp);
183
184   /* Wait for a reply, return good/bad news  */
185   W (ret);
186   return ret;
187 }
188 static int
189 api_map_del_domain (vat_main_t * vam)
190 {
191   unformat_input_t *i = vam->input;
192   vl_api_map_del_domain_t *mp;
193
194   u32 num_m_args = 0;
195   u32 index;
196   int ret;
197
198   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
199     {
200       if (unformat (i, "index %d", &index))
201        num_m_args++;
202       else
203        {
204          clib_warning ("parse error '%U'", format_unformat_error, i);
205          return -99;
206        }
207     }
208
209   if (num_m_args != 1)
210     {
211       errmsg ("mandatory argument(s) missing");
212       return -99;
213     }
214
215   /* Construct the API message */
216   M (MAP_DEL_DOMAIN, mp);
217
218   mp->index = ntohl (index);
219
220   /* send it... */
221   S (mp);
222
223   /* Wait for a reply, return good/bad news  */
224   W (ret);
225   return ret;
226 }
227
228 static int
229 api_map_add_del_rule (vat_main_t * vam)
230 {
231   unformat_input_t *i = vam->input;
232   vl_api_map_add_del_rule_t *mp;
233   u8 is_add = 1;
234   ip6_address_t ip6_dst;
235   u32 num_m_args = 0, index, psid = 0;
236   int ret;
237
238   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
239     {
240       if (unformat (i, "index %d", &index))
241        num_m_args++;
242       else if (unformat (i, "psid %d", &psid))
243        num_m_args++;
244       else if (unformat (i, "dst %U", unformat_ip6_address, &ip6_dst))
245        num_m_args++;
246       else if (unformat (i, "del"))
247        {
248          is_add = 0;
249        }
250       else
251        {
252          clib_warning ("parse error '%U'", format_unformat_error, i);
253          return -99;
254        }
255     }
256
257   /* Construct the API message */
258   M (MAP_ADD_DEL_RULE, mp);
259
260   mp->index = ntohl (index);
261   mp->is_add = is_add;
262   clib_memcpy (mp->ip6_dst, &ip6_dst, sizeof (ip6_dst));
263   mp->psid = ntohs (psid);
264
265   /* send it... */
266   S (mp);
267
268   /* Wait for a reply, return good/bad news  */
269   W (ret);
270   return ret;
271 }
272 static int
273 api_map_domain_dump (vat_main_t * vam)
274 {
275   map_test_main_t *mm = &map_test_main;
276   vl_api_map_domain_dump_t *mp;
277   vl_api_control_ping_t *mp_ping;
278   int ret;
279
280   /* Construct the API message */
281   M (MAP_DOMAIN_DUMP, mp);
282
283   /* send it... */
284   S (mp);
285
286   /* Use a control ping for synchronization */
287   mp_ping = vl_msg_api_alloc_as_if_client (sizeof (*mp_ping));
288   mp_ping->_vl_msg_id = htons (mm->ping_id);
289   mp_ping->client_index = vam->my_client_index;
290
291   fformat (vam->ofp, "Sending ping id=%d\n", mm->ping_id);
292
293   vam->result_ready = 0;
294   S (mp_ping);
295
296   W (ret);
297
298   return ret;
299 }
300
301 static int
302 api_map_rule_dump (vat_main_t * vam)
303 {
304   map_test_main_t *mm = &map_test_main;
305   unformat_input_t *i = vam->input;
306   vl_api_map_rule_dump_t *mp;
307   vl_api_control_ping_t *mp_ping;
308   u32 domain_index = ~0;
309   int ret;
310
311   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
312     {
313       if (unformat (i, "index %u", &domain_index))
314        ;
315       else
316        break;
317     }
318
319   if (domain_index == ~0)
320     {
321       clib_warning ("parse error: domain index expected");
322       return -99;
323     }
324
325   /* Construct the API message */
326   M (MAP_RULE_DUMP, mp);
327
328   mp->domain_index = htonl (domain_index);
329
330   /* send it... */
331   S (mp);
332
333   /* Use a control ping for synchronization */
334   /* Use a control ping for synchronization */
335   mp_ping = vl_msg_api_alloc_as_if_client (sizeof (*mp_ping));
336   mp_ping->_vl_msg_id = htons (mm->ping_id);
337   mp_ping->client_index = vam->my_client_index;
338
339   vam->result_ready = 0;
340   S (mp_ping);
341
342   W (ret);
343   return ret;
344 }
345
346 static void vl_api_map_add_domain_reply_t_handler
347   (vl_api_map_add_domain_reply_t * mp)
348 {
349   vat_main_t *vam = &vat_main;
350   i32 retval = ntohl (mp->retval);
351
352   if (vam->json_output) {
353     vat_json_node_t node;
354     vat_json_init_object (&node);
355     vat_json_object_add_int (&node, "retval", ntohl (mp->retval));
356     vat_json_object_add_uint (&node, "index", ntohl (mp->index));
357     vat_json_print (vam->ofp, &node);
358     vat_json_free (&node);
359   }
360
361   if (vam->async_mode)
362     {
363       vam->async_errors += (retval < 0);
364     }
365   else
366     {
367       vam->retval = retval;
368       vam->result_ready = 1;
369     }
370 }
371
372 static void vl_api_map_domain_details_t_handler_json
373   (vl_api_map_domain_details_t * mp)
374 {
375   vat_json_node_t *node = NULL;
376   vat_main_t *vam = &vat_main;
377   struct in6_addr ip6;
378   struct in_addr ip4;
379
380   if (VAT_JSON_ARRAY != vam->json_tree.type)
381     {
382       ASSERT (VAT_JSON_NONE == vam->json_tree.type);
383       vat_json_init_array (&vam->json_tree);
384     }
385
386   node = vat_json_array_add (&vam->json_tree);
387   vat_json_init_object (node);
388
389   vat_json_object_add_uint (node, "domain_index",
390                            clib_net_to_host_u32 (mp->domain_index));
391   clib_memcpy (&ip6, mp->ip6_prefix, sizeof (ip6));
392   vat_json_object_add_ip6 (node, "ip6_prefix", ip6);
393   clib_memcpy (&ip4, mp->ip4_prefix, sizeof (ip4));
394   vat_json_object_add_ip4 (node, "ip4_prefix", ip4);
395   clib_memcpy (&ip6, mp->ip6_src, sizeof (ip6));
396   vat_json_object_add_ip6 (node, "ip6_src", ip6);
397   vat_json_object_add_int (node, "ip6_prefix_len", mp->ip6_prefix_len);
398   vat_json_object_add_int (node, "ip4_prefix_len", mp->ip4_prefix_len);
399   vat_json_object_add_int (node, "ip6_src_len", mp->ip6_src_len);
400   vat_json_object_add_int (node, "ea_bits_len", mp->ea_bits_len);
401   vat_json_object_add_int (node, "psid_offset", mp->psid_offset);
402   vat_json_object_add_int (node, "psid_length", mp->psid_length);
403   vat_json_object_add_uint (node, "flags", mp->flags);
404   vat_json_object_add_uint (node, "mtu", clib_net_to_host_u16 (mp->mtu));
405   vat_json_object_add_int (node, "is_translation", mp->is_translation);
406 }
407
408 static void vl_api_map_domain_details_t_handler
409   (vl_api_map_domain_details_t * mp)
410 {
411   vat_main_t *vam = &vat_main;
412
413   if (vam->json_output)
414     return vl_api_map_domain_details_t_handler_json (mp);
415
416   if (mp->is_translation)
417     {
418       print (vam->ofp,
419             "* %U/%d (ipv4-prefix) %U/%d (ipv6-prefix) %U/%d (ip6-src) index: %u",
420             format_ip4_address, mp->ip4_prefix, mp->ip4_prefix_len,
421             format_ip6_address, mp->ip6_prefix, mp->ip6_prefix_len,
422             format_ip6_address, mp->ip6_src, mp->ip6_src_len,
423             clib_net_to_host_u32 (mp->domain_index));
424     }
425   else
426     {
427       print (vam->ofp,
428             "* %U/%d (ipv4-prefix) %U/%d (ipv6-prefix) %U (ip6-src) index: %u",
429             format_ip4_address, mp->ip4_prefix, mp->ip4_prefix_len,
430             format_ip6_address, mp->ip6_prefix, mp->ip6_prefix_len,
431             format_ip6_address, mp->ip6_src,
432             clib_net_to_host_u32 (mp->domain_index));
433     }
434   print (vam->ofp, "  ea-len %d psid-offset %d psid-len %d mtu %d %s",
435         mp->ea_bits_len, mp->psid_offset, mp->psid_length, mp->mtu,
436         mp->is_translation ? "map-t" : "");
437 }
438
439 /* 
440  * List of messages that the api test plugin sends,
441  * and that the data plane plugin processes
442  */
443 #define foreach_vpe_api_msg                             \
444 _(map_add_domain,                                       \
445   "ip4-pfx <ip4pfx> ip6-pfx <ip6pfx> "                  \
446   "ip6-src <ip6addr> "                                  \
447   "ea-bits-len <n> psid-offset <n> psid-len <n>")       \
448 _(map_del_domain, "index <n>")                          \
449 _(map_add_del_rule,                                     \
450   "index <n> psid <n> dst <ip6addr> [del]")             \
451 _(map_domain_dump, "")                                  \
452 _(map_rule_dump, "index <map-domain>")
453
454 static void map_api_hookup (vat_main_t *vam)
455 {
456     map_test_main_t * mm = &map_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 + mm->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) \
471     hash_set_mem (vam->function_by_name, #n, api_##n);
472     foreach_vpe_api_msg;
473 #undef _    
474     
475     /* Help strings */
476 #define _(n,h) hash_set_mem (vam->help_by_name, #n, h);
477     foreach_vpe_api_msg;
478 #undef _
479 }
480
481 clib_error_t * vat_plugin_register (vat_main_t *vam)
482 {
483   map_test_main_t * mm = &map_test_main;
484   u8 * name;
485
486   mm->vat_main = vam;
487
488   name = format (0, "map_%08x%c", api_version, 0);
489   mm->msg_id_base = vl_client_get_first_plugin_msg_id ((char *) name);
490
491   /* Get the control ping ID */
492 #define _(id,n,crc) \
493   const char *id ## _CRC __attribute__ ((unused)) = #n "_" #crc;
494   foreach_vl_msg_name_crc_vpe;
495 #undef _
496   mm->ping_id = vl_msg_api_get_msg_index ((u8 *) (VL_API_CONTROL_PING_CRC));
497
498   if (mm->msg_id_base != (u16) ~0)
499     map_api_hookup (vam);
500
501   vec_free(name);
502
503   return 0;
504 }