Initial commit of vpp code.
[vpp.git] / vpp / api / test_ha.c
1 /*
2  *------------------------------------------------------------------
3  * api.c - message handler registration
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/mman.h>
24 #include <sys/stat.h>
25 #include <netinet/in.h>
26 #include <signal.h>
27 #include <pthread.h>
28 #include <unistd.h>
29 #include <time.h>
30 #include <fcntl.h>
31 #include <string.h>
32 #include <vppinfra/clib.h>
33 #include <vppinfra/vec.h>
34 #include <vppinfra/hash.h>
35 #include <vppinfra/bitmap.h>
36 #include <vppinfra/fifo.h>
37 #include <vppinfra/time.h>
38 #include <vppinfra/mheap.h>
39 #include <vppinfra/heap.h>
40 #include <vppinfra/pool.h>
41 #include <vppinfra/format.h>
42 #include <vppinfra/error.h>
43
44 #include <vnet/vnet.h>
45 #include <vlib/vlib.h>
46 #include <vlib/unix/unix.h>
47 #include <vlibapi/api.h>
48 #include <vlibmemory/api.h>
49 #include <svm.h>
50 #include <svmdb.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
73 vl_shmem_hdr_t *shmem_hdr;
74
75 typedef struct {
76     u32 pings_sent;
77     u32 pings_replied;
78     volatile u32 signal_received;
79
80     /* convenience */
81     unix_shared_memory_queue_t * vl_input_queue;
82     u32 my_client_index;
83     svmdb_client_t * svmdb_client;
84 } test_main_t;
85
86 test_main_t test_main;
87
88 static void vl_api_control_ping_reply_t_handler 
89 (vl_api_control_ping_reply_t * mp)
90 {
91     test_main_t * tm = &test_main;
92
93     fformat(stdout, "control ping reply from pid %d\n",
94             ntohl (mp->vpe_pid));
95     tm->pings_replied++;
96 }
97
98 void vlib_cli_output (struct vlib_main_t * vm, char * fmt, ...)
99 {
100     clib_warning ("BUG: vlib_cli_output callled...");
101 }
102
103 #define foreach_api_msg                         \
104 _(CONTROL_PING_REPLY,control_ping_reply)
105
106 void ping (test_main_t *tm)
107 {
108     vl_api_control_ping_t * mp;
109
110     mp = vl_msg_api_alloc (sizeof (*mp));
111     memset(mp, 0, sizeof (*mp));
112     mp->_vl_msg_id = ntohs (VL_API_CONTROL_PING);
113     mp->client_index = tm->my_client_index;
114     mp->context = 0xdeadbeef;
115     
116     vl_msg_api_send_shmem (tm->vl_input_queue, (u8 *)&mp);
117 }
118
119 static void noop_handler (void *notused) { }
120
121 int connect_to_vpe(char *name)
122 {
123     int rv=0;
124     test_main_t * tm = &test_main;
125     api_main_t * am = &api_main;
126
127     rv = vl_client_connect_to_vlib("/vpe-api", name, 32);
128     if (rv < 0)
129         return rv;
130
131 #define _(N,n)                                                  \
132     vl_msg_api_set_handlers(VL_API_##N, #n,                     \
133                            vl_api_##n##_t_handler,              \
134                            noop_handler,                        \
135                            vl_api_##n##_t_endian,               \
136                            vl_api_##n##_t_print,                \
137                            sizeof(vl_api_##n##_t), 1); 
138     foreach_api_msg;
139 #undef _
140
141     shmem_hdr = api_main.shmem_hdr;
142     tm->vl_input_queue = shmem_hdr->vl_input_queue;
143     tm->my_client_index = am->my_client_index;
144     return 0;
145 }
146
147 int disconnect_from_vpe(void)
148 {
149     vl_client_disconnect_from_vlib();
150     
151     return 0;
152 }
153
154 void signal_handler (int signo)
155 {
156     test_main_t * tm = &test_main;
157
158     tm->signal_received = 1;
159 }
160
161
162 int main (int argc, char **argv)
163 {
164     test_main_t * tm = &test_main;
165     api_main_t * am = &api_main;
166     u32 swt_pid = 0;
167     int connected = 0;
168
169     signal (SIGINT, signal_handler);
170
171     while (1) {
172         if (tm->signal_received)
173             break;
174         
175         if (am->shmem_hdr)
176             swt_pid = am->shmem_hdr->vl_pid;
177         
178         /* If kill returns 0, the vpe-f process is alive */
179         if (kill(swt_pid, 0) == 0) {
180             /* Try to connect */
181             if (connected == 0) {
182                 fformat (stdout, "Connect to VPE-f\n");
183                 if (connect_to_vpe("test_ha_client") >= 0) {
184                     tm->pings_sent = 0;
185                     tm->pings_replied = 0;
186                     connected = 1;
187                 } else {
188                     fformat(stdout, "Connect failed, sleep and retry...\n");
189                     sleep(1);
190                     continue;
191                 }
192             }
193             tm->pings_sent ++;
194             ping(tm);
195
196             sleep (1);
197             
198             /* havent heard back in 3 seconds, disco / reco */
199             if ((tm->pings_replied + 3) <= tm->pings_sent) {
200                 fformat (stdout, "VPE-f pid %d not responding\n", swt_pid);
201                 swt_pid = 0;
202                 disconnect_from_vpe();
203                 connected = 0;
204             }
205         } else {
206             if (connected) {
207                 fformat (stdout, "VPE-f pid %d died\n", swt_pid);
208                 swt_pid = 0;
209                 disconnect_from_vpe();
210                 connected = 0;
211             }
212             sleep (1);
213         }
214     }
215
216     fformat (stdout, "Signal received, graceful exit\n");
217     disconnect_from_vpe();
218     exit (0);
219 }