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"
40 #include "zebra/rib.h"
41 #include "zebra/zserv.h"
42 #include "zebra/debug.h"
43 #include "zebra/router-id.h"
44 #include "zebra/irdp.h"
45 #include "zebra/rtadv.h"
46 #include "zebra/zebra_fpm.h"
47 #include "zebra/zebra_ptm.h"
48 #include "zebra/zebra_ns.h"
49 #include "zebra/redistribute.h"
50 #include "zebra/zebra_mpls.h"
52 #define ZEBRA_PTM_SUPPORT
55 struct zebra_t zebrad
=
57 .rtm_table_default
= 0,
63 /* VTY Socket prefix */
64 char vty_sock_path
[MAXPATHLEN
] = ZEBRA_VTYSH_PATH
;
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;
79 /* Receive buffer size for netlink socket */
80 u_int32_t nl_rcvbufsize
= 4194304;
81 #endif /* HAVE_NETLINK */
83 /* Command line options. */
84 #define OPTION_VTYSOCK 1000
85 struct option longopts
[] =
87 { "batch", no_argument
, NULL
, 'b'},
88 { "daemon", no_argument
, NULL
, 'd'},
89 { "allow_delete", no_argument
, NULL
, 'a'},
90 { "keep_kernel", no_argument
, NULL
, 'k'},
91 { "fpm_format", required_argument
, NULL
, 'F'},
92 { "config_file", required_argument
, NULL
, 'f'},
93 { "pid_file", required_argument
, NULL
, 'i'},
94 { "socket", required_argument
, NULL
, 'z'},
95 { "help", no_argument
, NULL
, 'h'},
96 { "vty_addr", required_argument
, NULL
, 'A'},
97 { "vty_port", required_argument
, NULL
, 'P'},
98 { "vty_socket", required_argument
, NULL
, OPTION_VTYSOCK
},
99 { "retain", no_argument
, NULL
, 'r'},
100 { "dryrun", no_argument
, NULL
, 'C'},
102 { "nl-bufsize", required_argument
, NULL
, 's'},
103 #endif /* HAVE_NETLINK */
104 { "user", required_argument
, NULL
, 'u'},
105 { "group", required_argument
, NULL
, 'g'},
106 { "version", no_argument
, NULL
, 'v'},
110 zebra_capabilities_t _caps_p
[] =
117 /* zebra privileges to run with */
118 struct zebra_privs_t zserv_privs
=
120 #if defined(FRR_USER) && defined(FRR_GROUP)
125 .vty_group
= VTY_GROUP
,
128 .cap_num_p
= array_size(_caps_p
),
132 /* Default configuration file path. */
133 char config_default
[] = SYSCONFDIR DEFAULT_CONFIG_FILE
;
135 /* Process ID saved for use by init system */
136 const char *pid_file
= PATH_ZEBRA_PID
;
138 /* Help information display. */
140 usage (char *progname
, int status
)
143 fprintf (stderr
, "Try `%s --help' for more information.\n", progname
);
146 printf ("Usage : %s [OPTION...]\n\n"\
147 "Daemon which manages kernel routing table management and "\
148 "redistribution between different routing protocols.\n\n"\
149 "-b, --batch Runs in batch mode\n"\
150 "-d, --daemon Runs in daemon mode\n"\
151 "-a, --allow_delete Allow other processes to delete Quagga Routes\n" \
152 "-f, --config_file Set configuration file name\n"\
153 "-F, --fpm_format Set fpm format to 'netlink' or 'protobuf'\n"\
154 "-i, --pid_file Set process identifier file name\n"\
155 "-z, --socket Set path of zebra socket\n"\
156 "-k, --keep_kernel Don't delete old routes which installed by "\
158 "-C, --dryrun Check configuration for validity and exit\n"\
159 "-A, --vty_addr Set vty's bind address\n"\
160 "-P, --vty_port Set vty's port number\n"\
161 " --vty_socket Override vty socket path\n"\
162 "-r, --retain When program terminates, retain added route "\
164 "-u, --user User to run as\n"\
165 "-g, --group Group to run as\n", progname
);
167 printf ("-s, --nl-bufsize Set netlink receive buffer size\n");
168 #endif /* HAVE_NETLINK */
169 printf ("-v, --version Print program version\n"\
170 "-h, --help Display this help and exit\n"\
172 "Report bugs to %s\n", FRR_BUG_ADDRESS
);
178 /* SIGHUP handler. */
182 zlog_info ("SIGHUP received");
184 /* Reload of config file. */
188 /* SIGINT handler. */
193 struct zebra_vrf
*zvrf
;
194 struct zebra_ns
*zns
;
196 zlog_notice ("Terminating on signal");
203 list_delete_all_node (zebrad
.client_list
);
206 RB_FOREACH (vrf
, vrf_name_head
, &vrfs_by_name
)
210 SET_FLAG (zvrf
->flags
, ZEBRA_VRF_RETAIN
);
214 zns
= zebra_ns_lookup (NS_DEFAULT
);
215 zebra_ns_disable (0, (void **)&zns
);
217 access_list_reset ();
218 prefix_list_reset ();
222 zprivs_terminate (&zserv_privs
);
223 list_delete (zebrad
.client_list
);
224 work_queue_free (zebrad
.ribq
);
225 if (zebrad
.lsp_process_q
)
226 work_queue_free (zebrad
.lsp_process_q
);
227 meta_queue_free (zebrad
.mq
);
228 thread_master_free (zebrad
.master
);
230 closezlog (zlog_default
);
235 /* SIGUSR1 handler. */
242 struct quagga_signal_t zebra_signals
[] =
262 /* Main startup routine. */
264 main (int argc
, char **argv
)
267 char *vty_addr
= NULL
;
268 int vty_port
= ZEBRA_VTY_PORT
;
272 char *config_file
= NULL
;
274 struct thread thread
;
275 char *zserv_path
= NULL
;
276 char *fpm_format
= NULL
;
278 /* Set umask before anything for security */
281 /* preserve my name */
282 progname
= ((p
= strrchr (argv
[0], '/')) ? ++p
: argv
[0]);
284 zlog_default
= openzlog (progname
, ZLOG_ZEBRA
, 0,
285 LOG_CONS
|LOG_NDELAY
|LOG_PID
, LOG_DAEMON
);
286 zprivs_init (&zserv_privs
);
287 #if defined(HAVE_CUMULUS)
288 zlog_set_level (NULL
, ZLOG_DEST_SYSLOG
, zlog_default
->default_lvl
);
296 opt
= getopt_long (argc
, argv
, "bdakf:F:i:z:hA:P:ru:g:vs:C", longopts
, 0);
298 opt
= getopt_long (argc
, argv
, "bdakf:F:i:z:hA:P:ru:g:vC", longopts
, 0);
299 #endif /* HAVE_NETLINK */
317 keep_kernel_mode
= 1;
323 config_file
= optarg
;
338 /* Deal with atoi() returning 0 on failure, and zebra not
339 listening on zebra port... */
340 if (strcmp(optarg
, "0") == 0)
345 vty_port
= atoi (optarg
);
346 if (vty_port
<= 0 || vty_port
> 0xffff)
347 vty_port
= ZEBRA_VTY_PORT
;
350 set_socket_path(vty_sock_path
, ZEBRA_VTYSH_PATH
, optarg
, sizeof (vty_sock_path
));
357 nl_rcvbufsize
= atoi (optarg
);
359 #endif /* HAVE_NETLINK */
361 zserv_privs
.user
= optarg
;
364 zserv_privs
.group
= optarg
;
367 print_version (progname
);
379 /* Make master thread emulator. */
380 zebrad
.master
= thread_master_create ();
382 /* Vty related initialize. */
383 signal_init (zebrad
.master
, array_size(zebra_signals
), zebra_signals
);
385 vty_config_lockless ();
386 vty_init (zebrad
.master
);
389 /* Zebra related initialize. */
394 router_id_cmd_init ();
398 #if defined (HAVE_RTADV)
405 #ifdef ZEBRA_PTM_SUPPORT
410 zebra_mpls_vty_init ();
412 /* For debug purpose. */
413 /* SET_FLAG (zebra_debug_event, ZEBRA_DEBUG_EVENT); */
415 /* Initialize NS( and implicitly the VRF module), and make kernel routing socket. */
420 #endif /* HAVE_SNMP */
423 zfpm_init (zebrad
.master
, 1, 0, fpm_format
);
425 zfpm_init (zebrad
.master
, 0, 0, fpm_format
);
428 /* Process the configuration file. Among other configuration
429 * directives we can meet those installing static routes. Such
430 * requests will not be executed immediately, but queued in
431 * zebra->ribq structure until we enter the main execution loop.
432 * The notifications from kernel will show originating PID equal
433 * to that after daemon() completes (if ever called).
435 vty_read_config (config_file
, config_default
);
437 /* Don't start execution if we are in dry-run mode */
442 /* rib_weed_tables (); */
444 /* Exit when zebra is working in batch mode. */
449 if (daemon_mode
&& daemon (0, 0) < 0)
451 zlog_err("Zebra daemon failed: %s", strerror(errno
));
455 /* Output pid of zebra. */
456 pid_output (pid_file
);
458 /* After we have successfully acquired the pidfile, we can be sure
459 * about being the only copy of zebra process, which is submitting
460 * changes to the FIB.
461 * Clean up zebra-originated routes. The requests will be sent to OS
462 * immediately, so originating PID in notifications from kernel
463 * will be equal to the current getpid(). To know about such routes,
464 * we have to have route_read() called before.
466 if (! keep_kernel_mode
)
469 /* Needed for BSD routing socket. */
472 /* This must be done only after locking pidfile (bug #403). */
473 zebra_zserv_socket_init (zserv_path
);
475 /* Make vty server socket. */
476 vty_serv_sock (vty_addr
, vty_port
, vty_sock_path
);
479 zlog_notice ("Zebra %s starting: vty@%d", FRR_VERSION
, vty_port
);
481 while (thread_fetch (zebrad
.master
, &thread
))
482 thread_call (&thread
);