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
;
137 struct listnode
*ln
, *nn
;
138 struct zserv
*client
;
140 zlog_notice("Terminating on signal");
144 for (ALL_LIST_ELEMENTS(zebrad
.client_list
, ln
, nn
, client
))
145 zserv_close_client(client
);
147 list_delete_all_node(zebrad
.client_list
);
151 RB_FOREACH (vrf
, vrf_name_head
, &vrfs_by_name
) {
154 SET_FLAG(zvrf
->flags
, ZEBRA_VRF_RETAIN
);
156 if (zebrad
.lsp_process_q
)
157 work_queue_free_and_null(&zebrad
.lsp_process_q
);
160 ns_walk_func(zebra_ns_disabled
);
161 zebra_ns_notify_close();
167 list_delete_and_null(&zebrad
.client_list
);
168 work_queue_free_and_null(&zebrad
.ribq
);
169 meta_queue_free(zebrad
.mq
);
175 /* SIGUSR1 handler. */
176 static void sigusr1(void)
181 struct quagga_signal_t zebra_signals
[] = {
201 zebra
, ZEBRA
, .vty_port
= ZEBRA_VTY_PORT
, .flags
= FRR_NO_ZCLIENT
,
204 "Daemon which manages kernel routing table management "
205 "and\nredistribution between different routing protocols.",
207 .signals
= zebra_signals
, .n_signals
= array_size(zebra_signals
),
209 .privs
= &zserv_privs
, )
211 /* Main startup routine. */
212 int main(int argc
, char **argv
)
214 // int batch_mode = 0;
215 char *zserv_path
= NULL
;
216 /* Socket to external label manager */
217 char *lblmgr_path
= NULL
;
218 struct sockaddr_storage dummy
;
220 #if defined(HANDLE_ZAPI_FUZZING)
221 char *fuzzing
= NULL
;
224 vrf_configure_backend(VRF_BACKEND_VRF_LITE
);
225 logicalrouter_configure_backend(LOGICALROUTER_BACKEND_NETNS
);
227 frr_preinit(&zebra_di
, argc
, argv
);
234 #if defined(HANDLE_ZAPI_FUZZING)
239 " -b, --batch Runs in batch mode\n"
240 " -a, --allow_delete Allow other processes to delete zebra routes\n"
241 " -z, --socket Set path of zebra socket\n"
242 " -e, --ecmp Specify ECMP to use.\n"
243 " -l, --label_socket Socket to external label manager\n"
244 " -k, --keep_kernel Don't delete old routes which were installed by zebra.\n"
245 " -r, --retain When program terminates, retain added route by zebra.\n"
247 " -n, --vrfwnetns Use NetNS as VRF backend\n"
248 " -s, --nl-bufsize Set netlink receive buffer size\n"
249 " --v6-rr-semantics Use v6 RR semantics\n"
250 #endif /* HAVE_NETLINK */
251 #if defined(HANDLE_ZAPI_FUZZING)
252 " -c <file> Bypass normal startup and use this file for testing of zapi"
257 int opt
= frr_getopt(argc
, argv
, NULL
);
272 keep_kernel_mode
= 1;
275 multipath_num
= atoi(optarg
);
276 if (multipath_num
> MULTIPATH_NUM
277 || multipath_num
<= 0) {
279 "Multipath Number specified must be less than %d and greater than 0",
286 if (!frr_zclient_addr(&dummy
, &dummylen
, optarg
)) {
288 "Invalid zserv socket path: %s\n",
294 lblmgr_path
= optarg
;
301 nl_rcvbufsize
= atoi(optarg
);
304 vrf_configure_backend(VRF_BACKEND_NETNS
);
305 logicalrouter_configure_backend(
306 LOGICALROUTER_BACKEND_OFF
);
308 case OPTION_V6_RR_SEMANTICS
:
309 v6_rr_semantics
= true;
311 #endif /* HAVE_NETLINK */
312 #if defined(HANDLE_ZAPI_FUZZING)
323 vty_config_lockless();
324 zebrad
.master
= frr_init();
326 /* Zebra related initialize. */
331 router_id_cmd_init();
334 * Initialize NS( and implicitly the VRF module), and make kernel
341 #if defined(HAVE_RTADV)
345 #ifdef ZEBRA_PTM_SUPPORT
350 zebra_mpls_vty_init();
354 /* For debug purpose. */
355 /* SET_FLAG (zebra_debug_event, ZEBRA_DEBUG_EVENT); */
357 /* Process the configuration file. Among other configuration
358 * directives we can meet those installing static routes. Such
359 * requests will not be executed immediately, but queued in
360 * zebra->ribq structure until we enter the main execution loop.
361 * The notifications from kernel will show originating PID equal
362 * to that after daemon() completes (if ever called).
366 /* After we have successfully acquired the pidfile, we can be sure
367 * about being the only copy of zebra process, which is submitting
368 * changes to the FIB.
369 * Clean up zebra-originated routes. The requests will be sent to OS
370 * immediately, so originating PID in notifications from kernel
371 * will be equal to the current getpid(). To know about such routes,
372 * we have to have route_read() called before.
374 if (!keep_kernel_mode
)
377 /* Needed for BSD routing socket. */
380 /* Intialize pthread library */
383 /* Start Zebra API server */
384 zserv_start(zserv_path
);
386 /* Init label manager */
387 label_manager_init(lblmgr_path
);
392 #if defined(HANDLE_ZAPI_FUZZING)
394 zserv_read_file(fuzzing
);
400 frr_run(zebrad
.master
);