]> git.proxmox.com Git - mirror_frr.git/blob - zebra/main.c
Merge remote-tracking branch 'origin/master' into EIGRP
[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_ptm.h"
47 #include "zebra/zebra_ns.h"
48 #include "zebra/redistribute.h"
49 #include "zebra/zebra_mpls.h"
50 #include "zebra/label_manager.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 { "socket", required_argument, NULL, 'z'},
87 { "ecmp", required_argument, NULL, 'e'},
88 { "label_socket", no_argument, NULL, 'l'},
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 closezlog ();
172
173 exit (0);
174 }
175
176 /* SIGUSR1 handler. */
177 static void
178 sigusr1 (void)
179 {
180 zlog_rotate();
181 }
182
183 struct quagga_signal_t zebra_signals[] =
184 {
185 {
186 .signal = SIGHUP,
187 .handler = &sighup,
188 },
189 {
190 .signal = SIGUSR1,
191 .handler = &sigusr1,
192 },
193 {
194 .signal = SIGINT,
195 .handler = &sigint,
196 },
197 {
198 .signal = SIGTERM,
199 .handler = &sigint,
200 },
201 };
202
203 FRR_DAEMON_INFO(zebra, ZEBRA,
204 .vty_port = ZEBRA_VTY_PORT,
205 .flags = FRR_NO_ZCLIENT,
206
207 .proghelp = "Daemon which manages kernel routing table management "
208 "and\nredistribution between different routing protocols.",
209
210 .signals = zebra_signals,
211 .n_signals = array_size(zebra_signals),
212
213 .privs = &zserv_privs,
214 )
215
216 /* Main startup routine. */
217 int
218 main (int argc, char **argv)
219 {
220 // int batch_mode = 0;
221 char *zserv_path = NULL;
222 /* Socket to external label manager */
223 char *lblmgr_path = NULL;
224
225 frr_preinit(&zebra_di, argc, argv);
226
227 frr_opt_add("bakz:e:l: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 zebra routes\n"
234 " -z, --socket Set path of zebra socket\n"
235 " -e, --ecmp Specify ECMP to use.\n"
236 " -l, --label_socket Socket to external label manager\n"\
237 " -k, --keep_kernel Don't delete old routes which installed by zebra.\n"
238 " -r, --retain When program terminates, retain added route by zebra.\n"
239 #ifdef HAVE_NETLINK
240 " -s, --nl-bufsize Set netlink receive buffer size\n"
241 #endif /* HAVE_NETLINK */
242 );
243
244 while (1)
245 {
246 int opt = frr_getopt(argc, argv, NULL);
247
248 if (opt == EOF)
249 break;
250
251 switch (opt)
252 {
253 case 0:
254 break;
255 case 'b':
256 // batch_mode = 1;
257 break;
258 case 'a':
259 allow_delete = 1;
260 break;
261 case 'k':
262 keep_kernel_mode = 1;
263 break;
264 case 'e':
265 multipath_num = atoi (optarg);
266 if (multipath_num > MULTIPATH_NUM || multipath_num <= 0)
267 {
268 zlog_err ("Multipath Number specified must be less than %d and greater than 0", MULTIPATH_NUM);
269 return 1;
270 }
271 break;
272 case 'z':
273 zserv_path = optarg;
274 break;
275 case 'l':
276 lblmgr_path = optarg;
277 break;
278 case 'r':
279 retain_mode = 1;
280 break;
281 #ifdef HAVE_NETLINK
282 case 's':
283 nl_rcvbufsize = atoi (optarg);
284 break;
285 #endif /* HAVE_NETLINK */
286 default:
287 frr_help_exit (1);
288 break;
289 }
290 }
291
292 vty_config_lockless ();
293 zebrad.master = frr_init();
294
295 /* Zebra related initialize. */
296 zebra_init ();
297 rib_init ();
298 zebra_if_init ();
299 zebra_debug_init ();
300 router_id_cmd_init ();
301 zebra_vty_init ();
302 access_list_init ();
303 prefix_list_init ();
304 #if defined (HAVE_RTADV)
305 rtadv_cmd_init ();
306 #endif
307 #ifdef HAVE_IRDP
308 irdp_init();
309 #endif
310 /* PTM socket */
311 #ifdef ZEBRA_PTM_SUPPORT
312 zebra_ptm_init();
313 #endif
314
315 zebra_mpls_init ();
316 zebra_mpls_vty_init ();
317
318 /* For debug purpose. */
319 /* SET_FLAG (zebra_debug_event, ZEBRA_DEBUG_EVENT); */
320
321 /* Initialize NS( and implicitly the VRF module), and make kernel routing socket. */
322 zebra_ns_init ();
323
324 /* Process the configuration file. Among other configuration
325 * directives we can meet those installing static routes. Such
326 * requests will not be executed immediately, but queued in
327 * zebra->ribq structure until we enter the main execution loop.
328 * The notifications from kernel will show originating PID equal
329 * to that after daemon() completes (if ever called).
330 */
331 frr_config_fork();
332
333 /* Clean up rib -- before fork (?) */
334 /* rib_weed_tables (); */
335
336 /* After we have successfully acquired the pidfile, we can be sure
337 * about being the only copy of zebra process, which is submitting
338 * changes to the FIB.
339 * Clean up zebra-originated routes. The requests will be sent to OS
340 * immediately, so originating PID in notifications from kernel
341 * will be equal to the current getpid(). To know about such routes,
342 * we have to have route_read() called before.
343 */
344 if (! keep_kernel_mode)
345 rib_sweep_route ();
346
347 /* Needed for BSD routing socket. */
348 pid = getpid ();
349
350 /* This must be done only after locking pidfile (bug #403). */
351 zebra_zserv_socket_init (zserv_path);
352
353 /* Init label manager */
354 label_manager_init (lblmgr_path);
355
356 frr_run (zebrad.master);
357
358 /* Not reached... */
359 return 0;
360 }