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;
43 svmdb_map_args_t _ma, *ma= &_ma;
45 struct timespec _req, *req = &_req;
46 struct timespec _rem, *rem = &_rem;
49 clib_error ("vpp_restart: must be root...");
51 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
53 if (unformat (i, "verbose") || unformat (i, "v"))
55 else if (unformat (i, "chroot %s", &chroot_path))
59 clib_error ("unknown input `%U'", format_unformat_error, i);
65 * Step 1: look up the current VPP pid in the shared-memory database
67 memset (ma, 0, sizeof (*ma));
68 ma->root_path = (char *)chroot_path;
70 svmdb_client = svmdb_map (ma);
72 pidp = svmdb_local_get_variable_reference (svmdb_client,
77 clib_error ("'vpp_pid' svm variable not found, vpp has never run?");
81 /* Spin for up to 10 seconds for vpp to start */
82 for (wait = 0; wait < 1000; wait++)
85 req->tv_nsec = 10000*1000; /* 10 ms */
86 while (nanosleep(req, rem) < 0)
93 clib_error ("VPP not runnning...");
101 * Step 2: sanity check the pid we discovered
104 fformat(stdout, "Sanity check current vpp pid %d\n", old_pid);
106 if (kill (old_pid, 0) < 0)
108 svmdb_unmap (svmdb_client);
109 clib_error ("vpp current pid %d not running...", old_pid);
114 fformat(stdout, "Sanity check vpp pid %d OK\n", old_pid);
117 * Step 3: figure out the current vpp <--> client shared-VM file
120 if (stat("/dev/shm/vpe-api", &statb) < 0)
122 clib_unix_error ("stat fail");
126 old_inode = statb.st_ino;
129 fformat(stdout, "Old inode %u\n", old_inode);
131 /* Note: restart wipes out the shared VM database*/
132 svmdb_unmap (svmdb_client);
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
140 if (kill (old_pid, SIGTERM) < 0)
142 clib_unix_error ("SIGTERM fail");
149 * Step 5: wait up to 15 seconds for a new incarnation of
150 * the shared-VM API segment to appear.
152 for (wait = 0; wait < 150; wait++)
154 if ((stat("/dev/shm/vpe-api", &statb) < 0)
155 || statb.st_ino == old_inode)
158 req->tv_nsec = 100000*1000; /* 100 ms */
159 while (nanosleep(req, rem) < 0)
167 clib_error ("Timeout waiting for new inode to appear...");
171 if (verbose && sleeps > 0)
172 fformat(stdout, "Inode sleeps %d\n", sleeps);
175 fformat(stdout, "New inode %u\n", statb.st_ino);
178 * Step 6: remap the SVM database
180 svmdb_client = svmdb_map (ma);
182 pidp = svmdb_local_get_variable_reference (svmdb_client,
187 clib_error ("post_restart: 'vpp_pid' svm variable not found,"
188 "vpp did not restart?");
195 * Step 7: wait for vpp to publish its new PID
198 /* Spin for up to 15 seconds */
199 for (wait = 0; wait < 150; wait++)
201 if (*pidp && (*pidp != old_pid))
204 req->tv_nsec = 100000*1000; /* 100 ms */
205 while (nanosleep(req, rem) < 0)
210 clib_error ("Timeout waiting for vpp to publish pid after restart...");
217 if (verbose && sleeps)
218 fformat(stdout, "pid sleeps %d\n", sleeps);
221 fformat (stdout, "New PID %d... Restarted...\n", *pidp);
223 svmdb_unmap (svmdb_client);
227 int main (int argc, char **argv)
232 clib_mem_init (0, 64ULL<<20);
234 unformat_init_command_line (&i, argv);
235 ret = restart_main_fn (&i);