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>
21 #include "affinitymap.h"
23 #include "routing_nb.h"
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"
49 #define ZEBRA_PTM_SUPPORT
54 /* Pacify zclient.o in libfrr, which expects this variable. */
55 struct event_loop
*master
;
57 /* Route retain mode flag. */
62 bool v6_rr_semantics
= false;
64 /* Receive buffer size for kernel control sockets */
65 #define RCVBUFSIZE_MIN 4194304
67 uint32_t rcvbufsize
= RCVBUFSIZE_MIN
;
69 uint32_t rcvbufsize
= 128 * 1024;
72 #define OPTION_V6_RR_SEMANTICS 2000
73 #define OPTION_ASIC_OFFLOAD 2001
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
},
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 */
91 zebra_capabilities_t _caps_p
[] = {ZCAP_NET_ADMIN
, ZCAP_SYS_ADMIN
,
94 ZCAP_IPC_LOCK
, ZCAP_READ_SEARCH
,
99 /* zebra privileges to run with */
100 struct zebra_privs_t zserv_privs
= {
101 #if defined(FRR_USER) && defined(FRR_GROUP)
106 .vty_group
= VTY_GROUP
,
109 .cap_num_p
= array_size(_caps_p
),
112 /* SIGHUP handler. */
113 static void sighup(void)
115 zlog_info("SIGHUP received");
117 /* Reload of config file. */
121 /* SIGINT handler. */
122 static void sigint(void)
125 struct zebra_vrf
*zvrf
;
126 struct listnode
*ln
, *nn
;
127 struct zserv
*client
;
128 static bool sigint_done
;
135 zlog_notice("Terminating on signal");
137 atomic_store_explicit(&zrouter
.in_shutdown
, true,
138 memory_order_relaxed
);
140 /* send RA lifetime of 0 before stopping. rfc4861/6.2.5 */
145 /* Stop the opaque module pthread */
148 zebra_dplane_pre_finish();
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
);
154 /* Clean up zapi clients and server module */
155 for (ALL_LIST_ELEMENTS(zrouter
.client_list
, ln
, nn
, client
))
156 zserv_close_client(client
);
159 list_delete_all_node(zrouter
.client_list
);
161 /* Once all the zclients are cleaned up, clean up the opaque module */
162 zebra_opaque_finish();
167 zebra_nhg_mark_keep();
168 RB_FOREACH (vrf
, vrf_name_head
, &vrfs_by_name
) {
171 SET_FLAG(zvrf
->flags
, ZEBRA_VRF_RETAIN
);
175 if (zrouter
.lsp_process_q
)
176 work_queue_free_and_null(&zrouter
.lsp_process_q
);
180 ns_walk_func(zebra_ns_early_shutdown
, NULL
, NULL
);
181 zebra_ns_notify_close();
186 * zebra_routemap_finish will
187 * 1 set rmap upd timer to 0 so that rmap update wont be scheduled again
188 * 2 Put off the rmap update thread
191 zebra_routemap_finish();
193 list_delete(&zrouter
.client_list
);
195 /* Indicate that all new dplane work has been enqueued. When that
196 * work is complete, the dataplane will enqueue an event
197 * with the 'finalize' function.
199 zebra_dplane_finish();
203 * Final shutdown step for the zebra main thread. This is run after all
204 * async update processing has completed.
206 void zebra_finalize(struct event
*dummy
)
208 zlog_info("Zebra final shutdown");
210 /* Stop dplane thread and finish any cleanup */
211 zebra_dplane_shutdown();
213 /* Final shutdown of ns resources */
214 ns_walk_func(zebra_ns_final_shutdown
, NULL
, NULL
);
216 zebra_router_terminate();
223 /* SIGUSR1 handler. */
224 static void sigusr1(void)
229 struct frr_signal_t zebra_signals
[] = {
248 /* clang-format off */
249 static const struct frr_yang_module_info
*const zebra_yang_modules
[] = {
256 &frr_affinity_map_info
,
257 &frr_zebra_route_map_info
,
259 /* clang-format on */
262 zebra
, ZEBRA
, .vty_port
= ZEBRA_VTY_PORT
, .flags
= FRR_NO_ZCLIENT
,
265 "Daemon which manages kernel routing table management and\nredistribution between different routing protocols.",
267 .signals
= zebra_signals
, .n_signals
= array_size(zebra_signals
),
269 .privs
= &zserv_privs
,
271 .yang_modules
= zebra_yang_modules
,
272 .n_yang_modules
= array_size(zebra_yang_modules
),
275 /* Main startup routine. */
276 int main(int argc
, char **argv
)
278 // int batch_mode = 0;
279 char *zserv_path
= NULL
;
280 struct sockaddr_storage dummy
;
282 bool asic_offload
= false;
283 bool notify_on_ack
= true;
285 graceful_restart
= 0;
286 vrf_configure_backend(VRF_BACKEND_VRF_LITE
);
288 frr_preinit(&zebra_di
, argc
, argv
);
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"
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"
309 " -s, Set kernel socket receive buffer size\n"
310 #endif /* HAVE_NETLINK */
314 int opt
= frr_getopt(argc
, argv
, NULL
);
326 zrouter
.allow_delete
= true;
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
) {
335 EC_ZEBRA_BAD_MULTIPATH_NUM
,
336 "Multipath Number specified must be less than %u and greater than 0",
340 zrouter
.multipath_num
= parsed_multipath
;
345 if (!frr_zclient_addr(&dummy
, &dummylen
, optarg
)) {
347 "Invalid zserv socket path: %s\n",
356 graceful_restart
= atoi(optarg
);
359 rcvbufsize
= atoi(optarg
);
360 if (rcvbufsize
< RCVBUFSIZE_MIN
)
362 "Rcvbufsize is smaller than recommended value: %d\n",
367 vrf_configure_backend(VRF_BACKEND_NETNS
);
369 case OPTION_V6_RR_SEMANTICS
:
370 v6_rr_semantics
= true;
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;
379 #endif /* HAVE_NETLINK */
385 zrouter
.master
= frr_init();
387 /* Zebra related initialize. */
388 zebra_router_init(asic_offload
, notify_on_ack
);
395 * Initialize NS( and implicitly the VRF module), and make kernel
398 router_id_cmd_init();
404 #ifdef ZEBRA_PTM_SUPPORT
409 zebra_mpls_vty_init();
415 zebra_srv6_vty_init();
417 /* For debug purpose. */
418 /* SET_FLAG (zebra_debug_event, ZEBRA_DEBUG_EVENT); */
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).
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.
437 zrouter
.startup_time
= monotime(NULL
);
438 event_add_timer(zrouter
.master
, rib_sweep_route
, NULL
, graceful_restart
,
441 /* Needed for BSD routing socket. */
444 /* Start dataplane system */
445 zebra_dplane_start();
447 /* Start the ted module, before zserv */
448 zebra_opaque_start();
450 /* Start Zebra API server */
451 zserv_start(zserv_path
);
453 /* Init label manager */
454 label_manager_init();
459 /* Config handler Init */
465 frr_run(zrouter
.master
);