5023afd7d634171423e76861449cf76a6303ff2d
[vpp.git] / src / plugins / ioam / export / ioam_export_test.c
1 /*
2  * Copyright (c) 2016 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  * ioam_export_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 <vlibsocket/api.h>
25 #include <vppinfra/error.h>
26
27 #define __plugin_msg_base export_test_main.msg_id_base
28 #include <vlibapi/vat_helper_macros.h>
29
30
31 /* Declare message IDs */
32 #include <ioam/export/ioam_export_msg_enum.h>
33
34 /* define message structures */
35 #define vl_typedefs
36 #include <ioam/export/ioam_export_all_api_h.h>
37 #undef vl_typedefs
38
39 /* declare message handlers for each api */
40
41 #define vl_endianfun            /* define message structures */
42 #include <ioam/export/ioam_export_all_api_h.h>
43 #undef vl_endianfun
44
45 /* instantiate all the print functions we know about */
46 #define vl_print(handle, ...)
47 #define vl_printfun
48 #include <ioam/export/ioam_export_all_api_h.h>
49 #undef vl_printfun
50
51 /* Get the API version number. */
52 #define vl_api_version(n,v) static u32 api_version=(v);
53 #include <ioam/export/ioam_export_all_api_h.h>
54 #undef vl_api_version
55
56
57 typedef struct
58 {
59   /* API message ID base */
60   u16 msg_id_base;
61   vat_main_t *vat_main;
62 } export_test_main_t;
63
64 export_test_main_t export_test_main;
65
66 #define foreach_standard_reply_retval_handler   \
67 _(ioam_export_ip6_enable_disable_reply)
68
69 #define _(n)                                            \
70     static void vl_api_##n##_t_handler                  \
71     (vl_api_##n##_t * mp)                               \
72     {                                                   \
73         vat_main_t * vam = export_test_main.vat_main;   \
74         i32 retval = ntohl(mp->retval);                 \
75         if (vam->async_mode) {                          \
76             vam->async_errors += (retval < 0);          \
77         } else {                                        \
78             vam->retval = retval;                       \
79             vam->result_ready = 1;                      \
80         }                                               \
81     }
82 foreach_standard_reply_retval_handler;
83 #undef _
84
85 /*
86  * Table of message reply handlers, must include boilerplate handlers
87  * we just generated
88  */
89 #define foreach_vpe_api_reply_msg                                       \
90 _(IOAM_EXPORT_IP6_ENABLE_DISABLE_REPLY, ioam_export_ip6_enable_disable_reply)
91
92
93 static int
94 api_ioam_export_ip6_enable_disable (vat_main_t * vam)
95 {
96   unformat_input_t *i = vam->input;
97   int is_disable = 0;
98   vl_api_ioam_export_ip6_enable_disable_t *mp;
99   int ret;
100
101   /* Parse args required to build the message */
102   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
103     {
104       if (unformat (i, "disable"))
105         is_disable = 1;
106       else
107         break;
108     }
109
110   /* Construct the API message */
111   M(IOAM_EXPORT_IP6_ENABLE_DISABLE, mp);
112   mp->is_disable = is_disable;
113
114   /* send it... */
115   S(mp);
116
117   /* Wait for a reply... */
118   W (ret);
119   return ret;
120 }
121
122 /*
123  * List of messages that the api test plugin sends,
124  * and that the data plane plugin processes
125  */
126 #define foreach_vpe_api_msg \
127 _(ioam_export_ip6_enable_disable, "<intfc> [disable]")
128
129 static void
130 ioam_export_vat_api_hookup (vat_main_t * vam)
131 {
132   export_test_main_t *sm = &export_test_main;
133   /* Hook up handlers for replies from the data plane plug-in */
134 #define _(N,n)                                                  \
135     vl_msg_api_set_handlers((VL_API_##N + sm->msg_id_base),     \
136                            #n,                                  \
137                            vl_api_##n##_t_handler,              \
138                            vl_noop_handler,                     \
139                            vl_api_##n##_t_endian,               \
140                            vl_api_##n##_t_print,                \
141                            sizeof(vl_api_##n##_t), 1);
142   foreach_vpe_api_reply_msg;
143 #undef _
144
145   /* API messages we can send */
146 #define _(n,h) hash_set_mem (vam->function_by_name, #n, api_##n);
147   foreach_vpe_api_msg;
148 #undef _
149
150   /* Help strings */
151 #define _(n,h) hash_set_mem (vam->help_by_name, #n, h);
152   foreach_vpe_api_msg;
153 #undef _
154 }
155
156 clib_error_t *
157 vat_plugin_register (vat_main_t * vam)
158 {
159   export_test_main_t *sm = &export_test_main;
160   u8 *name;
161
162   sm->vat_main = vam;
163
164   name = format (0, "ioam_export_%08x%c", api_version, 0);
165   sm->msg_id_base = vl_client_get_first_plugin_msg_id ((char *) name);
166
167   if (sm->msg_id_base != (u16) ~ 0)
168     ioam_export_vat_api_hookup (vam);
169
170   vec_free (name);
171
172   return 0;
173 }