dpdk: Add support for Mellanox ConnectX-4 devices
[vpp.git] / src / examples / vlib / mc_test.c
1 /*
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
22 typedef struct
23 {
24   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
36 always_inline u32
37 choose_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)
41     r +=
42       random_u32 (&tm->seed) % (1 + tm->max_n_msg_bytes -
43                                 tm->min_n_msg_bytes);
44   return r;
45 }
46
47 static mc_test_main_t mc_test_main;
48
49 static void
50 serialize_test_msg (serialize_main_t * m, va_list * va)
51 {
52   mc_test_main_t *tm = &mc_test_main;
53   u32 n_bytes = choose_msg_size (tm);
54   u8 *msg;
55   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
63 static void
64 unserialize_test_msg (serialize_main_t * m, va_list * va)
65 {
66   mc_test_main_t *tm = &mc_test_main;
67   u32 i, n_bytes, dump_msg = tm->verbose;
68   u8 *p;
69   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
84 MC_SERIALIZE_MSG (test_msg, static) =
85 {
86 .name = "test_msg",.serialize = serialize_test_msg,.unserialize =
87     unserialize_test_msg,};
88
89 #define SERIALIZE 1
90
91 #define EVENT_JOIN_STREAM       10
92 #define EVENT_SEND_DATA         11
93
94 static void
95 test_rx_callback (mc_main_t * mcm,
96                   mc_stream_t * stream,
97                   mc_peer_id_t peer_id, u32 buffer_index)
98 {
99   if (SERIALIZE)
100     {
101       return mc_unserialize (mcm, stream, buffer_index);
102     }
103   else
104     {
105 #if DEBUG > 1
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);
109
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);
113
114 #endif
115     }
116 }
117
118 static u8 *
119 test_snapshot_callback (mc_main_t * mcm,
120                         u8 * data_vector, u32 last_global_sequence_processed)
121 {
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
138 static void
139 test_handle_snapshot_callback (mc_main_t * mcm, u8 * data, u32 n_data_bytes)
140 {
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
150 static mc_socket_main_t mc_socket_main;
151
152 static uword
153 mc_test_process (vlib_main_t * vm,
154                  vlib_node_runtime_t * node, vlib_frame_t * f)
155 {
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;
161   f64 delay;
162   mc_stream_config_t config;
163   clib_error_t *error;
164   int i;
165   char *intfcs[] = { "eth1", "eth0", "ce" };
166
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;
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
178   error = mc_socket_main_init (&mc_socket_main, intfcs, ARRAY_LEN (intfcs));
179   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
198         case EVENT_SEND_DATA:
199           {
200             f64 times[2];
201
202             if (stream_index == ~0)
203               stream_index = mc_stream_join (mcm, &config);
204
205             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           }
237
238         default:
239           clib_warning ("bug");
240           break;
241         }
242
243       if (event_data)
244         _vec_len (event_data) = 0;
245     }
246 }
247
248 /* *INDENT-OFF* */
249 VLIB_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* */
254
255 static clib_error_t *
256 mc_test_command (vlib_main_t * vm,
257                  unformat_input_t * input, vlib_cli_command_t * cmd)
258 {
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     }
268   else if (unformat (input, "send %f", &npkts) || unformat (input, "send"))
269     {
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);
273
274       return 0;
275     }
276   else
277     return unformat_parse_error (input);
278 }
279
280 /* *INDENT-OFF* */
281 VLIB_CLI_COMMAND (test_mc_command, static) =
282 {
283 .path = "test mc",.short_help = "Test mc command",.function =
284     mc_test_command,};
285 /* *INDENT-ON* */
286
287 static clib_error_t *
288 mc_show_command (vlib_main_t * vm,
289                  unformat_input_t * input, vlib_cli_command_t * cmd)
290 {
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
296 /* *INDENT-OFF* */
297 VLIB_CLI_COMMAND (show_mc_command, static) =
298 {
299 .path = "show mc",.short_help = "Show mc command",.function =
300     mc_show_command,};
301 /* *INDENT-ON* */
302
303 static clib_error_t *
304 mc_clear_command (vlib_main_t * vm,
305                   unformat_input_t * input, vlib_cli_command_t * cmd)
306 {
307   mc_main_t *mcm = &mc_socket_main.mc_main;
308   mc_clear_stream_stats (mcm);
309   return 0;
310 }
311
312 /* *INDENT-OFF* */
313 VLIB_CLI_COMMAND (clear_mc_command, static) =
314 {
315 .path = "clear mc",.short_help = "Clear mc command",.function =
316     mc_clear_command,};
317 /* *INDENT-ON* */
318
319 static clib_error_t *
320 mc_config (vlib_main_t * vm, unformat_input_t * input)
321 {
322   mc_test_main_t *tm = &mc_test_main;
323   mc_socket_main_t *msm = &mc_socket_main;
324   clib_error_t *error = 0;
325
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,
370                                EVENT_SEND_DATA,
371                                (uword) tm->n_packets_to_send);
372
373   return error;
374 }
375
376 VLIB_CONFIG_FUNCTION (mc_config, "mc");
377
378 /*
379  * fd.io coding-style-patch-verification: ON
380  *
381  * Local Variables:
382  * eval: (c-set-style "gnu")
383  * End:
384  */