Dave Barach | 2b836cf | 2016-04-22 09:54:22 -0400 | [diff] [blame] | 1 | /* |
| 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 Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 32 | int |
| 33 | restart_main_fn (unformat_input_t * i) |
Dave Barach | 2b836cf | 2016-04-22 09:54:22 -0400 | [diff] [blame] | 34 | { |
| 35 | int verbose = 0; |
| 36 | int old_pid; |
| 37 | int wait; |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 38 | u8 *chroot_path = 0; |
| 39 | svmdb_client_t *svmdb_client; |
Dave Barach | 2b836cf | 2016-04-22 09:54:22 -0400 | [diff] [blame] | 40 | volatile pid_t *pidp; |
| 41 | struct stat statb; |
| 42 | ino_t old_inode; |
| 43 | int sleeps; |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 44 | svmdb_map_args_t _ma, *ma = &_ma; |
Dave Barach | 2b836cf | 2016-04-22 09:54:22 -0400 | [diff] [blame] | 45 | |
| 46 | struct timespec _req, *req = &_req; |
| 47 | struct timespec _rem, *rem = &_rem; |
| 48 | |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 49 | if (geteuid ()) |
Dave Barach | 2b836cf | 2016-04-22 09:54:22 -0400 | [diff] [blame] | 50 | 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 Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 55 | verbose = 1; |
Dave Barach | 2b836cf | 2016-04-22 09:54:22 -0400 | [diff] [blame] | 56 | else if (unformat (i, "chroot %s", &chroot_path)) |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 57 | ; |
Dave Barach | 2b836cf | 2016-04-22 09:54:22 -0400 | [diff] [blame] | 58 | else |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 59 | { |
| 60 | clib_error ("unknown input `%U'", format_unformat_error, i); |
| 61 | return 1; |
| 62 | } |
Dave Barach | 2b836cf | 2016-04-22 09:54:22 -0400 | [diff] [blame] | 63 | } |
| 64 | |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 65 | /* |
| 66 | * Step 1: look up the current VPP pid in the shared-memory database |
Dave Barach | 2b836cf | 2016-04-22 09:54:22 -0400 | [diff] [blame] | 67 | */ |
Dave Barach | 98cfc1a | 2016-07-18 14:23:36 -0400 | [diff] [blame] | 68 | memset (ma, 0, sizeof (*ma)); |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 69 | ma->root_path = (char *) chroot_path; |
Dave Barach | 98cfc1a | 2016-07-18 14:23:36 -0400 | [diff] [blame] | 70 | |
| 71 | svmdb_client = svmdb_map (ma); |
Dave Barach | 2b836cf | 2016-04-22 09:54:22 -0400 | [diff] [blame] | 72 | |
| 73 | pidp = svmdb_local_get_variable_reference (svmdb_client, |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 74 | SVMDB_NAMESPACE_VEC, "vpp_pid"); |
Dave Barach | 2b836cf | 2016-04-22 09:54:22 -0400 | [diff] [blame] | 75 | 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 Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 85 | req->tv_nsec = 10000 * 1000; /* 10 ms */ |
| 86 | while (nanosleep (req, rem) < 0) |
| 87 | *req = *rem; |
| 88 | |
| 89 | if (*pidp) |
| 90 | goto found2; |
Dave Barach | 2b836cf | 2016-04-22 09:54:22 -0400 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | clib_error ("VPP not runnning..."); |
| 94 | return 3; |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 95 | |
| 96 | found2: |
Dave Barach | 2b836cf | 2016-04-22 09:54:22 -0400 | [diff] [blame] | 97 | |
| 98 | old_pid = *pidp; |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 99 | |
Dave Barach | 2b836cf | 2016-04-22 09:54:22 -0400 | [diff] [blame] | 100 | /* |
| 101 | * Step 2: sanity check the pid we discovered |
| 102 | */ |
| 103 | if (verbose) |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 104 | fformat (stdout, "Sanity check current vpp pid %d\n", old_pid); |
Dave Barach | 2b836cf | 2016-04-22 09:54:22 -0400 | [diff] [blame] | 105 | |
| 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 Barach | 2b836cf | 2016-04-22 09:54:22 -0400 | [diff] [blame] | 112 | |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 113 | 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 Barach | 2b836cf | 2016-04-22 09:54:22 -0400 | [diff] [blame] | 119 | */ |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 120 | if (stat ("/dev/shm/vpe-api", &statb) < 0) |
Dave Barach | 2b836cf | 2016-04-22 09:54:22 -0400 | [diff] [blame] | 121 | { |
| 122 | clib_unix_error ("stat fail"); |
| 123 | return 4; |
| 124 | } |
| 125 | |
| 126 | old_inode = statb.st_ino; |
| 127 | |
| 128 | if (verbose) |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 129 | fformat (stdout, "Old inode %u\n", old_inode); |
Dave Barach | 2b836cf | 2016-04-22 09:54:22 -0400 | [diff] [blame] | 130 | |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 131 | /* Note: restart wipes out the shared VM database */ |
Dave Barach | 2b836cf | 2016-04-22 09:54:22 -0400 | [diff] [blame] | 132 | 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 Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 148 | /* |
| 149 | * Step 5: wait up to 15 seconds for a new incarnation of |
Dave Barach | 2b836cf | 2016-04-22 09:54:22 -0400 | [diff] [blame] | 150 | * the shared-VM API segment to appear. |
| 151 | */ |
| 152 | for (wait = 0; wait < 150; wait++) |
| 153 | { |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 154 | 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 Barach | 2b836cf | 2016-04-22 09:54:22 -0400 | [diff] [blame] | 163 | else |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 164 | goto new_inode; |
Dave Barach | 2b836cf | 2016-04-22 09:54:22 -0400 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | clib_error ("Timeout waiting for new inode to appear..."); |
| 168 | return 5; |
| 169 | |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 170 | new_inode: |
Dave Barach | 2b836cf | 2016-04-22 09:54:22 -0400 | [diff] [blame] | 171 | if (verbose && sleeps > 0) |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 172 | fformat (stdout, "Inode sleeps %d\n", sleeps); |
Dave Barach | 2b836cf | 2016-04-22 09:54:22 -0400 | [diff] [blame] | 173 | |
| 174 | if (verbose) |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 175 | fformat (stdout, "New inode %u\n", statb.st_ino); |
Dave Barach | 2b836cf | 2016-04-22 09:54:22 -0400 | [diff] [blame] | 176 | |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 177 | /* |
| 178 | * Step 6: remap the SVM database |
Dave Barach | 2b836cf | 2016-04-22 09:54:22 -0400 | [diff] [blame] | 179 | */ |
Dave Barach | 98cfc1a | 2016-07-18 14:23:36 -0400 | [diff] [blame] | 180 | svmdb_client = svmdb_map (ma); |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 181 | |
Dave Barach | 2b836cf | 2016-04-22 09:54:22 -0400 | [diff] [blame] | 182 | pidp = svmdb_local_get_variable_reference (svmdb_client, |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 183 | SVMDB_NAMESPACE_VEC, "vpp_pid"); |
Dave Barach | 2b836cf | 2016-04-22 09:54:22 -0400 | [diff] [blame] | 184 | if (pidp == 0) |
| 185 | { |
| 186 | clib_error ("post_restart: 'vpp_pid' svm variable not found," |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 187 | "vpp did not restart?"); |
Dave Barach | 2b836cf | 2016-04-22 09:54:22 -0400 | [diff] [blame] | 188 | return 2; |
| 189 | } |
| 190 | |
| 191 | sleeps = 0; |
| 192 | |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 193 | /* |
Dave Barach | 2b836cf | 2016-04-22 09:54:22 -0400 | [diff] [blame] | 194 | * 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 Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 201 | goto restarted; |
Dave Barach | 2b836cf | 2016-04-22 09:54:22 -0400 | [diff] [blame] | 202 | req->tv_sec = 0; |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 203 | req->tv_nsec = 100000 * 1000; /* 100 ms */ |
| 204 | while (nanosleep (req, rem) < 0) |
| 205 | *req = *rem; |
Dave Barach | 2b836cf | 2016-04-22 09:54:22 -0400 | [diff] [blame] | 206 | sleeps++; |
| 207 | } |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 208 | |
Dave Barach | 2b836cf | 2016-04-22 09:54:22 -0400 | [diff] [blame] | 209 | clib_error ("Timeout waiting for vpp to publish pid after restart..."); |
| 210 | return 4; |
| 211 | |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 212 | restarted: |
Dave Barach | 2b836cf | 2016-04-22 09:54:22 -0400 | [diff] [blame] | 213 | |
| 214 | /* Done... */ |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 215 | |
Dave Barach | 2b836cf | 2016-04-22 09:54:22 -0400 | [diff] [blame] | 216 | if (verbose && sleeps) |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 217 | fformat (stdout, "pid sleeps %d\n", sleeps); |
Dave Barach | 2b836cf | 2016-04-22 09:54:22 -0400 | [diff] [blame] | 218 | |
| 219 | if (verbose) |
| 220 | fformat (stdout, "New PID %d... Restarted...\n", *pidp); |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 221 | |
Dave Barach | 2b836cf | 2016-04-22 09:54:22 -0400 | [diff] [blame] | 222 | svmdb_unmap (svmdb_client); |
| 223 | return 0; |
| 224 | } |
| 225 | |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 226 | int |
| 227 | main (int argc, char **argv) |
Dave Barach | 2b836cf | 2016-04-22 09:54:22 -0400 | [diff] [blame] | 228 | { |
| 229 | unformat_input_t i; |
| 230 | int ret; |
| 231 | |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 232 | clib_mem_init (0, 64ULL << 20); |
Dave Barach | 2b836cf | 2016-04-22 09:54:22 -0400 | [diff] [blame] | 233 | |
| 234 | unformat_init_command_line (&i, argv); |
| 235 | ret = restart_main_fn (&i); |
| 236 | unformat_free (&i); |
| 237 | return ret; |
| 238 | } |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 239 | |
| 240 | /* |
| 241 | * fd.io coding-style-patch-verification: ON |
| 242 | * |
| 243 | * Local Variables: |
| 244 | * eval: (c-set-style "gnu") |
| 245 | * End: |
| 246 | */ |