1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* zebra daemon main routine.
3 * Copyright (C) 1997, 98 Kunihiro Ishiguro
8 #include <lib/version.h>
22 #include "routing_nb.h"
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"
48 #define ZEBRA_PTM_SUPPORT
53 /* Pacify zclient.o in libfrr, which expects this variable. */
54 struct thread_master
*master
;
56 /* Route retain mode flag. */
61 bool v6_rr_semantics
= false;
63 /* Receive buffer size for kernel control sockets */
64 #define RCVBUFSIZE_MIN 4194304
66 uint32_t rcvbufsize
= RCVBUFSIZE_MIN
;
68 uint32_t rcvbufsize
= 128 * 1024;
71 #define OPTION_V6_RR_SEMANTICS 2000
72 #define OPTION_ASIC_OFFLOAD 2001
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
},
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 */
90 zebra_capabilities_t _caps_p
[] = {ZCAP_NET_ADMIN
, ZCAP_SYS_ADMIN
,
93 ZCAP_IPC_LOCK
, ZCAP_READ_SEARCH
,
98 /* zebra privileges to run with */
99 struct zebra_privs_t zserv_privs
= {
100 #if defined(FRR_USER) && defined(FRR_GROUP)
105 .vty_group
= VTY_GROUP
,
108 .cap_num_p
= array_size(_caps_p
),
111 /* SIGHUP handler. */
112 static void sighup(void)
114 zlog_info("SIGHUP received");
116 /* Reload of config file. */
120 /* SIGINT handler. */
121 static void sigint(void)
124 struct zebra_vrf
*zvrf
;
125 struct listnode
*ln
, *nn
;
126 struct zserv
*client
;
127 static bool sigint_done
;
134 zlog_notice("Terminating on signal");
136 atomic_store_explicit(&zrouter
.in_shutdown
, true,
137 memory_order_relaxed
);
139 /* send RA lifetime of 0 before stopping. rfc4861/6.2.5 */
144 /* Stop the opaque module pthread */
147 zebra_dplane_pre_finish();
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
);
153 /* Clean up zapi clients and server module */
154 for (ALL_LIST_ELEMENTS(zrouter
.client_list
, ln
, nn
, client
))
155 zserv_close_client(client
);
158 list_delete_all_node(zrouter
.client_list
);
160 /* Once all the zclients are cleaned up, clean up the opaque module */
161 zebra_opaque_finish();
166 zebra_nhg_mark_keep();
167 RB_FOREACH (vrf
, vrf_name_head
, &vrfs_by_name
) {
170 SET_FLAG(zvrf
->flags
, ZEBRA_VRF_RETAIN
);
174 if (zrouter
.lsp_process_q
)
175 work_queue_free_and_null(&zrouter
.lsp_process_q
);
179 ns_walk_func(zebra_ns_early_shutdown
, NULL
, NULL
);
180 zebra_ns_notify_close();
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
190 zebra_routemap_finish();
192 list_delete(&zrouter
.client_list
);
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.
198 zebra_dplane_finish();
202 * Final shutdown step for the zebra main thread. This is run after all
203 * async update processing has completed.
205 void zebra_finalize(struct thread
*dummy
)
207 zlog_info("Zebra final shutdown");
209 /* Stop dplane thread and finish any cleanup */
210 zebra_dplane_shutdown();
212 /* Final shutdown of ns resources */
213 ns_walk_func(zebra_ns_final_shutdown
, NULL
, NULL
);
215 zebra_router_terminate();
222 /* SIGUSR1 handler. */
223 static void sigusr1(void)
228 struct frr_signal_t zebra_signals
[] = {
247 static const struct frr_yang_module_info
*const zebra_yang_modules
[] = {
254 &frr_zebra_route_map_info
,
258 zebra
, ZEBRA
, .vty_port
= ZEBRA_VTY_PORT
, .flags
= FRR_NO_ZCLIENT
,
261 "Daemon which manages kernel routing table management and\nredistribution between different routing protocols.",
263 .signals
= zebra_signals
, .n_signals
= array_size(zebra_signals
),
265 .privs
= &zserv_privs
,
267 .yang_modules
= zebra_yang_modules
,
268 .n_yang_modules
= array_size(zebra_yang_modules
),
271 /* Main startup routine. */
272 int main(int argc
, char **argv
)
274 // int batch_mode = 0;
275 char *zserv_path
= NULL
;
276 struct sockaddr_storage dummy
;
278 bool asic_offload
= false;
279 bool notify_on_ack
= true;
281 graceful_restart
= 0;
282 vrf_configure_backend(VRF_BACKEND_VRF_LITE
);
284 frr_preinit(&zebra_di
, argc
, argv
);
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"
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"
305 " -s, Set kernel socket receive buffer size\n"
306 #endif /* HAVE_NETLINK */
310 int opt
= frr_getopt(argc
, argv
, NULL
);
322 zrouter
.allow_delete
= true;
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
) {
331 EC_ZEBRA_BAD_MULTIPATH_NUM
,
332 "Multipath Number specified must be less than %u and greater than 0",
336 zrouter
.multipath_num
= parsed_multipath
;
341 if (!frr_zclient_addr(&dummy
, &dummylen
, optarg
)) {
343 "Invalid zserv socket path: %s\n",
352 graceful_restart
= atoi(optarg
);
355 rcvbufsize
= atoi(optarg
);
356 if (rcvbufsize
< RCVBUFSIZE_MIN
)
358 "Rcvbufsize is smaller than recommended value: %d\n",
363 vrf_configure_backend(VRF_BACKEND_NETNS
);
365 case OPTION_V6_RR_SEMANTICS
:
366 v6_rr_semantics
= true;
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;
375 #endif /* HAVE_NETLINK */
381 zrouter
.master
= frr_init();
383 /* Zebra related initialize. */
384 zebra_router_init(asic_offload
, notify_on_ack
);
391 * Initialize NS( and implicitly the VRF module), and make kernel
394 router_id_cmd_init();
400 #ifdef ZEBRA_PTM_SUPPORT
405 zebra_mpls_vty_init();
411 zebra_srv6_vty_init();
413 /* For debug purpose. */
414 /* SET_FLAG (zebra_debug_event, ZEBRA_DEBUG_EVENT); */
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).
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.
433 zrouter
.startup_time
= monotime(NULL
);
434 thread_add_timer(zrouter
.master
, rib_sweep_route
, NULL
,
435 graceful_restart
, &zrouter
.sweeper
);
437 /* Needed for BSD routing socket. */
440 /* Start dataplane system */
441 zebra_dplane_start();
443 /* Start the ted module, before zserv */
444 zebra_opaque_start();
446 /* Start Zebra API server */
447 zserv_start(zserv_path
);
449 /* Init label manager */
450 label_manager_init();
455 /* Config handler Init */
461 frr_run(zrouter
.master
);