]>
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 | |
ca776988 | 128 | #ifdef HAVE_IRDP |
d62a17ae | 129 | irdp_finish(); |
ca776988 | 130 | #endif |
718e3744 | 131 | |
d62a17ae | 132 | zebra_ptm_finish(); |
133 | list_delete_all_node(zebrad.client_list); | |
134 | ||
135 | if (retain_mode) | |
136 | RB_FOREACH(vrf, vrf_name_head, &vrfs_by_name) | |
137 | { | |
138 | zvrf = vrf->info; | |
139 | if (zvrf) | |
140 | SET_FLAG(zvrf->flags, ZEBRA_VRF_RETAIN); | |
141 | } | |
142 | vrf_terminate(); | |
143 | ||
144 | zns = zebra_ns_lookup(NS_DEFAULT); | |
145 | zebra_ns_disable(0, (void **)&zns); | |
146 | ||
147 | access_list_reset(); | |
148 | prefix_list_reset(); | |
149 | route_map_finish(); | |
150 | cmd_terminate(); | |
151 | vty_terminate(); | |
152 | zprivs_terminate(&zserv_privs); | |
153 | list_delete(zebrad.client_list); | |
154 | work_queue_free(zebrad.ribq); | |
155 | if (zebrad.lsp_process_q) | |
156 | work_queue_free(zebrad.lsp_process_q); | |
157 | meta_queue_free(zebrad.mq); | |
158 | thread_master_free(zebrad.master); | |
159 | closezlog(); | |
160 | ||
161 | exit(0); | |
718e3744 | 162 | } |
163 | ||
164 | /* SIGUSR1 handler. */ | |
d62a17ae | 165 | static void sigusr1(void) |
718e3744 | 166 | { |
d62a17ae | 167 | zlog_rotate(); |
718e3744 | 168 | } |
169 | ||
d62a17ae | 170 | struct quagga_signal_t zebra_signals[] = { |
171 | { | |
172 | .signal = SIGHUP, | |
173 | .handler = &sighup, | |
174 | }, | |
175 | { | |
176 | .signal = SIGUSR1, | |
177 | .handler = &sigusr1, | |
178 | }, | |
179 | { | |
180 | .signal = SIGINT, | |
181 | .handler = &sigint, | |
182 | }, | |
183 | { | |
184 | .signal = SIGTERM, | |
185 | .handler = &sigint, | |
186 | }, | |
2d75d052 | 187 | }; |
b72ede27 | 188 | |
d62a17ae | 189 | FRR_DAEMON_INFO( |
190 | zebra, ZEBRA, .vty_port = ZEBRA_VTY_PORT, .flags = FRR_NO_ZCLIENT, | |
4f04a76b | 191 | |
d62a17ae | 192 | .proghelp = |
193 | "Daemon which manages kernel routing table management " | |
4f04a76b DL |
194 | "and\nredistribution between different routing protocols.", |
195 | ||
d62a17ae | 196 | .signals = zebra_signals, .n_signals = array_size(zebra_signals), |
4f04a76b | 197 | |
d62a17ae | 198 | .privs = &zserv_privs, ) |
4f04a76b | 199 | |
718e3744 | 200 | /* Main startup routine. */ |
d62a17ae | 201 | int main(int argc, char **argv) |
718e3744 | 202 | { |
d62a17ae | 203 | // int batch_mode = 0; |
204 | char *zserv_path = NULL; | |
205 | /* Socket to external label manager */ | |
206 | char *lblmgr_path = NULL; | |
fea12efb | 207 | |
d62a17ae | 208 | frr_preinit(&zebra_di, argc, argv); |
718e3744 | 209 | |
d62a17ae | 210 | frr_opt_add( |
211 | "bakz:e:l:r" | |
4f04a76b | 212 | #ifdef HAVE_NETLINK |
d62a17ae | 213 | "s:" |
c05795b1 | 214 | #endif |
d62a17ae | 215 | , |
216 | longopts, | |
217 | " -b, --batch Runs in batch mode\n" | |
218 | " -a, --allow_delete Allow other processes to delete zebra routes\n" | |
219 | " -z, --socket Set path of zebra socket\n" | |
220 | " -e, --ecmp Specify ECMP to use.\n" | |
221 | " -l, --label_socket Socket to external label manager\n" | |
222 | " -k, --keep_kernel Don't delete old routes which installed by zebra.\n" | |
223 | " -r, --retain When program terminates, retain added route by zebra.\n" | |
4f04a76b | 224 | #ifdef HAVE_NETLINK |
d62a17ae | 225 | " -s, --nl-bufsize Set netlink receive buffer size\n" |
4f04a76b | 226 | #endif /* HAVE_NETLINK */ |
9d303b37 | 227 | ); |
d62a17ae | 228 | |
229 | while (1) { | |
230 | int opt = frr_getopt(argc, argv, NULL); | |
231 | ||
232 | if (opt == EOF) | |
233 | break; | |
234 | ||
235 | switch (opt) { | |
236 | case 0: | |
237 | break; | |
238 | case 'b': | |
239 | // batch_mode = 1; | |
240 | break; | |
241 | case 'a': | |
242 | allow_delete = 1; | |
243 | break; | |
244 | case 'k': | |
245 | keep_kernel_mode = 1; | |
246 | break; | |
247 | case 'e': | |
248 | multipath_num = atoi(optarg); | |
249 | if (multipath_num > MULTIPATH_NUM | |
250 | || multipath_num <= 0) { | |
251 | zlog_err( | |
252 | "Multipath Number specified must be less than %d and greater than 0", | |
253 | MULTIPATH_NUM); | |
254 | return 1; | |
255 | } | |
256 | break; | |
257 | case 'z': | |
258 | zserv_path = optarg; | |
259 | break; | |
260 | case 'l': | |
261 | lblmgr_path = optarg; | |
262 | break; | |
263 | case 'r': | |
264 | retain_mode = 1; | |
265 | break; | |
c34b6b57 | 266 | #ifdef HAVE_NETLINK |
d62a17ae | 267 | case 's': |
268 | nl_rcvbufsize = atoi(optarg); | |
269 | break; | |
c34b6b57 | 270 | #endif /* HAVE_NETLINK */ |
d62a17ae | 271 | default: |
272 | frr_help_exit(1); | |
273 | break; | |
274 | } | |
718e3744 | 275 | } |
d62a17ae | 276 | |
277 | vty_config_lockless(); | |
278 | zebrad.master = frr_init(); | |
279 | ||
280 | /* Zebra related initialize. */ | |
281 | zebra_init(); | |
282 | rib_init(); | |
283 | zebra_if_init(); | |
284 | zebra_debug_init(); | |
285 | router_id_cmd_init(); | |
286 | zebra_vty_init(); | |
287 | access_list_init(); | |
288 | prefix_list_init(); | |
289 | #if defined(HAVE_RTADV) | |
290 | rtadv_cmd_init(); | |
36735ed9 | 291 | #endif |
ca776988 | 292 | #ifdef HAVE_IRDP |
d62a17ae | 293 | irdp_init(); |
ca776988 | 294 | #endif |
d62a17ae | 295 | /* PTM socket */ |
244c1cdc | 296 | #ifdef ZEBRA_PTM_SUPPORT |
d62a17ae | 297 | zebra_ptm_init(); |
244c1cdc | 298 | #endif |
718e3744 | 299 | |
d62a17ae | 300 | zebra_mpls_init(); |
301 | zebra_mpls_vty_init(); | |
2dd0d726 | 302 | zebra_pw_vty_init(); |
7758e3f3 | 303 | |
d62a17ae | 304 | /* For debug purpose. */ |
305 | /* SET_FLAG (zebra_debug_event, ZEBRA_DEBUG_EVENT); */ | |
718e3744 | 306 | |
d62a17ae | 307 | /* Initialize NS( and implicitly the VRF module), and make kernel |
308 | * routing socket. */ | |
309 | zebra_ns_init(); | |
718e3744 | 310 | |
d62a17ae | 311 | /* Process the configuration file. Among other configuration |
9d303b37 DL |
312 | * directives we can meet those installing static routes. Such |
313 | * requests will not be executed immediately, but queued in | |
314 | * zebra->ribq structure until we enter the main execution loop. | |
315 | * The notifications from kernel will show originating PID equal | |
316 | * to that after daemon() completes (if ever called). | |
317 | */ | |
d62a17ae | 318 | frr_config_fork(); |
718e3744 | 319 | |
d62a17ae | 320 | /* Clean up rib -- before fork (?) */ |
321 | /* rib_weed_tables (); */ | |
718e3744 | 322 | |
d62a17ae | 323 | /* After we have successfully acquired the pidfile, we can be sure |
9d303b37 DL |
324 | * about being the only copy of zebra process, which is submitting |
325 | * changes to the FIB. | |
326 | * Clean up zebra-originated routes. The requests will be sent to OS | |
327 | * immediately, so originating PID in notifications from kernel | |
328 | * will be equal to the current getpid(). To know about such routes, | |
329 | * we have to have route_read() called before. | |
330 | */ | |
d62a17ae | 331 | if (!keep_kernel_mode) |
332 | rib_sweep_route(); | |
91b7351d | 333 | |
d62a17ae | 334 | /* Needed for BSD routing socket. */ |
335 | pid = getpid(); | |
718e3744 | 336 | |
d62a17ae | 337 | /* This must be done only after locking pidfile (bug #403). */ |
338 | zebra_zserv_socket_init(zserv_path); | |
97be79f9 | 339 | |
d62a17ae | 340 | /* Init label manager */ |
341 | label_manager_init(lblmgr_path); | |
fea12efb | 342 | |
d62a17ae | 343 | frr_run(zebrad.master); |
718e3744 | 344 | |
d62a17ae | 345 | /* Not reached... */ |
346 | return 0; | |
718e3744 | 347 | } |