blob: 464d07cc471c6e55187d5208d5e9bdafcb98b1f3 [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
2 * mc_test.c: test program for vlib mc
3 *
4 * Copyright (c) 2010 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
18#include <vlib/vlib.h>
19#include <vlib/unix/mc_socket.h>
20#include <vppinfra/random.h>
21
Dave Barach9b8ffd92016-07-08 08:13:45 -040022typedef struct
23{
Ed Warnickecb9cada2015-12-08 15:45:58 -070024 u32 min_n_msg_bytes;
25 u32 max_n_msg_bytes;
26 u32 tx_serial;
27 u32 rx_serial;
28 u32 seed;
29 u32 verbose;
30 u32 validate;
31 u32 window_size;
32 f64 min_delay, max_delay;
33 f64 n_packets_to_send;
34} mc_test_main_t;
35
36always_inline u32
37choose_msg_size (mc_test_main_t * tm)
38{
39 u32 r = tm->min_n_msg_bytes;
40 if (tm->max_n_msg_bytes > tm->min_n_msg_bytes)
Dave Barach9b8ffd92016-07-08 08:13:45 -040041 r +=
42 random_u32 (&tm->seed) % (1 + tm->max_n_msg_bytes -
43 tm->min_n_msg_bytes);
Ed Warnickecb9cada2015-12-08 15:45:58 -070044 return r;
45}
46
47static mc_test_main_t mc_test_main;
48
Dave Barach9b8ffd92016-07-08 08:13:45 -040049static void
50serialize_test_msg (serialize_main_t * m, va_list * va)
Ed Warnickecb9cada2015-12-08 15:45:58 -070051{
Dave Barach9b8ffd92016-07-08 08:13:45 -040052 mc_test_main_t *tm = &mc_test_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -070053 u32 n_bytes = choose_msg_size (tm);
Dave Barach9b8ffd92016-07-08 08:13:45 -040054 u8 *msg;
Ed Warnickecb9cada2015-12-08 15:45:58 -070055 int i;
56 serialize_integer (m, n_bytes, sizeof (n_bytes));
57 msg = serialize_get (m, n_bytes);
58 for (i = 0; i < n_bytes; i++)
59 msg[i] = i + tm->tx_serial;
60 tm->tx_serial += n_bytes;
61}
62
Dave Barach9b8ffd92016-07-08 08:13:45 -040063static void
64unserialize_test_msg (serialize_main_t * m, va_list * va)
Ed Warnickecb9cada2015-12-08 15:45:58 -070065{
Dave Barach9b8ffd92016-07-08 08:13:45 -040066 mc_test_main_t *tm = &mc_test_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -070067 u32 i, n_bytes, dump_msg = tm->verbose;
Dave Barach9b8ffd92016-07-08 08:13:45 -040068 u8 *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -070069 unserialize_integer (m, &n_bytes, sizeof (n_bytes));
70 p = unserialize_get (m, n_bytes);
71 if (tm->validate)
72 for (i = 0; i < n_bytes; i++)
73 if (p[i] != ((tm->rx_serial + i) & 0xff))
74 {
75 clib_warning ("corrupt msg at offset %d", i);
76 dump_msg = 1;
77 break;
78 }
79 if (dump_msg)
80 clib_warning ("got %d bytes, %U", n_bytes, format_hex_bytes, p, n_bytes);
81 tm->rx_serial += n_bytes;
82}
83
Dave Barach9b8ffd92016-07-08 08:13:45 -040084MC_SERIALIZE_MSG (test_msg, static) =
85{
86.name = "test_msg",.serialize = serialize_test_msg,.unserialize =
87 unserialize_test_msg,};
Ed Warnickecb9cada2015-12-08 15:45:58 -070088
89#define SERIALIZE 1
90
91#define EVENT_JOIN_STREAM 10
92#define EVENT_SEND_DATA 11
93
Dave Barach9b8ffd92016-07-08 08:13:45 -040094static void
95test_rx_callback (mc_main_t * mcm,
96 mc_stream_t * stream,
97 mc_peer_id_t peer_id, u32 buffer_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -070098{
99 if (SERIALIZE)
100 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400101 return mc_unserialize (mcm, stream, buffer_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700102 }
103 else
104 {
105#if DEBUG > 1
Dave Barach9b8ffd92016-07-08 08:13:45 -0400106 vlib_main_t *vm = mcm->vlib_main;
107 vlib_buffer_t *b = vlib_get_buffer (vm, buffer_index);
108 u8 *dp = vlib_buffer_get_current (b);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700109
Dave Barach9b8ffd92016-07-08 08:13:45 -0400110 fformat (stdout, "RX from %U %U\n",
111 stream->transport->format_peer_id, peer_id,
112 format_hex_bytes, dp, tm->n_msg_bytes);
113
Ed Warnickecb9cada2015-12-08 15:45:58 -0700114#endif
115 }
116}
117
118static u8 *
119test_snapshot_callback (mc_main_t * mcm,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400120 u8 * data_vector, u32 last_global_sequence_processed)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700121{
122 if (SERIALIZE)
123 {
124 serialize_main_t m;
125
126 /* Append serialized data to data vector. */
127 serialize_open_vector (&m, data_vector);
128 m.stream.current_buffer_index = vec_len (data_vector);
129
130 return serialize_close_vector (&m);
131 }
132 else
133 return format (data_vector,
134 "snapshot, last global seq 0x%x",
135 last_global_sequence_processed);
136}
137
138static void
Dave Barach9b8ffd92016-07-08 08:13:45 -0400139test_handle_snapshot_callback (mc_main_t * mcm, u8 * data, u32 n_data_bytes)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700140{
141 if (SERIALIZE)
142 {
143 serialize_main_t s;
144 unserialize_open_data (&s, data, n_data_bytes);
145 }
146 else
147 clib_warning ("snapshot `%*s'", n_data_bytes, data);
148}
149
150static mc_socket_main_t mc_socket_main;
151
152static uword
153mc_test_process (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400154 vlib_node_runtime_t * node, vlib_frame_t * f)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700155{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400156 mc_test_main_t *tm = &mc_test_main;
157 mc_socket_main_t *msm = &mc_socket_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700158 mc_main_t *mcm = &msm->mc_main;
159 uword event_type, *event_data = 0;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400160 u32 data_serial = 0, stream_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700161 f64 delay;
162 mc_stream_config_t config;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400163 clib_error_t *error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700164 int i;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400165 char *intfcs[] = { "eth1", "eth0", "ce" };
Ed Warnickecb9cada2015-12-08 15:45:58 -0700166
Dave Barachb7b92992018-10-17 10:38:51 -0400167 clib_memset (&config, 0, sizeof (config));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700168 config.name = "test";
169 config.window_size = tm->window_size;
170 config.rx_buffer = test_rx_callback;
171 config.catchup_snapshot = test_snapshot_callback;
172 config.catchup = test_handle_snapshot_callback;
173 stream_index = ~0;
174
175 msm->multicast_tx_ip4_address_host_byte_order = 0xefff0100;
176 msm->base_multicast_udp_port_host_byte_order = 0xffab;
177
Dave Barach9b8ffd92016-07-08 08:13:45 -0400178 error = mc_socket_main_init (&mc_socket_main, intfcs, ARRAY_LEN (intfcs));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700179 if (error)
180 {
181 clib_error_report (error);
182 exit (1);
183 }
184
185 mcm->we_can_be_relay_master = 1;
186
187 while (1)
188 {
189 vlib_process_wait_for_event (vm);
190 event_type = vlib_process_get_events (vm, &event_data);
191
192 switch (event_type)
193 {
194 case EVENT_JOIN_STREAM:
195 stream_index = mc_stream_join (mcm, &config);
196 break;
197
Dave Barach9b8ffd92016-07-08 08:13:45 -0400198 case EVENT_SEND_DATA:
199 {
200 f64 times[2];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700201
Dave Barach9b8ffd92016-07-08 08:13:45 -0400202 if (stream_index == ~0)
203 stream_index = mc_stream_join (mcm, &config);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700204
Dave Barach9b8ffd92016-07-08 08:13:45 -0400205 times[0] = vlib_time_now (vm);
206 for (i = 0; i < event_data[0]; i++)
207 {
208 u32 bi;
209 if (SERIALIZE)
210 {
211 mc_serialize_stream (mcm, stream_index, &test_msg,
212 data_serial);
213 }
214 else
215 {
216 u8 *mp;
217 mp = mc_get_vlib_buffer (vm, sizeof (mp[0]), &bi);
218 mp[0] = data_serial;
219 mc_stream_send (mcm, stream_index, bi);
220 }
221 if (tm->min_delay > 0)
222 {
223 delay =
224 tm->min_delay +
225 random_f64 (&tm->seed) * (tm->max_delay -
226 tm->min_delay);
227 vlib_process_suspend (vm, delay);
228 }
229 data_serial++;
230 }
231 times[1] = vlib_time_now (vm);
232 clib_warning ("done sending %d; %.4e per sec",
233 event_data[0],
234 (f64) event_data[0] / (times[1] - times[0]));
235 break;
236 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700237
238 default:
239 clib_warning ("bug");
240 break;
241 }
Dave Barach9b8ffd92016-07-08 08:13:45 -0400242
Ed Warnickecb9cada2015-12-08 15:45:58 -0700243 if (event_data)
244 _vec_len (event_data) = 0;
245 }
246}
247
Dave Barach9b8ffd92016-07-08 08:13:45 -0400248/* *INDENT-OFF* */
249VLIB_REGISTER_NODE (mc_test_process_node, static) =
250{
251.function = mc_test_process,.type = VLIB_NODE_TYPE_PROCESS,.name =
252 "mc-test-process",};
253/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700254
255static clib_error_t *
256mc_test_command (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400257 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700258{
259 f64 npkts = 10;
260
261 if (unformat (input, "join"))
262 {
263 vlib_cli_output (vm, "Join stream...\n");
264 vlib_process_signal_event (vm, mc_test_process_node.index,
265 EVENT_JOIN_STREAM, 0);
266 return 0;
267 }
Dave Barach9b8ffd92016-07-08 08:13:45 -0400268 else if (unformat (input, "send %f", &npkts) || unformat (input, "send"))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700269 {
270 vlib_process_signal_event (vm, mc_test_process_node.index,
271 EVENT_SEND_DATA, (uword) npkts);
272 vlib_cli_output (vm, "Send %.0f pkts...\n", npkts);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400273
Ed Warnickecb9cada2015-12-08 15:45:58 -0700274 return 0;
275 }
276 else
277 return unformat_parse_error (input);
278}
279
Dave Barach9b8ffd92016-07-08 08:13:45 -0400280/* *INDENT-OFF* */
281VLIB_CLI_COMMAND (test_mc_command, static) =
282{
283.path = "test mc",.short_help = "Test mc command",.function =
284 mc_test_command,};
285/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700286
287static clib_error_t *
288mc_show_command (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400289 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700290{
291 mc_main_t *mcm = &mc_socket_main.mc_main;
292 vlib_cli_output (vm, "%U", format_mc_main, mcm);
293 return 0;
294}
295
Dave Barach9b8ffd92016-07-08 08:13:45 -0400296/* *INDENT-OFF* */
297VLIB_CLI_COMMAND (show_mc_command, static) =
298{
299.path = "show mc",.short_help = "Show mc command",.function =
300 mc_show_command,};
301/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700302
303static clib_error_t *
304mc_clear_command (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400305 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700306{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400307 mc_main_t *mcm = &mc_socket_main.mc_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700308 mc_clear_stream_stats (mcm);
309 return 0;
310}
311
Dave Barach9b8ffd92016-07-08 08:13:45 -0400312/* *INDENT-OFF* */
313VLIB_CLI_COMMAND (clear_mc_command, static) =
314{
315.path = "clear mc",.short_help = "Clear mc command",.function =
316 mc_clear_command,};
317/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700318
319static clib_error_t *
320mc_config (vlib_main_t * vm, unformat_input_t * input)
321{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400322 mc_test_main_t *tm = &mc_test_main;
323 mc_socket_main_t *msm = &mc_socket_main;
324 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700325
326 tm->min_n_msg_bytes = 4;
327 tm->max_n_msg_bytes = 4;
328 tm->window_size = 8;
329 tm->seed = getpid ();
330 tm->verbose = 0;
331 tm->validate = 1;
332 tm->min_delay = 10e-6;
333 tm->max_delay = 10e-3;
334 tm->n_packets_to_send = 0;
335 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
336 {
337 if (unformat (input, "interface %s", &msm->multicast_interface_name))
338 ;
339
340 else if (unformat (input, "n-bytes %d", &tm->max_n_msg_bytes))
341 tm->min_n_msg_bytes = tm->max_n_msg_bytes;
342 else if (unformat (input, "max-n-bytes %d", &tm->max_n_msg_bytes))
343 ;
344 else if (unformat (input, "min-n-bytes %d", &tm->min_n_msg_bytes))
345 ;
346 else if (unformat (input, "seed %d", &tm->seed))
347 ;
348 else if (unformat (input, "window %d", &tm->window_size))
349 ;
350 else if (unformat (input, "verbose"))
351 tm->verbose = 1;
352 else if (unformat (input, "no-validate"))
353 tm->validate = 0;
354 else if (unformat (input, "min-delay %f", &tm->min_delay))
355 ;
356 else if (unformat (input, "max-delay %f", &tm->max_delay))
357 ;
358 else if (unformat (input, "no-delay"))
359 tm->min_delay = tm->max_delay = 0;
360 else if (unformat (input, "n-packets %f", &tm->n_packets_to_send))
361 ;
362
363 else
364 return clib_error_return (0, "unknown input `%U'",
365 format_unformat_error, input);
366 }
367
368 if (tm->n_packets_to_send > 0)
369 vlib_process_signal_event (vm, mc_test_process_node.index,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400370 EVENT_SEND_DATA,
371 (uword) tm->n_packets_to_send);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700372
373 return error;
374}
375
376VLIB_CONFIG_FUNCTION (mc_config, "mc");
Dave Barach9b8ffd92016-07-08 08:13:45 -0400377
378/*
379 * fd.io coding-style-patch-verification: ON
380 *
381 * Local Variables:
382 * eval: (c-set-style "gnu")
383 * End:
384 */