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 { "ecmp", required_argument
, NULL
, 'e'},
100 { "retain", no_argument
, NULL
, 'r'},
101 { "dryrun", no_argument
, NULL
, 'C'},
103 { "nl-bufsize", required_argument
, NULL
, 's'},
104 #endif /* HAVE_NETLINK */
105 { "user", required_argument
, NULL
, 'u'},
106 { "group", required_argument
, NULL
, 'g'},
107 { "version", no_argument
, NULL
, 'v'},
111 zebra_capabilities_t _caps_p
[] =
118 /* zebra privileges to run with */
119 struct zebra_privs_t zserv_privs
=
121 #if defined(FRR_USER) && defined(FRR_GROUP)
126 .vty_group
= VTY_GROUP
,
129 .cap_num_p
= array_size(_caps_p
),
133 /* Default configuration file path. */
134 char config_default
[] = SYSCONFDIR DEFAULT_CONFIG_FILE
;
136 /* Process ID saved for use by init system */
137 const char *pid_file
= PATH_ZEBRA_PID
;
139 unsigned int multipath_num
= MULTIPATH_NUM
;
141 /* Help information display. */
143 usage (char *progname
, int status
)
146 fprintf (stderr
, "Try `%s --help' for more information.\n", progname
);
149 printf ("Usage : %s [OPTION...]\n\n"\
150 "Daemon which manages kernel routing table management and "\
151 "redistribution between different routing protocols.\n\n"\
152 "-b, --batch Runs in batch mode\n"\
153 "-d, --daemon Runs in daemon mode\n"\
154 "-a, --allow_delete Allow other processes to delete Quagga Routes\n" \
155 "-f, --config_file Set configuration file name\n"\
156 "-F, --fpm_format Set fpm format to 'netlink' or 'protobuf'\n"\
157 "-i, --pid_file Set process identifier file name\n"\
158 "-z, --socket Set path of zebra socket\n"\
159 "-k, --keep_kernel Don't delete old routes which installed by "\
161 "-C, --dryrun Check configuration for validity and exit\n"\
162 "-A, --vty_addr Set vty's bind address\n"\
163 "-P, --vty_port Set vty's port number\n"\
164 " --vty_socket Override vty socket path\n"\
165 "-r, --retain When program terminates, retain added route "\
167 "-u, --user User to run as\n"\
168 "-g, --group Group to run as\n", progname
);
170 printf ("-s, --nl-bufsize Set netlink receive buffer size\n");
171 #endif /* HAVE_NETLINK */
172 printf ("-v, --version Print program version\n"\
173 "-h, --help Display this help and exit\n"\
175 "Report bugs to %s\n", FRR_BUG_ADDRESS
);
181 /* SIGHUP handler. */
185 zlog_info ("SIGHUP received");
187 /* Reload of config file. */
191 /* SIGINT handler. */
196 struct zebra_vrf
*zvrf
;
197 struct zebra_ns
*zns
;
199 zlog_notice ("Terminating on signal");
206 list_delete_all_node (zebrad
.client_list
);
209 RB_FOREACH (vrf
, vrf_name_head
, &vrfs_by_name
)
213 SET_FLAG (zvrf
->flags
, ZEBRA_VRF_RETAIN
);
217 zns
= zebra_ns_lookup (NS_DEFAULT
);
218 zebra_ns_disable (0, (void **)&zns
);
220 access_list_reset ();
221 prefix_list_reset ();
225 zprivs_terminate (&zserv_privs
);
226 list_delete (zebrad
.client_list
);
227 work_queue_free (zebrad
.ribq
);
228 if (zebrad
.lsp_process_q
)
229 work_queue_free (zebrad
.lsp_process_q
);
230 meta_queue_free (zebrad
.mq
);
231 thread_master_free (zebrad
.master
);
233 closezlog (zlog_default
);
238 /* SIGUSR1 handler. */
245 struct quagga_signal_t zebra_signals
[] =
265 /* Main startup routine. */
267 main (int argc
, char **argv
)
270 char *vty_addr
= NULL
;
271 int vty_port
= ZEBRA_VTY_PORT
;
275 char *config_file
= NULL
;
277 struct thread thread
;
278 char *zserv_path
= NULL
;
279 char *fpm_format
= NULL
;
281 /* Set umask before anything for security */
284 /* preserve my name */
285 progname
= ((p
= strrchr (argv
[0], '/')) ? ++p
: argv
[0]);
287 zlog_default
= openzlog (progname
, ZLOG_ZEBRA
, 0,
288 LOG_CONS
|LOG_NDELAY
|LOG_PID
, LOG_DAEMON
);
289 zprivs_init (&zserv_privs
);
290 #if defined(HAVE_CUMULUS)
291 zlog_set_level (NULL
, ZLOG_DEST_SYSLOG
, zlog_default
->default_lvl
);
299 opt
= getopt_long (argc
, argv
, "bdakf:F:i:z:hA:P:ru:g:vs:C", longopts
, 0);
301 opt
= getopt_long (argc
, argv
, "bdakf:F:i:z:hA:P:ru:g:vC", longopts
, 0);
302 #endif /* HAVE_NETLINK */
320 keep_kernel_mode
= 1;
326 config_file
= optarg
;
335 multipath_num
= atoi (optarg
);
336 if (multipath_num
> MULTIPATH_NUM
|| multipath_num
<= 0)
338 zlog_err ("Multipath Number specified must be less than %d and greater than 0", MULTIPATH_NUM
);
349 /* Deal with atoi() returning 0 on failure, and zebra not
350 listening on zebra port... */
351 if (strcmp(optarg
, "0") == 0)
356 vty_port
= atoi (optarg
);
357 if (vty_port
<= 0 || vty_port
> 0xffff)
358 vty_port
= ZEBRA_VTY_PORT
;
361 set_socket_path(vty_sock_path
, ZEBRA_VTYSH_PATH
, optarg
, sizeof (vty_sock_path
));
368 nl_rcvbufsize
= atoi (optarg
);
370 #endif /* HAVE_NETLINK */
372 zserv_privs
.user
= optarg
;
375 zserv_privs
.group
= optarg
;
378 print_version (progname
);
390 /* Make master thread emulator. */
391 zebrad
.master
= thread_master_create ();
393 /* Vty related initialize. */
394 signal_init (zebrad
.master
, array_size(zebra_signals
), zebra_signals
);
396 vty_config_lockless ();
397 vty_init (zebrad
.master
);
400 /* Zebra related initialize. */
405 router_id_cmd_init ();
409 #if defined (HAVE_RTADV)
416 #ifdef ZEBRA_PTM_SUPPORT
421 zebra_mpls_vty_init ();
423 /* For debug purpose. */
424 /* SET_FLAG (zebra_debug_event, ZEBRA_DEBUG_EVENT); */
426 /* Initialize NS( and implicitly the VRF module), and make kernel routing socket. */
431 #endif /* HAVE_SNMP */
434 zfpm_init (zebrad
.master
, 1, 0, fpm_format
);
436 zfpm_init (zebrad
.master
, 0, 0, fpm_format
);
439 /* Process the configuration file. Among other configuration
440 * directives we can meet those installing static routes. Such
441 * requests will not be executed immediately, but queued in
442 * zebra->ribq structure until we enter the main execution loop.
443 * The notifications from kernel will show originating PID equal
444 * to that after daemon() completes (if ever called).
446 vty_read_config (config_file
, config_default
);
448 /* Don't start execution if we are in dry-run mode */
453 /* rib_weed_tables (); */
455 /* Exit when zebra is working in batch mode. */
460 if (daemon_mode
&& daemon (0, 0) < 0)
462 zlog_err("Zebra daemon failed: %s", strerror(errno
));
466 /* Output pid of zebra. */
467 pid_output (pid_file
);
469 /* After we have successfully acquired the pidfile, we can be sure
470 * about being the only copy of zebra process, which is submitting
471 * changes to the FIB.
472 * Clean up zebra-originated routes. The requests will be sent to OS
473 * immediately, so originating PID in notifications from kernel
474 * will be equal to the current getpid(). To know about such routes,
475 * we have to have route_read() called before.
477 if (! keep_kernel_mode
)
480 /* Needed for BSD routing socket. */
483 /* This must be done only after locking pidfile (bug #403). */
484 zebra_zserv_socket_init (zserv_path
);
486 /* Make vty server socket. */
487 vty_serv_sock (vty_addr
, vty_port
, vty_sock_path
);
490 zlog_notice ("Zebra %s starting: vty@%d", FRR_VERSION
, vty_port
);
492 while (thread_fetch (zebrad
.master
, &thread
))
493 thread_call (&thread
);