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"
54 #include "zebra/zebra_pbr.h"
56 #define ZEBRA_PTM_SUPPORT
59 struct zebra_t zebrad
= {
60 .rtm_table_default
= 0,
61 .packets_to_process
= ZEBRA_ZAPI_PACKETS_TO_PROCESS
,
67 /* Pacify zclient.o in libfrr, which expects this variable. */
68 struct thread_master
*master
;
70 /* Route retain mode flag. */
73 /* Allow non-quagga entities to delete quagga routes */
76 /* Don't delete kernel route. */
77 int keep_kernel_mode
= 0;
79 bool v6_rr_semantics
= false;
82 /* Receive buffer size for netlink socket */
83 uint32_t nl_rcvbufsize
= 4194304;
84 #endif /* HAVE_NETLINK */
86 #define OPTION_V6_RR_SEMANTICS 2000
87 /* Command line options. */
88 struct option longopts
[] = {
89 {"batch", no_argument
, NULL
, 'b'},
90 {"allow_delete", no_argument
, NULL
, 'a'},
91 {"keep_kernel", no_argument
, NULL
, 'k'},
92 {"socket", required_argument
, NULL
, 'z'},
93 {"ecmp", required_argument
, NULL
, 'e'},
94 {"label_socket", no_argument
, NULL
, 'l'},
95 {"retain", no_argument
, NULL
, 'r'},
97 {"vrfwnetns", no_argument
, NULL
, 'n'},
98 {"nl-bufsize", required_argument
, NULL
, 's'},
99 {"v6-rr-semantics", no_argument
, NULL
, OPTION_V6_RR_SEMANTICS
},
100 #endif /* HAVE_NETLINK */
103 zebra_capabilities_t _caps_p
[] = {
104 ZCAP_NET_ADMIN
, ZCAP_SYS_ADMIN
, ZCAP_NET_RAW
,
107 /* zebra privileges to run with */
108 struct zebra_privs_t zserv_privs
= {
109 #if defined(FRR_USER) && defined(FRR_GROUP)
114 .vty_group
= VTY_GROUP
,
117 .cap_num_p
= array_size(_caps_p
),
120 unsigned int multipath_num
= MULTIPATH_NUM
;
122 /* SIGHUP handler. */
123 static void sighup(void)
125 zlog_info("SIGHUP received");
127 /* Reload of config file. */
131 /* SIGINT handler. */
132 static void sigint(void)
135 struct zebra_vrf
*zvrf
;
137 zlog_notice("Terminating on signal");
141 list_delete_all_node(zebrad
.client_list
);
145 RB_FOREACH (vrf
, vrf_name_head
, &vrfs_by_name
) {
148 SET_FLAG(zvrf
->flags
, ZEBRA_VRF_RETAIN
);
150 if (zebrad
.lsp_process_q
)
151 work_queue_free_and_null(&zebrad
.lsp_process_q
);
154 ns_walk_func(zebra_ns_disabled
);
155 zebra_ns_notify_close();
161 list_delete_and_null(&zebrad
.client_list
);
162 work_queue_free_and_null(&zebrad
.ribq
);
163 meta_queue_free(zebrad
.mq
);
169 /* SIGUSR1 handler. */
170 static void sigusr1(void)
175 struct quagga_signal_t zebra_signals
[] = {
195 zebra
, ZEBRA
, .vty_port
= ZEBRA_VTY_PORT
, .flags
= FRR_NO_ZCLIENT
,
198 "Daemon which manages kernel routing table management "
199 "and\nredistribution between different routing protocols.",
201 .signals
= zebra_signals
, .n_signals
= array_size(zebra_signals
),
203 .privs
= &zserv_privs
, )
205 /* Main startup routine. */
206 int main(int argc
, char **argv
)
208 // int batch_mode = 0;
209 char *zserv_path
= NULL
;
210 /* Socket to external label manager */
211 char *lblmgr_path
= NULL
;
212 struct sockaddr_storage dummy
;
214 #if defined(HANDLE_ZAPI_FUZZING)
215 char *fuzzing
= NULL
;
218 vrf_configure_backend(VRF_BACKEND_VRF_LITE
);
219 logicalrouter_configure_backend(LOGICALROUTER_BACKEND_NETNS
);
221 frr_preinit(&zebra_di
, argc
, argv
);
228 #if defined(HANDLE_ZAPI_FUZZING)
233 " -b, --batch Runs in batch mode\n"
234 " -a, --allow_delete Allow other processes to delete zebra routes\n"
235 " -z, --socket Set path of zebra socket\n"
236 " -e, --ecmp Specify ECMP to use.\n"
237 " -l, --label_socket Socket to external label manager\n"
238 " -k, --keep_kernel Don't delete old routes which installed by zebra.\n"
239 " -r, --retain When program terminates, retain added route by zebra.\n"
241 " -n, --vrfwnetns Set VRF with NetNS\n"
242 " -s, --nl-bufsize Set netlink receive buffer size\n"
243 " --v6-rr-semantics Use v6 RR semantics\n"
244 #endif /* HAVE_NETLINK */
245 #if defined(HANDLE_ZAPI_FUZZING)
246 " -c <file> Bypass normal startup use this file for tetsting of zapi"
251 int opt
= frr_getopt(argc
, argv
, NULL
);
266 keep_kernel_mode
= 1;
269 multipath_num
= atoi(optarg
);
270 if (multipath_num
> MULTIPATH_NUM
271 || multipath_num
<= 0) {
273 "Multipath Number specified must be less than %d and greater than 0",
280 if (!frr_zclient_addr(&dummy
, &dummylen
, optarg
)) {
282 "Invalid zserv socket path: %s\n",
288 lblmgr_path
= optarg
;
295 nl_rcvbufsize
= atoi(optarg
);
298 vrf_configure_backend(VRF_BACKEND_NETNS
);
299 logicalrouter_configure_backend(
300 LOGICALROUTER_BACKEND_OFF
);
302 case OPTION_V6_RR_SEMANTICS
:
303 v6_rr_semantics
= true;
305 #endif /* HAVE_NETLINK */
306 #if defined(HANDLE_ZAPI_FUZZING)
317 vty_config_lockless();
318 zebrad
.master
= frr_init();
320 /* Zebra related initialize. */
325 router_id_cmd_init();
328 * Initialize NS( and implicitly the VRF module), and make kernel
335 #if defined(HAVE_RTADV)
339 #ifdef ZEBRA_PTM_SUPPORT
344 zebra_mpls_vty_init();
348 /* For debug purpose. */
349 /* SET_FLAG (zebra_debug_event, ZEBRA_DEBUG_EVENT); */
351 #if defined(HANDLE_ZAPI_FUZZING)
353 zserv_read_file(fuzzing
);
358 /* Process the configuration file. Among other configuration
359 * directives we can meet those installing static routes. Such
360 * requests will not be executed immediately, but queued in
361 * zebra->ribq structure until we enter the main execution loop.
362 * The notifications from kernel will show originating PID equal
363 * to that after daemon() completes (if ever called).
367 /* After we have successfully acquired the pidfile, we can be sure
368 * about being the only copy of zebra process, which is submitting
369 * changes to the FIB.
370 * Clean up zebra-originated routes. The requests will be sent to OS
371 * immediately, so originating PID in notifications from kernel
372 * will be equal to the current getpid(). To know about such routes,
373 * we have to have route_read() called before.
375 if (!keep_kernel_mode
)
378 /* Needed for BSD routing socket. */
381 /* This must be done only after locking pidfile (bug #403). */
382 zebra_zserv_socket_init(zserv_path
);
384 /* Init label manager */
385 label_manager_init(lblmgr_path
);
390 frr_run(zebrad
.master
);