1 /* zebra daemon main routine.
2 * Copyright (C) 1997, 98 Kunihiro Ishiguro
4 * This file is part of GNU Zebra.
6 * GNU Zebra is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
11 * GNU Zebra is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 #include <lib/version.h>
37 #include "routing_nb.h"
39 #include "zebra/zebra_router.h"
40 #include "zebra/zebra_errors.h"
41 #include "zebra/rib.h"
42 #include "zebra/zserv.h"
43 #include "zebra/debug.h"
44 #include "zebra/router-id.h"
45 #include "zebra/irdp.h"
46 #include "zebra/rtadv.h"
47 #include "zebra/zebra_ptm.h"
48 #include "zebra/zebra_ns.h"
49 #include "zebra/redistribute.h"
50 #include "zebra/zebra_mpls.h"
51 #include "zebra/label_manager.h"
52 #include "zebra/zebra_netns_notify.h"
53 #include "zebra/zebra_rnh.h"
54 #include "zebra/zebra_pbr.h"
55 #include "zebra/zebra_vxlan.h"
56 #include "zebra/zebra_routemap.h"
57 #include "zebra/zebra_nb.h"
58 #include "zebra/zebra_opaque.h"
59 #include "zebra/zebra_srte.h"
60 #include "zebra/zebra_srv6.h"
61 #include "zebra/zebra_srv6_vty.h"
63 #define ZEBRA_PTM_SUPPORT
68 /* Pacify zclient.o in libfrr, which expects this variable. */
69 struct thread_master
*master
;
71 /* Route retain mode flag. */
74 /* Allow non-frr entities to delete frr routes */
79 bool v6_rr_semantics
= false;
81 /* Receive buffer size for kernel control sockets */
82 #define RCVBUFSIZE_MIN 4194304
84 uint32_t rcvbufsize
= RCVBUFSIZE_MIN
;
86 uint32_t rcvbufsize
= 128 * 1024;
89 #define OPTION_V6_RR_SEMANTICS 2000
90 #define OPTION_ASIC_OFFLOAD 2001
92 /* Command line options. */
93 const struct option longopts
[] = {
94 {"batch", no_argument
, NULL
, 'b'},
95 {"allow_delete", no_argument
, NULL
, 'a'},
96 {"socket", required_argument
, NULL
, 'z'},
97 {"ecmp", required_argument
, NULL
, 'e'},
98 {"retain", no_argument
, NULL
, 'r'},
99 {"graceful_restart", required_argument
, NULL
, 'K'},
100 {"asic-offload", optional_argument
, NULL
, OPTION_ASIC_OFFLOAD
},
102 {"vrfwnetns", no_argument
, NULL
, 'n'},
103 {"nl-bufsize", required_argument
, NULL
, 's'},
104 {"v6-rr-semantics", no_argument
, NULL
, OPTION_V6_RR_SEMANTICS
},
105 #endif /* HAVE_NETLINK */
108 zebra_capabilities_t _caps_p
[] = {ZCAP_NET_ADMIN
, ZCAP_SYS_ADMIN
,
111 ZCAP_IPC_LOCK
, ZCAP_READ_SEARCH
,
116 /* zebra privileges to run with */
117 struct zebra_privs_t zserv_privs
= {
118 #if defined(FRR_USER) && defined(FRR_GROUP)
123 .vty_group
= VTY_GROUP
,
126 .cap_num_p
= array_size(_caps_p
),
129 /* SIGHUP handler. */
130 static void sighup(void)
132 zlog_info("SIGHUP received");
134 /* Reload of config file. */
138 /* SIGINT handler. */
139 static void sigint(void)
142 struct zebra_vrf
*zvrf
;
143 struct listnode
*ln
, *nn
;
144 struct zserv
*client
;
145 static bool sigint_done
;
152 zlog_notice("Terminating on signal");
154 atomic_store_explicit(&zrouter
.in_shutdown
, true,
155 memory_order_relaxed
);
157 /* send RA lifetime of 0 before stopping. rfc4861/6.2.5 */
162 /* Stop the opaque module pthread */
165 zebra_dplane_pre_finish();
167 /* Clean up GR related info. */
168 zebra_gr_stale_client_cleanup(zrouter
.stale_client_list
);
169 list_delete_all_node(zrouter
.stale_client_list
);
171 /* Clean up zapi clients and server module */
172 for (ALL_LIST_ELEMENTS(zrouter
.client_list
, ln
, nn
, client
))
173 zserv_close_client(client
);
176 list_delete_all_node(zrouter
.client_list
);
178 /* Once all the zclients are cleaned up, clean up the opaque module */
179 zebra_opaque_finish();
184 zebra_nhg_mark_keep();
185 RB_FOREACH (vrf
, vrf_name_head
, &vrfs_by_name
) {
188 SET_FLAG(zvrf
->flags
, ZEBRA_VRF_RETAIN
);
192 if (zrouter
.lsp_process_q
)
193 work_queue_free_and_null(&zrouter
.lsp_process_q
);
197 ns_walk_func(zebra_ns_early_shutdown
, NULL
, NULL
);
198 zebra_ns_notify_close();
203 * zebra_routemap_finish will
204 * 1 set rmap upd timer to 0 so that rmap update wont be scheduled again
205 * 2 Put off the rmap update thread
208 zebra_routemap_finish();
210 list_delete(&zrouter
.client_list
);
212 /* Indicate that all new dplane work has been enqueued. When that
213 * work is complete, the dataplane will enqueue an event
214 * with the 'finalize' function.
216 zebra_dplane_finish();
220 * Final shutdown step for the zebra main thread. This is run after all
221 * async update processing has completed.
223 void zebra_finalize(struct thread
*dummy
)
225 zlog_info("Zebra final shutdown");
227 /* Final shutdown of ns resources */
228 ns_walk_func(zebra_ns_final_shutdown
, NULL
, NULL
);
230 /* Stop dplane thread and finish any cleanup */
231 zebra_dplane_shutdown();
233 zebra_router_terminate();
239 /* SIGUSR1 handler. */
240 static void sigusr1(void)
245 struct frr_signal_t zebra_signals
[] = {
264 static const struct frr_yang_module_info
*const zebra_yang_modules
[] = {
271 &frr_zebra_route_map_info
,
275 zebra
, ZEBRA
, .vty_port
= ZEBRA_VTY_PORT
, .flags
= FRR_NO_ZCLIENT
,
278 "Daemon which manages kernel routing table management and\nredistribution between different routing protocols.",
280 .signals
= zebra_signals
, .n_signals
= array_size(zebra_signals
),
282 .privs
= &zserv_privs
,
284 .yang_modules
= zebra_yang_modules
,
285 .n_yang_modules
= array_size(zebra_yang_modules
),
288 /* Main startup routine. */
289 int main(int argc
, char **argv
)
291 // int batch_mode = 0;
292 char *zserv_path
= NULL
;
293 struct sockaddr_storage dummy
;
295 bool asic_offload
= false;
296 bool notify_on_ack
= true;
298 graceful_restart
= 0;
299 vrf_configure_backend(VRF_BACKEND_VRF_LITE
);
301 frr_preinit(&zebra_di
, argc
, argv
);
310 " -b, --batch Runs in batch mode\n"
311 " -a, --allow_delete Allow other processes to delete zebra routes\n"
312 " -z, --socket Set path of zebra socket\n"
313 " -e, --ecmp Specify ECMP to use.\n"
314 " -r, --retain When program terminates, retain added route by zebra.\n"
315 " -K, --graceful_restart Graceful restart at the kernel level, timer in seconds for expiration\n"
316 " -A, --asic-offload FRR is interacting with an asic underneath the linux kernel\n"
318 " -s, --nl-bufsize Set netlink receive buffer size\n"
319 " -n, --vrfwnetns Use NetNS as VRF backend\n"
320 " --v6-rr-semantics Use v6 RR semantics\n"
322 " -s, Set kernel socket receive buffer size\n"
323 #endif /* HAVE_NETLINK */
327 int opt
= frr_getopt(argc
, argv
, NULL
);
342 unsigned long int parsed_multipath
=
343 strtoul(optarg
, NULL
, 10);
344 if (parsed_multipath
== 0
345 || parsed_multipath
> MULTIPATH_NUM
346 || parsed_multipath
> UINT32_MAX
) {
348 EC_ZEBRA_BAD_MULTIPATH_NUM
,
349 "Multipath Number specified must be less than %u and greater than 0",
353 zrouter
.multipath_num
= parsed_multipath
;
358 if (!frr_zclient_addr(&dummy
, &dummylen
, optarg
)) {
360 "Invalid zserv socket path: %s\n",
369 graceful_restart
= atoi(optarg
);
372 rcvbufsize
= atoi(optarg
);
373 if (rcvbufsize
< RCVBUFSIZE_MIN
)
375 "Rcvbufsize is smaller than recommended value: %d\n",
380 vrf_configure_backend(VRF_BACKEND_NETNS
);
382 case OPTION_V6_RR_SEMANTICS
:
383 v6_rr_semantics
= true;
385 case OPTION_ASIC_OFFLOAD
:
386 if (!strcmp(optarg
, "notify_on_offload"))
387 notify_on_ack
= false;
388 if (!strcmp(optarg
, "notify_on_ack"))
389 notify_on_ack
= true;
392 #endif /* HAVE_NETLINK */
398 zrouter
.master
= frr_init();
400 /* Zebra related initialize. */
401 zebra_router_init(asic_offload
, notify_on_ack
);
408 * Initialize NS( and implicitly the VRF module), and make kernel
411 router_id_cmd_init();
417 #ifdef ZEBRA_PTM_SUPPORT
422 zebra_mpls_vty_init();
428 zebra_srv6_vty_init();
430 /* For debug purpose. */
431 /* SET_FLAG (zebra_debug_event, ZEBRA_DEBUG_EVENT); */
433 /* Process the configuration file. Among other configuration
434 * directives we can meet those installing static routes. Such
435 * requests will not be executed immediately, but queued in
436 * zebra->ribq structure until we enter the main execution loop.
437 * The notifications from kernel will show originating PID equal
438 * to that after daemon() completes (if ever called).
442 /* After we have successfully acquired the pidfile, we can be sure
443 * about being the only copy of zebra process, which is submitting
444 * changes to the FIB.
445 * Clean up zebra-originated routes. The requests will be sent to OS
446 * immediately, so originating PID in notifications from kernel
447 * will be equal to the current getpid(). To know about such routes,
448 * we have to have route_read() called before.
450 zrouter
.startup_time
= monotime(NULL
);
451 thread_add_timer(zrouter
.master
, rib_sweep_route
, NULL
,
452 graceful_restart
, &zrouter
.sweeper
);
454 /* Needed for BSD routing socket. */
457 /* Start dataplane system */
458 zebra_dplane_start();
460 /* Start the ted module, before zserv */
461 zebra_opaque_start();
463 /* Start Zebra API server */
464 zserv_start(zserv_path
);
466 /* Init label manager */
467 label_manager_init();
472 /* Config handler Init */
478 frr_run(zrouter
.master
);