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