STN: Fix stn_rules_dump/details to follow API convention
[vpp.git] / src / plugins / stn / stn_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 #include <vat/vat.h>
16 #include <vlibapi/api.h>
17 #include <vlibmemory/api.h>
18 #include <vppinfra/error.h>
19
20 #include <stn/stn.h>
21
22 #define __plugin_msg_base stn_test_main.msg_id_base
23 #include <vlibapi/vat_helper_macros.h>
24
25 /* Declare message IDs */
26 #include <stn/stn_msg_enum.h>
27
28 /* Get CRC codes of the messages defined outside of this plugin */
29 #define vl_msg_name_crc_list
30 #include <vpp/api/vpe_all_api_h.h>
31 #undef vl_msg_name_crc_list
32
33 /* define message structures */
34 #define vl_typedefs
35 #include <vpp/api/vpe_all_api_h.h>
36 #include <stn/stn_all_api_h.h>
37 #undef vl_typedefs
38
39 /* define message structures */
40 #define vl_endianfun
41 #include <stn/stn_all_api_h.h>
42 #undef vl_endianfun
43
44 /* instantiate all the print functions we know about */
45 #define vl_print(handle, ...)
46 #define vl_printfun
47 #include <stn/stn_all_api_h.h>
48 #undef vl_printfun
49
50 /* Get the API version number. */
51 #define vl_api_version(n,v) static u32 api_version=(v);
52 #include <stn/stn_all_api_h.h>
53 #undef vl_api_version
54
55 typedef struct
56 {
57   /* API message ID base */
58   u16 msg_id_base;
59   u32 ping_id;
60   vat_main_t *vat_main;
61 } stn_test_main_t;
62
63 stn_test_main_t stn_test_main;
64
65 /*
66  * Table of message reply handlers, must include boilerplate handlers
67  * we just generated
68  */
69 #define foreach_stn_api_reply_msg                       \
70 _(STN_RULES_DETAILS, stn_rules_details)
71
72 static int
73 api_stn_rules_dump (vat_main_t * vam)
74 {
75   stn_test_main_t *sm = &stn_test_main;
76   vl_api_stn_rules_dump_t *mp;
77   vl_api_control_ping_t *mp_ping;
78   int ret;
79
80   if (!vam->json_output)
81     {
82       print (vam->ofp, "%=20s", "STN Rules");
83     }
84
85   M (STN_RULES_DUMP, mp);
86   /* send it... */
87   S (mp);
88
89   /* Use a control ping for synchronization */
90   mp_ping = vl_msg_api_alloc_as_if_client (sizeof (*mp_ping));
91   mp_ping->_vl_msg_id = htons (sm->ping_id);
92   mp_ping->client_index = vam->my_client_index;
93
94   fformat (vam->ofp, "Sending ping id=%d\n", sm->ping_id);
95
96   vam->result_ready = 0;
97   S (mp_ping);
98
99   /* Wait for a reply... */
100   W (ret);
101   return ret;
102 }
103
104 static void
105 vl_api_stn_rules_details_t_handler (vl_api_stn_rules_details_t * mp)
106 {
107   vat_main_t *vam = stn_test_main.vat_main;
108   fformat (vam->ofp, "addr: %U sw_if_index: %u\n",
109            mp->is_ip4 ? format_ip4_address : format_ip6_address,
110            mp->ip_address, clib_net_to_host_u32 (mp->sw_if_index));
111 }
112
113 /*
114  * List of messages that the api test plugin sends,
115  * and that the data plane plugin processes
116  */
117 #define foreach_stn_api_msg                             \
118 _(stn_rules_dump, "")                                   \
119
120 static void
121 stn_vat_api_hookup (vat_main_t * vam)
122 {
123   stn_test_main_t *sm = &stn_test_main;
124   /* Hook up handlers for replies from the data plane plug-in */
125 #define _(N,n)                                                  \
126     vl_msg_api_set_handlers((VL_API_##N + sm->msg_id_base),     \
127                            #n,                                  \
128                            vl_api_##n##_t_handler,              \
129                            vl_noop_handler,                     \
130                            vl_api_##n##_t_endian,               \
131                            vl_api_##n##_t_print,                \
132                            sizeof(vl_api_##n##_t), 1);
133   foreach_stn_api_reply_msg;
134 #undef _
135
136   /* API messages we can send */
137 #define _(n,h) hash_set_mem (vam->function_by_name, #n, api_##n);
138   foreach_stn_api_msg;
139 #undef _
140
141   /* Help strings */
142 #define _(n,h) hash_set_mem (vam->help_by_name, #n, h);
143   foreach_stn_api_msg;
144 #undef _
145 }
146
147 clib_error_t *
148 vat_plugin_register (vat_main_t * vam)
149 {
150   stn_test_main_t *sm = &stn_test_main;
151   u8 *name;
152
153   sm->vat_main = vam;
154
155   name = format (0, "stn_%08x%c", api_version, 0);
156   sm->msg_id_base = vl_client_get_first_plugin_msg_id ((char *) name);
157
158   /* Get the control ping ID */
159 #define _(id,n,crc) \
160   const char *id ## _CRC __attribute__ ((unused)) = #n "_" #crc;
161   foreach_vl_msg_name_crc_vpe;
162 #undef _
163   sm->ping_id = vl_msg_api_get_msg_index ((u8 *) (VL_API_CONTROL_PING_CRC));
164
165   if (sm->msg_id_base != (u16) ~ 0)
166     stn_vat_api_hookup (vam);
167
168   vec_free (name);
169
170   return 0;
171 }
172
173 /*
174  * fd.io coding-style-patch-verification: ON
175  *
176  * Local Variables:
177  * eval: (c-set-style "gnu")
178  * End:
179  */