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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <sys/types.h>
23 #include <vppinfra/clib.h>
24 #include <vppinfra/vec.h>
25 #include <vppinfra/hash.h>
27 #include <vppinfra/format.h>
28 #include <vppinfra/error.h>
29 #include <vppinfra/time.h>
30 #include <vppinfra/macros.h>
32 int restart_main_fn (unformat_input_t * i)
38 svmdb_client_t * svmdb_client;
44 struct timespec _req, *req = &_req;
45 struct timespec _rem, *rem = &_rem;
48 clib_error ("vpp_restart: must be root...");
50 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
52 if (unformat (i, "verbose") || unformat (i, "v"))
54 else if (unformat (i, "chroot %s", &chroot_path))
58 clib_error ("unknown input `%U'", format_unformat_error, i);
64 * Step 1: look up the current VPP pid in the shared-memory database
66 svmdb_client = svmdb_map_chroot ((char *) chroot_path);
68 pidp = svmdb_local_get_variable_reference (svmdb_client,
73 clib_error ("'vpp_pid' svm variable not found, vpp has never run?");
77 /* Spin for up to 10 seconds for vpp to start */
78 for (wait = 0; wait < 1000; wait++)
81 req->tv_nsec = 10000*1000; /* 10 ms */
82 while (nanosleep(req, rem) < 0)
89 clib_error ("VPP not runnning...");
97 * Step 2: sanity check the pid we discovered
100 fformat(stdout, "Sanity check current vpp pid %d\n", old_pid);
102 if (kill (old_pid, 0) < 0)
104 svmdb_unmap (svmdb_client);
105 clib_error ("vpp current pid %d not running...", old_pid);
110 fformat(stdout, "Sanity check vpp pid %d OK\n", old_pid);
113 * Step 3: figure out the current vpp <--> client shared-VM file
116 if (stat("/dev/shm/vpe-api", &statb) < 0)
118 clib_unix_error ("stat fail");
122 old_inode = statb.st_ino;
125 fformat(stdout, "Old inode %u\n", old_inode);
127 /* Note: restart wipes out the shared VM database*/
128 svmdb_unmap (svmdb_client);
131 * Step 4: send SIGTERM to vpp.
132 * systemd et al. will restart vpp after wiping out the shared-VM
133 * database and (crucially) the shared API messaging segment
136 if (kill (old_pid, SIGTERM) < 0)
138 clib_unix_error ("SIGTERM fail");
145 * Step 5: wait up to 15 seconds for a new incarnation of
146 * the shared-VM API segment to appear.
148 for (wait = 0; wait < 150; wait++)
150 if ((stat("/dev/shm/vpe-api", &statb) < 0)
151 || statb.st_ino == old_inode)
154 req->tv_nsec = 100000*1000; /* 100 ms */
155 while (nanosleep(req, rem) < 0)
163 clib_error ("Timeout waiting for new inode to appear...");
167 if (verbose && sleeps > 0)
168 fformat(stdout, "Inode sleeps %d\n", sleeps);
171 fformat(stdout, "New inode %u\n", statb.st_ino);
174 * Step 6: remap the SVM database
176 svmdb_client = svmdb_map_chroot ((char *) chroot_path);
178 pidp = svmdb_local_get_variable_reference (svmdb_client,
183 clib_error ("post_restart: 'vpp_pid' svm variable not found,"
184 "vpp did not restart?");
191 * Step 7: wait for vpp to publish its new PID
194 /* Spin for up to 15 seconds */
195 for (wait = 0; wait < 150; wait++)
197 if (*pidp && (*pidp != old_pid))
200 req->tv_nsec = 100000*1000; /* 100 ms */
201 while (nanosleep(req, rem) < 0)
206 clib_error ("Timeout waiting for vpp to publish pid after restart...");
213 if (verbose && sleeps)
214 fformat(stdout, "pid sleeps %d\n", sleeps);
217 fformat (stdout, "New PID %d... Restarted...\n", *pidp);
219 svmdb_unmap (svmdb_client);
223 int main (int argc, char **argv)
228 clib_mem_init (0, 64ULL<<20);
230 unformat_init_command_line (&i, argv);
231 ret = restart_main_fn (&i);