cbfef48d12326c87ef0787fad52ee982de74b29e
[vpp.git] / vpp / vpp-api / summary_stats_client.c
1 /*
2  *------------------------------------------------------------------
3  * summary_stats_client -
4  *
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:
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
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  *------------------------------------------------------------------
18  */
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <sys/types.h>
23 #include <sys/socket.h>
24 #include <sys/mman.h>
25 #include <sys/stat.h>
26 #include <netinet/in.h>
27 #include <netdb.h>
28 #include <signal.h>
29 #include <pthread.h>
30 #include <unistd.h>
31 #include <time.h>
32 #include <fcntl.h>
33 #include <string.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>
45
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>
51
52 #include <vpp-api/vpe_msg_enum.h>
53
54 #include <vnet/ip/ip.h>
55
56 #define f64_endian(a)
57 #define f64_print(a,b)
58
59 #define vl_typedefs             /* define message structures */
60 #include <vpp-api/vpe_all_api_h.h>
61 #undef vl_typedefs
62
63 #define vl_endianfun            /* define message structures */
64 #include <vpp-api/vpe_all_api_h.h>
65 #undef vl_endianfun
66
67 /* instantiate all the print functions we know about */
68 #define vl_print(handle, ...)
69 #define vl_printfun
70 #include <vpp-api/vpe_all_api_h.h>
71 #undef vl_printfun
72
73 vl_shmem_hdr_t *shmem_hdr;
74
75 typedef struct
76 {
77   volatile int sigterm_received;
78
79   struct sockaddr_in send_data_addr;
80   int send_data_socket;
81   u8 *display_name;
82
83   /* convenience */
84   unix_shared_memory_queue_t *vl_input_queue;
85   u32 my_client_index;
86 } test_main_t;
87
88 test_main_t test_main;
89
90 /*
91  * Satisfy external references when -lvlib is not available.
92  */
93 void
94 vlib_cli_output (struct vlib_main_t *vm, char *fmt, ...)
95 {
96   clib_warning ("vlib_cli_output callled...");
97 }
98
99
100 static void
101 vl_api_vnet_summary_stats_reply_t_handler (vl_api_vnet_summary_stats_reply_t *
102                                            mp)
103 {
104   test_main_t *tm = &test_main;
105   static u8 *sb;
106   int n;
107
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);
115
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);
123
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));
127
128   if (n != vec_len (sb))
129     clib_unix_warning ("sendto");
130
131 }
132
133 #define foreach_api_msg                                                 \
134 _(VNET_SUMMARY_STATS_REPLY, vnet_summary_stats_reply)
135
136 int
137 connect_to_vpe (char *name)
138 {
139   int rv = 0;
140
141   rv = vl_client_connect_to_vlib ("/vpe-api", name, 32);
142
143 #define _(N,n)                                                  \
144     vl_msg_api_set_handlers(VL_API_##N, #n,                     \
145                            vl_api_##n##_t_handler,              \
146                            vl_noop_handler,                     \
147                            vl_api_##n##_t_endian,               \
148                            vl_api_##n##_t_print,                \
149                            sizeof(vl_api_##n##_t), 1);
150   foreach_api_msg;
151 #undef _
152
153   shmem_hdr = api_main.shmem_hdr;
154
155   return rv;
156 }
157
158 int
159 disconnect_from_vpe (void)
160 {
161   vl_client_disconnect_from_vlib ();
162   return 0;
163 }
164
165 static void
166 sigterm_handler (int sig)
167 {
168   test_main_t *tm = &test_main;
169   tm->sigterm_received = 1;
170 }
171
172 /* Parse an IP4 address %d.%d.%d.%d. */
173 uword
174 unformat_ip4_address (unformat_input_t * input, va_list * args)
175 {
176   u8 *result = va_arg (*args, u8 *);
177   unsigned a[4];
178
179   if (!unformat (input, "%d.%d.%d.%d", &a[0], &a[1], &a[2], &a[3]))
180     return 0;
181
182   if (a[0] >= 256 || a[1] >= 256 || a[2] >= 256 || a[3] >= 256)
183     return 0;
184
185   result[0] = a[0];
186   result[1] = a[1];
187   result[2] = a[2];
188   result[3] = a[3];
189
190   return 1;
191 }
192
193 int
194 main (int argc, char **argv)
195 {
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;
204
205   collector_ip.as_u32 = (u32) ~ 0;
206
207   unformat_init_command_line (input, argv);
208
209   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
210     {
211       if (unformat (input, "collector-ip %U",
212                     unformat_ip4_address, &collector_ip))
213         ;
214       else if (unformat (input, "display-name %v", &display_name))
215         ;
216       else if (unformat (input, "collector-port %d", &collector_port))
217         ;
218       else
219         {
220           error =
221             clib_error_return
222             (0, "Usage: %s collector-ip <ip>\n"
223              "    [display-name <string>] [collector-port <num>]\n"
224              "    port defaults to 7654", argv[0]);
225           break;
226         }
227     }
228
229   if (error == 0 && collector_ip.as_u32 == (u32) ~ 0)
230     error = clib_error_return (0, "collector-ip not set...\n");
231
232
233   if (error)
234     {
235       clib_error_report (error);
236       exit (1);
237     }
238
239   if (display_name == 0)
240     {
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]);
245     }
246
247
248   connect_to_vpe ("test_client");
249
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;
253
254   signal (SIGTERM, sigterm_handler);
255   signal (SIGINT, sigterm_handler);
256   signal (SIGQUIT, sigterm_handler);
257
258   /* data (multicast) RX socket */
259   tm->send_data_socket = socket (PF_INET, SOCK_DGRAM, IPPROTO_UDP);
260   if (tm->send_data_socket < 0)
261     {
262       clib_unix_warning (0, "data_rx_socket");
263       exit (1);
264     }
265
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);
270
271   fformat (stdout, "Send SIGINT or SIGTERM to quit...\n");
272
273   while (1)
274     {
275       sleep (5);
276
277       if (tm->sigterm_received)
278         break;
279       /* Poll for stats */
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);
285     }
286
287   fformat (stdout, "Exiting...\n");
288
289   disconnect_from_vpe ();
290   exit (0);
291 }
292
293 /*
294  * fd.io coding-style-patch-verification: ON
295  *
296  * Local Variables:
297  * eval: (c-set-style "gnu")
298  * End:
299  */