2 *------------------------------------------------------------------
3 * summary_stats_client -
5 * Copyright (c) 2010 Cisco and/or its affiliates.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at:
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *------------------------------------------------------------------
22 #include <sys/types.h>
23 #include <sys/socket.h>
26 #include <netinet/in.h>
34 #include <vppinfra/clib.h>
35 #include <vppinfra/vec.h>
36 #include <vppinfra/hash.h>
37 #include <vppinfra/bitmap.h>
38 #include <vppinfra/fifo.h>
39 #include <vppinfra/time.h>
40 #include <vppinfra/mheap.h>
41 #include <vppinfra/heap.h>
42 #include <vppinfra/pool.h>
43 #include <vppinfra/format.h>
44 #include <vppinfra/error.h>
46 #include <vnet/vnet.h>
47 #include <vlib/vlib.h>
48 #include <vlib/unix/unix.h>
49 #include <vlibapi/api.h>
50 #include <vlibmemory/api.h>
52 #include <vpp/api/vpe_msg_enum.h>
54 #include <vnet/ip/ip.h>
57 #define f64_print(a,b)
59 #define vl_typedefs /* define message structures */
60 #include <vpp/api/vpe_all_api_h.h>
63 #define vl_endianfun /* define message structures */
64 #include <vpp/api/vpe_all_api_h.h>
67 /* instantiate all the print functions we know about */
68 #define vl_print(handle, ...)
70 #include <vpp/api/vpe_all_api_h.h>
73 vl_shmem_hdr_t *shmem_hdr;
77 volatile int sigterm_received;
79 struct sockaddr_in send_data_addr;
84 unix_shared_memory_queue_t *vl_input_queue;
88 test_main_t test_main;
91 * Satisfy external references when -lvlib is not available.
93 vlib_main_t vlib_global_main;
94 vlib_main_t **vlib_mains;
97 vlib_cli_output (struct vlib_main_t *vm, char *fmt, ...)
99 clib_warning ("vlib_cli_output callled...");
104 vl_api_vnet_get_summary_stats_reply_t_handler
105 (vl_api_vnet_get_summary_stats_reply_t * mp)
107 test_main_t *tm = &test_main;
111 printf ("total rx pkts %llu, total rx bytes %llu\n",
112 (unsigned long long) mp->total_pkts[0],
113 (unsigned long long) mp->total_bytes[0]);
114 printf ("total tx pkts %llu, total tx bytes %llu\n",
115 (unsigned long long) mp->total_pkts[1],
116 (unsigned long long) mp->total_bytes[1]);
117 printf ("vector rate %.2f\n", mp->vector_rate);
119 vec_reset_length (sb);
120 sb = format (sb, "%v,%.0f,%llu,%llu,%llu,%llu\n%c",
121 tm->display_name, mp->vector_rate,
122 (unsigned long long) mp->total_pkts[0],
123 (unsigned long long) mp->total_bytes[0],
124 (unsigned long long) mp->total_pkts[1],
125 (unsigned long long) mp->total_bytes[1], 0);
127 n = sendto (tm->send_data_socket, sb, vec_len (sb),
128 0, (struct sockaddr *) &tm->send_data_addr,
129 sizeof (tm->send_data_addr));
131 if (n != vec_len (sb))
132 clib_unix_warning ("sendto");
136 #define foreach_api_msg \
137 _(VNET_GET_SUMMARY_STATS_REPLY, vnet_get_summary_stats_reply)
140 connect_to_vpe (char *name)
144 rv = vl_client_connect_to_vlib ("/vpe-api", name, 32);
147 vl_msg_api_set_handlers(VL_API_##N, #n, \
148 vl_api_##n##_t_handler, \
150 vl_api_##n##_t_endian, \
151 vl_api_##n##_t_print, \
152 sizeof(vl_api_##n##_t), 1);
156 shmem_hdr = api_main.shmem_hdr;
162 disconnect_from_vpe (void)
164 vl_client_disconnect_from_vlib ();
169 sigterm_handler (int sig)
171 test_main_t *tm = &test_main;
172 tm->sigterm_received = 1;
175 /* Parse an IP4 address %d.%d.%d.%d. */
177 unformat_ip4_address (unformat_input_t * input, va_list * args)
179 u8 *result = va_arg (*args, u8 *);
182 if (!unformat (input, "%d.%d.%d.%d", &a[0], &a[1], &a[2], &a[3]))
185 if (a[0] >= 256 || a[1] >= 256 || a[2] >= 256 || a[3] >= 256)
197 main (int argc, char **argv)
199 api_main_t *am = &api_main;
200 test_main_t *tm = &test_main;
201 vl_api_vnet_get_summary_stats_t *mp;
202 unformat_input_t _input, *input = &_input;
203 clib_error_t *error = 0;
204 ip4_address_t collector_ip;
205 u8 *display_name = 0;
206 u16 collector_port = 7654;
208 collector_ip.as_u32 = (u32) ~ 0;
210 unformat_init_command_line (input, argv);
212 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
214 if (unformat (input, "collector-ip %U",
215 unformat_ip4_address, &collector_ip))
217 else if (unformat (input, "display-name %v", &display_name))
219 else if (unformat (input, "collector-port %d", &collector_port))
225 (0, "Usage: %s collector-ip <ip>\n"
226 " [display-name <string>] [collector-port <num>]\n"
227 " port defaults to 7654", argv[0]);
232 if (error == 0 && collector_ip.as_u32 == (u32) ~ 0)
233 error = clib_error_return (0, "collector-ip not set...\n");
238 clib_error_report (error);
242 if (display_name == 0)
244 display_name = format (0, "vpe-to-%d.%d.%d.%d",
245 collector_ip.as_u8[0],
246 collector_ip.as_u8[1],
247 collector_ip.as_u8[2], collector_ip.as_u8[3]);
251 connect_to_vpe ("test_client");
253 tm->vl_input_queue = shmem_hdr->vl_input_queue;
254 tm->my_client_index = am->my_client_index;
255 tm->display_name = display_name;
257 signal (SIGTERM, sigterm_handler);
258 signal (SIGINT, sigterm_handler);
259 signal (SIGQUIT, sigterm_handler);
261 /* data (multicast) RX socket */
262 tm->send_data_socket = socket (PF_INET, SOCK_DGRAM, IPPROTO_UDP);
263 if (tm->send_data_socket < 0)
265 clib_unix_warning (0, "data_rx_socket");
269 memset (&tm->send_data_addr, 0, sizeof (tm->send_data_addr));
270 tm->send_data_addr.sin_family = AF_INET;
271 tm->send_data_addr.sin_addr.s_addr = collector_ip.as_u32;
272 tm->send_data_addr.sin_port = htons (collector_port);
274 fformat (stdout, "Send SIGINT or SIGTERM to quit...\n");
280 if (tm->sigterm_received)
283 mp = vl_msg_api_alloc (sizeof (*mp));
284 memset (mp, 0, sizeof (*mp));
285 mp->_vl_msg_id = ntohs (VL_API_VNET_GET_SUMMARY_STATS);
286 mp->client_index = tm->my_client_index;
287 vl_msg_api_send_shmem (tm->vl_input_queue, (u8 *) & mp);
290 fformat (stdout, "Exiting...\n");
292 disconnect_from_vpe ();
297 * fd.io coding-style-patch-verification: ON
300 * eval: (c-set-style "gnu")