]> git.proxmox.com Git - mirror_frr.git/blob - zebra/main.c
Merge pull request #4497 from opensourcerouting/rcu
[mirror_frr.git] / zebra / main.c
1 /* zebra daemon main routine.
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 *
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
19 */
20
21 #include <zebra.h>
22
23 #include <lib/version.h>
24 #include "getopt.h"
25 #include "command.h"
26 #include "thread.h"
27 #include "filter.h"
28 #include "memory.h"
29 #include "zebra_memory.h"
30 #include "memory_vty.h"
31 #include "prefix.h"
32 #include "log.h"
33 #include "plist.h"
34 #include "privs.h"
35 #include "sigevent.h"
36 #include "vrf.h"
37 #include "libfrr.h"
38 #include "routemap.h"
39 #include "frr_pthread.h"
40
41 #include "zebra/zebra_router.h"
42 #include "zebra/zebra_errors.h"
43 #include "zebra/rib.h"
44 #include "zebra/zserv.h"
45 #include "zebra/debug.h"
46 #include "zebra/router-id.h"
47 #include "zebra/irdp.h"
48 #include "zebra/rtadv.h"
49 #include "zebra/zebra_ptm.h"
50 #include "zebra/zebra_ns.h"
51 #include "zebra/redistribute.h"
52 #include "zebra/zebra_mpls.h"
53 #include "zebra/label_manager.h"
54 #include "zebra/zebra_netns_notify.h"
55 #include "zebra/zebra_rnh.h"
56 #include "zebra/zebra_pbr.h"
57 #include "zebra/zebra_vxlan.h"
58
59 #if defined(HANDLE_NETLINK_FUZZING)
60 #include "zebra/kernel_netlink.h"
61 #endif /* HANDLE_NETLINK_FUZZING */
62
63 #define ZEBRA_PTM_SUPPORT
64
65 /* process id. */
66 pid_t pid;
67
68 /* Pacify zclient.o in libfrr, which expects this variable. */
69 struct thread_master *master;
70
71 /* Route retain mode flag. */
72 int retain_mode = 0;
73
74 /* Allow non-quagga entities to delete quagga routes */
75 int allow_delete = 0;
76
77 int graceful_restart;
78
79 bool v6_rr_semantics = false;
80
81 #ifdef HAVE_NETLINK
82 /* Receive buffer size for netlink socket */
83 uint32_t nl_rcvbufsize = 4194304;
84 #endif /* HAVE_NETLINK */
85
86 #define OPTION_V6_RR_SEMANTICS 2000
87 /* Command line options. */
88 struct option longopts[] = {
89 {"batch", no_argument, NULL, 'b'},
90 {"allow_delete", no_argument, NULL, 'a'},
91 {"keep_kernel", no_argument, NULL, 'k'},
92 {"socket", required_argument, NULL, 'z'},
93 {"ecmp", required_argument, NULL, 'e'},
94 {"retain", no_argument, NULL, 'r'},
95 {"vrfdefaultname", required_argument, NULL, 'o'},
96 {"graceful_restart", required_argument, NULL, 'K'},
97 #ifdef HAVE_NETLINK
98 {"vrfwnetns", no_argument, NULL, 'n'},
99 {"nl-bufsize", required_argument, NULL, 's'},
100 {"v6-rr-semantics", no_argument, NULL, OPTION_V6_RR_SEMANTICS},
101 #endif /* HAVE_NETLINK */
102 {0}};
103
104 zebra_capabilities_t _caps_p[] = {
105 ZCAP_NET_ADMIN, ZCAP_SYS_ADMIN, ZCAP_NET_RAW,
106 };
107
108 /* zebra privileges to run with */
109 struct zebra_privs_t zserv_privs = {
110 #if defined(FRR_USER) && defined(FRR_GROUP)
111 .user = FRR_USER,
112 .group = FRR_GROUP,
113 #endif
114 #ifdef VTY_GROUP
115 .vty_group = VTY_GROUP,
116 #endif
117 .caps_p = _caps_p,
118 .cap_num_p = array_size(_caps_p),
119 .cap_num_i = 0};
120
121 /* SIGHUP handler. */
122 static void sighup(void)
123 {
124 zlog_info("SIGHUP received");
125
126 /* Reload of config file. */
127 ;
128 }
129
130 /* SIGINT handler. */
131 static void sigint(void)
132 {
133 struct vrf *vrf;
134 struct zebra_vrf *zvrf;
135 struct listnode *ln, *nn;
136 struct zserv *client;
137 static bool sigint_done;
138
139 if (sigint_done)
140 return;
141
142 sigint_done = true;
143
144 zlog_notice("Terminating on signal");
145
146 frr_early_fini();
147
148 zebra_dplane_pre_finish();
149
150 for (ALL_LIST_ELEMENTS(zrouter.client_list, ln, nn, client))
151 zserv_close_client(client);
152
153 zserv_close();
154 list_delete_all_node(zrouter.client_list);
155
156 zebra_ptm_finish();
157
158 if (retain_mode)
159 RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name) {
160 zvrf = vrf->info;
161 if (zvrf)
162 SET_FLAG(zvrf->flags, ZEBRA_VRF_RETAIN);
163 }
164 if (zrouter.lsp_process_q)
165 work_queue_free_and_null(&zrouter.lsp_process_q);
166 vrf_terminate();
167
168 ns_walk_func(zebra_ns_early_shutdown);
169 zebra_ns_notify_close();
170
171 access_list_reset();
172 prefix_list_reset();
173 route_map_finish();
174
175 list_delete(&zrouter.client_list);
176
177 /* Indicate that all new dplane work has been enqueued. When that
178 * work is complete, the dataplane will enqueue an event
179 * with the 'finalize' function.
180 */
181 zebra_dplane_finish();
182 }
183
184 /*
185 * Final shutdown step for the zebra main thread. This is run after all
186 * async update processing has completed.
187 */
188 int zebra_finalize(struct thread *dummy)
189 {
190 zlog_info("Zebra final shutdown");
191
192 /* Final shutdown of ns resources */
193 ns_walk_func(zebra_ns_final_shutdown);
194
195 /* Stop dplane thread and finish any cleanup */
196 zebra_dplane_shutdown();
197
198 zebra_router_terminate();
199
200 frr_fini();
201 exit(0);
202 }
203
204 /* SIGUSR1 handler. */
205 static void sigusr1(void)
206 {
207 zlog_rotate();
208 }
209
210 struct quagga_signal_t zebra_signals[] = {
211 {
212 .signal = SIGHUP,
213 .handler = &sighup,
214 },
215 {
216 .signal = SIGUSR1,
217 .handler = &sigusr1,
218 },
219 {
220 .signal = SIGINT,
221 .handler = &sigint,
222 },
223 {
224 .signal = SIGTERM,
225 .handler = &sigint,
226 },
227 };
228
229 static const struct frr_yang_module_info *zebra_yang_modules[] = {
230 &frr_interface_info,
231 };
232
233 FRR_DAEMON_INFO(
234 zebra, ZEBRA, .vty_port = ZEBRA_VTY_PORT, .flags = FRR_NO_ZCLIENT,
235
236 .proghelp =
237 "Daemon which manages kernel routing table management "
238 "and\nredistribution between different routing protocols.",
239
240 .signals = zebra_signals, .n_signals = array_size(zebra_signals),
241
242 .privs = &zserv_privs,
243
244 .yang_modules = zebra_yang_modules,
245 .n_yang_modules = array_size(zebra_yang_modules), )
246
247 /* Main startup routine. */
248 int main(int argc, char **argv)
249 {
250 // int batch_mode = 0;
251 char *zserv_path = NULL;
252 char *vrf_default_name_configured = NULL;
253 struct sockaddr_storage dummy;
254 socklen_t dummylen;
255 #if defined(HANDLE_ZAPI_FUZZING)
256 char *zapi_fuzzing = NULL;
257 #endif /* HANDLE_ZAPI_FUZZING */
258 #if defined(HANDLE_NETLINK_FUZZING)
259 char *netlink_fuzzing = NULL;
260 #endif /* HANDLE_NETLINK_FUZZING */
261
262 graceful_restart = 0;
263 vrf_configure_backend(VRF_BACKEND_VRF_LITE);
264
265 frr_preinit(&zebra_di, argc, argv);
266
267 frr_opt_add(
268 "baz:e:o:rK:"
269 #ifdef HAVE_NETLINK
270 "s:n"
271 #endif
272 #if defined(HANDLE_ZAPI_FUZZING)
273 "c:"
274 #endif /* HANDLE_ZAPI_FUZZING */
275 #if defined(HANDLE_NETLINK_FUZZING)
276 "w:"
277 #endif /* HANDLE_NETLINK_FUZZING */
278 ,
279 longopts,
280 " -b, --batch Runs in batch mode\n"
281 " -a, --allow_delete Allow other processes to delete zebra routes\n"
282 " -z, --socket Set path of zebra socket\n"
283 " -e, --ecmp Specify ECMP to use.\n"
284 " -r, --retain When program terminates, retain added route by zebra.\n"
285 " -o, --vrfdefaultname Set default VRF name.\n"
286 " -K, --graceful_restart Graceful restart at the kernel level, timer in seconds for expiration\n"
287 #ifdef HAVE_NETLINK
288 " -n, --vrfwnetns Use NetNS as VRF backend\n"
289 " -s, --nl-bufsize Set netlink receive buffer size\n"
290 " --v6-rr-semantics Use v6 RR semantics\n"
291 #endif /* HAVE_NETLINK */
292 #if defined(HANDLE_ZAPI_FUZZING)
293 " -c <file> Bypass normal startup and use this file for testing of zapi\n"
294 #endif /* HANDLE_ZAPI_FUZZING */
295 #if defined(HANDLE_NETLINK_FUZZING)
296 " -w <file> Bypass normal startup and use this file for testing of netlink input\n"
297 #endif /* HANDLE_NETLINK_FUZZING */
298 );
299
300 while (1) {
301 int opt = frr_getopt(argc, argv, NULL);
302
303 if (opt == EOF)
304 break;
305
306 switch (opt) {
307 case 0:
308 break;
309 case 'b':
310 // batch_mode = 1;
311 break;
312 case 'a':
313 allow_delete = 1;
314 break;
315 case 'e':
316 zrouter.multipath_num = atoi(optarg);
317 if (zrouter.multipath_num > MULTIPATH_NUM
318 || zrouter.multipath_num <= 0) {
319 flog_err(
320 EC_ZEBRA_BAD_MULTIPATH_NUM,
321 "Multipath Number specified must be less than %d and greater than 0",
322 MULTIPATH_NUM);
323 return 1;
324 }
325 break;
326 case 'o':
327 vrf_default_name_configured = optarg;
328 break;
329 case 'z':
330 zserv_path = optarg;
331 if (!frr_zclient_addr(&dummy, &dummylen, optarg)) {
332 fprintf(stderr,
333 "Invalid zserv socket path: %s\n",
334 optarg);
335 exit(1);
336 }
337 break;
338 case 'r':
339 retain_mode = 1;
340 break;
341 case 'K':
342 graceful_restart = atoi(optarg);
343 break;
344 #ifdef HAVE_NETLINK
345 case 's':
346 nl_rcvbufsize = atoi(optarg);
347 break;
348 case 'n':
349 vrf_configure_backend(VRF_BACKEND_NETNS);
350 break;
351 case OPTION_V6_RR_SEMANTICS:
352 v6_rr_semantics = true;
353 break;
354 #endif /* HAVE_NETLINK */
355 #if defined(HANDLE_ZAPI_FUZZING)
356 case 'c':
357 zapi_fuzzing = optarg;
358 break;
359 #endif /* HANDLE_ZAPI_FUZZING */
360 #if defined(HANDLE_NETLINK_FUZZING)
361 case 'w':
362 netlink_fuzzing = optarg;
363 /* This ensures we are aren't writing any of the
364 * startup netlink messages that happen when we
365 * just want to read.
366 */
367 netlink_read = true;
368 break;
369 #endif /* HANDLE_NETLINK_FUZZING */
370 default:
371 frr_help_exit(1);
372 break;
373 }
374 }
375
376 zrouter.master = frr_init();
377
378 /* Initialize pthread library */
379 frr_pthread_init();
380
381 /* Zebra related initialize. */
382 zebra_router_init();
383 zserv_init();
384 rib_init();
385 zebra_if_init();
386 zebra_debug_init();
387 router_id_cmd_init();
388
389 /*
390 * Initialize NS( and implicitly the VRF module), and make kernel
391 * routing socket. */
392 zebra_ns_init((const char *)vrf_default_name_configured);
393 zebra_vty_init();
394 access_list_init();
395 prefix_list_init();
396 #if defined(HAVE_RTADV)
397 rtadv_cmd_init();
398 #endif
399 /* PTM socket */
400 #ifdef ZEBRA_PTM_SUPPORT
401 zebra_ptm_init();
402 #endif
403
404 zebra_mpls_init();
405 zebra_mpls_vty_init();
406 zebra_pw_vty_init();
407 zebra_pbr_init();
408
409 /* For debug purpose. */
410 /* SET_FLAG (zebra_debug_event, ZEBRA_DEBUG_EVENT); */
411
412 /* Process the configuration file. Among other configuration
413 * directives we can meet those installing static routes. Such
414 * requests will not be executed immediately, but queued in
415 * zebra->ribq structure until we enter the main execution loop.
416 * The notifications from kernel will show originating PID equal
417 * to that after daemon() completes (if ever called).
418 */
419 frr_config_fork();
420
421 /* After we have successfully acquired the pidfile, we can be sure
422 * about being the only copy of zebra process, which is submitting
423 * changes to the FIB.
424 * Clean up zebra-originated routes. The requests will be sent to OS
425 * immediately, so originating PID in notifications from kernel
426 * will be equal to the current getpid(). To know about such routes,
427 * we have to have route_read() called before.
428 */
429 zrouter.startup_time = monotime(NULL);
430 thread_add_timer(zrouter.master, rib_sweep_route,
431 NULL, graceful_restart, NULL);
432
433 /* Needed for BSD routing socket. */
434 pid = getpid();
435
436 /* Start dataplane system */
437 zebra_dplane_start();
438
439 /* Start Zebra API server */
440 zserv_start(zserv_path);
441
442 /* Init label manager */
443 label_manager_init();
444
445 /* RNH init */
446 zebra_rnh_init();
447
448 /* Config handler Init */
449 zebra_evpn_init();
450
451 /* Error init */
452 zebra_error_init();
453
454 #if defined(HANDLE_ZAPI_FUZZING)
455 if (zapi_fuzzing) {
456 zserv_read_file(zapi_fuzzing);
457 exit(0);
458 }
459 #endif /* HANDLE_ZAPI_FUZZING */
460 #if defined(HANDLE_NETLINK_FUZZING)
461 if (netlink_fuzzing) {
462 netlink_read_init(netlink_fuzzing);
463 exit(0);
464 }
465 #endif /* HANDLE_NETLINK_FUZZING */
466
467
468 frr_run(zrouter.master);
469
470 /* Not reached... */
471 return 0;
472 }