blob: a92cd61896512bb41f4ca7498cc16cf2cbfaf3de [file] [log] [blame]
Paul Jakma457eb9a2006-07-27 19:59:58 +00001/* main routine.
2 * Copyright (C) 1997, 98 Kunihiro Ishiguro
3 *
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
7 * later version.
8 *
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.
13 *
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
17 * 02111-1307, USA.
18 */
19
20#include <zebra.h>
21
22#include <lib/version.h>
23#include "getopt.h"
24#include "command.h"
25#include "thread.h"
26#include "filter.h"
27#include "memory.h"
28#include "prefix.h"
29#include "log.h"
30#include "privs.h"
31#include "sigevent.h"
Feng Lu41f44a22015-05-22 11:39:56 +020032#include "vrf.h"
Paul Jakma457eb9a2006-07-27 19:59:58 +000033
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"
39
40/* Zebra instance */
41struct zebra_t zebrad =
42{
43 .rtm_table_default = 0,
44};
45
46/* process id. */
Paul Jakma457eb9a2006-07-27 19:59:58 +000047pid_t pid;
48
49/* zebra_rib's workqueue hold time. Private export for use by test code only */
50extern int rib_process_hold_time;
51
52/* Pacify zclient.o in libzebra, which expects this variable. */
53struct thread_master *master;
54
55/* Command line options. */
56struct option longopts[] =
57{
58 { "batch", no_argument, NULL, 'b'},
59 { "daemon", no_argument, NULL, 'd'},
Paul Jakma457eb9a2006-07-27 19:59:58 +000060 { "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'},
66 { 0 }
67};
68
69zebra_capabilities_t _caps_p [] =
70{
71 ZCAP_NET_ADMIN,
72 ZCAP_SYS_ADMIN,
73 ZCAP_NET_RAW,
74};
75
76/* Default configuration file path. */
77char config_default[] = SYSCONFDIR DEFAULT_CONFIG_FILE;
78
79/* Process ID saved for use by init system */
80const char *pid_file = PATH_ZEBRA_PID;
81
82/* Help information display. */
83static void
84usage (char *progname, int status)
85{
86 if (status != 0)
87 fprintf (stderr, "Try `%s --help' for more information.\n", progname);
88 else
89 {
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"\
Paul Jakma457eb9a2006-07-27 19:59:58 +000096 "-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"\
101 "\n"\
102 "Report bugs to %s\n", progname, ZEBRA_BUG_ADDRESS);
103 }
104
105 exit (status);
106}
David Lamparter6b0655a2014-06-04 06:53:35 +0200107
Paul Jakma457eb9a2006-07-27 19:59:58 +0000108static unsigned int test_ifindex = 0;
109
110/* testrib commands */
111DEFUN (test_interface_state,
112 test_interface_state_cmd,
113 "state (up|down)",
114 "configure interface\n"
115 "up\n"
116 "down\n")
117{
118 struct interface *ifp;
119 if (argc < 1)
120 return CMD_WARNING;
121
122 ifp = vty->index;
123 if (ifp->ifindex == IFINDEX_INTERNAL)
124 {
125 ifp->ifindex = ++test_ifindex;
126 ifp->mtu = 1500;
127 ifp->flags = IFF_BROADCAST|IFF_MULTICAST;
128 }
129
130 switch (argv[0][0])
131 {
132 case 'u':
133 SET_FLAG (ifp->flags, IFF_UP);
134 if_add_update (ifp);
135 printf ("up\n");
136 break;
137 case 'd':
138 UNSET_FLAG (ifp->flags, IFF_UP);
139 if_delete_update (ifp);
140 printf ("down\n");
141 break;
142 default:
143 return CMD_WARNING;
144 }
145 return CMD_SUCCESS;
146}
147
148static void
149test_cmd_init (void)
150{
151 install_element (INTERFACE_NODE, &test_interface_state_cmd);
152}
David Lamparter6b0655a2014-06-04 06:53:35 +0200153
Paul Jakma457eb9a2006-07-27 19:59:58 +0000154/* SIGHUP handler. */
155static void
156sighup (void)
157{
158 zlog_info ("SIGHUP received");
159
160 /* Reload of config file. */
161 ;
162}
163
164/* SIGINT handler. */
165static void
166sigint (void)
167{
168 zlog_notice ("Terminating on signal");
169
170 exit (0);
171}
172
173/* SIGUSR1 handler. */
174static void
175sigusr1 (void)
176{
177 zlog_rotate (NULL);
178}
179
180struct quagga_signal_t zebra_signals[] =
181{
182 {
183 .signal = SIGHUP,
184 .handler = &sighup,
185 },
186 {
187 .signal = SIGUSR1,
188 .handler = &sigusr1,
189 },
190 {
191 .signal = SIGINT,
192 .handler = &sigint,
193 },
194 {
195 .signal = SIGTERM,
196 .handler = &sigint,
197 },
198};
David Lamparter6b0655a2014-06-04 06:53:35 +0200199
Feng Lu41f44a22015-05-22 11:39:56 +0200200/* Callback upon creating a new VRF. */
201static int
202zebra_vrf_new (vrf_id_t vrf_id, void **info)
203{
204 struct zebra_vrf *zvrf = *info;
205
206 if (! zvrf)
207 {
208 zvrf = zebra_vrf_alloc (vrf_id);
209 *info = (void *)zvrf;
210 }
211
212 return 0;
213}
214
215/* Zebra VRF initialization. */
216static void
217zebra_vrf_init (void)
218{
219 vrf_add_hook (VRF_NEW_HOOK, zebra_vrf_new);
220 vrf_init ();
221}
222
Paul Jakma457eb9a2006-07-27 19:59:58 +0000223/* Main startup routine. */
224int
225main (int argc, char **argv)
226{
227 char *p;
228 char *vty_addr = NULL;
229 int vty_port = 0;
230 int batch_mode = 0;
231 int daemon_mode = 0;
232 char *config_file = NULL;
233 char *progname;
234 struct thread thread;
235
236 /* Set umask before anything for security */
237 umask (0027);
238
239 /* preserve my name */
240 progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]);
241
242 zlog_default = openzlog (progname, ZLOG_ZEBRA,
243 LOG_CONS|LOG_NDELAY|LOG_PID, LOG_DAEMON);
244
245 while (1)
246 {
247 int opt;
248
Jeremy Jacksonc77cffd2008-12-28 12:57:42 -0500249 opt = getopt_long (argc, argv, "bdf:hA:P:r:v", longopts, 0);
Paul Jakma457eb9a2006-07-27 19:59:58 +0000250
251 if (opt == EOF)
252 break;
253
254 switch (opt)
255 {
256 case 0:
257 break;
258 case 'b':
259 batch_mode = 1;
260 case 'd':
261 daemon_mode = 1;
262 break;
Paul Jakma457eb9a2006-07-27 19:59:58 +0000263 case 'f':
264 config_file = optarg;
265 break;
266 case 'A':
267 vty_addr = optarg;
268 break;
269 case 'P':
270 /* Deal with atoi() returning 0 on failure, and zebra not
271 listening on zebra port... */
272 if (strcmp(optarg, "0") == 0)
273 {
274 vty_port = 0;
275 break;
276 }
277 vty_port = atoi (optarg);
278 break;
279 case 'r':
280 rib_process_hold_time = atoi(optarg);
281 break;
282 case 'v':
283 print_version (progname);
284 exit (0);
285 break;
286 case 'h':
287 usage (progname, 0);
288 break;
289 default:
290 usage (progname, 1);
291 break;
292 }
293 }
294
295 /* port and conf file mandatory */
296 if (!vty_port || !config_file)
Paul Jakma302d53f2009-08-06 11:48:48 +0100297 {
298 fprintf (stderr, "Error: --vty_port and --config_file arguments"
299 " are both required\n");
300 usage (progname, 1);
301 }
Paul Jakma457eb9a2006-07-27 19:59:58 +0000302
303 /* Make master thread emulator. */
304 zebrad.master = thread_master_create ();
305
306 /* Vty related initialize. */
Balaji.G837d16c2012-09-26 14:09:10 +0530307 signal_init (zebrad.master, array_size(zebra_signals), zebra_signals);
Paul Jakma457eb9a2006-07-27 19:59:58 +0000308 cmd_init (1);
309 vty_init (zebrad.master);
310 memory_init ();
311 if_init();
312 zebra_debug_init ();
313 zebra_if_init ();
314 test_cmd_init ();
315
316 /* Zebra related initialize. */
317 rib_init ();
318 access_list_init ();
319
320 /* Make kernel routing socket. */
Feng Lu41f44a22015-05-22 11:39:56 +0200321 zebra_vrf_init ();
Paul Jakma457eb9a2006-07-27 19:59:58 +0000322 kernel_init ();
323 route_read ();
324 zebra_vty_init();
325
Paul Jakma457eb9a2006-07-27 19:59:58 +0000326 /* Configuration file read*/
327 vty_read_config (config_file, config_default);
328
329 /* Clean up rib. */
330 rib_weed_tables ();
331
332 /* Exit when zebra is working in batch mode. */
333 if (batch_mode)
334 exit (0);
335
Paul Jakma457eb9a2006-07-27 19:59:58 +0000336 /* Daemonize. */
Stephen Hemminger065de902009-08-07 11:13:49 -0700337 if (daemon_mode && daemon (0, 0) < 0)
338 {
339 perror("daemon start failed");
340 exit (1);
341 }
Paul Jakma457eb9a2006-07-27 19:59:58 +0000342
343 /* Needed for BSD routing socket. */
344 pid = getpid ();
345
346 /* Make vty server socket. */
347 vty_serv_sock (vty_addr, vty_port, "/tmp/test_zebra");
348
349 /* Print banner. */
350 zlog_notice ("Zebra %s starting: vty@%d", QUAGGA_VERSION, vty_port);
351
352 while (thread_fetch (zebrad.master, &thread))
353 thread_call (&thread);
354
355 /* Not reached... */
356 return 0;
357}