blob: 17014ea7312b633374cbabd293906279e4d4f550 [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
Feng Lu267ceb22015-05-22 11:40:09 +0200215/* Callback upon enabling a VRF. */
216static int
217zebra_vrf_enable (vrf_id_t vrf_id, void **info)
218{
219 struct zebra_vrf *zvrf = (struct zebra_vrf *) (*info);
220
221 assert (zvrf);
222
223 return 0;
224}
225
226/* Callback upon disabling a VRF. */
227static int
228zebra_vrf_disable (vrf_id_t vrf_id, void **info)
229{
230 struct zebra_vrf *zvrf = (struct zebra_vrf *) (*info);
231 struct listnode *list_node;
232 struct interface *ifp;
233
234 assert (zvrf);
235
236 rib_close_table (zvrf->table[AFI_IP][SAFI_UNICAST]);
237 rib_close_table (zvrf->table[AFI_IP6][SAFI_UNICAST]);
238
239 for (ALL_LIST_ELEMENTS_RO (vrf_iflist (vrf_id), list_node, ifp))
240 {
241 int operative = if_is_operative (ifp);
242 UNSET_FLAG (ifp->flags, IFF_UP);
243 if (operative)
244 if_down (ifp);
245 }
246
247 return 0;
248}
249
Feng Lu41f44a22015-05-22 11:39:56 +0200250/* Zebra VRF initialization. */
251static void
252zebra_vrf_init (void)
253{
254 vrf_add_hook (VRF_NEW_HOOK, zebra_vrf_new);
Feng Lu267ceb22015-05-22 11:40:09 +0200255 vrf_add_hook (VRF_ENABLE_HOOK, zebra_vrf_enable);
256 vrf_add_hook (VRF_DISABLE_HOOK, zebra_vrf_disable);
Feng Lu41f44a22015-05-22 11:39:56 +0200257 vrf_init ();
258}
259
Paul Jakma457eb9a2006-07-27 19:59:58 +0000260/* Main startup routine. */
261int
262main (int argc, char **argv)
263{
264 char *p;
265 char *vty_addr = NULL;
266 int vty_port = 0;
267 int batch_mode = 0;
268 int daemon_mode = 0;
269 char *config_file = NULL;
270 char *progname;
271 struct thread thread;
272
273 /* Set umask before anything for security */
274 umask (0027);
275
276 /* preserve my name */
277 progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]);
278
279 zlog_default = openzlog (progname, ZLOG_ZEBRA,
280 LOG_CONS|LOG_NDELAY|LOG_PID, LOG_DAEMON);
281
282 while (1)
283 {
284 int opt;
285
Jeremy Jacksonc77cffd2008-12-28 12:57:42 -0500286 opt = getopt_long (argc, argv, "bdf:hA:P:r:v", longopts, 0);
Paul Jakma457eb9a2006-07-27 19:59:58 +0000287
288 if (opt == EOF)
289 break;
290
291 switch (opt)
292 {
293 case 0:
294 break;
295 case 'b':
296 batch_mode = 1;
297 case 'd':
298 daemon_mode = 1;
299 break;
Paul Jakma457eb9a2006-07-27 19:59:58 +0000300 case 'f':
301 config_file = optarg;
302 break;
303 case 'A':
304 vty_addr = optarg;
305 break;
306 case 'P':
307 /* Deal with atoi() returning 0 on failure, and zebra not
308 listening on zebra port... */
309 if (strcmp(optarg, "0") == 0)
310 {
311 vty_port = 0;
312 break;
313 }
314 vty_port = atoi (optarg);
315 break;
316 case 'r':
317 rib_process_hold_time = atoi(optarg);
318 break;
319 case 'v':
320 print_version (progname);
321 exit (0);
322 break;
323 case 'h':
324 usage (progname, 0);
325 break;
326 default:
327 usage (progname, 1);
328 break;
329 }
330 }
331
332 /* port and conf file mandatory */
333 if (!vty_port || !config_file)
Paul Jakma302d53f2009-08-06 11:48:48 +0100334 {
335 fprintf (stderr, "Error: --vty_port and --config_file arguments"
336 " are both required\n");
337 usage (progname, 1);
338 }
Paul Jakma457eb9a2006-07-27 19:59:58 +0000339
340 /* Make master thread emulator. */
341 zebrad.master = thread_master_create ();
342
343 /* Vty related initialize. */
Balaji.G837d16c2012-09-26 14:09:10 +0530344 signal_init (zebrad.master, array_size(zebra_signals), zebra_signals);
Paul Jakma457eb9a2006-07-27 19:59:58 +0000345 cmd_init (1);
346 vty_init (zebrad.master);
347 memory_init ();
Paul Jakma457eb9a2006-07-27 19:59:58 +0000348 zebra_debug_init ();
349 zebra_if_init ();
350 test_cmd_init ();
351
352 /* Zebra related initialize. */
353 rib_init ();
354 access_list_init ();
355
356 /* Make kernel routing socket. */
Feng Lu41f44a22015-05-22 11:39:56 +0200357 zebra_vrf_init ();
Paul Jakma457eb9a2006-07-27 19:59:58 +0000358 kernel_init ();
359 route_read ();
360 zebra_vty_init();
361
Paul Jakma457eb9a2006-07-27 19:59:58 +0000362 /* Configuration file read*/
363 vty_read_config (config_file, config_default);
364
365 /* Clean up rib. */
366 rib_weed_tables ();
367
368 /* Exit when zebra is working in batch mode. */
369 if (batch_mode)
370 exit (0);
371
Paul Jakma457eb9a2006-07-27 19:59:58 +0000372 /* Daemonize. */
Stephen Hemminger065de902009-08-07 11:13:49 -0700373 if (daemon_mode && daemon (0, 0) < 0)
374 {
375 perror("daemon start failed");
376 exit (1);
377 }
Paul Jakma457eb9a2006-07-27 19:59:58 +0000378
379 /* Needed for BSD routing socket. */
380 pid = getpid ();
381
382 /* Make vty server socket. */
383 vty_serv_sock (vty_addr, vty_port, "/tmp/test_zebra");
384
385 /* Print banner. */
386 zlog_notice ("Zebra %s starting: vty@%d", QUAGGA_VERSION, vty_port);
387
388 while (thread_fetch (zebrad.master, &thread))
389 thread_call (&thread);
390
391 /* Not reached... */
392 return 0;
393}