]>
Commit | Line | Data |
---|---|---|
edd7c245 | 1 | /* zebra daemon main routine. |
718e3744 | 2 | * Copyright (C) 1997, 98 Kunihiro Ishiguro |
3 | * | |
4 | * This file is part of GNU Zebra. | |
5 | * | |
6 | * GNU Zebra is free software; you can redistribute it and/or modify it | |
7 | * under the terms of the GNU General Public License as published by the | |
8 | * Free Software Foundation; either version 2, or (at your option) any | |
9 | * later version. | |
10 | * | |
11 | * GNU Zebra is distributed in the hope that it will be useful, but | |
12 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | * General Public License for more details. | |
15 | * | |
896014f4 DL |
16 | * You should have received a copy of the GNU General Public License along |
17 | * with this program; see the file COPYING; if not, write to the Free Software | |
18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
718e3744 | 19 | */ |
20 | ||
21 | #include <zebra.h> | |
22 | ||
5e4fa164 | 23 | #include <lib/version.h> |
718e3744 | 24 | #include "getopt.h" |
25 | #include "command.h" | |
26 | #include "thread.h" | |
27 | #include "filter.h" | |
28 | #include "memory.h" | |
4a1ab8e4 | 29 | #include "zebra_memory.h" |
fc7948fa | 30 | #include "memory_vty.h" |
718e3744 | 31 | #include "prefix.h" |
32 | #include "log.h" | |
7514fb77 | 33 | #include "plist.h" |
edd7c245 | 34 | #include "privs.h" |
2d75d052 | 35 | #include "sigevent.h" |
b72ede27 | 36 | #include "vrf.h" |
4f04a76b | 37 | #include "libfrr.h" |
718e3744 | 38 | |
39 | #include "zebra/rib.h" | |
40 | #include "zebra/zserv.h" | |
41 | #include "zebra/debug.h" | |
18a6dce6 | 42 | #include "zebra/router-id.h" |
ca776988 | 43 | #include "zebra/irdp.h" |
a1ac18c4 | 44 | #include "zebra/rtadv.h" |
244c1cdc | 45 | #include "zebra/zebra_ptm.h" |
fe18ee2d | 46 | #include "zebra/zebra_ns.h" |
e2b1be64 | 47 | #include "zebra/redistribute.h" |
7758e3f3 | 48 | #include "zebra/zebra_mpls.h" |
fea12efb | 49 | #include "zebra/label_manager.h" |
244c1cdc DS |
50 | |
51 | #define ZEBRA_PTM_SUPPORT | |
718e3744 | 52 | |
b21b19c5 | 53 | /* Zebra instance */ |
d62a17ae | 54 | struct zebra_t zebrad = { |
55 | .rtm_table_default = 0, | |
b21b19c5 | 56 | }; |
718e3744 | 57 | |
58 | /* process id. */ | |
718e3744 | 59 | pid_t pid; |
60 | ||
55c72803 | 61 | /* Pacify zclient.o in libfrr, which expects this variable. */ |
87efd646 | 62 | struct thread_master *master; |
63 | ||
718e3744 | 64 | /* Route retain mode flag. */ |
65 | int retain_mode = 0; | |
66 | ||
6baf7bb8 DS |
67 | /* Allow non-quagga entities to delete quagga routes */ |
68 | int allow_delete = 0; | |
69 | ||
718e3744 | 70 | /* Don't delete kernel route. */ |
71 | int keep_kernel_mode = 0; | |
72 | ||
c34b6b57 | 73 | #ifdef HAVE_NETLINK |
74 | /* Receive buffer size for netlink socket */ | |
b6286c70 | 75 | u_int32_t nl_rcvbufsize = 4194304; |
c34b6b57 | 76 | #endif /* HAVE_NETLINK */ |
77 | ||
718e3744 | 78 | /* Command line options. */ |
d62a17ae | 79 | struct option longopts[] = {{"batch", no_argument, NULL, 'b'}, |
80 | {"allow_delete", no_argument, NULL, 'a'}, | |
81 | {"keep_kernel", no_argument, NULL, 'k'}, | |
82 | {"socket", required_argument, NULL, 'z'}, | |
83 | {"ecmp", required_argument, NULL, 'e'}, | |
84 | {"label_socket", no_argument, NULL, 'l'}, | |
85 | {"retain", no_argument, NULL, 'r'}, | |
c34b6b57 | 86 | #ifdef HAVE_NETLINK |
d62a17ae | 87 | {"nl-bufsize", required_argument, NULL, 's'}, |
c34b6b57 | 88 | #endif /* HAVE_NETLINK */ |
d62a17ae | 89 | {0}}; |
718e3744 | 90 | |
d62a17ae | 91 | zebra_capabilities_t _caps_p[] = { |
9d303b37 | 92 | ZCAP_NET_ADMIN, ZCAP_SYS_ADMIN, ZCAP_NET_RAW, |
edd7c245 | 93 | }; |
94 | ||
95 | /* zebra privileges to run with */ | |
d62a17ae | 96 | struct zebra_privs_t zserv_privs = { |
b2f36157 | 97 | #if defined(FRR_USER) && defined(FRR_GROUP) |
d62a17ae | 98 | .user = FRR_USER, |
99 | .group = FRR_GROUP, | |
edd7c245 | 100 | #endif |
101 | #ifdef VTY_GROUP | |
d62a17ae | 102 | .vty_group = VTY_GROUP, |
edd7c245 | 103 | #endif |
d62a17ae | 104 | .caps_p = _caps_p, |
105 | .cap_num_p = array_size(_caps_p), | |
106 | .cap_num_i = 0}; | |
edd7c245 | 107 | |
37fe7731 DS |
108 | unsigned int multipath_num = MULTIPATH_NUM; |
109 | ||
718e3744 | 110 | /* SIGHUP handler. */ |
d62a17ae | 111 | static void sighup(void) |
718e3744 | 112 | { |
d62a17ae | 113 | zlog_info("SIGHUP received"); |
718e3744 | 114 | |
d62a17ae | 115 | /* Reload of config file. */ |
116 | ; | |
718e3744 | 117 | } |
118 | ||
119 | /* SIGINT handler. */ | |
d62a17ae | 120 | static void sigint(void) |
718e3744 | 121 | { |
d62a17ae | 122 | struct vrf *vrf; |
123 | struct zebra_vrf *zvrf; | |
124 | struct zebra_ns *zns; | |
fe18ee2d | 125 | |
d62a17ae | 126 | zlog_notice("Terminating on signal"); |
718e3744 | 127 | |
03951374 | 128 | frr_early_fini(); |
718e3744 | 129 | |
d62a17ae | 130 | list_delete_all_node(zebrad.client_list); |
f88bd20c | 131 | zebra_ptm_finish(); |
d62a17ae | 132 | |
133 | if (retain_mode) | |
a2addae8 | 134 | RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name) { |
d62a17ae | 135 | zvrf = vrf->info; |
136 | if (zvrf) | |
137 | SET_FLAG(zvrf->flags, ZEBRA_VRF_RETAIN); | |
138 | } | |
139 | vrf_terminate(); | |
140 | ||
141 | zns = zebra_ns_lookup(NS_DEFAULT); | |
142 | zebra_ns_disable(0, (void **)&zns); | |
143 | ||
144 | access_list_reset(); | |
145 | prefix_list_reset(); | |
146 | route_map_finish(); | |
03951374 | 147 | |
affe9e99 | 148 | list_delete_and_null(&zebrad.client_list); |
d62a17ae | 149 | work_queue_free(zebrad.ribq); |
150 | if (zebrad.lsp_process_q) | |
151 | work_queue_free(zebrad.lsp_process_q); | |
152 | meta_queue_free(zebrad.mq); | |
d62a17ae | 153 | |
03951374 | 154 | frr_fini(); |
d62a17ae | 155 | exit(0); |
718e3744 | 156 | } |
157 | ||
158 | /* SIGUSR1 handler. */ | |
d62a17ae | 159 | static void sigusr1(void) |
718e3744 | 160 | { |
d62a17ae | 161 | zlog_rotate(); |
718e3744 | 162 | } |
163 | ||
d62a17ae | 164 | struct quagga_signal_t zebra_signals[] = { |
165 | { | |
166 | .signal = SIGHUP, | |
167 | .handler = &sighup, | |
168 | }, | |
169 | { | |
170 | .signal = SIGUSR1, | |
171 | .handler = &sigusr1, | |
172 | }, | |
173 | { | |
174 | .signal = SIGINT, | |
175 | .handler = &sigint, | |
176 | }, | |
177 | { | |
178 | .signal = SIGTERM, | |
179 | .handler = &sigint, | |
180 | }, | |
2d75d052 | 181 | }; |
b72ede27 | 182 | |
d62a17ae | 183 | FRR_DAEMON_INFO( |
184 | zebra, ZEBRA, .vty_port = ZEBRA_VTY_PORT, .flags = FRR_NO_ZCLIENT, | |
4f04a76b | 185 | |
d62a17ae | 186 | .proghelp = |
187 | "Daemon which manages kernel routing table management " | |
4f04a76b DL |
188 | "and\nredistribution between different routing protocols.", |
189 | ||
d62a17ae | 190 | .signals = zebra_signals, .n_signals = array_size(zebra_signals), |
4f04a76b | 191 | |
d62a17ae | 192 | .privs = &zserv_privs, ) |
4f04a76b | 193 | |
718e3744 | 194 | /* Main startup routine. */ |
d62a17ae | 195 | int main(int argc, char **argv) |
718e3744 | 196 | { |
d62a17ae | 197 | // int batch_mode = 0; |
198 | char *zserv_path = NULL; | |
199 | /* Socket to external label manager */ | |
200 | char *lblmgr_path = NULL; | |
689f5a8c DL |
201 | struct sockaddr_storage dummy; |
202 | socklen_t dummylen; | |
fea12efb | 203 | |
d62a17ae | 204 | frr_preinit(&zebra_di, argc, argv); |
718e3744 | 205 | |
d62a17ae | 206 | frr_opt_add( |
207 | "bakz:e:l:r" | |
4f04a76b | 208 | #ifdef HAVE_NETLINK |
d62a17ae | 209 | "s:" |
c05795b1 | 210 | #endif |
d62a17ae | 211 | , |
212 | longopts, | |
213 | " -b, --batch Runs in batch mode\n" | |
214 | " -a, --allow_delete Allow other processes to delete zebra routes\n" | |
215 | " -z, --socket Set path of zebra socket\n" | |
216 | " -e, --ecmp Specify ECMP to use.\n" | |
217 | " -l, --label_socket Socket to external label manager\n" | |
218 | " -k, --keep_kernel Don't delete old routes which installed by zebra.\n" | |
219 | " -r, --retain When program terminates, retain added route by zebra.\n" | |
4f04a76b | 220 | #ifdef HAVE_NETLINK |
d62a17ae | 221 | " -s, --nl-bufsize Set netlink receive buffer size\n" |
4f04a76b | 222 | #endif /* HAVE_NETLINK */ |
9d303b37 | 223 | ); |
d62a17ae | 224 | |
225 | while (1) { | |
226 | int opt = frr_getopt(argc, argv, NULL); | |
227 | ||
228 | if (opt == EOF) | |
229 | break; | |
230 | ||
231 | switch (opt) { | |
232 | case 0: | |
233 | break; | |
234 | case 'b': | |
235 | // batch_mode = 1; | |
236 | break; | |
237 | case 'a': | |
238 | allow_delete = 1; | |
239 | break; | |
240 | case 'k': | |
241 | keep_kernel_mode = 1; | |
242 | break; | |
243 | case 'e': | |
244 | multipath_num = atoi(optarg); | |
245 | if (multipath_num > MULTIPATH_NUM | |
246 | || multipath_num <= 0) { | |
247 | zlog_err( | |
248 | "Multipath Number specified must be less than %d and greater than 0", | |
249 | MULTIPATH_NUM); | |
250 | return 1; | |
251 | } | |
252 | break; | |
253 | case 'z': | |
254 | zserv_path = optarg; | |
689f5a8c DL |
255 | if (!frr_zclient_addr(&dummy, &dummylen, optarg)) { |
256 | fprintf(stderr, | |
257 | "Invalid zserv socket path: %s\n", | |
258 | optarg); | |
259 | exit(1); | |
260 | } | |
d62a17ae | 261 | break; |
262 | case 'l': | |
263 | lblmgr_path = optarg; | |
264 | break; | |
265 | case 'r': | |
266 | retain_mode = 1; | |
267 | break; | |
c34b6b57 | 268 | #ifdef HAVE_NETLINK |
d62a17ae | 269 | case 's': |
270 | nl_rcvbufsize = atoi(optarg); | |
271 | break; | |
c34b6b57 | 272 | #endif /* HAVE_NETLINK */ |
d62a17ae | 273 | default: |
274 | frr_help_exit(1); | |
275 | break; | |
276 | } | |
718e3744 | 277 | } |
d62a17ae | 278 | |
279 | vty_config_lockless(); | |
280 | zebrad.master = frr_init(); | |
281 | ||
282 | /* Zebra related initialize. */ | |
283 | zebra_init(); | |
284 | rib_init(); | |
285 | zebra_if_init(); | |
286 | zebra_debug_init(); | |
287 | router_id_cmd_init(); | |
288 | zebra_vty_init(); | |
289 | access_list_init(); | |
290 | prefix_list_init(); | |
291 | #if defined(HAVE_RTADV) | |
292 | rtadv_cmd_init(); | |
36735ed9 | 293 | #endif |
d62a17ae | 294 | /* PTM socket */ |
244c1cdc | 295 | #ifdef ZEBRA_PTM_SUPPORT |
d62a17ae | 296 | zebra_ptm_init(); |
244c1cdc | 297 | #endif |
718e3744 | 298 | |
d62a17ae | 299 | zebra_mpls_init(); |
300 | zebra_mpls_vty_init(); | |
2dd0d726 | 301 | zebra_pw_vty_init(); |
7758e3f3 | 302 | |
d62a17ae | 303 | /* For debug purpose. */ |
304 | /* SET_FLAG (zebra_debug_event, ZEBRA_DEBUG_EVENT); */ | |
718e3744 | 305 | |
d62a17ae | 306 | /* Initialize NS( and implicitly the VRF module), and make kernel |
307 | * routing socket. */ | |
308 | zebra_ns_init(); | |
718e3744 | 309 | |
d62a17ae | 310 | /* Process the configuration file. Among other configuration |
9d303b37 DL |
311 | * directives we can meet those installing static routes. Such |
312 | * requests will not be executed immediately, but queued in | |
313 | * zebra->ribq structure until we enter the main execution loop. | |
314 | * The notifications from kernel will show originating PID equal | |
315 | * to that after daemon() completes (if ever called). | |
316 | */ | |
d62a17ae | 317 | frr_config_fork(); |
718e3744 | 318 | |
d62a17ae | 319 | /* Clean up rib -- before fork (?) */ |
320 | /* rib_weed_tables (); */ | |
718e3744 | 321 | |
d62a17ae | 322 | /* After we have successfully acquired the pidfile, we can be sure |
9d303b37 DL |
323 | * about being the only copy of zebra process, which is submitting |
324 | * changes to the FIB. | |
325 | * Clean up zebra-originated routes. The requests will be sent to OS | |
326 | * immediately, so originating PID in notifications from kernel | |
327 | * will be equal to the current getpid(). To know about such routes, | |
328 | * we have to have route_read() called before. | |
329 | */ | |
d62a17ae | 330 | if (!keep_kernel_mode) |
331 | rib_sweep_route(); | |
91b7351d | 332 | |
d62a17ae | 333 | /* Needed for BSD routing socket. */ |
334 | pid = getpid(); | |
718e3744 | 335 | |
d62a17ae | 336 | /* This must be done only after locking pidfile (bug #403). */ |
337 | zebra_zserv_socket_init(zserv_path); | |
97be79f9 | 338 | |
d62a17ae | 339 | /* Init label manager */ |
340 | label_manager_init(lblmgr_path); | |
fea12efb | 341 | |
d62a17ae | 342 | frr_run(zebrad.master); |
718e3744 | 343 | |
d62a17ae | 344 | /* Not reached... */ |
345 | return 0; | |
718e3744 | 346 | } |