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
);
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
186 zebra_routemap_finish();
190 list_delete(&zrouter
.client_list
);
192 /* Indicate that all new dplane work has been enqueued. When that
193 * work is complete, the dataplane will enqueue an event
194 * with the 'finalize' function.
196 zebra_dplane_finish();
200 * Final shutdown step for the zebra main thread. This is run after all
201 * async update processing has completed.
203 void zebra_finalize(struct event
*dummy
)
205 zlog_info("Zebra final shutdown");
209 ns_walk_func(zebra_ns_early_shutdown
, NULL
, NULL
);
210 zebra_ns_notify_close();
212 /* Stop dplane thread and finish any cleanup */
213 zebra_dplane_shutdown();
215 /* Final shutdown of ns resources */
216 ns_walk_func(zebra_ns_final_shutdown
, NULL
, NULL
);
218 zebra_router_terminate();
225 /* SIGUSR1 handler. */
226 static void sigusr1(void)
231 struct frr_signal_t zebra_signals
[] = {
250 /* clang-format off */
251 static const struct frr_yang_module_info
*const zebra_yang_modules
[] = {
258 &frr_affinity_map_info
,
259 &frr_zebra_route_map_info
,
261 /* clang-format on */
264 zebra
, ZEBRA
, .vty_port
= ZEBRA_VTY_PORT
, .flags
= FRR_NO_ZCLIENT
,
267 "Daemon which manages kernel routing table management and\nredistribution between different routing protocols.",
269 .signals
= zebra_signals
, .n_signals
= array_size(zebra_signals
),
271 .privs
= &zserv_privs
,
273 .yang_modules
= zebra_yang_modules
,
274 .n_yang_modules
= array_size(zebra_yang_modules
),
277 /* Main startup routine. */
278 int main(int argc
, char **argv
)
280 // int batch_mode = 0;
281 char *zserv_path
= NULL
;
282 struct sockaddr_storage dummy
;
284 bool asic_offload
= false;
285 bool notify_on_ack
= true;
287 graceful_restart
= 0;
288 vrf_configure_backend(VRF_BACKEND_VRF_LITE
);
290 frr_preinit(&zebra_di
, argc
, argv
);
299 " -b, --batch Runs in batch mode\n"
300 " -a, --allow_delete Allow other processes to delete zebra routes\n"
301 " -z, --socket Set path of zebra socket\n"
302 " -e, --ecmp Specify ECMP to use.\n"
303 " -r, --retain When program terminates, retain added route by zebra.\n"
304 " -K, --graceful_restart Graceful restart at the kernel level, timer in seconds for expiration\n"
305 " -A, --asic-offload FRR is interacting with an asic underneath the linux kernel\n"
307 " -s, --nl-bufsize Set netlink receive buffer size\n"
308 " -n, --vrfwnetns Use NetNS as VRF backend\n"
309 " --v6-rr-semantics Use v6 RR semantics\n"
311 " -s, Set kernel socket receive buffer size\n"
312 #endif /* HAVE_NETLINK */
316 int opt
= frr_getopt(argc
, argv
, NULL
);
328 zrouter
.allow_delete
= true;
331 unsigned long int parsed_multipath
=
332 strtoul(optarg
, NULL
, 10);
333 if (parsed_multipath
== 0
334 || parsed_multipath
> MULTIPATH_NUM
335 || parsed_multipath
> UINT32_MAX
) {
337 EC_ZEBRA_BAD_MULTIPATH_NUM
,
338 "Multipath Number specified must be less than %u and greater than 0",
342 zrouter
.multipath_num
= parsed_multipath
;
347 if (!frr_zclient_addr(&dummy
, &dummylen
, optarg
)) {
349 "Invalid zserv socket path: %s\n",
358 graceful_restart
= atoi(optarg
);
361 rcvbufsize
= atoi(optarg
);
362 if (rcvbufsize
< RCVBUFSIZE_MIN
)
364 "Rcvbufsize is smaller than recommended value: %d\n",
369 vrf_configure_backend(VRF_BACKEND_NETNS
);
371 case OPTION_V6_RR_SEMANTICS
:
372 v6_rr_semantics
= true;
374 case OPTION_ASIC_OFFLOAD
:
375 if (!strcmp(optarg
, "notify_on_offload"))
376 notify_on_ack
= false;
377 if (!strcmp(optarg
, "notify_on_ack"))
378 notify_on_ack
= true;
381 #endif /* HAVE_NETLINK */
387 zrouter
.master
= frr_init();
389 /* Zebra related initialize. */
390 zebra_router_init(asic_offload
, notify_on_ack
);
397 * Initialize NS( and implicitly the VRF module), and make kernel
400 router_id_cmd_init();
406 #ifdef ZEBRA_PTM_SUPPORT
411 zebra_mpls_vty_init();
417 zebra_srv6_vty_init();
419 /* For debug purpose. */
420 /* SET_FLAG (zebra_debug_event, ZEBRA_DEBUG_EVENT); */
422 /* Process the configuration file. Among other configuration
423 * directives we can meet those installing static routes. Such
424 * requests will not be executed immediately, but queued in
425 * zebra->ribq structure until we enter the main execution loop.
426 * The notifications from kernel will show originating PID equal
427 * to that after daemon() completes (if ever called).
431 /* After we have successfully acquired the pidfile, we can be sure
432 * about being the only copy of zebra process, which is submitting
433 * changes to the FIB.
434 * Clean up zebra-originated routes. The requests will be sent to OS
435 * immediately, so originating PID in notifications from kernel
436 * will be equal to the current getpid(). To know about such routes,
437 * we have to have route_read() called before.
439 zrouter
.startup_time
= monotime(NULL
);
440 event_add_timer(zrouter
.master
, rib_sweep_route
, NULL
, graceful_restart
,
443 /* Needed for BSD routing socket. */
446 /* Start dataplane system */
447 zebra_dplane_start();
449 /* Start the ted module, before zserv */
450 zebra_opaque_start();
452 /* Start Zebra API server */
453 zserv_start(zserv_path
);
455 /* Init label manager */
456 label_manager_init();
461 /* Config handler Init */
467 frr_run(zrouter
.master
);