]> git.proxmox.com Git - mirror_frr.git/blob - zebra/main.c
*: add frr_run()
[mirror_frr.git] / zebra / main.c
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 "libfrr.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 */
55 struct zebra_t zebrad =
56 {
57 .rtm_table_default = 0,
58 };
59
60 /* process id. */
61 pid_t pid;
62
63 /* Pacify zclient.o in libfrr, which expects this variable. */
64 struct thread_master *master;
65
66 /* Route retain mode flag. */
67 int retain_mode = 0;
68
69 /* Allow non-quagga entities to delete quagga routes */
70 int allow_delete = 0;
71
72 /* Don't delete kernel route. */
73 int keep_kernel_mode = 0;
74
75 #ifdef HAVE_NETLINK
76 /* Receive buffer size for netlink socket */
77 u_int32_t nl_rcvbufsize = 4194304;
78 #endif /* HAVE_NETLINK */
79
80 /* Command line options. */
81 struct option longopts[] =
82 {
83 { "batch", no_argument, NULL, 'b'},
84 { "allow_delete", no_argument, NULL, 'a'},
85 { "keep_kernel", no_argument, NULL, 'k'},
86 { "fpm_format", required_argument, NULL, 'F'},
87 { "socket", required_argument, NULL, 'z'},
88 { "ecmp", required_argument, NULL, 'e'},
89 { "retain", no_argument, NULL, 'r'},
90 #ifdef HAVE_NETLINK
91 { "nl-bufsize", required_argument, NULL, 's'},
92 #endif /* HAVE_NETLINK */
93 { 0 }
94 };
95
96 zebra_capabilities_t _caps_p [] =
97 {
98 ZCAP_NET_ADMIN,
99 ZCAP_SYS_ADMIN,
100 ZCAP_NET_RAW,
101 };
102
103 /* zebra privileges to run with */
104 struct zebra_privs_t zserv_privs =
105 {
106 #if defined(FRR_USER) && defined(FRR_GROUP)
107 .user = FRR_USER,
108 .group = FRR_GROUP,
109 #endif
110 #ifdef VTY_GROUP
111 .vty_group = VTY_GROUP,
112 #endif
113 .caps_p = _caps_p,
114 .cap_num_p = array_size(_caps_p),
115 .cap_num_i = 0
116 };
117
118 unsigned int multipath_num = MULTIPATH_NUM;
119
120 /* SIGHUP handler. */
121 static void
122 sighup (void)
123 {
124 zlog_info ("SIGHUP received");
125
126 /* Reload of config file. */
127 ;
128 }
129
130 /* SIGINT handler. */
131 static void
132 sigint (void)
133 {
134 struct vrf *vrf;
135 struct zebra_vrf *zvrf;
136 struct zebra_ns *zns;
137
138 zlog_notice ("Terminating on signal");
139
140 #ifdef HAVE_IRDP
141 irdp_finish();
142 #endif
143
144 zebra_ptm_finish();
145 list_delete_all_node (zebrad.client_list);
146
147 if (retain_mode)
148 RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name)
149 {
150 zvrf = vrf->info;
151 if (zvrf)
152 SET_FLAG (zvrf->flags, ZEBRA_VRF_RETAIN);
153 }
154 vrf_terminate ();
155
156 zns = zebra_ns_lookup (NS_DEFAULT);
157 zebra_ns_disable (0, (void **)&zns);
158
159 access_list_reset ();
160 prefix_list_reset ();
161 route_map_finish ();
162 cmd_terminate ();
163 vty_terminate ();
164 zprivs_terminate (&zserv_privs);
165 list_delete (zebrad.client_list);
166 work_queue_free (zebrad.ribq);
167 if (zebrad.lsp_process_q)
168 work_queue_free (zebrad.lsp_process_q);
169 meta_queue_free (zebrad.mq);
170 thread_master_free (zebrad.master);
171 if (zlog_default)
172 closezlog (zlog_default);
173
174 exit (0);
175 }
176
177 /* SIGUSR1 handler. */
178 static void
179 sigusr1 (void)
180 {
181 zlog_rotate (NULL);
182 }
183
184 struct quagga_signal_t zebra_signals[] =
185 {
186 {
187 .signal = SIGHUP,
188 .handler = &sighup,
189 },
190 {
191 .signal = SIGUSR1,
192 .handler = &sigusr1,
193 },
194 {
195 .signal = SIGINT,
196 .handler = &sigint,
197 },
198 {
199 .signal = SIGTERM,
200 .handler = &sigint,
201 },
202 };
203
204 FRR_DAEMON_INFO(zebra, ZEBRA,
205 .vty_port = ZEBRA_VTY_PORT,
206 .flags = FRR_NO_ZCLIENT,
207
208 .proghelp = "Daemon which manages kernel routing table management "
209 "and\nredistribution between different routing protocols.",
210
211 .signals = zebra_signals,
212 .n_signals = array_size(zebra_signals),
213
214 .privs = &zserv_privs,
215 )
216
217 /* Main startup routine. */
218 int
219 main (int argc, char **argv)
220 {
221 // int batch_mode = 0;
222 char *zserv_path = NULL;
223 char *fpm_format = NULL;
224
225 frr_preinit(&zebra_di, argc, argv);
226
227 frr_opt_add("bakF:z:r"
228 #ifdef HAVE_NETLINK
229 "s:"
230 #endif
231 , longopts,
232 "-b, --batch Runs in batch mode\n"
233 "-a, --allow_delete Allow other processes to delete Quagga Routes\n"
234 "-F, --fpm_format Set fpm format to 'netlink' or 'protobuf'\n"
235 "-z, --socket Set path of zebra socket\n"
236 "-k, --keep_kernel Don't delete old routes which installed by zebra.\n"
237 "-r, --retain When program terminates, retain added route by zebra.\n"
238 #ifdef HAVE_NETLINK
239 "-s, --nl-bufsize Set netlink receive buffer size\n"
240 #endif /* HAVE_NETLINK */
241 );
242
243 while (1)
244 {
245 int opt = frr_getopt(argc, argv, NULL);
246
247 if (opt == EOF)
248 break;
249
250 switch (opt)
251 {
252 case 0:
253 break;
254 case 'b':
255 // batch_mode = 1;
256 break;
257 case 'a':
258 allow_delete = 1;
259 break;
260 case 'k':
261 keep_kernel_mode = 1;
262 break;
263 case 'F':
264 fpm_format = optarg;
265 break;
266 case 'e':
267 multipath_num = atoi (optarg);
268 if (multipath_num > MULTIPATH_NUM || multipath_num <= 0)
269 {
270 zlog_err ("Multipath Number specified must be less than %d and greater than 0", MULTIPATH_NUM);
271 return 1;
272 }
273 break;
274 case 'z':
275 zserv_path = optarg;
276 break;
277 case 'r':
278 retain_mode = 1;
279 break;
280 #ifdef HAVE_NETLINK
281 case 's':
282 nl_rcvbufsize = atoi (optarg);
283 break;
284 #endif /* HAVE_NETLINK */
285 default:
286 frr_help_exit (1);
287 break;
288 }
289 }
290
291 vty_config_lockless ();
292 zebrad.master = frr_init();
293
294 /* Zebra related initialize. */
295 zebra_init ();
296 rib_init ();
297 zebra_if_init ();
298 zebra_debug_init ();
299 router_id_cmd_init ();
300 zebra_vty_init ();
301 access_list_init ();
302 prefix_list_init ();
303 #if defined (HAVE_RTADV)
304 rtadv_cmd_init ();
305 #endif
306 #ifdef HAVE_IRDP
307 irdp_init();
308 #endif
309 /* PTM socket */
310 #ifdef ZEBRA_PTM_SUPPORT
311 zebra_ptm_init();
312 #endif
313
314 zebra_mpls_init ();
315 zebra_mpls_vty_init ();
316
317 /* For debug purpose. */
318 /* SET_FLAG (zebra_debug_event, ZEBRA_DEBUG_EVENT); */
319
320 /* Initialize NS( and implicitly the VRF module), and make kernel routing socket. */
321 zebra_ns_init ();
322
323 #ifdef HAVE_SNMP
324 zebra_snmp_init ();
325 #endif /* HAVE_SNMP */
326
327 #ifdef HAVE_FPM
328 zfpm_init (zebrad.master, 1, 0, fpm_format);
329 #else
330 zfpm_init (zebrad.master, 0, 0, fpm_format);
331 #endif
332
333 /* Process the configuration file. Among other configuration
334 * directives we can meet those installing static routes. Such
335 * requests will not be executed immediately, but queued in
336 * zebra->ribq structure until we enter the main execution loop.
337 * The notifications from kernel will show originating PID equal
338 * to that after daemon() completes (if ever called).
339 */
340 frr_config_fork();
341
342 /* Clean up rib -- before fork (?) */
343 /* rib_weed_tables (); */
344
345 /* After we have successfully acquired the pidfile, we can be sure
346 * about being the only copy of zebra process, which is submitting
347 * changes to the FIB.
348 * Clean up zebra-originated routes. The requests will be sent to OS
349 * immediately, so originating PID in notifications from kernel
350 * will be equal to the current getpid(). To know about such routes,
351 * we have to have route_read() called before.
352 */
353 if (! keep_kernel_mode)
354 rib_sweep_route ();
355
356 /* Needed for BSD routing socket. */
357 pid = getpid ();
358
359 /* This must be done only after locking pidfile (bug #403). */
360 zebra_zserv_socket_init (zserv_path);
361
362 frr_run (zebrad.master);
363
364 /* Not reached... */
365 return 0;
366 }