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"
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"
55 #define ZEBRA_PTM_SUPPORT
58 struct zebra_t zebrad
= {
59 .rtm_table_default
= 0,
60 .packets_to_process
= ZEBRA_ZAPI_PACKETS_TO_PROCESS
,
66 /* Pacify zclient.o in libfrr, which expects this variable. */
67 struct thread_master
*master
;
69 /* Route retain mode flag. */
72 /* Allow non-quagga entities to delete quagga routes */
75 /* Don't delete kernel route. */
76 int keep_kernel_mode
= 0;
78 bool v6_rr_semantics
= false;
81 /* Receive buffer size for netlink socket */
82 uint32_t nl_rcvbufsize
= 4194304;
83 #endif /* HAVE_NETLINK */
85 #define OPTION_V6_RR_SEMANTICS 2000
86 /* Command line options. */
87 struct option longopts
[] = {
88 {"batch", no_argument
, NULL
, 'b'},
89 {"allow_delete", no_argument
, NULL
, 'a'},
90 {"keep_kernel", no_argument
, NULL
, 'k'},
91 {"socket", required_argument
, NULL
, 'z'},
92 {"ecmp", required_argument
, NULL
, 'e'},
93 {"label_socket", no_argument
, NULL
, 'l'},
94 {"retain", no_argument
, NULL
, 'r'},
96 {"vrfwnetns", no_argument
, NULL
, 'n'},
97 {"nl-bufsize", required_argument
, NULL
, 's'},
98 {"v6-rr-semantics", no_argument
, NULL
, OPTION_V6_RR_SEMANTICS
},
99 #endif /* HAVE_NETLINK */
102 zebra_capabilities_t _caps_p
[] = {
103 ZCAP_NET_ADMIN
, ZCAP_SYS_ADMIN
, ZCAP_NET_RAW
,
106 /* zebra privileges to run with */
107 struct zebra_privs_t zserv_privs
= {
108 #if defined(FRR_USER) && defined(FRR_GROUP)
113 .vty_group
= VTY_GROUP
,
116 .cap_num_p
= array_size(_caps_p
),
119 unsigned int multipath_num
= MULTIPATH_NUM
;
121 /* SIGHUP handler. */
122 static void sighup(void)
124 zlog_info("SIGHUP received");
126 /* Reload of config file. */
130 /* SIGINT handler. */
131 static void sigint(void)
134 struct zebra_vrf
*zvrf
;
136 zlog_notice("Terminating on signal");
140 list_delete_all_node(zebrad
.client_list
);
144 RB_FOREACH (vrf
, vrf_name_head
, &vrfs_by_name
) {
147 SET_FLAG(zvrf
->flags
, ZEBRA_VRF_RETAIN
);
149 if (zebrad
.lsp_process_q
)
150 work_queue_free_and_null(&zebrad
.lsp_process_q
);
153 ns_walk_func(zebra_ns_disabled
);
154 zebra_ns_notify_close();
160 list_delete_and_null(&zebrad
.client_list
);
161 work_queue_free_and_null(&zebrad
.ribq
);
162 meta_queue_free(zebrad
.mq
);
168 /* SIGUSR1 handler. */
169 static void sigusr1(void)
174 struct quagga_signal_t zebra_signals
[] = {
194 zebra
, ZEBRA
, .vty_port
= ZEBRA_VTY_PORT
, .flags
= FRR_NO_ZCLIENT
,
197 "Daemon which manages kernel routing table management "
198 "and\nredistribution between different routing protocols.",
200 .signals
= zebra_signals
, .n_signals
= array_size(zebra_signals
),
202 .privs
= &zserv_privs
, )
204 /* Main startup routine. */
205 int main(int argc
, char **argv
)
207 // int batch_mode = 0;
208 char *zserv_path
= NULL
;
209 /* Socket to external label manager */
210 char *lblmgr_path
= NULL
;
211 struct sockaddr_storage dummy
;
213 #if defined(HANDLE_ZAPI_FUZZING)
214 char *fuzzing
= NULL
;
217 vrf_configure_backend(VRF_BACKEND_VRF_LITE
);
218 logicalrouter_configure_backend(LOGICALROUTER_BACKEND_NETNS
);
220 frr_preinit(&zebra_di
, argc
, argv
);
227 #if defined(HANDLE_ZAPI_FUZZING)
232 " -b, --batch Runs in batch mode\n"
233 " -a, --allow_delete Allow other processes to delete zebra routes\n"
234 " -z, --socket Set path of zebra socket\n"
235 " -e, --ecmp Specify ECMP to use.\n"
236 " -l, --label_socket Socket to external label manager\n"
237 " -k, --keep_kernel Don't delete old routes which installed by zebra.\n"
238 " -r, --retain When program terminates, retain added route by zebra.\n"
240 " -n, --vrfwnetns Set VRF with NetNS\n"
241 " -s, --nl-bufsize Set netlink receive buffer size\n"
242 " --v6-rr-semantics Use v6 RR semantics\n"
243 #endif /* HAVE_NETLINK */
244 #if defined(HANDLE_ZAPI_FUZZING)
245 " -c <file> Bypass normal startup use this file for tetsting of zapi"
250 int opt
= frr_getopt(argc
, argv
, NULL
);
265 keep_kernel_mode
= 1;
268 multipath_num
= atoi(optarg
);
269 if (multipath_num
> MULTIPATH_NUM
270 || multipath_num
<= 0) {
272 "Multipath Number specified must be less than %d and greater than 0",
279 if (!frr_zclient_addr(&dummy
, &dummylen
, optarg
)) {
281 "Invalid zserv socket path: %s\n",
287 lblmgr_path
= optarg
;
294 nl_rcvbufsize
= atoi(optarg
);
297 vrf_configure_backend(VRF_BACKEND_NETNS
);
298 logicalrouter_configure_backend(
299 LOGICALROUTER_BACKEND_OFF
);
301 case OPTION_V6_RR_SEMANTICS
:
302 v6_rr_semantics
= true;
304 #endif /* HAVE_NETLINK */
305 #if defined(HANDLE_ZAPI_FUZZING)
316 vty_config_lockless();
317 zebrad
.master
= frr_init();
319 /* Zebra related initialize. */
324 router_id_cmd_init();
327 * Initialize NS( and implicitly the VRF module), and make kernel
334 #if defined(HAVE_RTADV)
338 #ifdef ZEBRA_PTM_SUPPORT
343 zebra_mpls_vty_init();
346 /* For debug purpose. */
347 /* SET_FLAG (zebra_debug_event, ZEBRA_DEBUG_EVENT); */
349 #if defined(HANDLE_ZAPI_FUZZING)
351 zserv_read_file(fuzzing
);
356 /* Process the configuration file. Among other configuration
357 * directives we can meet those installing static routes. Such
358 * requests will not be executed immediately, but queued in
359 * zebra->ribq structure until we enter the main execution loop.
360 * The notifications from kernel will show originating PID equal
361 * to that after daemon() completes (if ever called).
365 /* After we have successfully acquired the pidfile, we can be sure
366 * about being the only copy of zebra process, which is submitting
367 * changes to the FIB.
368 * Clean up zebra-originated routes. The requests will be sent to OS
369 * immediately, so originating PID in notifications from kernel
370 * will be equal to the current getpid(). To know about such routes,
371 * we have to have route_read() called before.
373 if (!keep_kernel_mode
)
376 /* Needed for BSD routing socket. */
379 /* This must be done only after locking pidfile (bug #403). */
380 zebra_zserv_socket_init(zserv_path
);
382 /* Init label manager */
383 label_manager_init(lblmgr_path
);
388 frr_run(zebrad
.master
);