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
17 * along with GNU Zebra; see the file COPYING. If not, write to the Free
18 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
24 #include <lib/version.h>
30 #include "zebra_memory.h"
31 #include "memory_vty.h"
39 #include "zebra/rib.h"
40 #include "zebra/zserv.h"
41 #include "zebra/debug.h"
42 #include "zebra/router-id.h"
43 #include "zebra/irdp.h"
44 #include "zebra/rtadv.h"
45 #include "zebra/zebra_fpm.h"
46 #include "zebra/zebra_ptm.h"
47 #include "zebra/zebra_ns.h"
48 #include "zebra/redistribute.h"
49 #include "zebra/zebra_mpls.h"
51 #define ZEBRA_PTM_SUPPORT
54 struct zebra_t zebrad
=
56 .rtm_table_default
= 0,
62 /* Pacify zclient.o in libzebra, which expects this variable. */
63 struct thread_master
*master
;
65 /* Route retain mode flag. */
68 /* Allow non-quagga entities to delete quagga routes */
71 /* Don't delete kernel route. */
72 int keep_kernel_mode
= 0;
75 /* Receive buffer size for netlink socket */
76 u_int32_t nl_rcvbufsize
= 4194304;
77 #endif /* HAVE_NETLINK */
79 /* Command line options. */
80 struct option longopts
[] =
82 { "batch", no_argument
, NULL
, 'b'},
83 { "daemon", no_argument
, NULL
, 'd'},
84 { "allow_delete", no_argument
, NULL
, 'a'},
85 { "keep_kernel", no_argument
, NULL
, 'k'},
86 { "fpm_format", required_argument
, NULL
, 'F'},
87 { "config_file", required_argument
, NULL
, 'f'},
88 { "pid_file", required_argument
, NULL
, 'i'},
89 { "socket", required_argument
, NULL
, 'z'},
90 { "help", no_argument
, NULL
, 'h'},
91 { "vty_addr", required_argument
, NULL
, 'A'},
92 { "vty_port", required_argument
, NULL
, 'P'},
93 { "retain", no_argument
, NULL
, 'r'},
94 { "dryrun", no_argument
, NULL
, 'C'},
96 { "nl-bufsize", required_argument
, NULL
, 's'},
97 #endif /* HAVE_NETLINK */
98 { "user", required_argument
, NULL
, 'u'},
99 { "group", required_argument
, NULL
, 'g'},
100 { "version", no_argument
, NULL
, 'v'},
104 zebra_capabilities_t _caps_p
[] =
111 /* zebra privileges to run with */
112 struct zebra_privs_t zserv_privs
=
114 #if defined(FRR_USER) && defined(FRR_GROUP)
119 .vty_group
= VTY_GROUP
,
122 .cap_num_p
= array_size(_caps_p
),
126 /* Default configuration file path. */
127 char config_default
[] = SYSCONFDIR DEFAULT_CONFIG_FILE
;
129 /* Process ID saved for use by init system */
130 const char *pid_file
= PATH_ZEBRA_PID
;
132 /* Help information display. */
134 usage (char *progname
, int status
)
137 fprintf (stderr
, "Try `%s --help' for more information.\n", progname
);
140 printf ("Usage : %s [OPTION...]\n\n"\
141 "Daemon which manages kernel routing table management and "\
142 "redistribution between different routing protocols.\n\n"\
143 "-b, --batch Runs in batch mode\n"\
144 "-d, --daemon Runs in daemon mode\n"\
145 "-a, --allow_delete Allow other processes to delete Quagga Routes\n" \
146 "-f, --config_file Set configuration file name\n"\
147 "-F, --fpm_format Set fpm format to 'netlink' or 'protobuf'\n"\
148 "-i, --pid_file Set process identifier file name\n"\
149 "-z, --socket Set path of zebra socket\n"\
150 "-k, --keep_kernel Don't delete old routes which installed by "\
152 "-C, --dryrun Check configuration for validity and exit\n"\
153 "-A, --vty_addr Set vty's bind address\n"\
154 "-P, --vty_port Set vty's port number\n"\
155 "-r, --retain When program terminates, retain added route "\
157 "-u, --user User to run as\n"\
158 "-g, --group Group to run as\n", progname
);
160 printf ("-s, --nl-bufsize Set netlink receive buffer size\n");
161 #endif /* HAVE_NETLINK */
162 printf ("-v, --version Print program version\n"\
163 "-h, --help Display this help and exit\n"\
165 "Report bugs to %s\n", FRR_BUG_ADDRESS
);
171 /* SIGHUP handler. */
175 zlog_info ("SIGHUP received");
177 /* Reload of config file. */
181 /* SIGINT handler. */
186 struct zebra_vrf
*zvrf
;
187 struct zebra_ns
*zns
;
189 zlog_notice ("Terminating on signal");
196 list_delete_all_node (zebrad
.client_list
);
199 RB_FOREACH (vrf
, vrf_name_head
, &vrfs_by_name
)
203 SET_FLAG (zvrf
->flags
, ZEBRA_VRF_RETAIN
);
207 zns
= zebra_ns_lookup (NS_DEFAULT
);
208 zebra_ns_disable (0, (void **)&zns
);
210 access_list_reset ();
211 prefix_list_reset ();
215 zprivs_terminate (&zserv_privs
);
216 list_delete (zebrad
.client_list
);
217 work_queue_free (zebrad
.ribq
);
218 if (zebrad
.lsp_process_q
)
219 work_queue_free (zebrad
.lsp_process_q
);
220 meta_queue_free (zebrad
.mq
);
221 thread_master_free (zebrad
.master
);
223 closezlog (zlog_default
);
228 /* SIGUSR1 handler. */
235 struct quagga_signal_t zebra_signals
[] =
255 /* Main startup routine. */
257 main (int argc
, char **argv
)
260 char *vty_addr
= NULL
;
261 int vty_port
= ZEBRA_VTY_PORT
;
265 char *config_file
= NULL
;
267 struct thread thread
;
268 char *zserv_path
= NULL
;
269 char *fpm_format
= NULL
;
271 /* Set umask before anything for security */
274 /* preserve my name */
275 progname
= ((p
= strrchr (argv
[0], '/')) ? ++p
: argv
[0]);
277 zlog_default
= openzlog (progname
, ZLOG_ZEBRA
, 0,
278 LOG_CONS
|LOG_NDELAY
|LOG_PID
, LOG_DAEMON
);
279 zprivs_init (&zserv_privs
);
280 #if defined(HAVE_CUMULUS)
281 zlog_set_level (NULL
, ZLOG_DEST_SYSLOG
, zlog_default
->default_lvl
);
289 opt
= getopt_long (argc
, argv
, "bdakf:F:i:z:hA:P:ru:g:vs:C", longopts
, 0);
291 opt
= getopt_long (argc
, argv
, "bdakf:F:i:z:hA:P:ru:g:vC", longopts
, 0);
292 #endif /* HAVE_NETLINK */
310 keep_kernel_mode
= 1;
316 config_file
= optarg
;
331 /* Deal with atoi() returning 0 on failure, and zebra not
332 listening on zebra port... */
333 if (strcmp(optarg
, "0") == 0)
338 vty_port
= atoi (optarg
);
339 if (vty_port
<= 0 || vty_port
> 0xffff)
340 vty_port
= ZEBRA_VTY_PORT
;
347 nl_rcvbufsize
= atoi (optarg
);
349 #endif /* HAVE_NETLINK */
351 zserv_privs
.user
= optarg
;
354 zserv_privs
.group
= optarg
;
357 print_version (progname
);
369 /* Make master thread emulator. */
370 zebrad
.master
= thread_master_create ();
372 /* Vty related initialize. */
373 signal_init (zebrad
.master
, array_size(zebra_signals
), zebra_signals
);
375 vty_config_lockless ();
376 vty_init (zebrad
.master
);
379 /* Zebra related initialize. */
384 router_id_cmd_init ();
388 #if defined (HAVE_RTADV)
395 #ifdef ZEBRA_PTM_SUPPORT
400 zebra_mpls_vty_init ();
402 /* For debug purpose. */
403 /* SET_FLAG (zebra_debug_event, ZEBRA_DEBUG_EVENT); */
405 /* Initialize NS( and implicitly the VRF module), and make kernel routing socket. */
410 #endif /* HAVE_SNMP */
413 zfpm_init (zebrad
.master
, 1, 0, fpm_format
);
415 zfpm_init (zebrad
.master
, 0, 0, fpm_format
);
418 /* Process the configuration file. Among other configuration
419 * directives we can meet those installing static routes. Such
420 * requests will not be executed immediately, but queued in
421 * zebra->ribq structure until we enter the main execution loop.
422 * The notifications from kernel will show originating PID equal
423 * to that after daemon() completes (if ever called).
425 vty_read_config (config_file
, config_default
);
427 /* Don't start execution if we are in dry-run mode */
432 /* rib_weed_tables (); */
434 /* Exit when zebra is working in batch mode. */
439 if (daemon_mode
&& daemon (0, 0) < 0)
441 zlog_err("Zebra daemon failed: %s", strerror(errno
));
445 /* Output pid of zebra. */
446 pid_output (pid_file
);
448 /* After we have successfully acquired the pidfile, we can be sure
449 * about being the only copy of zebra process, which is submitting
450 * changes to the FIB.
451 * Clean up zebra-originated routes. The requests will be sent to OS
452 * immediately, so originating PID in notifications from kernel
453 * will be equal to the current getpid(). To know about such routes,
454 * we have to have route_read() called before.
456 if (! keep_kernel_mode
)
459 /* Needed for BSD routing socket. */
462 /* This must be done only after locking pidfile (bug #403). */
463 zebra_zserv_socket_init (zserv_path
);
465 /* Make vty server socket. */
466 vty_serv_sock (vty_addr
, vty_port
, ZEBRA_VTYSH_PATH
);
469 zlog_notice ("Zebra %s starting: vty@%d", FRR_VERSION
, vty_port
);
471 while (thread_fetch (zebrad
.master
, &thread
))
472 thread_call (&thread
);