test_ha.c revision 39d69112
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/svm.h>
50#include <svm/svmdb.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
73vl_shmem_hdr_t *shmem_hdr;
74
75typedef struct
76{
77  u32 pings_sent;
78  u32 pings_replied;
79  volatile u32 signal_received;
80
81  /* convenience */
82  svm_queue_t *vl_input_queue;
83  u32 my_client_index;
84  svmdb_client_t *svmdb_client;
85} test_main_t;
86
87test_main_t test_main;
88
89static void vl_api_control_ping_reply_t_handler
90  (vl_api_control_ping_reply_t * mp)
91{
92  test_main_t *tm = &test_main;
93
94  fformat (stdout, "control ping reply from pid %d\n", ntohl (mp->vpe_pid));
95  tm->pings_replied++;
96}
97
98vlib_main_t vlib_global_main;
99vlib_main_t **vlib_mains;
100
101void
102vlib_cli_output (struct vlib_main_t *vm, char *fmt, ...)
103{
104  clib_warning ("BUG: vlib_cli_output called...");
105}
106
107#define foreach_api_msg                         \
108_(CONTROL_PING_REPLY,control_ping_reply)
109
110void
111ping (test_main_t * tm)
112{
113  vl_api_control_ping_t *mp;
114
115  mp = vl_msg_api_alloc (sizeof (*mp));
116  clib_memset (mp, 0, sizeof (*mp));
117  mp->_vl_msg_id = ntohs (VL_API_CONTROL_PING);
118  mp->client_index = tm->my_client_index;
119  mp->context = 0xdeadbeef;
120
121  vl_msg_api_send_shmem (tm->vl_input_queue, (u8 *) & mp);
122}
123
124static void
125noop_handler (void *notused)
126{
127}
128
129int
130connect_to_vpe (char *name)
131{
132  int rv = 0;
133  test_main_t *tm = &test_main;
134  api_main_t *am = vlibapi_get_main ();
135
136  rv = vl_client_connect_to_vlib ("/vpe-api", name, 32);
137  if (rv < 0)
138    return rv;
139
140#define _(N,n)                                                  \
141    vl_msg_api_set_handlers(VL_API_##N, #n,                     \
142                           vl_api_##n##_t_handler,              \
143                           noop_handler,                        \
144                           vl_api_##n##_t_endian,               \
145                           vl_api_##n##_t_print,                \
146                           sizeof(vl_api_##n##_t), 1);
147  foreach_api_msg;
148#undef _
149
150  shmem_hdr = api_main.shmem_hdr;
151  tm->vl_input_queue = shmem_hdr->vl_input_queue;
152  tm->my_client_index = am->my_client_index;
153  return 0;
154}
155
156int
157disconnect_from_vpe (void)
158{
159  vl_client_disconnect_from_vlib ();
160
161  return 0;
162}
163
164void
165signal_handler (int signo)
166{
167  test_main_t *tm = &test_main;
168
169  tm->signal_received = 1;
170}
171
172
173int
174main (int argc, char **argv)
175{
176  test_main_t *tm = &test_main;
177  api_main_t *am = vlibapi_get_main ();
178  u32 swt_pid = 0;
179  int connected = 0;
180
181  signal (SIGINT, signal_handler);
182
183  while (1)
184    {
185      if (tm->signal_received)
186	break;
187
188      if (am->shmem_hdr)
189	swt_pid = am->shmem_hdr->vl_pid;
190
191      /* If kill returns 0, the vpe-f process is alive */
192      if (kill (swt_pid, 0) == 0)
193	{
194	  /* Try to connect */
195	  if (connected == 0)
196	    {
197	      fformat (stdout, "Connect to VPE-f\n");
198	      if (connect_to_vpe ("test_ha_client") >= 0)
199		{
200		  tm->pings_sent = 0;
201		  tm->pings_replied = 0;
202		  connected = 1;
203		}
204	      else
205		{
206		  fformat (stdout, "Connect failed, sleep and retry...\n");
207		  sleep (1);
208		  continue;
209		}
210	    }
211	  tm->pings_sent++;
212	  ping (tm);
213
214	  sleep (1);
215
216	  /* havent heard back in 3 seconds, disco / reco */
217	  if ((tm->pings_replied + 3) <= tm->pings_sent)
218	    {
219	      fformat (stdout, "VPE-f pid %d not responding\n", swt_pid);
220	      swt_pid = 0;
221	      disconnect_from_vpe ();
222	      connected = 0;
223	    }
224	}
225      else
226	{
227	  if (connected)
228	    {
229	      fformat (stdout, "VPE-f pid %d died\n", swt_pid);
230	      swt_pid = 0;
231	      disconnect_from_vpe ();
232	      connected = 0;
233	    }
234	  sleep (1);
235	}
236    }
237
238  fformat (stdout, "Signal received, graceful exit\n");
239  disconnect_from_vpe ();
240  exit (0);
241}
242
243/*
244 * fd.io coding-style-patch-verification: ON
245 *
246 * Local Variables:
247 * eval: (c-set-style "gnu")
248 * End:
249 */
250