e873114fab93f9a1459be4a0e96a662dfd5fb893
[vpp.git] / vpp-api / python / pneum / test_pneum.c
1 /*
2  *------------------------------------------------------------------
3  * test_pneum.c
4  * 
5  * Copyright (c) 2016 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 #include <stdio.h>
20 #include <stdlib.h>
21 #include <sys/types.h>
22 #include <sys/socket.h>
23 #include <sys/mman.h>
24 #include <sys/stat.h>
25 #include <netinet/in.h>
26 #include <netdb.h>
27
28 #include <time.h>       /* time_t, time (for timestamp in second) */
29 #include <sys/timeb.h>  /* ftime, timeb (for timestamp in millisecond) */
30 #include <sys/time.h>   /* gettimeofday, timeval (for timestamp in microsecond) */
31
32 #include <vnet/vnet.h>
33 #include <vlib/vlib.h>
34 #include <vlib/unix/unix.h>
35 #include <vlibapi/api.h>
36 #include <vlibmemory/api.h>
37 #include <vnet/ip/ip.h>
38
39 #include <vpp-api/vpe_msg_enum.h>
40 #include <signal.h>
41 #include <setjmp.h>
42 #include "pneum.h"
43
44 #define vl_typedefs             /* define message structures */
45 #include <vpp-api/vpe_all_api_h.h> 
46 #undef vl_typedefs
47
48 /* we are not linking with vlib */
49 vlib_main_t vlib_global_main;
50 vlib_main_t **vlib_mains;
51
52 volatile int sigterm_received = 0;
53 volatile u32 result_ready;
54 volatile u16 result_msg_id;
55
56 /* M_NOALLOC: construct, but don't yet send a message */
57
58 #define M_NOALLOC(T,t)                          \
59   do {                                          \
60     result_ready = 0;                           \
61     memset (mp, 0, sizeof (*mp));               \
62     mp->_vl_msg_id = ntohs (VL_API_##T);        \
63     mp->client_index = am->my_client_index;     \
64   } while(0);
65
66
67
68 int
69 wrap_pneum_callback (char *data, int len)
70 {
71   //printf("Callback %d\n", len);
72   result_ready = 1;
73   result_msg_id = ntohs(*((u16 *)data));
74   return (0);
75 }
76
77 int main (int argc, char ** argv)
78 {
79   api_main_t * am = &api_main;
80   vl_api_show_version_t message;
81   vl_api_show_version_t *mp;
82   int async = 1;
83   int rv = pneum_connect("pneum_client", NULL, NULL);
84
85   if (rv != 0) {
86     printf("Connect failed: %d\n", rv);
87     exit(rv);
88   }
89  
90  struct timeb timer_msec;
91   long long int timestamp_msec_start; /* timestamp in millisecond. */
92   if (!ftime(&timer_msec)) {
93     timestamp_msec_start = ((long long int) timer_msec.time) * 1000ll + 
94       (long long int) timer_msec.millitm;
95   }
96   else {
97     timestamp_msec_start = -1;
98   }
99
100  
101   /*
102    * Test vpe_api_write and vpe_api_read to send and recv message for an
103    * API 
104    */
105   int i;
106   long int no_msgs = 10000;
107   mp = &message;
108
109   for (i = 0; i < no_msgs; i++) {
110     /* Construct the API message */
111     M_NOALLOC(SHOW_VERSION, show_version);
112     pneum_write((char *)mp, sizeof(*mp));
113 #ifndef __COVERITY__
114     /* As given, async is always 1. Shut up Coverity about it */
115     if (!async)
116       while (result_ready == 0);
117 #endif
118   }
119   if (async) {
120     vl_api_control_ping_t control;
121     vl_api_control_ping_t *mp;
122     mp = &control;
123     M_NOALLOC(CONTROL_PING, control_ping);
124     pneum_write((char *)mp, sizeof(*mp));
125
126     while (result_msg_id != VL_API_CONTROL_PING_REPLY);
127   }
128
129   long long int timestamp_msec_end; /* timestamp in millisecond. */
130   if (!ftime(&timer_msec)) {
131     timestamp_msec_end = ((long long int) timer_msec.time) * 1000ll + 
132       (long long int) timer_msec.millitm;
133   }
134   else {
135     timestamp_msec_end = -1;
136   }
137   
138   printf("Took %lld msec, %lld msgs/msec \n", (timestamp_msec_end - timestamp_msec_start),
139          no_msgs/(timestamp_msec_end - timestamp_msec_start));
140   fformat(stdout, "Exiting...\n");
141   pneum_disconnect();
142   exit (0);
143 }