FD.io VPP  v16.09
Vector Packet Processing
summary_stats_client.c
Go to the documentation of this file.
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 
74 
75 typedef struct
76 {
77  volatile int sigterm_received;
78 
79  struct sockaddr_in send_data_addr;
82 
83  /* convenience */
86 } test_main_t;
87 
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
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);
151 #undef _
152 
153  shmem_hdr = api_main.shmem_hdr;
154 
155  return rv;
156 }
157 
158 int
160 {
162  return 0;
163 }
164 
165 static void
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;
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 
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 =
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;
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 
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  */
int vl_client_connect_to_vlib(char *svm_name, char *client_name, int rx_queue_size)
uword unformat(unformat_input_t *i, char *fmt,...)
Definition: unformat.c:966
a
Definition: bitmap.h:516
void vl_msg_api_send_shmem(unix_shared_memory_queue_t *q, u8 *elem)
void vl_client_disconnect_from_vlib(void)
Fixed length block allocator.
unix_shared_memory_queue_t * vl_input_queue
Definition: api.h:73
int my_client_index
Definition: api.h:171
#define UNFORMAT_END_OF_INPUT
Definition: format.h:143
Request for a single block of summary stats.
Definition: vpe.api:710
#define clib_error_report(e)
Definition: error.h:125
#define foreach_api_msg
#define vec_reset_length(v)
Reset vector length to zero NULL-pointer tolerant.
static void sigterm_handler(int sig)
api_main_t api_main
Definition: api.h:185
struct sockaddr_in send_data_addr
#define clib_warning(format, args...)
Definition: error.h:59
struct vl_shmem_hdr_ * shmem_hdr
Definition: api.h:132
test_main_t test_main
uword unformat_ip4_address(unformat_input_t *input, va_list *args)
vl_shmem_hdr_t * shmem_hdr
static void vl_api_vnet_summary_stats_reply_t_handler(vl_api_vnet_summary_stats_reply_t *mp)
void unformat_init_command_line(unformat_input_t *input, char *argv[])
Definition: unformat.c:1001
void * vl_msg_api_alloc(int nbytes)
#define clib_unix_warning(format, args...)
Definition: error.h:68
int connect_to_vpe(char *name)
int disconnect_from_vpe(void)
Reply for vnet_get_summary_stats request.
Definition: vpe.api:723
unsigned int u32
Definition: types.h:88
u8 * format(u8 *s, char *fmt,...)
Definition: format.c:418
Bitmaps built as vectors of machine words.
volatile int sigterm_received
u64 uword
Definition: types.h:112
int main(int argc, char **argv)
unsigned short u16
Definition: types.h:57
unix_shared_memory_queue_t * vl_input_queue
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
unsigned char u8
Definition: types.h:56
word fformat(FILE *f, char *fmt,...)
Definition: format.c:452
static uword unformat_check_input(unformat_input_t *i)
Definition: format.h:169
#define clib_error_return(e, args...)
Definition: error.h:111
struct _unformat_input_t unformat_input_t
void vlib_cli_output(struct vlib_main_t *vm, char *fmt,...)
CLIB vectors are ubiquitous dynamically resized arrays with by user defined "headers".
struct _unix_shared_memory_queue unix_shared_memory_queue_t