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>
29 #include "zebra_memory.h"
30 #include "memory_vty.h"
37 #include "logicalrouter.h"
40 #include "frr_pthread.h"
42 #include "zebra/rib.h"
43 #include "zebra/zserv.h"
44 #include "zebra/debug.h"
45 #include "zebra/router-id.h"
46 #include "zebra/irdp.h"
47 #include "zebra/rtadv.h"
48 #include "zebra/zebra_ptm.h"
49 #include "zebra/zebra_ns.h"
50 #include "zebra/redistribute.h"
51 #include "zebra/zebra_mpls.h"
52 #include "zebra/label_manager.h"
53 #include "zebra/zebra_netns_notify.h"
54 #include "zebra/zebra_rnh.h"
55 #include "zebra/zebra_pbr.h"
57 #define ZEBRA_PTM_SUPPORT
60 struct zebra_t zebrad
= {
61 .rtm_table_default
= 0,
62 .packets_to_process
= ZEBRA_ZAPI_PACKETS_TO_PROCESS
,
68 /* Pacify zclient.o in libfrr, which expects this variable. */
69 struct thread_master
*master
;
71 /* Route retain mode flag. */
74 /* Allow non-quagga entities to delete quagga routes */
77 /* Don't delete kernel route. */
78 int keep_kernel_mode
= 0;
80 bool v6_rr_semantics
= false;
83 /* Receive buffer size for netlink socket */
84 uint32_t nl_rcvbufsize
= 4194304;
85 #endif /* HAVE_NETLINK */
87 #define OPTION_V6_RR_SEMANTICS 2000
88 /* Command line options. */
89 struct option longopts
[] = {
90 {"batch", no_argument
, NULL
, 'b'},
91 {"allow_delete", no_argument
, NULL
, 'a'},
92 {"keep_kernel", no_argument
, NULL
, 'k'},
93 {"socket", required_argument
, NULL
, 'z'},
94 {"ecmp", required_argument
, NULL
, 'e'},
95 {"label_socket", no_argument
, NULL
, 'l'},
96 {"retain", no_argument
, NULL
, 'r'},
98 {"vrfwnetns", no_argument
, NULL
, 'n'},
99 {"nl-bufsize", required_argument
, NULL
, 's'},
100 {"v6-rr-semantics", no_argument
, NULL
, OPTION_V6_RR_SEMANTICS
},
101 #endif /* HAVE_NETLINK */
104 zebra_capabilities_t _caps_p
[] = {
105 ZCAP_NET_ADMIN
, ZCAP_SYS_ADMIN
, ZCAP_NET_RAW
,
108 /* zebra privileges to run with */
109 struct zebra_privs_t zserv_privs
= {
110 #if defined(FRR_USER) && defined(FRR_GROUP)
115 .vty_group
= VTY_GROUP
,
118 .cap_num_p
= array_size(_caps_p
),
121 unsigned int multipath_num
= MULTIPATH_NUM
;
123 /* SIGHUP handler. */
124 static void sighup(void)
126 zlog_info("SIGHUP received");
128 /* Reload of config file. */
132 /* SIGINT handler. */
133 static void sigint(void)
136 struct zebra_vrf
*zvrf
;
138 zlog_notice("Terminating on signal");
142 list_delete_all_node(zebrad
.client_list
);
146 RB_FOREACH (vrf
, vrf_name_head
, &vrfs_by_name
) {
149 SET_FLAG(zvrf
->flags
, ZEBRA_VRF_RETAIN
);
151 if (zebrad
.lsp_process_q
)
152 work_queue_free_and_null(&zebrad
.lsp_process_q
);
155 ns_walk_func(zebra_ns_disabled
);
156 zebra_ns_notify_close();
162 list_delete_and_null(&zebrad
.client_list
);
163 work_queue_free_and_null(&zebrad
.ribq
);
164 meta_queue_free(zebrad
.mq
);
170 /* SIGUSR1 handler. */
171 static void sigusr1(void)
176 struct quagga_signal_t zebra_signals
[] = {
196 zebra
, ZEBRA
, .vty_port
= ZEBRA_VTY_PORT
, .flags
= FRR_NO_ZCLIENT
,
199 "Daemon which manages kernel routing table management "
200 "and\nredistribution between different routing protocols.",
202 .signals
= zebra_signals
, .n_signals
= array_size(zebra_signals
),
204 .privs
= &zserv_privs
, )
206 /* Main startup routine. */
207 int main(int argc
, char **argv
)
209 // int batch_mode = 0;
210 char *zserv_path
= NULL
;
211 /* Socket to external label manager */
212 char *lblmgr_path
= NULL
;
213 struct sockaddr_storage dummy
;
215 #if defined(HANDLE_ZAPI_FUZZING)
216 char *fuzzing
= NULL
;
219 vrf_configure_backend(VRF_BACKEND_VRF_LITE
);
220 logicalrouter_configure_backend(LOGICALROUTER_BACKEND_NETNS
);
222 frr_preinit(&zebra_di
, argc
, argv
);
229 #if defined(HANDLE_ZAPI_FUZZING)
234 " -b, --batch Runs in batch mode\n"
235 " -a, --allow_delete Allow other processes to delete zebra routes\n"
236 " -z, --socket Set path of zebra socket\n"
237 " -e, --ecmp Specify ECMP to use.\n"
238 " -l, --label_socket Socket to external label manager\n"
239 " -k, --keep_kernel Don't delete old routes which installed by zebra.\n"
240 " -r, --retain When program terminates, retain added route by zebra.\n"
242 " -n, --vrfwnetns Set VRF with NetNS\n"
243 " -s, --nl-bufsize Set netlink receive buffer size\n"
244 " --v6-rr-semantics Use v6 RR semantics\n"
245 #endif /* HAVE_NETLINK */
246 #if defined(HANDLE_ZAPI_FUZZING)
247 " -c <file> Bypass normal startup use this file for tetsting of zapi"
252 int opt
= frr_getopt(argc
, argv
, NULL
);
267 keep_kernel_mode
= 1;
270 multipath_num
= atoi(optarg
);
271 if (multipath_num
> MULTIPATH_NUM
272 || multipath_num
<= 0) {
274 "Multipath Number specified must be less than %d and greater than 0",
281 if (!frr_zclient_addr(&dummy
, &dummylen
, optarg
)) {
283 "Invalid zserv socket path: %s\n",
289 lblmgr_path
= optarg
;
296 nl_rcvbufsize
= atoi(optarg
);
299 vrf_configure_backend(VRF_BACKEND_NETNS
);
300 logicalrouter_configure_backend(
301 LOGICALROUTER_BACKEND_OFF
);
303 case OPTION_V6_RR_SEMANTICS
:
304 v6_rr_semantics
= true;
306 #endif /* HAVE_NETLINK */
307 #if defined(HANDLE_ZAPI_FUZZING)
318 vty_config_lockless();
319 zebrad
.master
= frr_init();
321 /* Zebra related initialize. */
326 router_id_cmd_init();
329 * Initialize NS( and implicitly the VRF module), and make kernel
336 #if defined(HAVE_RTADV)
340 #ifdef ZEBRA_PTM_SUPPORT
345 zebra_mpls_vty_init();
349 /* For debug purpose. */
350 /* SET_FLAG (zebra_debug_event, ZEBRA_DEBUG_EVENT); */
352 #if defined(HANDLE_ZAPI_FUZZING)
354 zserv_read_file(fuzzing
);
359 /* Process the configuration file. Among other configuration
360 * directives we can meet those installing static routes. Such
361 * requests will not be executed immediately, but queued in
362 * zebra->ribq structure until we enter the main execution loop.
363 * The notifications from kernel will show originating PID equal
364 * to that after daemon() completes (if ever called).
368 /* After we have successfully acquired the pidfile, we can be sure
369 * about being the only copy of zebra process, which is submitting
370 * changes to the FIB.
371 * Clean up zebra-originated routes. The requests will be sent to OS
372 * immediately, so originating PID in notifications from kernel
373 * will be equal to the current getpid(). To know about such routes,
374 * we have to have route_read() called before.
376 if (!keep_kernel_mode
)
379 /* Needed for BSD routing socket. */
382 /* Intialize pthread library */
385 /* Start Zebra API server */
386 zserv_start(zserv_path
);
388 /* Init label manager */
389 label_manager_init(lblmgr_path
);
394 frr_run(zebrad
.master
);