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>
32 f64 min_delay, max_delay;
33 f64 n_packets_to_send;
37 choose_msg_size (mc_test_main_t * tm)
39 u32 r = tm->min_n_msg_bytes;
40 if (tm->max_n_msg_bytes > tm->min_n_msg_bytes)
42 random_u32 (&tm->seed) % (1 + tm->max_n_msg_bytes -
47 static mc_test_main_t mc_test_main;
50 serialize_test_msg (serialize_main_t * m, va_list * va)
52 mc_test_main_t *tm = &mc_test_main;
53 u32 n_bytes = choose_msg_size (tm);
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;
64 unserialize_test_msg (serialize_main_t * m, va_list * va)
66 mc_test_main_t *tm = &mc_test_main;
67 u32 i, n_bytes, dump_msg = tm->verbose;
69 unserialize_integer (m, &n_bytes, sizeof (n_bytes));
70 p = unserialize_get (m, n_bytes);
72 for (i = 0; i < n_bytes; i++)
73 if (p[i] != ((tm->rx_serial + i) & 0xff))
75 clib_warning ("corrupt msg at offset %d", i);
80 clib_warning ("got %d bytes, %U", n_bytes, format_hex_bytes, p, n_bytes);
81 tm->rx_serial += n_bytes;
84 MC_SERIALIZE_MSG (test_msg, static) =
86 .name = "test_msg",.serialize = serialize_test_msg,.unserialize =
87 unserialize_test_msg,};
91 #define EVENT_JOIN_STREAM 10
92 #define EVENT_SEND_DATA 11
95 test_rx_callback (mc_main_t * mcm,
97 mc_peer_id_t peer_id, u32 buffer_index)
101 return mc_unserialize (mcm, stream, buffer_index);
106 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);
110 fformat (stdout, "RX from %U %U\n",
111 stream->transport->format_peer_id, peer_id,
112 format_hex_bytes, dp, tm->n_msg_bytes);
119 test_snapshot_callback (mc_main_t * mcm,
120 u8 * data_vector, u32 last_global_sequence_processed)
126 /* Append serialized data to data vector. */
127 serialize_open_vector (&m, data_vector);
128 m.stream.current_buffer_index = vec_len (data_vector);
130 return serialize_close_vector (&m);
133 return format (data_vector,
134 "snapshot, last global seq 0x%x",
135 last_global_sequence_processed);
139 test_handle_snapshot_callback (mc_main_t * mcm, u8 * data, u32 n_data_bytes)
144 unserialize_open_data (&s, data, n_data_bytes);
147 clib_warning ("snapshot `%*s'", n_data_bytes, data);
150 static mc_socket_main_t mc_socket_main;
153 mc_test_process (vlib_main_t * vm,
154 vlib_node_runtime_t * node, vlib_frame_t * f)
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;
165 char *intfcs[] = { "eth1", "eth0", "ce" };
167 clib_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:
202 if (stream_index == ~0)
203 stream_index = mc_stream_join (mcm, &config);
205 times[0] = vlib_time_now (vm);
206 for (i = 0; i < event_data[0]; i++)
211 mc_serialize_stream (mcm, stream_index, &test_msg,
217 mp = mc_get_vlib_buffer (vm, sizeof (mp[0]), &bi);
219 mc_stream_send (mcm, stream_index, bi);
221 if (tm->min_delay > 0)
225 random_f64 (&tm->seed) * (tm->max_delay -
227 vlib_process_suspend (vm, delay);
231 times[1] = vlib_time_now (vm);
232 clib_warning ("done sending %d; %.4e per sec",
234 (f64) event_data[0] / (times[1] - times[0]));
239 clib_warning ("bug");
244 _vec_len (event_data) = 0;
249 VLIB_REGISTER_NODE (mc_test_process_node, static) =
251 .function = mc_test_process,.type = VLIB_NODE_TYPE_PROCESS,.name =
255 static clib_error_t *
256 mc_test_command (vlib_main_t * vm,
257 unformat_input_t * input, vlib_cli_command_t * cmd)
261 if (unformat (input, "join"))
263 vlib_cli_output (vm, "Join stream...\n");
264 vlib_process_signal_event (vm, mc_test_process_node.index,
265 EVENT_JOIN_STREAM, 0);
268 else if (unformat (input, "send %f", &npkts) || unformat (input, "send"))
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);
277 return unformat_parse_error (input);
281 VLIB_CLI_COMMAND (test_mc_command, static) =
283 .path = "test mc",.short_help = "Test mc command",.function =
287 static clib_error_t *
288 mc_show_command (vlib_main_t * vm,
289 unformat_input_t * input, vlib_cli_command_t * cmd)
291 mc_main_t *mcm = &mc_socket_main.mc_main;
292 vlib_cli_output (vm, "%U", format_mc_main, mcm);
297 VLIB_CLI_COMMAND (show_mc_command, static) =
299 .path = "show mc",.short_help = "Show mc command",.function =
303 static clib_error_t *
304 mc_clear_command (vlib_main_t * vm,
305 unformat_input_t * input, vlib_cli_command_t * cmd)
307 mc_main_t *mcm = &mc_socket_main.mc_main;
308 mc_clear_stream_stats (mcm);
313 VLIB_CLI_COMMAND (clear_mc_command, static) =
315 .path = "clear mc",.short_help = "Clear mc command",.function =
319 static clib_error_t *
320 mc_config (vlib_main_t * vm, unformat_input_t * input)
322 mc_test_main_t *tm = &mc_test_main;
323 mc_socket_main_t *msm = &mc_socket_main;
324 clib_error_t *error = 0;
326 tm->min_n_msg_bytes = 4;
327 tm->max_n_msg_bytes = 4;
329 tm->seed = getpid ();
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)
337 if (unformat (input, "interface %s", &msm->multicast_interface_name))
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))
344 else if (unformat (input, "min-n-bytes %d", &tm->min_n_msg_bytes))
346 else if (unformat (input, "seed %d", &tm->seed))
348 else if (unformat (input, "window %d", &tm->window_size))
350 else if (unformat (input, "verbose"))
352 else if (unformat (input, "no-validate"))
354 else if (unformat (input, "min-delay %f", &tm->min_delay))
356 else if (unformat (input, "max-delay %f", &tm->max_delay))
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))
364 return clib_error_return (0, "unknown input `%U'",
365 format_unformat_error, input);
368 if (tm->n_packets_to_send > 0)
369 vlib_process_signal_event (vm, mc_test_process_node.index,
371 (uword) tm->n_packets_to_send);
376 VLIB_CONFIG_FUNCTION (mc_config, "mc");
379 * fd.io coding-style-patch-verification: ON
382 * eval: (c-set-style "gnu")