]> git.proxmox.com Git - mirror_frr.git/blame_incremental - zebra/main.c
Merge pull request #153 from LabNConsulting/working/2.0/patch-set/rr-part2
[mirror_frr.git] / zebra / main.c
... / ...
CommitLineData
1/* zebra daemon main routine.
2 * Copyright (C) 1997, 98 Kunihiro Ishiguro
3 *
4 * This file is part of GNU Zebra.
5 *
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
9 * later version.
10 *
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.
15 *
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
19 * 02111-1307, USA.
20 */
21
22#include <zebra.h>
23
24#include <lib/version.h>
25#include "getopt.h"
26#include "command.h"
27#include "thread.h"
28#include "filter.h"
29#include "memory.h"
30#include "zebra_memory.h"
31#include "memory_vty.h"
32#include "prefix.h"
33#include "log.h"
34#include "plist.h"
35#include "privs.h"
36#include "sigevent.h"
37#include "vrf.h"
38#include "sockopt.h"
39
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"
51
52#define ZEBRA_PTM_SUPPORT
53
54/* Zebra instance */
55struct zebra_t zebrad =
56{
57 .rtm_table_default = 0,
58};
59
60/* process id. */
61pid_t pid;
62
63/* VTY Socket prefix */
64char vty_sock_path[MAXPATHLEN] = ZEBRA_VTYSH_PATH;
65
66/* Pacify zclient.o in libzebra, which expects this variable. */
67struct thread_master *master;
68
69/* Route retain mode flag. */
70int retain_mode = 0;
71
72/* Allow non-quagga entities to delete quagga routes */
73int allow_delete = 0;
74
75/* Don't delete kernel route. */
76int keep_kernel_mode = 0;
77
78#ifdef HAVE_NETLINK
79/* Receive buffer size for netlink socket */
80u_int32_t nl_rcvbufsize = 4194304;
81#endif /* HAVE_NETLINK */
82
83/* Command line options. */
84#define OPTION_VTYSOCK 1000
85struct option longopts[] =
86{
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'},
101#ifdef HAVE_NETLINK
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'},
107 { 0 }
108};
109
110zebra_capabilities_t _caps_p [] =
111{
112 ZCAP_NET_ADMIN,
113 ZCAP_SYS_ADMIN,
114 ZCAP_NET_RAW,
115};
116
117/* zebra privileges to run with */
118struct zebra_privs_t zserv_privs =
119{
120#if defined(FRR_USER) && defined(FRR_GROUP)
121 .user = FRR_USER,
122 .group = FRR_GROUP,
123#endif
124#ifdef VTY_GROUP
125 .vty_group = VTY_GROUP,
126#endif
127 .caps_p = _caps_p,
128 .cap_num_p = array_size(_caps_p),
129 .cap_num_i = 0
130};
131
132/* Default configuration file path. */
133char config_default[] = SYSCONFDIR DEFAULT_CONFIG_FILE;
134
135/* Process ID saved for use by init system */
136const char *pid_file = PATH_ZEBRA_PID;
137
138/* Help information display. */
139static void
140usage (char *progname, int status)
141{
142 if (status != 0)
143 fprintf (stderr, "Try `%s --help' for more information.\n", progname);
144 else
145 {
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 "\
157 "zebra.\n"\
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 "\
163 "by zebra.\n"\
164 "-u, --user User to run as\n"\
165 "-g, --group Group to run as\n", progname);
166#ifdef HAVE_NETLINK
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"\
171 "\n"\
172 "Report bugs to %s\n", FRR_BUG_ADDRESS);
173 }
174
175 exit (status);
176}
177
178/* SIGHUP handler. */
179static void
180sighup (void)
181{
182 zlog_info ("SIGHUP received");
183
184 /* Reload of config file. */
185 ;
186}
187
188/* SIGINT handler. */
189static void
190sigint (void)
191{
192 struct vrf *vrf;
193 struct zebra_vrf *zvrf;
194 struct zebra_ns *zns;
195
196 zlog_notice ("Terminating on signal");
197
198#ifdef HAVE_IRDP
199 irdp_finish();
200#endif
201
202 zebra_ptm_finish();
203 list_delete_all_node (zebrad.client_list);
204
205 if (retain_mode)
206 RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name)
207 {
208 zvrf = vrf->info;
209 if (zvrf)
210 SET_FLAG (zvrf->flags, ZEBRA_VRF_RETAIN);
211 }
212 vrf_terminate ();
213
214 zns = zebra_ns_lookup (NS_DEFAULT);
215 zebra_ns_disable (0, (void **)&zns);
216
217 access_list_reset ();
218 prefix_list_reset ();
219 route_map_finish ();
220 cmd_terminate ();
221 vty_terminate ();
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);
229 if (zlog_default)
230 closezlog (zlog_default);
231
232 exit (0);
233}
234
235/* SIGUSR1 handler. */
236static void
237sigusr1 (void)
238{
239 zlog_rotate (NULL);
240}
241
242struct quagga_signal_t zebra_signals[] =
243{
244 {
245 .signal = SIGHUP,
246 .handler = &sighup,
247 },
248 {
249 .signal = SIGUSR1,
250 .handler = &sigusr1,
251 },
252 {
253 .signal = SIGINT,
254 .handler = &sigint,
255 },
256 {
257 .signal = SIGTERM,
258 .handler = &sigint,
259 },
260};
261
262/* Main startup routine. */
263int
264main (int argc, char **argv)
265{
266 char *p;
267 char *vty_addr = NULL;
268 int vty_port = ZEBRA_VTY_PORT;
269 int dryrun = 0;
270 int batch_mode = 0;
271 int daemon_mode = 0;
272 char *config_file = NULL;
273 char *progname;
274 struct thread thread;
275 char *zserv_path = NULL;
276 char *fpm_format = NULL;
277
278 /* Set umask before anything for security */
279 umask (0027);
280
281 /* preserve my name */
282 progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]);
283
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);
289#endif
290
291 while (1)
292 {
293 int opt;
294
295#ifdef HAVE_NETLINK
296 opt = getopt_long (argc, argv, "bdakf:F:i:z:hA:P:ru:g:vs:C", longopts, 0);
297#else
298 opt = getopt_long (argc, argv, "bdakf:F:i:z:hA:P:ru:g:vC", longopts, 0);
299#endif /* HAVE_NETLINK */
300
301 if (opt == EOF)
302 break;
303
304 switch (opt)
305 {
306 case 0:
307 break;
308 case 'b':
309 batch_mode = 1;
310 case 'd':
311 daemon_mode = 1;
312 break;
313 case 'a':
314 allow_delete = 1;
315 break;
316 case 'k':
317 keep_kernel_mode = 1;
318 break;
319 case 'C':
320 dryrun = 1;
321 break;
322 case 'f':
323 config_file = optarg;
324 break;
325 case 'F':
326 fpm_format = optarg;
327 break;
328 case 'A':
329 vty_addr = optarg;
330 break;
331 case 'i':
332 pid_file = optarg;
333 break;
334 case 'z':
335 zserv_path = optarg;
336 break;
337 case 'P':
338 /* Deal with atoi() returning 0 on failure, and zebra not
339 listening on zebra port... */
340 if (strcmp(optarg, "0") == 0)
341 {
342 vty_port = 0;
343 break;
344 }
345 vty_port = atoi (optarg);
346 if (vty_port <= 0 || vty_port > 0xffff)
347 vty_port = ZEBRA_VTY_PORT;
348 break;
349 case OPTION_VTYSOCK:
350 set_socket_path(vty_sock_path, ZEBRA_VTYSH_PATH, optarg, sizeof (vty_sock_path));
351 break;
352 case 'r':
353 retain_mode = 1;
354 break;
355#ifdef HAVE_NETLINK
356 case 's':
357 nl_rcvbufsize = atoi (optarg);
358 break;
359#endif /* HAVE_NETLINK */
360 case 'u':
361 zserv_privs.user = optarg;
362 break;
363 case 'g':
364 zserv_privs.group = optarg;
365 break;
366 case 'v':
367 print_version (progname);
368 exit (0);
369 break;
370 case 'h':
371 usage (progname, 0);
372 break;
373 default:
374 usage (progname, 1);
375 break;
376 }
377 }
378
379 /* Make master thread emulator. */
380 zebrad.master = thread_master_create ();
381
382 /* Vty related initialize. */
383 signal_init (zebrad.master, array_size(zebra_signals), zebra_signals);
384 cmd_init (1);
385 vty_config_lockless ();
386 vty_init (zebrad.master);
387 memory_init ();
388
389 /* Zebra related initialize. */
390 zebra_init ();
391 rib_init ();
392 zebra_if_init ();
393 zebra_debug_init ();
394 router_id_cmd_init ();
395 zebra_vty_init ();
396 access_list_init ();
397 prefix_list_init ();
398#if defined (HAVE_RTADV)
399 rtadv_cmd_init ();
400#endif
401#ifdef HAVE_IRDP
402 irdp_init();
403#endif
404 /* PTM socket */
405#ifdef ZEBRA_PTM_SUPPORT
406 zebra_ptm_init();
407#endif
408
409 zebra_mpls_init ();
410 zebra_mpls_vty_init ();
411
412 /* For debug purpose. */
413 /* SET_FLAG (zebra_debug_event, ZEBRA_DEBUG_EVENT); */
414
415 /* Initialize NS( and implicitly the VRF module), and make kernel routing socket. */
416 zebra_ns_init ();
417
418#ifdef HAVE_SNMP
419 zebra_snmp_init ();
420#endif /* HAVE_SNMP */
421
422#ifdef HAVE_FPM
423 zfpm_init (zebrad.master, 1, 0, fpm_format);
424#else
425 zfpm_init (zebrad.master, 0, 0, fpm_format);
426#endif
427
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).
434 */
435 vty_read_config (config_file, config_default);
436
437 /* Don't start execution if we are in dry-run mode */
438 if (dryrun)
439 return(0);
440
441 /* Clean up rib. */
442 /* rib_weed_tables (); */
443
444 /* Exit when zebra is working in batch mode. */
445 if (batch_mode)
446 exit (0);
447
448 /* Daemonize. */
449 if (daemon_mode && daemon (0, 0) < 0)
450 {
451 zlog_err("Zebra daemon failed: %s", strerror(errno));
452 exit (1);
453 }
454
455 /* Output pid of zebra. */
456 pid_output (pid_file);
457
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.
465 */
466 if (! keep_kernel_mode)
467 rib_sweep_route ();
468
469 /* Needed for BSD routing socket. */
470 pid = getpid ();
471
472 /* This must be done only after locking pidfile (bug #403). */
473 zebra_zserv_socket_init (zserv_path);
474
475 /* Make vty server socket. */
476 vty_serv_sock (vty_addr, vty_port, vty_sock_path);
477
478 /* Print banner. */
479 zlog_notice ("Zebra %s starting: vty@%d", FRR_VERSION, vty_port);
480
481 while (thread_fetch (zebrad.master, &thread))
482 thread_call (&thread);
483
484 /* Not reached... */
485 return 0;
486}