FD.io VPP  v16.06
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 <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 <api/vpe_all_api_h.h>
61 #undef vl_typedefs
62 
63 #define vl_endianfun /* define message structures */
64 #include <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 <api/vpe_all_api_h.h>
71 #undef vl_printfun
72 
74 
75 typedef struct {
76  volatile int sigterm_received;
77 
78  struct sockaddr_in send_data_addr;
81 
82  /* convenience */
85 } test_main_t;
86 
88 
89 /*
90  * Satisfy external references when -lvlib is not available.
91  */
92 void vlib_cli_output (struct vlib_main_t * vm, char * fmt, ...)
93 {
94  clib_warning ("vlib_cli_output callled...");
95 }
96 
97 
98 static void
101 {
102  test_main_t * tm = &test_main;
103  static u8 *sb;
104  int n;
105 
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);
113 
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);
121 
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));
125 
126  if (n != vec_len (sb))
127  clib_unix_warning ("sendto");
128 
129 }
130 
131 #define foreach_api_msg \
132 _(VNET_SUMMARY_STATS_REPLY, vnet_summary_stats_reply)
133 
134 int connect_to_vpe(char *name)
135 {
136  int rv=0;
137 
138  rv = vl_client_connect_to_vlib("/vpe-api", name, 32);
139 
140 #define _(N,n) \
141  vl_msg_api_set_handlers(VL_API_##N, #n, \
142  vl_api_##n##_t_handler, \
143  vl_noop_handler, \
144  vl_api_##n##_t_endian, \
145  vl_api_##n##_t_print, \
146  sizeof(vl_api_##n##_t), 1);
148 #undef _
149 
150  shmem_hdr = api_main.shmem_hdr;
151 
152  return rv;
153 }
154 
156 {
158  return 0;
159 }
160 
161 static void sigterm_handler (int sig)
162 {
163  test_main_t *tm = &test_main;
164  tm->sigterm_received = 1;
165 }
166 
167 /* Parse an IP4 address %d.%d.%d.%d. */
168 uword unformat_ip4_address (unformat_input_t * input, va_list * args)
169 {
170  u8 * result = va_arg (*args, u8 *);
171  unsigned a[4];
172 
173  if (! unformat (input, "%d.%d.%d.%d", &a[0], &a[1], &a[2], &a[3]))
174  return 0;
175 
176  if (a[0] >= 256 || a[1] >= 256 || a[2] >= 256 || a[3] >= 256)
177  return 0;
178 
179  result[0] = a[0];
180  result[1] = a[1];
181  result[2] = a[2];
182  result[3] = a[3];
183 
184  return 1;
185 }
186 
187 int main (int argc, char ** argv)
188 {
189  api_main_t * am = &api_main;
190  test_main_t * tm = &test_main;
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;
197 
198  collector_ip.as_u32 = (u32)~0;
199 
200  unformat_init_command_line (input, argv);
201 
202  while (unformat_check_input(input) != UNFORMAT_END_OF_INPUT) {
203  if (unformat (input, "collector-ip %U",
204  unformat_ip4_address, &collector_ip))
205  ;
206  else if (unformat (input, "display-name %v", &display_name))
207  ;
208  else if (unformat (input, "collector-port %d", &collector_port))
209  ;
210  else {
211  error =
213  (0, "Usage: %s collector-ip <ip>\n"
214  " [display-name <string>] [collector-port <num>]\n"
215  " port defaults to 7654", argv[0]);
216  break;
217  }
218  }
219 
220  if (error == 0 && collector_ip.as_u32 == (u32)~0)
221  error = clib_error_return (0, "collector-ip not set...\n");
222 
223 
224  if (error) {
225  clib_error_report (error);
226  exit (1);
227  }
228 
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]);
235  }
236 
237 
238  connect_to_vpe("test_client");
239 
240  tm->vl_input_queue = shmem_hdr->vl_input_queue;
242  tm->display_name = display_name;
243 
244  signal(SIGTERM, sigterm_handler);
245  signal(SIGINT, sigterm_handler);
246  signal(SIGQUIT, sigterm_handler);
247 
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");
252  exit (1);
253  }
254 
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);
259 
260  fformat(stdout, "Send SIGINT or SIGTERM to quit...\n");
261 
262  while (1) {
263  sleep (5);
264 
265  if (tm->sigterm_received)
266  break;
267  /* Poll for stats */
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;
273  }
274 
275  fformat(stdout, "Exiting...\n");
276 
278  exit (0);
279 }
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:942
a
Definition: bitmap.h:393
void vl_msg_api_send_shmem(unix_shared_memory_queue_t *q, u8 *elem)
void vl_client_disconnect_from_vlib(void)
unix_shared_memory_queue_t * vl_input_queue
Definition: api.h:71
int my_client_index
Definition: api.h:148
#define UNFORMAT_END_OF_INPUT
Definition: format.h:142
Request for a single block of summary stats.
Definition: vpe.api:666
#define clib_error_report(e)
Definition: error.h:126
#define foreach_api_msg
#define vec_reset_length(v)
Reset vector length to zero NULL-pointer tolerant.
always_inline uword unformat_check_input(unformat_input_t *i)
Definition: format.h:168
static void sigterm_handler(int sig)
api_main_t api_main
Definition: api.h:162
struct sockaddr_in send_data_addr
#define clib_warning(format, args...)
Definition: error.h:59
struct vl_shmem_hdr_ * shmem_hdr
Definition: api.h:125
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:975
void * vl_msg_api_alloc(int nbytes)
int connect_to_vpe(char *name)
#define clib_unix_warning(format, args...)
Definition: error.h:68
int disconnect_from_vpe(void)
Reply for vnet_get_summary_stats request.
Definition: vpe.api:678
unsigned int u32
Definition: types.h:88
u8 * format(u8 *s, char *fmt,...)
Definition: format.c:405
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:437
#define clib_error_return(e, args...)
Definition: error.h:112
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