2 * Copyright (C) 1997, 98 Kunihiro Ishiguro
4 * GNU Zebra is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2, or (at your option) any
9 * GNU Zebra is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with GNU Zebra; see the file COPYING. If not, write to the Free
16 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
22 #include <lib/version.h>
34 #include "zebra/rib.h"
35 #include "zebra/zserv.h"
36 #include "zebra/debug.h"
37 #include "zebra/router-id.h"
38 #include "zebra/interface.h"
41 struct zebra_t zebrad =
43 .rtm_table_default = 0,
49 /* zebra_rib's workqueue hold time. Private export for use by test code only */
50 extern int rib_process_hold_time;
52 /* Pacify zclient.o in libzebra, which expects this variable. */
53 struct thread_master *master;
55 /* Command line options. */
56 struct option longopts[] =
58 { "batch", no_argument, NULL, 'b'},
59 { "daemon", no_argument, NULL, 'd'},
60 { "config_file", required_argument, NULL, 'f'},
61 { "help", no_argument, NULL, 'h'},
62 { "vty_addr", required_argument, NULL, 'A'},
63 { "vty_port", required_argument, NULL, 'P'},
64 { "version", no_argument, NULL, 'v'},
65 { "rib_hold", required_argument, NULL, 'r'},
69 zebra_capabilities_t _caps_p [] =
76 /* Default configuration file path. */
77 char config_default[] = SYSCONFDIR DEFAULT_CONFIG_FILE;
79 /* Process ID saved for use by init system */
80 const char *pid_file = PATH_ZEBRA_PID;
82 /* Help information display. */
84 usage (char *progname, int status)
87 fprintf (stderr, "Try `%s --help' for more information.\n", progname);
90 printf ("Usage : %s [OPTION...]\n\n"\
91 "Daemon which manages kernel routing table management and "\
92 "redistribution between different routing protocols.\n\n"\
93 "-b, --batch Runs in batch mode\n"\
94 "-d, --daemon Runs in daemon mode\n"\
95 "-f, --config_file Set configuration file name\n"\
96 "-A, --vty_addr Set vty's bind address\n"\
97 "-P, --vty_port Set vty's port number\n"\
98 "-r, --rib_hold Set rib-queue hold time\n"\
99 "-v, --version Print program version\n"\
100 "-h, --help Display this help and exit\n"\
102 "Report bugs to %s\n", progname, ZEBRA_BUG_ADDRESS);
108 static ifindex_t test_ifindex = 0;
110 /* testrib commands */
111 DEFUN (test_interface_state,
112 test_interface_state_cmd,
114 "configure interface\n"
118 struct interface *ifp;
123 if (ifp->ifindex == IFINDEX_INTERNAL)
125 ifp->ifindex = ++test_ifindex;
127 ifp->flags = IFF_BROADCAST|IFF_MULTICAST;
133 SET_FLAG (ifp->flags, IFF_UP);
138 UNSET_FLAG (ifp->flags, IFF_UP);
139 if_delete_update (ifp);
151 install_element (INTERFACE_NODE, &test_interface_state_cmd);
154 /* SIGHUP handler. */
158 zlog_info ("SIGHUP received");
160 /* Reload of config file. */
164 /* SIGINT handler. */
168 zlog_notice ("Terminating on signal");
173 /* SIGUSR1 handler. */
180 struct quagga_signal_t zebra_signals[] =
200 /* Callback upon creating a new VRF. */
202 zebra_vrf_new (vrf_id_t vrf_id, void **info)
204 struct zebra_vrf *zvrf = *info;
208 zvrf = zebra_vrf_alloc (vrf_id);
209 *info = (void *)zvrf;
215 /* Callback upon enabling a VRF. */
217 zebra_vrf_enable (vrf_id_t vrf_id, void **info)
219 struct zebra_vrf *zvrf = (struct zebra_vrf *) (*info);
229 /* Callback upon disabling a VRF. */
231 zebra_vrf_disable (vrf_id_t vrf_id, void **info)
233 struct zebra_vrf *zvrf = (struct zebra_vrf *) (*info);
234 struct listnode *list_node;
235 struct interface *ifp;
239 rib_close_table (zvrf->table[AFI_IP][SAFI_UNICAST]);
240 rib_close_table (zvrf->table[AFI_IP6][SAFI_UNICAST]);
242 for (ALL_LIST_ELEMENTS_RO (vrf_iflist (vrf_id), list_node, ifp))
244 int operative = if_is_operative (ifp);
245 UNSET_FLAG (ifp->flags, IFF_UP);
250 kernel_terminate (zvrf);
255 /* Zebra VRF initialization. */
257 zebra_vrf_init (void)
259 vrf_add_hook (VRF_NEW_HOOK, zebra_vrf_new);
260 vrf_add_hook (VRF_ENABLE_HOOK, zebra_vrf_enable);
261 vrf_add_hook (VRF_DISABLE_HOOK, zebra_vrf_disable);
265 /* Main startup routine. */
267 main (int argc, char **argv)
270 char *vty_addr = NULL;
274 char *config_file = NULL;
276 struct thread thread;
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,
285 LOG_CONS|LOG_NDELAY|LOG_PID, LOG_DAEMON);
291 opt = getopt_long (argc, argv, "bdf:hA:P:r:v", longopts, 0);
306 config_file = optarg;
312 /* Deal with atoi() returning 0 on failure, and zebra not
313 listening on zebra port... */
314 if (strcmp(optarg, "0") == 0)
319 vty_port = atoi (optarg);
322 rib_process_hold_time = atoi(optarg);
325 print_version (progname);
337 /* port and conf file mandatory */
338 if (!vty_port || !config_file)
340 fprintf (stderr, "Error: --vty_port and --config_file arguments"
341 " are both required\n");
345 /* Make master thread emulator. */
346 zebrad.master = thread_master_create ();
348 /* Vty related initialize. */
349 signal_init (zebrad.master, array_size(zebra_signals), zebra_signals);
351 vty_init (zebrad.master);
357 /* Zebra related initialize. */
361 /* Make kernel routing socket. */
365 /* Configuration file read*/
366 vty_read_config (config_file, config_default);
371 /* Exit when zebra is working in batch mode. */
376 if (daemon_mode && daemon (0, 0) < 0)
378 perror("daemon start failed");
382 /* Needed for BSD routing socket. */
385 /* Make vty server socket. */
386 vty_serv_sock (vty_addr, vty_port, "/tmp/test_zebra");
389 zlog_notice ("Zebra %s starting: vty@%d", QUAGGA_VERSION, vty_port);
391 while (thread_fetch (zebrad.master, &thread))
392 thread_call (&thread);