Line data Source code
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 0 : api_nsim_cross_connect_enable_disable (vat_main_t * vam)
42 : {
43 0 : unformat_input_t *i = vam->input;
44 0 : int enable_disable = 1;
45 0 : u32 sw_if_index0 = ~0;
46 0 : 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 0 : while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
53 : {
54 0 : if (unformat (i, "%U", unformat_sw_if_index, vam, &tmp))
55 : {
56 0 : if (sw_if_index0 == ~0)
57 0 : sw_if_index0 = tmp;
58 : else
59 0 : sw_if_index1 = tmp;
60 : }
61 0 : else if (unformat (i, "sw_if_index %d", &tmp))
62 : {
63 0 : if (sw_if_index0 == ~0)
64 0 : sw_if_index0 = tmp;
65 : else
66 0 : sw_if_index1 = tmp;
67 : }
68 0 : else if (unformat (i, "disable"))
69 0 : enable_disable = 0;
70 : else
71 0 : break;
72 : }
73 :
74 0 : if (sw_if_index0 == ~0 || sw_if_index1 == ~0)
75 : {
76 0 : errmsg ("missing interface name / explicit sw_if_index number \n");
77 0 : return -99;
78 : }
79 :
80 : /* Construct the API message */
81 0 : M (NSIM_CROSS_CONNECT_ENABLE_DISABLE, mp);
82 0 : mp->sw_if_index0 = ntohl (sw_if_index0);
83 0 : mp->sw_if_index1 = ntohl (sw_if_index1);
84 0 : mp->enable_disable = enable_disable;
85 :
86 : /* send it... */
87 0 : S (mp);
88 :
89 : /* Wait for a reply... */
90 0 : W (ret);
91 0 : return ret;
92 : }
93 :
94 : static int
95 0 : api_nsim_output_feature_enable_disable (vat_main_t * vam)
96 : {
97 0 : unformat_input_t *i = vam->input;
98 0 : int enable_disable = 1;
99 0 : 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 0 : while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
105 : {
106 0 : if (unformat (i, "%U", unformat_sw_if_index, vam, &sw_if_index))
107 : ;
108 0 : else if (unformat (i, "sw_if_index %d", &sw_if_index))
109 : ;
110 0 : else if (unformat (i, "disable"))
111 0 : enable_disable = 0;
112 : else
113 0 : break;
114 : }
115 :
116 0 : if (sw_if_index == ~0)
117 : {
118 0 : errmsg ("missing interface name / explicit sw_if_index number \n");
119 0 : return -99;
120 : }
121 :
122 : /* Construct the API message */
123 0 : M (NSIM_OUTPUT_FEATURE_ENABLE_DISABLE, mp);
124 0 : mp->sw_if_index = ntohl (sw_if_index);
125 0 : mp->enable_disable = enable_disable;
126 :
127 : /* send it... */
128 0 : S (mp);
129 :
130 : /* Wait for a reply... */
131 0 : W (ret);
132 0 : return ret;
133 : }
134 :
135 : static uword
136 0 : unformat_delay (unformat_input_t * input, va_list * args)
137 : {
138 0 : f64 *result = va_arg (*args, f64 *);
139 : f64 tmp;
140 :
141 0 : if (unformat (input, "%f us", &tmp))
142 0 : *result = tmp * 1e-6;
143 0 : else if (unformat (input, "%f ms", &tmp))
144 0 : *result = tmp * 1e-3;
145 0 : else if (unformat (input, "%f sec", &tmp))
146 0 : *result = tmp;
147 : else
148 0 : return 0;
149 :
150 0 : return 1;
151 : }
152 :
153 : static uword
154 0 : unformat_bandwidth (unformat_input_t * input, va_list * args)
155 : {
156 0 : f64 *result = va_arg (*args, f64 *);
157 : f64 tmp;
158 :
159 0 : if (unformat (input, "%f gbit", &tmp))
160 0 : *result = tmp * 1e9;
161 0 : else if (unformat (input, "%f gbyte", &tmp))
162 0 : *result = tmp * 8e9;
163 : else
164 0 : return 0;
165 0 : return 1;
166 : }
167 :
168 : static int
169 0 : api_nsim_configure (vat_main_t * vam)
170 : {
171 : vl_api_nsim_configure_t *mp;
172 0 : unformat_input_t *i = vam->input;
173 0 : f64 delay = 0.0, bandwidth = 0.0;
174 0 : f64 packet_size = 1500.0;
175 0 : u32 packets_per_drop = 0;
176 : int ret;
177 :
178 0 : while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
179 : {
180 0 : if (unformat (i, "delay %U", unformat_delay, &delay))
181 : ;
182 0 : else if (unformat (i, "bandwidth %U", unformat_bandwidth, &bandwidth))
183 : ;
184 0 : else if (unformat (i, "packet-size %f", &packet_size))
185 : ;
186 0 : else if (unformat (i, "packets-per-drop %u", &packets_per_drop))
187 : ;
188 : else
189 0 : break;
190 : }
191 :
192 0 : if (delay == 0.0 || bandwidth == 0.0)
193 : {
194 0 : errmsg ("must specify delay and bandwidth");
195 0 : return -99;
196 : }
197 :
198 : /* Construct the API message */
199 0 : M (NSIM_CONFIGURE, mp);
200 0 : mp->delay_in_usec = (u32) (delay * 1e6);
201 0 : mp->delay_in_usec = ntohl (mp->delay_in_usec);
202 0 : mp->average_packet_size = (u32) (packet_size);
203 0 : mp->average_packet_size = ntohl (mp->average_packet_size);
204 0 : mp->bandwidth_in_bits_per_second = (u64) (bandwidth);
205 0 : mp->bandwidth_in_bits_per_second =
206 0 : clib_host_to_net_u64 (mp->bandwidth_in_bits_per_second);
207 0 : mp->packets_per_drop = ntohl (packets_per_drop);
208 :
209 : /* send it... */
210 0 : S (mp);
211 :
212 : /* Wait for a reply... */
213 0 : W (ret);
214 0 : return ret;
215 : }
216 :
217 : static int
218 0 : api_nsim_configure2 (vat_main_t * vam)
219 : {
220 : vl_api_nsim_configure2_t *mp;
221 0 : unformat_input_t *i = vam->input;
222 0 : f64 delay = 0.0, bandwidth = 0.0;
223 0 : f64 packet_size = 1500.0;
224 0 : u32 packets_per_drop = 0, packets_per_reorder;
225 : int ret;
226 :
227 0 : while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
228 : {
229 0 : if (unformat (i, "delay %U", unformat_delay, &delay))
230 : ;
231 0 : else if (unformat (i, "bandwidth %U", unformat_bandwidth, &bandwidth))
232 : ;
233 0 : else if (unformat (i, "packet-size %f", &packet_size))
234 : ;
235 0 : else if (unformat (i, "packets-per-drop %u", &packets_per_drop))
236 : ;
237 0 : else if (unformat (i, "packets-per-reorder %u", &packets_per_reorder))
238 : ;
239 : else
240 0 : break;
241 : }
242 :
243 0 : if (delay == 0.0 || bandwidth == 0.0)
244 : {
245 0 : errmsg ("must specify delay and bandwidth");
246 0 : return -99;
247 : }
248 :
249 : /* Construct the API message */
250 0 : M (NSIM_CONFIGURE2, mp);
251 0 : mp->delay_in_usec = (u32) (delay * 1e6);
252 0 : mp->delay_in_usec = ntohl (mp->delay_in_usec);
253 0 : mp->average_packet_size = (u32) (packet_size);
254 0 : mp->average_packet_size = ntohl (mp->average_packet_size);
255 0 : mp->bandwidth_in_bits_per_second = (u64) (bandwidth);
256 0 : mp->bandwidth_in_bits_per_second =
257 0 : clib_host_to_net_u64 (mp->bandwidth_in_bits_per_second);
258 0 : mp->packets_per_drop = ntohl (packets_per_drop);
259 0 : mp->packets_per_reorder = ntohl (packets_per_reorder);
260 :
261 : /* send it... */
262 0 : S (mp);
263 :
264 : /* Wait for a reply... */
265 0 : W (ret);
266 0 : return ret;
267 : }
268 :
269 : #include <nsim/nsim.api_test.c>
270 :
271 : /*
272 : * fd.io coding-style-patch-verification: ON
273 : *
274 : * Local Variables:
275 : * eval: (c-set-style "gnu")
276 : * End:
277 : */
|