2e24ddc8c0a8c362c4d313e5356af301047f937e
[vpp.git] / src / plugins / cdp / cdp_test.c
1 /*
2  * cdp.c - vpp-api-test cdp protocol plug-in
3  *
4  * Copyright (c) 2011-2018 by Cisco and/or its affiliates.
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at:
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 #include <vat/vat.h>
18 #include <vlibapi/api.h>
19 #include <vlibmemory/api.h>
20 #include <vppinfra/error.h>
21
22 uword unformat_sw_if_index (unformat_input_t * input, va_list * args);
23
24 /* Declare message IDs */
25 #include <cdp/cdp_msg_enum.h>
26
27 /* define message structures */
28 #define vl_typedefs
29 #include <cdp/cdp_all_api_h.h>
30 #undef vl_typedefs
31
32 /* declare message handlers for each api */
33
34 #define vl_endianfun            /* define message structures */
35 #include <cdp/cdp_all_api_h.h>
36 #undef vl_endianfun
37
38 /* instantiate all the print functions we know about */
39 #define vl_print(handle, ...)
40 #define vl_printfun
41 #include <cdp/cdp_all_api_h.h>
42 #undef vl_printfun
43
44 /* Get the API version number. */
45 #define vl_api_version(n,v) static u32 api_version=(v);
46 #include <cdp/cdp_all_api_h.h>
47 #undef vl_api_version
48
49
50 typedef struct
51 {
52   /* API message ID base */
53   u16 msg_id_base;
54   vat_main_t *vat_main;
55 } cdp_test_main_t;
56
57 cdp_test_main_t cdp_test_main;
58
59 #define __plugin_msg_base cdp_test_main.msg_id_base
60 #include <vlibapi/vat_helper_macros.h>
61
62 #define foreach_standard_reply_retval_handler   \
63 _(cdp_enable_disable_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 = cdp_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 _(CDP_ENABLE_DISABLE_REPLY, cdp_enable_disable_reply)
87
88 static int
89 api_cdp_enable_disable (vat_main_t * vam)
90 {
91   unformat_input_t *i = vam->input;
92   int enable_disable = 1;
93   vl_api_cdp_enable_disable_t *mp;
94   int ret;
95
96   /* Parse args required to build the message */
97   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
98     {
99       if (unformat (i, "disable"))
100         enable_disable = 0;
101       else if (unformat (i, "enable"))
102         enable_disable = 1;
103       else
104         break;
105     }
106
107   /* Construct the API message */
108   M (CDP_ENABLE_DISABLE, mp);
109   mp->enable_disable = enable_disable;
110
111   /* send it... */
112   S (mp);
113
114   /* Wait for a reply... */
115   W (ret);
116   return ret;
117 }
118
119 /*
120  * List of messages that the api test plugin sends,
121  * and that the data plane plugin processes
122  */
123 #define foreach_vpe_api_msg \
124 _(cdp_enable_disable, "enable | disable")
125
126 static void
127 cdp_api_hookup (vat_main_t * vam)
128 {
129   cdp_test_main_t *sm = &cdp_test_main;
130   /* Hook up handlers for replies from the data plane plug-in */
131 #define _(N,n)                                                  \
132     vl_msg_api_set_handlers((VL_API_##N + sm->msg_id_base),     \
133                            #n,                                  \
134                            vl_api_##n##_t_handler,              \
135                            vl_noop_handler,                     \
136                            vl_api_##n##_t_endian,               \
137                            vl_api_##n##_t_print,                \
138                            sizeof(vl_api_##n##_t), 1);
139   foreach_vpe_api_reply_msg;
140 #undef _
141
142   /* API messages we can send */
143 #define _(n,h) hash_set_mem (vam->function_by_name, #n, api_##n);
144   foreach_vpe_api_msg;
145 #undef _
146
147   /* Help strings */
148 #define _(n,h) hash_set_mem (vam->help_by_name, #n, h);
149   foreach_vpe_api_msg;
150 #undef _
151 }
152
153 VAT_PLUGIN_REGISTER (cdp);
154
155 /*
156  * fd.io coding-style-patch-verification: ON
157  *
158  * Local Variables:
159  * eval: (c-set-style "gnu")
160  * End:
161  */