]> git.proxmox.com Git - mirror_frr.git/blame - zebra/main.c
Merge pull request #226 from donaldsharp/evpn_fix
[mirror_frr.git] / zebra / main.c
CommitLineData
edd7c245 1/* zebra daemon main routine.
718e3744 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
5e4fa164 24#include <lib/version.h>
718e3744 25#include "getopt.h"
26#include "command.h"
27#include "thread.h"
28#include "filter.h"
29#include "memory.h"
4a1ab8e4 30#include "zebra_memory.h"
fc7948fa 31#include "memory_vty.h"
718e3744 32#include "prefix.h"
33#include "log.h"
7514fb77 34#include "plist.h"
edd7c245 35#include "privs.h"
2d75d052 36#include "sigevent.h"
b72ede27 37#include "vrf.h"
ff1c42fb 38#include "sockopt.h"
718e3744 39
40#include "zebra/rib.h"
41#include "zebra/zserv.h"
42#include "zebra/debug.h"
18a6dce6 43#include "zebra/router-id.h"
ca776988 44#include "zebra/irdp.h"
a1ac18c4 45#include "zebra/rtadv.h"
5adc2528 46#include "zebra/zebra_fpm.h"
244c1cdc 47#include "zebra/zebra_ptm.h"
fe18ee2d 48#include "zebra/zebra_ns.h"
e2b1be64 49#include "zebra/redistribute.h"
7758e3f3 50#include "zebra/zebra_mpls.h"
244c1cdc
DS
51
52#define ZEBRA_PTM_SUPPORT
718e3744 53
b21b19c5 54/* Zebra instance */
55struct zebra_t zebrad =
56{
57 .rtm_table_default = 0,
58};
718e3744 59
60/* process id. */
718e3744 61pid_t pid;
62
48f8480d
MW
63/* VTY Socket prefix */
64char vty_sock_path[MAXPATHLEN] = ZEBRA_VTYSH_PATH;
65
55c72803 66/* Pacify zclient.o in libfrr, which expects this variable. */
87efd646 67struct thread_master *master;
68
718e3744 69/* Route retain mode flag. */
70int retain_mode = 0;
71
6baf7bb8
DS
72/* Allow non-quagga entities to delete quagga routes */
73int allow_delete = 0;
74
718e3744 75/* Don't delete kernel route. */
76int keep_kernel_mode = 0;
77
c34b6b57 78#ifdef HAVE_NETLINK
79/* Receive buffer size for netlink socket */
b6286c70 80u_int32_t nl_rcvbufsize = 4194304;
c34b6b57 81#endif /* HAVE_NETLINK */
82
718e3744 83/* Command line options. */
48f8480d 84#define OPTION_VTYSOCK 1000
718e3744 85struct option longopts[] =
86{
6baf7bb8
DS
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'},
fb0aa886 91 { "fpm_format", required_argument, NULL, 'F'},
6baf7bb8
DS
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'},
48f8480d 98 { "vty_socket", required_argument, NULL, OPTION_VTYSOCK },
6baf7bb8
DS
99 { "retain", no_argument, NULL, 'r'},
100 { "dryrun", no_argument, NULL, 'C'},
c34b6b57 101#ifdef HAVE_NETLINK
6baf7bb8 102 { "nl-bufsize", required_argument, NULL, 's'},
c34b6b57 103#endif /* HAVE_NETLINK */
6baf7bb8
DS
104 { "user", required_argument, NULL, 'u'},
105 { "group", required_argument, NULL, 'g'},
106 { "version", no_argument, NULL, 'v'},
718e3744 107 { 0 }
108};
109
edd7c245 110zebra_capabilities_t _caps_p [] =
111{
ceacedba 112 ZCAP_NET_ADMIN,
edd7c245 113 ZCAP_SYS_ADMIN,
ceacedba 114 ZCAP_NET_RAW,
edd7c245 115};
116
117/* zebra privileges to run with */
118struct zebra_privs_t zserv_privs =
119{
b2f36157
DL
120#if defined(FRR_USER) && defined(FRR_GROUP)
121 .user = FRR_USER,
122 .group = FRR_GROUP,
edd7c245 123#endif
124#ifdef VTY_GROUP
125 .vty_group = VTY_GROUP,
126#endif
127 .caps_p = _caps_p,
837d16cc 128 .cap_num_p = array_size(_caps_p),
edd7c245 129 .cap_num_i = 0
130};
131
718e3744 132/* Default configuration file path. */
718e3744 133char config_default[] = SYSCONFDIR DEFAULT_CONFIG_FILE;
134
135/* Process ID saved for use by init system */
fce954f8 136const char *pid_file = PATH_ZEBRA_PID;
718e3744 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 {
c34b6b57 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"\
6baf7bb8 151 "-a, --allow_delete Allow other processes to delete Quagga Routes\n" \
c34b6b57 152 "-f, --config_file Set configuration file name\n"\
fb0aa886 153 "-F, --fpm_format Set fpm format to 'netlink' or 'protobuf'\n"\
c34b6b57 154 "-i, --pid_file Set process identifier file name\n"\
b5114685 155 "-z, --socket Set path of zebra socket\n"\
c34b6b57 156 "-k, --keep_kernel Don't delete old routes which installed by "\
157 "zebra.\n"\
876b8be0 158 "-C, --dryrun Check configuration for validity and exit\n"\
c34b6b57 159 "-A, --vty_addr Set vty's bind address\n"\
160 "-P, --vty_port Set vty's port number\n"\
48f8480d 161 " --vty_socket Override vty socket path\n"\
c34b6b57 162 "-r, --retain When program terminates, retain added route "\
163 "by zebra.\n"\
c065230a 164 "-u, --user User to run as\n"\
19380819 165 "-g, --group Group to run as\n", progname);
c34b6b57 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"\
b2f36157 172 "Report bugs to %s\n", FRR_BUG_ADDRESS);
718e3744 173 }
174
175 exit (status);
176}
6b0655a2 177
718e3744 178/* SIGHUP handler. */
a1ac18c4 179static void
2d75d052 180sighup (void)
718e3744 181{
182 zlog_info ("SIGHUP received");
183
184 /* Reload of config file. */
185 ;
186}
187
188/* SIGINT handler. */
a1ac18c4 189static void
2d75d052 190sigint (void)
718e3744 191{
5a8dfcd8
RW
192 struct vrf *vrf;
193 struct zebra_vrf *zvrf;
fe18ee2d
DS
194 struct zebra_ns *zns;
195
887c44a4 196 zlog_notice ("Terminating on signal");
718e3744 197
ca776988 198#ifdef HAVE_IRDP
199 irdp_finish();
200#endif
718e3744 201
c8ed14dd 202 zebra_ptm_finish();
5a8dfcd8
RW
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 ();
fe18ee2d
DS
213
214 zns = zebra_ns_lookup (NS_DEFAULT);
215 zebra_ns_disable (0, (void **)&zns);
58ac32e2
RW
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);
5a8dfcd8
RW
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);
58ac32e2
RW
228 thread_master_free (zebrad.master);
229 if (zlog_default)
230 closezlog (zlog_default);
231
718e3744 232 exit (0);
233}
234
235/* SIGUSR1 handler. */
a1ac18c4 236static void
2d75d052 237sigusr1 (void)
718e3744 238{
239 zlog_rotate (NULL);
240}
241
2d75d052 242struct quagga_signal_t zebra_signals[] =
718e3744 243{
2d75d052 244 {
245 .signal = SIGHUP,
246 .handler = &sighup,
247 },
248 {
249 .signal = SIGUSR1,
250 .handler = &sigusr1,
251 },
252 {
253 .signal = SIGINT,
8c903fbb 254 .handler = &sigint,
2d75d052 255 },
f571dab0 256 {
257 .signal = SIGTERM,
258 .handler = &sigint,
259 },
2d75d052 260};
b72ede27 261
718e3744 262/* Main startup routine. */
263int
264main (int argc, char **argv)
265{
266 char *p;
267 char *vty_addr = NULL;
4fc4e7ab 268 int vty_port = ZEBRA_VTY_PORT;
876b8be0 269 int dryrun = 0;
718e3744 270 int batch_mode = 0;
271 int daemon_mode = 0;
272 char *config_file = NULL;
273 char *progname;
274 struct thread thread;
b5114685 275 char *zserv_path = NULL;
fb0aa886 276 char *fpm_format = NULL;
718e3744 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
7c8ff89e 284 zlog_default = openzlog (progname, ZLOG_ZEBRA, 0,
718e3744 285 LOG_CONS|LOG_NDELAY|LOG_PID, LOG_DAEMON);
2caa9b39 286 zprivs_init (&zserv_privs);
c05795b1
SK
287#if defined(HAVE_CUMULUS)
288 zlog_set_level (NULL, ZLOG_DEST_SYSLOG, zlog_default->default_lvl);
289#endif
718e3744 290
291 while (1)
292 {
293 int opt;
294
c34b6b57 295#ifdef HAVE_NETLINK
fb0aa886 296 opt = getopt_long (argc, argv, "bdakf:F:i:z:hA:P:ru:g:vs:C", longopts, 0);
c34b6b57 297#else
fb0aa886 298 opt = getopt_long (argc, argv, "bdakf:F:i:z:hA:P:ru:g:vC", longopts, 0);
c34b6b57 299#endif /* HAVE_NETLINK */
718e3744 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;
6baf7bb8
DS
313 case 'a':
314 allow_delete = 1;
315 break;
718e3744 316 case 'k':
317 keep_kernel_mode = 1;
318 break;
876b8be0
PJ
319 case 'C':
320 dryrun = 1;
321 break;
718e3744 322 case 'f':
323 config_file = optarg;
324 break;
fb0aa886
AS
325 case 'F':
326 fpm_format = optarg;
327 break;
718e3744 328 case 'A':
329 vty_addr = optarg;
330 break;
331 case 'i':
332 pid_file = optarg;
333 break;
b5114685
VT
334 case 'z':
335 zserv_path = optarg;
336 break;
718e3744 337 case 'P':
4fc4e7ab 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 }
718e3744 345 vty_port = atoi (optarg);
0d6b2ee2
PJ
346 if (vty_port <= 0 || vty_port > 0xffff)
347 vty_port = ZEBRA_VTY_PORT;
718e3744 348 break;
48f8480d
MW
349 case OPTION_VTYSOCK:
350 set_socket_path(vty_sock_path, ZEBRA_VTYSH_PATH, optarg, sizeof (vty_sock_path));
351 break;
718e3744 352 case 'r':
353 retain_mode = 1;
354 break;
c34b6b57 355#ifdef HAVE_NETLINK
356 case 's':
357 nl_rcvbufsize = atoi (optarg);
358 break;
359#endif /* HAVE_NETLINK */
c065230a 360 case 'u':
361 zserv_privs.user = optarg;
362 break;
363 case 'g':
364 zserv_privs.group = optarg;
365 break;
718e3744 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. */
b21b19c5 380 zebrad.master = thread_master_create ();
718e3744 381
382 /* Vty related initialize. */
837d16cc 383 signal_init (zebrad.master, array_size(zebra_signals), zebra_signals);
718e3744 384 cmd_init (1);
3ddccf18 385 vty_config_lockless ();
b21b19c5 386 vty_init (zebrad.master);
718e3744 387 memory_init ();
388
389 /* Zebra related initialize. */
390 zebra_init ();
391 rib_init ();
392 zebra_if_init ();
393 zebra_debug_init ();
c6ffe645 394 router_id_cmd_init ();
718e3744 395 zebra_vty_init ();
396 access_list_init ();
7514fb77 397 prefix_list_init ();
8da4e946 398#if defined (HAVE_RTADV)
cd80d74f 399 rtadv_cmd_init ();
36735ed9 400#endif
ca776988 401#ifdef HAVE_IRDP
402 irdp_init();
403#endif
244c1cdc
DS
404 /* PTM socket */
405#ifdef ZEBRA_PTM_SUPPORT
406 zebra_ptm_init();
407#endif
718e3744 408
7758e3f3 409 zebra_mpls_init ();
fe6c7157 410 zebra_mpls_vty_init ();
7758e3f3 411
718e3744 412 /* For debug purpose. */
413 /* SET_FLAG (zebra_debug_event, ZEBRA_DEBUG_EVENT); */
414
fe18ee2d
DS
415 /* Initialize NS( and implicitly the VRF module), and make kernel routing socket. */
416 zebra_ns_init ();
718e3744 417
718e3744 418#ifdef HAVE_SNMP
419 zebra_snmp_init ();
420#endif /* HAVE_SNMP */
421
5adc2528 422#ifdef HAVE_FPM
fb0aa886 423 zfpm_init (zebrad.master, 1, 0, fpm_format);
5adc2528 424#else
fb0aa886 425 zfpm_init (zebrad.master, 0, 0, fpm_format);
5adc2528
AS
426#endif
427
91b7351d
DO
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 */
320ec10a 435 vty_read_config (config_file, config_default);
718e3744 436
876b8be0
PJ
437 /* Don't start execution if we are in dry-run mode */
438 if (dryrun)
439 return(0);
440
718e3744 441 /* Clean up rib. */
7a4bb9c5 442 /* rib_weed_tables (); */
718e3744 443
444 /* Exit when zebra is working in batch mode. */
445 if (batch_mode)
446 exit (0);
447
718e3744 448 /* Daemonize. */
065de903
SH
449 if (daemon_mode && daemon (0, 0) < 0)
450 {
451 zlog_err("Zebra daemon failed: %s", strerror(errno));
452 exit (1);
453 }
718e3744 454
455 /* Output pid of zebra. */
456 pid_output (pid_file);
457
91b7351d
DO
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
718e3744 469 /* Needed for BSD routing socket. */
470 pid = getpid ();
471
97be79f9 472 /* This must be done only after locking pidfile (bug #403). */
b5114685 473 zebra_zserv_socket_init (zserv_path);
97be79f9 474
718e3744 475 /* Make vty server socket. */
48f8480d 476 vty_serv_sock (vty_addr, vty_port, vty_sock_path);
718e3744 477
887c44a4 478 /* Print banner. */
b2f36157 479 zlog_notice ("Zebra %s starting: vty@%d", FRR_VERSION, vty_port);
887c44a4 480
b21b19c5 481 while (thread_fetch (zebrad.master, &thread))
718e3744 482 thread_call (&thread);
483
484 /* Not reached... */
e8e1946e 485 return 0;
718e3744 486}