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