devices: Add queues params in create_if
[vpp.git] / src / 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/heap.h>
39 #include <vppinfra/pool.h>
40 #include <vppinfra/format.h>
41 #include <vppinfra/error.h>
42
43 #include <vnet/vnet.h>
44 #include <vlib/vlib.h>
45 #include <vlib/unix/unix.h>
46 #include <vlibapi/api.h>
47 #include <vlibmemory/api.h>
48 #include <svm/svm.h>
49 #include <svm/svmdb.h>
50
51 #include <vpp/api/vpe_msg_enum.h>
52
53 #include <vnet/ip/ip.h>
54
55 #define f64_endian(a)
56 #define f64_print(a,b)
57
58 #define vl_typedefs             /* define message structures */
59 #include <vpp/api/vpe_all_api_h.h>
60 #undef vl_typedefs
61
62 #define vl_endianfun            /* define message structures */
63 #include <vpp/api/vpe_all_api_h.h>
64 #undef vl_endianfun
65
66 /* instantiate all the print functions we know about */
67 #define vl_print(handle, ...)
68 #define vl_printfun
69 #include <vpp/api/vpe_all_api_h.h>
70 #undef vl_printfun
71
72 vl_shmem_hdr_t *shmem_hdr;
73
74 typedef struct
75 {
76   u32 pings_sent;
77   u32 pings_replied;
78   volatile u32 signal_received;
79
80   /* convenience */
81   svm_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", ntohl (mp->vpe_pid));
94   tm->pings_replied++;
95 }
96
97 void
98 vlib_cli_output (struct vlib_main_t *vm, char *fmt, ...)
99 {
100   clib_warning ("BUG: vlib_cli_output called...");
101 }
102
103 #define foreach_api_msg                         \
104 _(CONTROL_PING_REPLY,control_ping_reply)
105
106 void
107 ping (test_main_t * tm)
108 {
109   vl_api_control_ping_t *mp;
110
111   mp = vl_msg_api_alloc (sizeof (*mp));
112   clib_memset (mp, 0, sizeof (*mp));
113   mp->_vl_msg_id = ntohs (VL_API_CONTROL_PING);
114   mp->client_index = tm->my_client_index;
115   mp->context = 0xdeadbeef;
116
117   vl_msg_api_send_shmem (tm->vl_input_queue, (u8 *) & mp);
118 }
119
120 static void
121 noop_handler (void *notused)
122 {
123 }
124
125 int
126 connect_to_vpe (char *name)
127 {
128   int rv = 0;
129   test_main_t *tm = &test_main;
130   api_main_t *am = vlibapi_get_main ();
131
132   rv = vl_client_connect_to_vlib ("/vpe-api", name, 32);
133   if (rv < 0)
134     return rv;
135
136 #define _(N,n)                                                  \
137     vl_msg_api_set_handlers(VL_API_##N, #n,                     \
138                            vl_api_##n##_t_handler,              \
139                            noop_handler,                        \
140                            vl_api_##n##_t_endian,               \
141                            vl_api_##n##_t_print,                \
142                            sizeof(vl_api_##n##_t), 1);
143   foreach_api_msg;
144 #undef _
145
146   shmem_hdr = api_main.shmem_hdr;
147   tm->vl_input_queue = shmem_hdr->vl_input_queue;
148   tm->my_client_index = am->my_client_index;
149   return 0;
150 }
151
152 int
153 disconnect_from_vpe (void)
154 {
155   vl_client_disconnect_from_vlib ();
156
157   return 0;
158 }
159
160 void
161 signal_handler (int signo)
162 {
163   test_main_t *tm = &test_main;
164
165   tm->signal_received = 1;
166 }
167
168
169 int
170 main (int argc, char **argv)
171 {
172   test_main_t *tm = &test_main;
173   api_main_t *am = vlibapi_get_main ();
174   u32 swt_pid = 0;
175   int connected = 0;
176
177   signal (SIGINT, signal_handler);
178
179   while (1)
180     {
181       if (tm->signal_received)
182         break;
183
184       if (am->shmem_hdr)
185         swt_pid = am->shmem_hdr->vl_pid;
186
187       /* If kill returns 0, the vpe-f process is alive */
188       if (kill (swt_pid, 0) == 0)
189         {
190           /* Try to connect */
191           if (connected == 0)
192             {
193               fformat (stdout, "Connect to VPE-f\n");
194               if (connect_to_vpe ("test_ha_client") >= 0)
195                 {
196                   tm->pings_sent = 0;
197                   tm->pings_replied = 0;
198                   connected = 1;
199                 }
200               else
201                 {
202                   fformat (stdout, "Connect failed, sleep and retry...\n");
203                   sleep (1);
204                   continue;
205                 }
206             }
207           tm->pings_sent++;
208           ping (tm);
209
210           sleep (1);
211
212           /* havent heard back in 3 seconds, disco / reco */
213           if ((tm->pings_replied + 3) <= tm->pings_sent)
214             {
215               fformat (stdout, "VPE-f pid %d not responding\n", swt_pid);
216               swt_pid = 0;
217               disconnect_from_vpe ();
218               connected = 0;
219             }
220         }
221       else
222         {
223           if (connected)
224             {
225               fformat (stdout, "VPE-f pid %d died\n", swt_pid);
226               swt_pid = 0;
227               disconnect_from_vpe ();
228               connected = 0;
229             }
230           sleep (1);
231         }
232     }
233
234   fformat (stdout, "Signal received, graceful exit\n");
235   disconnect_from_vpe ();
236   exit (0);
237 }
238
239 /*
240  * fd.io coding-style-patch-verification: ON
241  *
242  * Local Variables:
243  * eval: (c-set-style "gnu")
244  * End:
245  */