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;
76 volatile int sigterm_received;
78 struct sockaddr_in send_data_addr;
83 unix_shared_memory_queue_t * vl_input_queue;
87 test_main_t test_main;
90 * Satisfy external references when -lvlib is not available.
92 void vlib_cli_output (struct vlib_main_t * vm, char * fmt, ...)
94 clib_warning ("vlib_cli_output callled...");
99 vl_api_vnet_summary_stats_reply_t_handler (
100 vl_api_vnet_summary_stats_reply_t * mp)
102 test_main_t * tm = &test_main;
106 printf ("total rx pkts %llu, total rx bytes %llu\n",
107 (unsigned long long) mp->total_pkts[0],
108 (unsigned long long) mp->total_bytes[0]);
109 printf ("total tx pkts %llu, total tx bytes %llu\n",
110 (unsigned long long) mp->total_pkts[1],
111 (unsigned long long) mp->total_bytes[1]);
112 printf ("vector rate %.2f\n", mp->vector_rate);
114 vec_reset_length (sb);
115 sb = format (sb, "%v,%.0f,%llu,%llu,%llu,%llu\n%c",
116 tm->display_name, mp->vector_rate,
117 (unsigned long long) mp->total_pkts[0],
118 (unsigned long long) mp->total_bytes[0],
119 (unsigned long long) mp->total_pkts[1],
120 (unsigned long long) mp->total_bytes[1], 0);
122 n = sendto (tm->send_data_socket, sb, vec_len(sb),
123 0, (struct sockaddr *)&tm->send_data_addr,
124 sizeof (tm->send_data_addr));
126 if (n != vec_len (sb))
127 clib_unix_warning ("sendto");
131 #define foreach_api_msg \
132 _(VNET_SUMMARY_STATS_REPLY, vnet_summary_stats_reply)
134 int connect_to_vpe(char *name)
138 rv = vl_client_connect_to_vlib("/vpe-api", name, 32);
141 vl_msg_api_set_handlers(VL_API_##N, #n, \
142 vl_api_##n##_t_handler, \
144 vl_api_##n##_t_endian, \
145 vl_api_##n##_t_print, \
146 sizeof(vl_api_##n##_t), 1);
150 shmem_hdr = api_main.shmem_hdr;
155 int disconnect_from_vpe(void)
157 vl_client_disconnect_from_vlib();
161 static void sigterm_handler (int sig)
163 test_main_t *tm = &test_main;
164 tm->sigterm_received = 1;
167 /* Parse an IP4 address %d.%d.%d.%d. */
168 uword unformat_ip4_address (unformat_input_t * input, va_list * args)
170 u8 * result = va_arg (*args, u8 *);
173 if (! unformat (input, "%d.%d.%d.%d", &a[0], &a[1], &a[2], &a[3]))
176 if (a[0] >= 256 || a[1] >= 256 || a[2] >= 256 || a[3] >= 256)
187 int main (int argc, char ** argv)
189 api_main_t * am = &api_main;
190 test_main_t * tm = &test_main;
191 vl_api_vnet_get_summary_stats_t * mp;
192 unformat_input_t _input, *input = &_input;
193 clib_error_t * error = 0;
194 ip4_address_t collector_ip;
195 u8 * display_name = 0;
196 u16 collector_port = 7654;
198 collector_ip.as_u32 = (u32)~0;
200 unformat_init_command_line (input, argv);
202 while (unformat_check_input(input) != UNFORMAT_END_OF_INPUT) {
203 if (unformat (input, "collector-ip %U",
204 unformat_ip4_address, &collector_ip))
206 else if (unformat (input, "display-name %v", &display_name))
208 else if (unformat (input, "collector-port %d", &collector_port))
213 (0, "Usage: %s collector-ip <ip>\n"
214 " [display-name <string>] [collector-port <num>]\n"
215 " port defaults to 7654", argv[0]);
220 if (error == 0 && collector_ip.as_u32 == (u32)~0)
221 error = clib_error_return (0, "collector-ip not set...\n");
225 clib_error_report (error);
229 if (display_name == 0) {
230 display_name = format (0, "vpe-to-%d.%d.%d.%d",
231 collector_ip.as_u8[0],
232 collector_ip.as_u8[1],
233 collector_ip.as_u8[2],
234 collector_ip.as_u8[3]);
238 connect_to_vpe("test_client");
240 tm->vl_input_queue = shmem_hdr->vl_input_queue;
241 tm->my_client_index = am->my_client_index;
242 tm->display_name = display_name;
244 signal(SIGTERM, sigterm_handler);
245 signal(SIGINT, sigterm_handler);
246 signal(SIGQUIT, sigterm_handler);
248 /* data (multicast) RX socket */
249 tm->send_data_socket = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
250 if (tm->send_data_socket < 0) {
251 clib_unix_warning(0, "data_rx_socket");
255 memset(&tm->send_data_addr, 0, sizeof(tm->send_data_addr));
256 tm->send_data_addr.sin_family = AF_INET;
257 tm->send_data_addr.sin_addr.s_addr = collector_ip.as_u32;
258 tm->send_data_addr.sin_port = htons(collector_port);
260 fformat(stdout, "Send SIGINT or SIGTERM to quit...\n");
265 if (tm->sigterm_received)
268 mp = vl_msg_api_alloc (sizeof (*mp));
269 memset(mp, 0, sizeof (*mp));
270 mp->_vl_msg_id = ntohs (VL_API_VNET_GET_SUMMARY_STATS);
271 mp->client_index = tm->my_client_index;
272 vl_msg_api_send_shmem (tm->vl_input_queue, (u8 *)&mp);
275 fformat(stdout, "Exiting...\n");
277 disconnect_from_vpe();