2 * mc_test.c: test program for vlib mc
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:
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <vlib/vlib.h>
19 #include <vlib/unix/mc_socket.h>
20 #include <vppinfra/random.h>
31 f64 min_delay, max_delay;
32 f64 n_packets_to_send;
36 choose_msg_size (mc_test_main_t * tm)
38 u32 r = tm->min_n_msg_bytes;
39 if (tm->max_n_msg_bytes > tm->min_n_msg_bytes)
40 r += random_u32 (&tm->seed) % (1 + tm->max_n_msg_bytes - tm->min_n_msg_bytes);
44 static mc_test_main_t mc_test_main;
46 static void serialize_test_msg (serialize_main_t * m, va_list * va)
48 mc_test_main_t * tm = &mc_test_main;
49 u32 n_bytes = choose_msg_size (tm);
52 serialize_integer (m, n_bytes, sizeof (n_bytes));
53 msg = serialize_get (m, n_bytes);
54 for (i = 0; i < n_bytes; i++)
55 msg[i] = i + tm->tx_serial;
56 tm->tx_serial += n_bytes;
59 static void unserialize_test_msg (serialize_main_t * m, va_list * va)
61 mc_test_main_t * tm = &mc_test_main;
62 u32 i, n_bytes, dump_msg = tm->verbose;
64 unserialize_integer (m, &n_bytes, sizeof (n_bytes));
65 p = unserialize_get (m, n_bytes);
67 for (i = 0; i < n_bytes; i++)
68 if (p[i] != ((tm->rx_serial + i) & 0xff))
70 clib_warning ("corrupt msg at offset %d", i);
75 clib_warning ("got %d bytes, %U", n_bytes, format_hex_bytes, p, n_bytes);
76 tm->rx_serial += n_bytes;
79 MC_SERIALIZE_MSG (test_msg, static) = {
81 .serialize = serialize_test_msg,
82 .unserialize = unserialize_test_msg,
87 #define EVENT_JOIN_STREAM 10
88 #define EVENT_SEND_DATA 11
90 static void test_rx_callback (mc_main_t * mcm,
97 return mc_unserialize (mcm, stream, buffer_index);
102 vlib_main_t * vm = mcm->vlib_main;
103 vlib_buffer_t * b = vlib_get_buffer (vm, buffer_index);
104 u8 * dp = vlib_buffer_get_current (b);
106 fformat(stdout, "RX from %U %U\n",
107 stream->transport->format_peer_id, peer_id,
108 format_hex_bytes, dp, tm->n_msg_bytes);
115 test_snapshot_callback (mc_main_t * mcm,
117 u32 last_global_sequence_processed)
123 /* Append serialized data to data vector. */
124 serialize_open_vector (&m, data_vector);
125 m.stream.current_buffer_index = vec_len (data_vector);
127 return serialize_close_vector (&m);
130 return format (data_vector,
131 "snapshot, last global seq 0x%x",
132 last_global_sequence_processed);
136 test_handle_snapshot_callback (mc_main_t * mcm,
143 unserialize_open_data (&s, data, n_data_bytes);
146 clib_warning ("snapshot `%*s'", n_data_bytes, data);
149 static mc_socket_main_t mc_socket_main;
152 mc_test_process (vlib_main_t * vm,
153 vlib_node_runtime_t * node,
156 mc_test_main_t * tm = &mc_test_main;
157 mc_socket_main_t * msm = &mc_socket_main;
158 mc_main_t *mcm = &msm->mc_main;
159 uword event_type, *event_data = 0;
160 u32 data_serial=0, stream_index;
162 mc_stream_config_t config;
163 clib_error_t * error;
165 char *intfcs[] = { "eth1", "eth0", "ce" };
167 memset (&config, 0, sizeof (config));
168 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;
175 msm->multicast_tx_ip4_address_host_byte_order = 0xefff0100;
176 msm->base_multicast_udp_port_host_byte_order = 0xffab;
178 error = mc_socket_main_init (&mc_socket_main, intfcs, ARRAY_LEN(intfcs));
181 clib_error_report (error);
185 mcm->we_can_be_relay_master = 1;
189 vlib_process_wait_for_event (vm);
190 event_type = vlib_process_get_events (vm, &event_data);
194 case EVENT_JOIN_STREAM:
195 stream_index = mc_stream_join (mcm, &config);
198 case EVENT_SEND_DATA: {
201 if (stream_index == ~0)
202 stream_index = mc_stream_join (mcm, &config);
204 times[0] = vlib_time_now (vm);
205 for (i = 0; i < event_data[0]; i++)
210 mc_serialize_stream (mcm, stream_index, &test_msg, data_serial);
215 mp = mc_get_vlib_buffer (vm, sizeof (mp[0]), &bi);
217 mc_stream_send (mcm, stream_index, bi);
219 if (tm->min_delay > 0)
221 delay = tm->min_delay + random_f64 (&tm->seed) * (tm->max_delay - tm->min_delay);
222 vlib_process_suspend (vm, delay);
226 times[1] = vlib_time_now (vm);
227 clib_warning ("done sending %d; %.4e per sec",
229 (f64) event_data[0] / (times[1] - times[0]));
234 clib_warning ("bug");
239 _vec_len (event_data) = 0;
243 VLIB_REGISTER_NODE (mc_test_process_node,static) = {
244 .function = mc_test_process,
245 .type = VLIB_NODE_TYPE_PROCESS,
246 .name = "mc-test-process",
249 static clib_error_t *
250 mc_test_command (vlib_main_t * vm,
251 unformat_input_t * input,
252 vlib_cli_command_t * cmd)
256 if (unformat (input, "join"))
258 vlib_cli_output (vm, "Join stream...\n");
259 vlib_process_signal_event (vm, mc_test_process_node.index,
260 EVENT_JOIN_STREAM, 0);
263 else if (unformat (input, "send %f", &npkts)
264 || unformat(input, "send"))
266 vlib_process_signal_event (vm, mc_test_process_node.index,
267 EVENT_SEND_DATA, (uword) npkts);
268 vlib_cli_output (vm, "Send %.0f pkts...\n", npkts);
273 return unformat_parse_error (input);
276 VLIB_CLI_COMMAND (test_mc_command, static) = {
278 .short_help = "Test mc command",
279 .function = mc_test_command,
282 static clib_error_t *
283 mc_show_command (vlib_main_t * vm,
284 unformat_input_t * input,
285 vlib_cli_command_t * cmd)
287 mc_main_t *mcm = &mc_socket_main.mc_main;
288 vlib_cli_output (vm, "%U", format_mc_main, mcm);
292 VLIB_CLI_COMMAND (show_mc_command, static) = {
294 .short_help = "Show mc command",
295 .function = mc_show_command,
298 static clib_error_t *
299 mc_clear_command (vlib_main_t * vm,
300 unformat_input_t * input,
301 vlib_cli_command_t * cmd)
303 mc_main_t * mcm = &mc_socket_main.mc_main;
304 mc_clear_stream_stats (mcm);
308 VLIB_CLI_COMMAND (clear_mc_command, static) = {
310 .short_help = "Clear mc command",
311 .function = mc_clear_command,
314 static clib_error_t *
315 mc_config (vlib_main_t * vm, unformat_input_t * input)
317 mc_test_main_t * tm = &mc_test_main;
318 mc_socket_main_t * msm = &mc_socket_main;
319 clib_error_t * error = 0;
321 tm->min_n_msg_bytes = 4;
322 tm->max_n_msg_bytes = 4;
324 tm->seed = getpid ();
327 tm->min_delay = 10e-6;
328 tm->max_delay = 10e-3;
329 tm->n_packets_to_send = 0;
330 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
332 if (unformat (input, "interface %s", &msm->multicast_interface_name))
335 else if (unformat (input, "n-bytes %d", &tm->max_n_msg_bytes))
336 tm->min_n_msg_bytes = tm->max_n_msg_bytes;
337 else if (unformat (input, "max-n-bytes %d", &tm->max_n_msg_bytes))
339 else if (unformat (input, "min-n-bytes %d", &tm->min_n_msg_bytes))
341 else if (unformat (input, "seed %d", &tm->seed))
343 else if (unformat (input, "window %d", &tm->window_size))
345 else if (unformat (input, "verbose"))
347 else if (unformat (input, "no-validate"))
349 else if (unformat (input, "min-delay %f", &tm->min_delay))
351 else if (unformat (input, "max-delay %f", &tm->max_delay))
353 else if (unformat (input, "no-delay"))
354 tm->min_delay = tm->max_delay = 0;
355 else if (unformat (input, "n-packets %f", &tm->n_packets_to_send))
359 return clib_error_return (0, "unknown input `%U'",
360 format_unformat_error, input);
363 if (tm->n_packets_to_send > 0)
364 vlib_process_signal_event (vm, mc_test_process_node.index,
365 EVENT_SEND_DATA, (uword) tm->n_packets_to_send);
370 VLIB_CONFIG_FUNCTION (mc_config, "mc");