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.
94 vlib_cli_output (struct vlib_main_t *vm, char *fmt, ...)
96 clib_warning ("vlib_cli_output callled...");
101 vl_api_vnet_summary_stats_reply_t_handler (vl_api_vnet_summary_stats_reply_t *
104 test_main_t *tm = &test_main;
108 printf ("total rx pkts %llu, total rx bytes %llu\n",
109 (unsigned long long) mp->total_pkts[0],
110 (unsigned long long) mp->total_bytes[0]);
111 printf ("total tx pkts %llu, total tx bytes %llu\n",
112 (unsigned long long) mp->total_pkts[1],
113 (unsigned long long) mp->total_bytes[1]);
114 printf ("vector rate %.2f\n", mp->vector_rate);
116 vec_reset_length (sb);
117 sb = format (sb, "%v,%.0f,%llu,%llu,%llu,%llu\n%c",
118 tm->display_name, mp->vector_rate,
119 (unsigned long long) mp->total_pkts[0],
120 (unsigned long long) mp->total_bytes[0],
121 (unsigned long long) mp->total_pkts[1],
122 (unsigned long long) mp->total_bytes[1], 0);
124 n = sendto (tm->send_data_socket, sb, vec_len (sb),
125 0, (struct sockaddr *) &tm->send_data_addr,
126 sizeof (tm->send_data_addr));
128 if (n != vec_len (sb))
129 clib_unix_warning ("sendto");
133 #define foreach_api_msg \
134 _(VNET_SUMMARY_STATS_REPLY, vnet_summary_stats_reply)
137 connect_to_vpe (char *name)
141 rv = vl_client_connect_to_vlib ("/vpe-api", name, 32);
144 vl_msg_api_set_handlers(VL_API_##N, #n, \
145 vl_api_##n##_t_handler, \
147 vl_api_##n##_t_endian, \
148 vl_api_##n##_t_print, \
149 sizeof(vl_api_##n##_t), 1);
153 shmem_hdr = api_main.shmem_hdr;
159 disconnect_from_vpe (void)
161 vl_client_disconnect_from_vlib ();
166 sigterm_handler (int sig)
168 test_main_t *tm = &test_main;
169 tm->sigterm_received = 1;
172 /* Parse an IP4 address %d.%d.%d.%d. */
174 unformat_ip4_address (unformat_input_t * input, va_list * args)
176 u8 *result = va_arg (*args, u8 *);
179 if (!unformat (input, "%d.%d.%d.%d", &a[0], &a[1], &a[2], &a[3]))
182 if (a[0] >= 256 || a[1] >= 256 || a[2] >= 256 || a[3] >= 256)
194 main (int argc, char **argv)
196 api_main_t *am = &api_main;
197 test_main_t *tm = &test_main;
198 vl_api_vnet_get_summary_stats_t *mp;
199 unformat_input_t _input, *input = &_input;
200 clib_error_t *error = 0;
201 ip4_address_t collector_ip;
202 u8 *display_name = 0;
203 u16 collector_port = 7654;
205 collector_ip.as_u32 = (u32) ~ 0;
207 unformat_init_command_line (input, argv);
209 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
211 if (unformat (input, "collector-ip %U",
212 unformat_ip4_address, &collector_ip))
214 else if (unformat (input, "display-name %v", &display_name))
216 else if (unformat (input, "collector-port %d", &collector_port))
222 (0, "Usage: %s collector-ip <ip>\n"
223 " [display-name <string>] [collector-port <num>]\n"
224 " port defaults to 7654", argv[0]);
229 if (error == 0 && collector_ip.as_u32 == (u32) ~ 0)
230 error = clib_error_return (0, "collector-ip not set...\n");
235 clib_error_report (error);
239 if (display_name == 0)
241 display_name = format (0, "vpe-to-%d.%d.%d.%d",
242 collector_ip.as_u8[0],
243 collector_ip.as_u8[1],
244 collector_ip.as_u8[2], collector_ip.as_u8[3]);
248 connect_to_vpe ("test_client");
250 tm->vl_input_queue = shmem_hdr->vl_input_queue;
251 tm->my_client_index = am->my_client_index;
252 tm->display_name = display_name;
254 signal (SIGTERM, sigterm_handler);
255 signal (SIGINT, sigterm_handler);
256 signal (SIGQUIT, sigterm_handler);
258 /* data (multicast) RX socket */
259 tm->send_data_socket = socket (PF_INET, SOCK_DGRAM, IPPROTO_UDP);
260 if (tm->send_data_socket < 0)
262 clib_unix_warning (0, "data_rx_socket");
266 memset (&tm->send_data_addr, 0, sizeof (tm->send_data_addr));
267 tm->send_data_addr.sin_family = AF_INET;
268 tm->send_data_addr.sin_addr.s_addr = collector_ip.as_u32;
269 tm->send_data_addr.sin_port = htons (collector_port);
271 fformat (stdout, "Send SIGINT or SIGTERM to quit...\n");
277 if (tm->sigterm_received)
280 mp = vl_msg_api_alloc (sizeof (*mp));
281 memset (mp, 0, sizeof (*mp));
282 mp->_vl_msg_id = ntohs (VL_API_VNET_GET_SUMMARY_STATS);
283 mp->client_index = tm->my_client_index;
284 vl_msg_api_send_shmem (tm->vl_input_queue, (u8 *) & mp);
287 fformat (stdout, "Exiting...\n");
289 disconnect_from_vpe ();
294 * fd.io coding-style-patch-verification: ON
297 * eval: (c-set-style "gnu")