]> git.proxmox.com Git - mirror_frr.git/blame - zebra/main.c
Merge pull request #1658 from Orange-OpenSource/TE
[mirror_frr.git] / zebra / main.c
CommitLineData
edd7c245 1/* zebra daemon main routine.
718e3744 2 * Copyright (C) 1997, 98 Kunihiro Ishiguro
3 *
4 * This file is part of GNU Zebra.
5 *
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
9 * later version.
10 *
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.
15 *
896014f4
DL
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
718e3744 19 */
20
21#include <zebra.h>
22
5e4fa164 23#include <lib/version.h>
718e3744 24#include "getopt.h"
25#include "command.h"
26#include "thread.h"
27#include "filter.h"
28#include "memory.h"
4a1ab8e4 29#include "zebra_memory.h"
fc7948fa 30#include "memory_vty.h"
718e3744 31#include "prefix.h"
32#include "log.h"
7514fb77 33#include "plist.h"
edd7c245 34#include "privs.h"
2d75d052 35#include "sigevent.h"
b72ede27 36#include "vrf.h"
4f04a76b 37#include "libfrr.h"
718e3744 38
39#include "zebra/rib.h"
40#include "zebra/zserv.h"
41#include "zebra/debug.h"
18a6dce6 42#include "zebra/router-id.h"
ca776988 43#include "zebra/irdp.h"
a1ac18c4 44#include "zebra/rtadv.h"
244c1cdc 45#include "zebra/zebra_ptm.h"
fe18ee2d 46#include "zebra/zebra_ns.h"
e2b1be64 47#include "zebra/redistribute.h"
7758e3f3 48#include "zebra/zebra_mpls.h"
fea12efb 49#include "zebra/label_manager.h"
244c1cdc
DS
50
51#define ZEBRA_PTM_SUPPORT
718e3744 52
b21b19c5 53/* Zebra instance */
d62a17ae 54struct zebra_t zebrad = {
55 .rtm_table_default = 0,
a37ef435 56 .packets_to_process = ZEBRA_ZAPI_PACKETS_TO_PROCESS,
b21b19c5 57};
718e3744 58
59/* process id. */
718e3744 60pid_t pid;
61
55c72803 62/* Pacify zclient.o in libfrr, which expects this variable. */
87efd646 63struct thread_master *master;
64
718e3744 65/* Route retain mode flag. */
66int retain_mode = 0;
67
6baf7bb8
DS
68/* Allow non-quagga entities to delete quagga routes */
69int allow_delete = 0;
70
718e3744 71/* Don't delete kernel route. */
72int keep_kernel_mode = 0;
73
c34b6b57 74#ifdef HAVE_NETLINK
75/* Receive buffer size for netlink socket */
b6286c70 76u_int32_t nl_rcvbufsize = 4194304;
c34b6b57 77#endif /* HAVE_NETLINK */
78
718e3744 79/* Command line options. */
d62a17ae 80struct option longopts[] = {{"batch", no_argument, NULL, 'b'},
81 {"allow_delete", no_argument, NULL, 'a'},
82 {"keep_kernel", no_argument, NULL, 'k'},
83 {"socket", required_argument, NULL, 'z'},
84 {"ecmp", required_argument, NULL, 'e'},
85 {"label_socket", no_argument, NULL, 'l'},
86 {"retain", no_argument, NULL, 'r'},
c34b6b57 87#ifdef HAVE_NETLINK
d62a17ae 88 {"nl-bufsize", required_argument, NULL, 's'},
c34b6b57 89#endif /* HAVE_NETLINK */
d62a17ae 90 {0}};
718e3744 91
d62a17ae 92zebra_capabilities_t _caps_p[] = {
9d303b37 93 ZCAP_NET_ADMIN, ZCAP_SYS_ADMIN, ZCAP_NET_RAW,
edd7c245 94};
95
96/* zebra privileges to run with */
d62a17ae 97struct zebra_privs_t zserv_privs = {
b2f36157 98#if defined(FRR_USER) && defined(FRR_GROUP)
d62a17ae 99 .user = FRR_USER,
100 .group = FRR_GROUP,
edd7c245 101#endif
102#ifdef VTY_GROUP
d62a17ae 103 .vty_group = VTY_GROUP,
edd7c245 104#endif
d62a17ae 105 .caps_p = _caps_p,
106 .cap_num_p = array_size(_caps_p),
107 .cap_num_i = 0};
edd7c245 108
37fe7731
DS
109unsigned int multipath_num = MULTIPATH_NUM;
110
718e3744 111/* SIGHUP handler. */
d62a17ae 112static void sighup(void)
718e3744 113{
d62a17ae 114 zlog_info("SIGHUP received");
718e3744 115
d62a17ae 116 /* Reload of config file. */
117 ;
718e3744 118}
119
120/* SIGINT handler. */
d62a17ae 121static void sigint(void)
718e3744 122{
d62a17ae 123 struct vrf *vrf;
124 struct zebra_vrf *zvrf;
125 struct zebra_ns *zns;
fe18ee2d 126
d62a17ae 127 zlog_notice("Terminating on signal");
718e3744 128
03951374 129 frr_early_fini();
718e3744 130
d62a17ae 131 list_delete_all_node(zebrad.client_list);
f88bd20c 132 zebra_ptm_finish();
d62a17ae 133
134 if (retain_mode)
a2addae8 135 RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name) {
d62a17ae 136 zvrf = vrf->info;
137 if (zvrf)
138 SET_FLAG(zvrf->flags, ZEBRA_VRF_RETAIN);
139 }
140 vrf_terminate();
141
142 zns = zebra_ns_lookup(NS_DEFAULT);
143 zebra_ns_disable(0, (void **)&zns);
144
145 access_list_reset();
146 prefix_list_reset();
147 route_map_finish();
03951374 148
affe9e99 149 list_delete_and_null(&zebrad.client_list);
d62a17ae 150 work_queue_free(zebrad.ribq);
151 if (zebrad.lsp_process_q)
152 work_queue_free(zebrad.lsp_process_q);
153 meta_queue_free(zebrad.mq);
d62a17ae 154
03951374 155 frr_fini();
d62a17ae 156 exit(0);
718e3744 157}
158
159/* SIGUSR1 handler. */
d62a17ae 160static void sigusr1(void)
718e3744 161{
d62a17ae 162 zlog_rotate();
718e3744 163}
164
d62a17ae 165struct quagga_signal_t zebra_signals[] = {
166 {
167 .signal = SIGHUP,
168 .handler = &sighup,
169 },
170 {
171 .signal = SIGUSR1,
172 .handler = &sigusr1,
173 },
174 {
175 .signal = SIGINT,
176 .handler = &sigint,
177 },
178 {
179 .signal = SIGTERM,
180 .handler = &sigint,
181 },
2d75d052 182};
b72ede27 183
d62a17ae 184FRR_DAEMON_INFO(
185 zebra, ZEBRA, .vty_port = ZEBRA_VTY_PORT, .flags = FRR_NO_ZCLIENT,
4f04a76b 186
d62a17ae 187 .proghelp =
188 "Daemon which manages kernel routing table management "
4f04a76b
DL
189 "and\nredistribution between different routing protocols.",
190
d62a17ae 191 .signals = zebra_signals, .n_signals = array_size(zebra_signals),
4f04a76b 192
d62a17ae 193 .privs = &zserv_privs, )
4f04a76b 194
718e3744 195/* Main startup routine. */
d62a17ae 196int main(int argc, char **argv)
718e3744 197{
d62a17ae 198 // int batch_mode = 0;
199 char *zserv_path = NULL;
200 /* Socket to external label manager */
201 char *lblmgr_path = NULL;
689f5a8c
DL
202 struct sockaddr_storage dummy;
203 socklen_t dummylen;
411314ed
DS
204#if defined(HANDLE_ZAPI_FUZZING)
205 char *fuzzing = NULL;
206#endif
fea12efb 207
d62a17ae 208 frr_preinit(&zebra_di, argc, argv);
718e3744 209
d62a17ae 210 frr_opt_add(
211 "bakz:e:l:r"
4f04a76b 212#ifdef HAVE_NETLINK
d62a17ae 213 "s:"
411314ed
DS
214#endif
215#if defined(HANDLE_ZAPI_FUZZING)
216 "c:"
c05795b1 217#endif
d62a17ae 218 ,
219 longopts,
220 " -b, --batch Runs in batch mode\n"
221 " -a, --allow_delete Allow other processes to delete zebra routes\n"
222 " -z, --socket Set path of zebra socket\n"
223 " -e, --ecmp Specify ECMP to use.\n"
224 " -l, --label_socket Socket to external label manager\n"
225 " -k, --keep_kernel Don't delete old routes which installed by zebra.\n"
226 " -r, --retain When program terminates, retain added route by zebra.\n"
4f04a76b 227#ifdef HAVE_NETLINK
d62a17ae 228 " -s, --nl-bufsize Set netlink receive buffer size\n"
4f04a76b 229#endif /* HAVE_NETLINK */
411314ed
DS
230#if defined(HANDLE_ZAPI_FUZZING)
231 " -c <file> Bypass normal startup use this file for tetsting of zapi"
232#endif
9d303b37 233 );
d62a17ae 234
235 while (1) {
236 int opt = frr_getopt(argc, argv, NULL);
237
238 if (opt == EOF)
239 break;
240
241 switch (opt) {
242 case 0:
243 break;
244 case 'b':
245 // batch_mode = 1;
246 break;
247 case 'a':
248 allow_delete = 1;
249 break;
250 case 'k':
251 keep_kernel_mode = 1;
252 break;
253 case 'e':
254 multipath_num = atoi(optarg);
255 if (multipath_num > MULTIPATH_NUM
256 || multipath_num <= 0) {
257 zlog_err(
258 "Multipath Number specified must be less than %d and greater than 0",
259 MULTIPATH_NUM);
260 return 1;
261 }
262 break;
263 case 'z':
264 zserv_path = optarg;
689f5a8c
DL
265 if (!frr_zclient_addr(&dummy, &dummylen, optarg)) {
266 fprintf(stderr,
267 "Invalid zserv socket path: %s\n",
268 optarg);
269 exit(1);
270 }
d62a17ae 271 break;
272 case 'l':
273 lblmgr_path = optarg;
274 break;
275 case 'r':
276 retain_mode = 1;
277 break;
c34b6b57 278#ifdef HAVE_NETLINK
d62a17ae 279 case 's':
280 nl_rcvbufsize = atoi(optarg);
281 break;
c34b6b57 282#endif /* HAVE_NETLINK */
411314ed
DS
283#if defined(HANDLE_ZAPI_FUZZING)
284 case 'c':
285 fuzzing = optarg;
286 break;
287#endif
d62a17ae 288 default:
289 frr_help_exit(1);
290 break;
291 }
718e3744 292 }
d62a17ae 293
294 vty_config_lockless();
295 zebrad.master = frr_init();
296
297 /* Zebra related initialize. */
5f145fb8 298 zserv_init();
d62a17ae 299 rib_init();
300 zebra_if_init();
301 zebra_debug_init();
302 router_id_cmd_init();
d62a17ae 303 access_list_init();
304 prefix_list_init();
305#if defined(HAVE_RTADV)
306 rtadv_cmd_init();
36735ed9 307#endif
d62a17ae 308/* PTM socket */
244c1cdc 309#ifdef ZEBRA_PTM_SUPPORT
d62a17ae 310 zebra_ptm_init();
244c1cdc 311#endif
718e3744 312
d62a17ae 313 zebra_mpls_init();
314 zebra_mpls_vty_init();
2dd0d726 315 zebra_pw_vty_init();
7758e3f3 316
d62a17ae 317 /* For debug purpose. */
318 /* SET_FLAG (zebra_debug_event, ZEBRA_DEBUG_EVENT); */
718e3744 319
d62a17ae 320 /* Initialize NS( and implicitly the VRF module), and make kernel
321 * routing socket. */
322 zebra_ns_init();
718e3744 323
523cafc4 324 /*
325 * Initialize show/config command after the vrf initialization is
326 * complete
327 */
b7cfce93
MK
328 zebra_vty_init();
329
411314ed
DS
330#if defined(HANDLE_ZAPI_FUZZING)
331 if (fuzzing) {
332 zserv_read_file(fuzzing);
333 exit(0);
334 }
335#endif
336
d62a17ae 337 /* Process the configuration file. Among other configuration
9d303b37
DL
338 * directives we can meet those installing static routes. Such
339 * requests will not be executed immediately, but queued in
340 * zebra->ribq structure until we enter the main execution loop.
341 * The notifications from kernel will show originating PID equal
342 * to that after daemon() completes (if ever called).
343 */
d62a17ae 344 frr_config_fork();
718e3744 345
d62a17ae 346 /* Clean up rib -- before fork (?) */
347 /* rib_weed_tables (); */
718e3744 348
d62a17ae 349 /* After we have successfully acquired the pidfile, we can be sure
9d303b37
DL
350 * about being the only copy of zebra process, which is submitting
351 * changes to the FIB.
352 * Clean up zebra-originated routes. The requests will be sent to OS
353 * immediately, so originating PID in notifications from kernel
354 * will be equal to the current getpid(). To know about such routes,
355 * we have to have route_read() called before.
356 */
d62a17ae 357 if (!keep_kernel_mode)
358 rib_sweep_route();
91b7351d 359
d62a17ae 360 /* Needed for BSD routing socket. */
361 pid = getpid();
718e3744 362
d62a17ae 363 /* This must be done only after locking pidfile (bug #403). */
364 zebra_zserv_socket_init(zserv_path);
97be79f9 365
d62a17ae 366 /* Init label manager */
367 label_manager_init(lblmgr_path);
fea12efb 368
d62a17ae 369 frr_run(zebrad.master);
718e3744 370
d62a17ae 371 /* Not reached... */
372 return 0;
718e3744 373}