nsim: remove api boilerplate
[vpp.git] / src / plugins / nsim / nsim_test.c
1 /*
2  * nsim.c - skeleton vpp-api-test plug-in
3  *
4  * Copyright (c) <current-year> <your-organization>
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 <nsim/nsim.api_enum.h>
26 #include <nsim/nsim.api_types.h>
27
28 typedef struct
29 {
30   /* API message ID base */
31   u16 msg_id_base;
32   vat_main_t *vat_main;
33 } nsim_test_main_t;
34
35 nsim_test_main_t nsim_test_main;
36
37 #define __plugin_msg_base nsim_test_main.msg_id_base
38 #include <vlibapi/vat_helper_macros.h>
39
40 static int
41 api_nsim_cross_connect_enable_disable (vat_main_t * vam)
42 {
43   unformat_input_t *i = vam->input;
44   int enable_disable = 1;
45   u32 sw_if_index0 = ~0;
46   u32 sw_if_index1 = ~0;
47   u32 tmp;
48   vl_api_nsim_cross_connect_enable_disable_t *mp;
49   int ret;
50
51   /* Parse args required to build the message */
52   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
53     {
54       if (unformat (i, "%U", unformat_sw_if_index, vam, &tmp))
55         {
56           if (sw_if_index0 == ~0)
57             sw_if_index0 = tmp;
58           else
59             sw_if_index1 = tmp;
60         }
61       else if (unformat (i, "sw_if_index %d", &tmp))
62         {
63           if (sw_if_index0 == ~0)
64             sw_if_index0 = tmp;
65           else
66             sw_if_index1 = tmp;
67         }
68       else if (unformat (i, "disable"))
69         enable_disable = 0;
70       else
71         break;
72     }
73
74   if (sw_if_index0 == ~0 || sw_if_index1 == ~0)
75     {
76       errmsg ("missing interface name / explicit sw_if_index number \n");
77       return -99;
78     }
79
80   /* Construct the API message */
81   M (NSIM_CROSS_CONNECT_ENABLE_DISABLE, mp);
82   mp->sw_if_index0 = ntohl (sw_if_index0);
83   mp->sw_if_index1 = ntohl (sw_if_index1);
84   mp->enable_disable = enable_disable;
85
86   /* send it... */
87   S (mp);
88
89   /* Wait for a reply... */
90   W (ret);
91   return ret;
92 }
93
94 static int
95 api_nsim_output_feature_enable_disable (vat_main_t * vam)
96 {
97   unformat_input_t *i = vam->input;
98   int enable_disable = 1;
99   u32 sw_if_index = ~0;
100   vl_api_nsim_output_feature_enable_disable_t *mp;
101   int ret;
102
103   /* Parse args required to build the message */
104   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
105     {
106       if (unformat (i, "%U", unformat_sw_if_index, vam, &sw_if_index))
107         ;
108       else if (unformat (i, "sw_if_index %d", &sw_if_index))
109         ;
110       else if (unformat (i, "disable"))
111         enable_disable = 0;
112       else
113         break;
114     }
115
116   if (sw_if_index == ~0)
117     {
118       errmsg ("missing interface name / explicit sw_if_index number \n");
119       return -99;
120     }
121
122   /* Construct the API message */
123   M (NSIM_OUTPUT_FEATURE_ENABLE_DISABLE, mp);
124   mp->sw_if_index = ntohl (sw_if_index);
125   mp->enable_disable = enable_disable;
126
127   /* send it... */
128   S (mp);
129
130   /* Wait for a reply... */
131   W (ret);
132   return ret;
133 }
134
135 static uword
136 unformat_delay (unformat_input_t * input, va_list * args)
137 {
138   f64 *result = va_arg (*args, f64 *);
139   f64 tmp;
140
141   if (unformat (input, "%f us", &tmp))
142     *result = tmp * 1e-6;
143   else if (unformat (input, "%f ms", &tmp))
144     *result = tmp * 1e-3;
145   else if (unformat (input, "%f sec", &tmp))
146     *result = tmp;
147   else
148     return 0;
149
150   return 1;
151 }
152
153 static uword
154 unformat_bandwidth (unformat_input_t * input, va_list * args)
155 {
156   f64 *result = va_arg (*args, f64 *);
157   f64 tmp;
158
159   if (unformat (input, "%f gbit", &tmp))
160     *result = tmp * 1e9;
161   else if (unformat (input, "%f gbyte", &tmp))
162     *result = tmp * 8e9;
163   else
164     return 0;
165   return 1;
166 }
167
168 static int
169 api_nsim_configure (vat_main_t * vam)
170 {
171   vl_api_nsim_configure_t *mp;
172   unformat_input_t *i = vam->input;
173   f64 delay = 0.0, bandwidth = 0.0;
174   f64 packet_size = 1500.0;
175   u32 packets_per_drop = 0;
176   int ret;
177
178   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
179     {
180       if (unformat (i, "delay %U", unformat_delay, &delay))
181         ;
182       else if (unformat (i, "bandwidth %U", unformat_bandwidth, &bandwidth))
183         ;
184       else if (unformat (i, "packet-size %f", &packet_size))
185         ;
186       else if (unformat (i, "packets-per-drop %u", &packets_per_drop))
187         ;
188       else
189         break;
190     }
191
192   if (delay == 0.0 || bandwidth == 0.0)
193     {
194       errmsg ("must specify delay and bandwidth");
195       return -99;
196     }
197
198   /* Construct the API message */
199   M (NSIM_CONFIGURE, mp);
200   mp->delay_in_usec = (u32) (delay * 1e6);
201   mp->delay_in_usec = ntohl (mp->delay_in_usec);
202   mp->average_packet_size = (u32) (packet_size);
203   mp->average_packet_size = ntohl (mp->average_packet_size);
204   mp->bandwidth_in_bits_per_second = (u64) (bandwidth);
205   mp->bandwidth_in_bits_per_second =
206     clib_host_to_net_u64 (mp->bandwidth_in_bits_per_second);
207   mp->packets_per_drop = ntohl (packets_per_drop);
208
209   /* send it... */
210   S (mp);
211
212   /* Wait for a reply... */
213   W (ret);
214   return ret;
215 }
216
217 #include <nsim/nsim.api_test.c>
218
219 /*
220  * fd.io coding-style-patch-verification: ON
221  *
222  * Local Variables:
223  * eval: (c-set-style "gnu")
224  * End:
225  */