blob: adc63f848c033e8c52766a00db1daa81a7995871 [file] [log] [blame]
Dave Barach2b836cf2016-04-22 09:54:22 -04001/*
2 * Copyright (c) 2015 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#include <stdio.h>
17#include <time.h>
18#include <sys/types.h>
19#include <signal.h>
20#include <sys/stat.h>
21#include <unistd.h>
22
23#include <vppinfra/clib.h>
24#include <vppinfra/vec.h>
25#include <vppinfra/hash.h>
26#include <svmdb.h>
27#include <vppinfra/format.h>
28#include <vppinfra/error.h>
29#include <vppinfra/time.h>
30#include <vppinfra/macros.h>
31
Dave Barach72d72232016-08-04 10:15:08 -040032int
33restart_main_fn (unformat_input_t * i)
Dave Barach2b836cf2016-04-22 09:54:22 -040034{
35 int verbose = 0;
36 int old_pid;
37 int wait;
Dave Barach72d72232016-08-04 10:15:08 -040038 u8 *chroot_path = 0;
39 svmdb_client_t *svmdb_client;
Dave Barach2b836cf2016-04-22 09:54:22 -040040 volatile pid_t *pidp;
41 struct stat statb;
42 ino_t old_inode;
43 int sleeps;
Dave Barach72d72232016-08-04 10:15:08 -040044 svmdb_map_args_t _ma, *ma = &_ma;
Dave Barach2b836cf2016-04-22 09:54:22 -040045
46 struct timespec _req, *req = &_req;
47 struct timespec _rem, *rem = &_rem;
48
Dave Barach72d72232016-08-04 10:15:08 -040049 if (geteuid ())
Dave Barach2b836cf2016-04-22 09:54:22 -040050 clib_error ("vpp_restart: must be root...");
51
52 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
53 {
54 if (unformat (i, "verbose") || unformat (i, "v"))
Dave Barach72d72232016-08-04 10:15:08 -040055 verbose = 1;
Dave Barach2b836cf2016-04-22 09:54:22 -040056 else if (unformat (i, "chroot %s", &chroot_path))
Dave Barach72d72232016-08-04 10:15:08 -040057 ;
Dave Barach2b836cf2016-04-22 09:54:22 -040058 else
Dave Barach72d72232016-08-04 10:15:08 -040059 {
60 clib_error ("unknown input `%U'", format_unformat_error, i);
61 return 1;
62 }
Dave Barach2b836cf2016-04-22 09:54:22 -040063 }
64
Dave Barach72d72232016-08-04 10:15:08 -040065 /*
66 * Step 1: look up the current VPP pid in the shared-memory database
Dave Barach2b836cf2016-04-22 09:54:22 -040067 */
Dave Barach98cfc1a2016-07-18 14:23:36 -040068 memset (ma, 0, sizeof (*ma));
Dave Barach72d72232016-08-04 10:15:08 -040069 ma->root_path = (char *) chroot_path;
Dave Barach98cfc1a2016-07-18 14:23:36 -040070
71 svmdb_client = svmdb_map (ma);
Dave Barach2b836cf2016-04-22 09:54:22 -040072
73 pidp = svmdb_local_get_variable_reference (svmdb_client,
Dave Barach72d72232016-08-04 10:15:08 -040074 SVMDB_NAMESPACE_VEC, "vpp_pid");
Dave Barach2b836cf2016-04-22 09:54:22 -040075 if (pidp == 0)
76 {
77 clib_error ("'vpp_pid' svm variable not found, vpp has never run?");
78 return 2;
79 }
80
81 /* Spin for up to 10 seconds for vpp to start */
82 for (wait = 0; wait < 1000; wait++)
83 {
84 req->tv_sec = 0;
Dave Barach72d72232016-08-04 10:15:08 -040085 req->tv_nsec = 10000 * 1000; /* 10 ms */
86 while (nanosleep (req, rem) < 0)
87 *req = *rem;
88
89 if (*pidp)
90 goto found2;
Dave Barach2b836cf2016-04-22 09:54:22 -040091 }
92
93 clib_error ("VPP not runnning...");
94 return 3;
Dave Barach72d72232016-08-04 10:15:08 -040095
96found2:
Dave Barach2b836cf2016-04-22 09:54:22 -040097
98 old_pid = *pidp;
Dave Barach72d72232016-08-04 10:15:08 -040099
Dave Barach2b836cf2016-04-22 09:54:22 -0400100 /*
101 * Step 2: sanity check the pid we discovered
102 */
103 if (verbose)
Dave Barach72d72232016-08-04 10:15:08 -0400104 fformat (stdout, "Sanity check current vpp pid %d\n", old_pid);
Dave Barach2b836cf2016-04-22 09:54:22 -0400105
106 if (kill (old_pid, 0) < 0)
107 {
108 svmdb_unmap (svmdb_client);
109 clib_error ("vpp current pid %d not running...", old_pid);
110 return 2;
111 }
Dave Barach2b836cf2016-04-22 09:54:22 -0400112
Dave Barach72d72232016-08-04 10:15:08 -0400113 if (verbose)
114 fformat (stdout, "Sanity check vpp pid %d OK\n", old_pid);
115
116 /*
117 * Step 3: figure out the current vpp <--> client shared-VM file
118 * inode number
Dave Barach2b836cf2016-04-22 09:54:22 -0400119 */
Dave Barach72d72232016-08-04 10:15:08 -0400120 if (stat ("/dev/shm/vpe-api", &statb) < 0)
Dave Barach2b836cf2016-04-22 09:54:22 -0400121 {
122 clib_unix_error ("stat fail");
123 return 4;
124 }
125
126 old_inode = statb.st_ino;
127
128 if (verbose)
Dave Barach72d72232016-08-04 10:15:08 -0400129 fformat (stdout, "Old inode %u\n", old_inode);
Dave Barach2b836cf2016-04-22 09:54:22 -0400130
Dave Barach72d72232016-08-04 10:15:08 -0400131 /* Note: restart wipes out the shared VM database */
Dave Barach2b836cf2016-04-22 09:54:22 -0400132 svmdb_unmap (svmdb_client);
133
134 /*
135 * Step 4: send SIGTERM to vpp.
136 * systemd et al. will restart vpp after wiping out the shared-VM
137 * database and (crucially) the shared API messaging segment
138 */
139
140 if (kill (old_pid, SIGTERM) < 0)
141 {
142 clib_unix_error ("SIGTERM fail");
143 return 3;
144 }
145
146 sleeps = 0;
147
Dave Barach72d72232016-08-04 10:15:08 -0400148 /*
149 * Step 5: wait up to 15 seconds for a new incarnation of
Dave Barach2b836cf2016-04-22 09:54:22 -0400150 * the shared-VM API segment to appear.
151 */
152 for (wait = 0; wait < 150; wait++)
153 {
Dave Barach72d72232016-08-04 10:15:08 -0400154 if ((stat ("/dev/shm/vpe-api", &statb) < 0)
155 || statb.st_ino == old_inode)
156 {
157 req->tv_sec = 0;
158 req->tv_nsec = 100000 * 1000; /* 100 ms */
159 while (nanosleep (req, rem) < 0)
160 *req = *rem;
161 sleeps++;
162 }
Dave Barach2b836cf2016-04-22 09:54:22 -0400163 else
Dave Barach72d72232016-08-04 10:15:08 -0400164 goto new_inode;
Dave Barach2b836cf2016-04-22 09:54:22 -0400165 }
166
167 clib_error ("Timeout waiting for new inode to appear...");
168 return 5;
169
Dave Barach72d72232016-08-04 10:15:08 -0400170new_inode:
Dave Barach2b836cf2016-04-22 09:54:22 -0400171 if (verbose && sleeps > 0)
Dave Barach72d72232016-08-04 10:15:08 -0400172 fformat (stdout, "Inode sleeps %d\n", sleeps);
Dave Barach2b836cf2016-04-22 09:54:22 -0400173
174 if (verbose)
Dave Barach72d72232016-08-04 10:15:08 -0400175 fformat (stdout, "New inode %u\n", statb.st_ino);
Dave Barach2b836cf2016-04-22 09:54:22 -0400176
Dave Barach72d72232016-08-04 10:15:08 -0400177 /*
178 * Step 6: remap the SVM database
Dave Barach2b836cf2016-04-22 09:54:22 -0400179 */
Dave Barach98cfc1a2016-07-18 14:23:36 -0400180 svmdb_client = svmdb_map (ma);
Dave Barach72d72232016-08-04 10:15:08 -0400181
Dave Barach2b836cf2016-04-22 09:54:22 -0400182 pidp = svmdb_local_get_variable_reference (svmdb_client,
Dave Barach72d72232016-08-04 10:15:08 -0400183 SVMDB_NAMESPACE_VEC, "vpp_pid");
Dave Barach2b836cf2016-04-22 09:54:22 -0400184 if (pidp == 0)
185 {
186 clib_error ("post_restart: 'vpp_pid' svm variable not found,"
Dave Barach72d72232016-08-04 10:15:08 -0400187 "vpp did not restart?");
Dave Barach2b836cf2016-04-22 09:54:22 -0400188 return 2;
189 }
190
191 sleeps = 0;
192
Dave Barach72d72232016-08-04 10:15:08 -0400193 /*
Dave Barach2b836cf2016-04-22 09:54:22 -0400194 * Step 7: wait for vpp to publish its new PID
195 */
196
197 /* Spin for up to 15 seconds */
198 for (wait = 0; wait < 150; wait++)
199 {
200 if (*pidp && (*pidp != old_pid))
Dave Barach72d72232016-08-04 10:15:08 -0400201 goto restarted;
Dave Barach2b836cf2016-04-22 09:54:22 -0400202 req->tv_sec = 0;
Dave Barach72d72232016-08-04 10:15:08 -0400203 req->tv_nsec = 100000 * 1000; /* 100 ms */
204 while (nanosleep (req, rem) < 0)
205 *req = *rem;
Dave Barach2b836cf2016-04-22 09:54:22 -0400206 sleeps++;
207 }
Dave Barach72d72232016-08-04 10:15:08 -0400208
Dave Barach2b836cf2016-04-22 09:54:22 -0400209 clib_error ("Timeout waiting for vpp to publish pid after restart...");
210 return 4;
211
Dave Barach72d72232016-08-04 10:15:08 -0400212restarted:
Dave Barach2b836cf2016-04-22 09:54:22 -0400213
214 /* Done... */
Dave Barach72d72232016-08-04 10:15:08 -0400215
Dave Barach2b836cf2016-04-22 09:54:22 -0400216 if (verbose && sleeps)
Dave Barach72d72232016-08-04 10:15:08 -0400217 fformat (stdout, "pid sleeps %d\n", sleeps);
Dave Barach2b836cf2016-04-22 09:54:22 -0400218
219 if (verbose)
220 fformat (stdout, "New PID %d... Restarted...\n", *pidp);
Dave Barach72d72232016-08-04 10:15:08 -0400221
Dave Barach2b836cf2016-04-22 09:54:22 -0400222 svmdb_unmap (svmdb_client);
223 return 0;
224}
225
Dave Barach72d72232016-08-04 10:15:08 -0400226int
227main (int argc, char **argv)
Dave Barach2b836cf2016-04-22 09:54:22 -0400228{
229 unformat_input_t i;
230 int ret;
231
Dave Barach72d72232016-08-04 10:15:08 -0400232 clib_mem_init (0, 64ULL << 20);
Dave Barach2b836cf2016-04-22 09:54:22 -0400233
234 unformat_init_command_line (&i, argv);
235 ret = restart_main_fn (&i);
236 unformat_free (&i);
237 return ret;
238}
Dave Barach72d72232016-08-04 10:15:08 -0400239
240/*
241 * fd.io coding-style-patch-verification: ON
242 *
243 * Local Variables:
244 * eval: (c-set-style "gnu")
245 * End:
246 */