FD.io VPP  v17.10-9-gd594711
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  */
95 
96 void
97 vlib_cli_output (struct vlib_main_t *vm, char *fmt, ...)
98 {
99  clib_warning ("vlib_cli_output callled...");
100 }
101 
102 
103 static void
106 {
107  test_main_t *tm = &test_main;
108  static u8 *sb;
109  int n;
110 
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);
118 
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);
126 
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));
130 
131  if (n != vec_len (sb))
132  clib_unix_warning ("sendto");
133 
134 }
135 
136 #define foreach_api_msg \
137 _(VNET_GET_SUMMARY_STATS_REPLY, vnet_get_summary_stats_reply)
138 
139 int
140 connect_to_vpe (char *name)
141 {
142  int rv = 0;
143 
144  rv = vl_client_connect_to_vlib ("/vpe-api", name, 32);
145 
146 #define _(N,n) \
147  vl_msg_api_set_handlers(VL_API_##N, #n, \
148  vl_api_##n##_t_handler, \
149  vl_noop_handler, \
150  vl_api_##n##_t_endian, \
151  vl_api_##n##_t_print, \
152  sizeof(vl_api_##n##_t), 1);
154 #undef _
155 
156  shmem_hdr = api_main.shmem_hdr;
157 
158  return rv;
159 }
160 
161 int
163 {
165  return 0;
166 }
167 
168 static void
170 {
171  test_main_t *tm = &test_main;
172  tm->sigterm_received = 1;
173 }
174 
175 /* Parse an IP4 address %d.%d.%d.%d. */
176 uword
177 unformat_ip4_address (unformat_input_t * input, va_list * args)
178 {
179  u8 *result = va_arg (*args, u8 *);
180  unsigned a[4];
181 
182  if (!unformat (input, "%d.%d.%d.%d", &a[0], &a[1], &a[2], &a[3]))
183  return 0;
184 
185  if (a[0] >= 256 || a[1] >= 256 || a[2] >= 256 || a[3] >= 256)
186  return 0;
187 
188  result[0] = a[0];
189  result[1] = a[1];
190  result[2] = a[2];
191  result[3] = a[3];
192 
193  return 1;
194 }
195 
196 int
197 main (int argc, char **argv)
198 {
199  api_main_t *am = &api_main;
200  test_main_t *tm = &test_main;
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;
207 
208  collector_ip.as_u32 = (u32) ~ 0;
209 
210  unformat_init_command_line (input, argv);
211 
213  {
214  if (unformat (input, "collector-ip %U",
215  unformat_ip4_address, &collector_ip))
216  ;
217  else if (unformat (input, "display-name %v", &display_name))
218  ;
219  else if (unformat (input, "collector-port %d", &collector_port))
220  ;
221  else
222  {
223  error =
225  (0, "Usage: %s collector-ip <ip>\n"
226  " [display-name <string>] [collector-port <num>]\n"
227  " port defaults to 7654", argv[0]);
228  break;
229  }
230  }
231 
232  if (error == 0 && collector_ip.as_u32 == (u32) ~ 0)
233  error = clib_error_return (0, "collector-ip not set...\n");
234 
235 
236  if (error)
237  {
238  clib_error_report (error);
239  exit (1);
240  }
241 
242  if (display_name == 0)
243  {
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]);
248  }
249 
250 
251  connect_to_vpe ("test_client");
252 
253  tm->vl_input_queue = shmem_hdr->vl_input_queue;
255  tm->display_name = display_name;
256 
257  signal (SIGTERM, sigterm_handler);
258  signal (SIGINT, sigterm_handler);
259  signal (SIGQUIT, sigterm_handler);
260 
261  /* data (multicast) RX socket */
262  tm->send_data_socket = socket (PF_INET, SOCK_DGRAM, IPPROTO_UDP);
263  if (tm->send_data_socket < 0)
264  {
265  clib_unix_warning (0, "data_rx_socket");
266  exit (1);
267  }
268 
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);
273 
274  fformat (stdout, "Send SIGINT or SIGTERM to quit...\n");
275 
276  while (1)
277  {
278  sleep (5);
279 
280  if (tm->sigterm_received)
281  break;
282  /* Poll for stats */
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);
288  }
289 
290  fformat (stdout, "Exiting...\n");
291 
293  exit (0);
294 }
295 
296 /*
297  * fd.io coding-style-patch-verification: ON
298  *
299  * Local Variables:
300  * eval: (c-set-style "gnu")
301  * End:
302  */
vlib_main_t vlib_global_main
vlib_main_t ** vlib_mains
a
Definition: bitmap.h:516
Fixed length block allocator.
unix_shared_memory_queue_t * vl_input_queue
Definition: api_common.h:68
int my_client_index
All VLIB-side message handlers use my_client_index to identify the queue / client.
Definition: api_common.h:286
Request for a single block of summary stats.
Definition: stats.api:239
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:419
#define foreach_api_msg
#define vec_reset_length(v)
Reset vector length to zero NULL-pointer tolerant.
static void sigterm_handler(int sig)
struct sockaddr_in send_data_addr
int vl_client_connect_to_vlib(const char *svm_name, const char *client_name, int rx_queue_size)
#define clib_error_return(e, args...)
Definition: error.h:99
struct vl_shmem_hdr_ * shmem_hdr
Binary API shared-memory segment header pointer.
Definition: api_common.h:241
test_main_t test_main
static void vl_api_vnet_get_summary_stats_reply_t_handler(vl_api_vnet_get_summary_stats_reply_t *mp)
void * vl_msg_api_alloc(int nbytes)
uword unformat_ip4_address(unformat_input_t *input, va_list *args)
vl_shmem_hdr_t * shmem_hdr
struct _unformat_input_t unformat_input_t
void unformat_init_command_line(unformat_input_t *input, char *argv[])
Definition: unformat.c:1007
word fformat(FILE *f, char *fmt,...)
Definition: format.c:453
API main structure, used by both vpp and binary API clients.
Definition: api_common.h:182
api_main_t api_main
Definition: api_shared.c:35
#define UNFORMAT_END_OF_INPUT
Definition: format.h:143
vlib_main_t * vm
Definition: buffer.c:283
#define clib_warning(format, args...)
Definition: error.h:59
int connect_to_vpe(char *name)
int disconnect_from_vpe(void)
void vl_msg_api_send_shmem(unix_shared_memory_queue_t *q, u8 *elem)
unsigned int u32
Definition: types.h:88
Bitmaps built as vectors of machine words.
#define clib_error_report(e)
Definition: error.h:113
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
void vl_client_disconnect_from_vlib(void)
#define clib_unix_warning(format, args...)
Definition: error.h:68
Reply for vnet_get_summary_stats request.
Definition: stats.api:252
void vlib_cli_output(struct vlib_main_t *vm, char *fmt,...)
uword unformat(unformat_input_t *i, const char *fmt,...)
Definition: unformat.c:972
CLIB vectors are ubiquitous dynamically resized arrays with by user defined "headers".
static uword unformat_check_input(unformat_input_t *i)
Definition: format.h:169
struct _unix_shared_memory_queue unix_shared_memory_queue_t