]> git.proxmox.com Git - mirror_frr.git/blob - zebra/main.c
Merge pull request #750 from donaldsharp/bgp_buffer
[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 along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #include <zebra.h>
22
23 #include <lib/version.h>
24 #include "getopt.h"
25 #include "command.h"
26 #include "thread.h"
27 #include "filter.h"
28 #include "memory.h"
29 #include "zebra_memory.h"
30 #include "memory_vty.h"
31 #include "prefix.h"
32 #include "log.h"
33 #include "plist.h"
34 #include "privs.h"
35 #include "sigevent.h"
36 #include "vrf.h"
37 #include "libfrr.h"
38
39 #include "zebra/rib.h"
40 #include "zebra/zserv.h"
41 #include "zebra/debug.h"
42 #include "zebra/router-id.h"
43 #include "zebra/irdp.h"
44 #include "zebra/rtadv.h"
45 #include "zebra/zebra_ptm.h"
46 #include "zebra/zebra_ns.h"
47 #include "zebra/redistribute.h"
48 #include "zebra/zebra_mpls.h"
49 #include "zebra/label_manager.h"
50
51 #define ZEBRA_PTM_SUPPORT
52
53 /* Zebra instance */
54 struct zebra_t zebrad =
55 {
56 .rtm_table_default = 0,
57 };
58
59 /* process id. */
60 pid_t pid;
61
62 /* Pacify zclient.o in libfrr, which expects this variable. */
63 struct thread_master *master;
64
65 /* Route retain mode flag. */
66 int retain_mode = 0;
67
68 /* Allow non-quagga entities to delete quagga routes */
69 int allow_delete = 0;
70
71 /* Don't delete kernel route. */
72 int keep_kernel_mode = 0;
73
74 #ifdef HAVE_NETLINK
75 /* Receive buffer size for netlink socket */
76 u_int32_t nl_rcvbufsize = 4194304;
77 #endif /* HAVE_NETLINK */
78
79 /* Command line options. */
80 struct option longopts[] =
81 {
82 { "batch", no_argument, NULL, 'b'},
83 { "allow_delete", no_argument, NULL, 'a'},
84 { "keep_kernel", no_argument, NULL, 'k'},
85 { "socket", required_argument, NULL, 'z'},
86 { "ecmp", required_argument, NULL, 'e'},
87 { "label_socket", no_argument, NULL, 'l'},
88 { "retain", no_argument, NULL, 'r'},
89 #ifdef HAVE_NETLINK
90 { "nl-bufsize", required_argument, NULL, 's'},
91 #endif /* HAVE_NETLINK */
92 { 0 }
93 };
94
95 zebra_capabilities_t _caps_p [] =
96 {
97 ZCAP_NET_ADMIN,
98 ZCAP_SYS_ADMIN,
99 ZCAP_NET_RAW,
100 };
101
102 /* zebra privileges to run with */
103 struct zebra_privs_t zserv_privs =
104 {
105 #if defined(FRR_USER) && defined(FRR_GROUP)
106 .user = FRR_USER,
107 .group = FRR_GROUP,
108 #endif
109 #ifdef VTY_GROUP
110 .vty_group = VTY_GROUP,
111 #endif
112 .caps_p = _caps_p,
113 .cap_num_p = array_size(_caps_p),
114 .cap_num_i = 0
115 };
116
117 unsigned int multipath_num = MULTIPATH_NUM;
118
119 /* SIGHUP handler. */
120 static void
121 sighup (void)
122 {
123 zlog_info ("SIGHUP received");
124
125 /* Reload of config file. */
126 ;
127 }
128
129 /* SIGINT handler. */
130 static void
131 sigint (void)
132 {
133 struct vrf *vrf;
134 struct zebra_vrf *zvrf;
135 struct zebra_ns *zns;
136
137 zlog_notice ("Terminating on signal");
138
139 #ifdef HAVE_IRDP
140 irdp_finish();
141 #endif
142
143 zebra_ptm_finish();
144 list_delete_all_node (zebrad.client_list);
145
146 if (retain_mode)
147 RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name)
148 {
149 zvrf = vrf->info;
150 if (zvrf)
151 SET_FLAG (zvrf->flags, ZEBRA_VRF_RETAIN);
152 }
153 vrf_terminate ();
154
155 zns = zebra_ns_lookup (NS_DEFAULT);
156 zebra_ns_disable (0, (void **)&zns);
157
158 access_list_reset ();
159 prefix_list_reset ();
160 route_map_finish ();
161 cmd_terminate ();
162 vty_terminate ();
163 zprivs_terminate (&zserv_privs);
164 list_delete (zebrad.client_list);
165 work_queue_free (zebrad.ribq);
166 if (zebrad.lsp_process_q)
167 work_queue_free (zebrad.lsp_process_q);
168 meta_queue_free (zebrad.mq);
169 thread_master_free (zebrad.master);
170 closezlog ();
171
172 exit (0);
173 }
174
175 /* SIGUSR1 handler. */
176 static void
177 sigusr1 (void)
178 {
179 zlog_rotate();
180 }
181
182 struct quagga_signal_t zebra_signals[] =
183 {
184 {
185 .signal = SIGHUP,
186 .handler = &sighup,
187 },
188 {
189 .signal = SIGUSR1,
190 .handler = &sigusr1,
191 },
192 {
193 .signal = SIGINT,
194 .handler = &sigint,
195 },
196 {
197 .signal = SIGTERM,
198 .handler = &sigint,
199 },
200 };
201
202 FRR_DAEMON_INFO(zebra, ZEBRA,
203 .vty_port = ZEBRA_VTY_PORT,
204 .flags = FRR_NO_ZCLIENT,
205
206 .proghelp = "Daemon which manages kernel routing table management "
207 "and\nredistribution between different routing protocols.",
208
209 .signals = zebra_signals,
210 .n_signals = array_size(zebra_signals),
211
212 .privs = &zserv_privs,
213 )
214
215 /* Main startup routine. */
216 int
217 main (int argc, char **argv)
218 {
219 // int batch_mode = 0;
220 char *zserv_path = NULL;
221 /* Socket to external label manager */
222 char *lblmgr_path = NULL;
223
224 frr_preinit(&zebra_di, argc, argv);
225
226 frr_opt_add("bakz:e:l:r"
227 #ifdef HAVE_NETLINK
228 "s:"
229 #endif
230 , longopts,
231 " -b, --batch Runs in batch mode\n"
232 " -a, --allow_delete Allow other processes to delete zebra routes\n"
233 " -z, --socket Set path of zebra socket\n"
234 " -e, --ecmp Specify ECMP to use.\n"
235 " -l, --label_socket Socket to external label manager\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 'e':
264 multipath_num = atoi (optarg);
265 if (multipath_num > MULTIPATH_NUM || multipath_num <= 0)
266 {
267 zlog_err ("Multipath Number specified must be less than %d and greater than 0", MULTIPATH_NUM);
268 return 1;
269 }
270 break;
271 case 'z':
272 zserv_path = optarg;
273 break;
274 case 'l':
275 lblmgr_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 /* Process the configuration file. Among other configuration
324 * directives we can meet those installing static routes. Such
325 * requests will not be executed immediately, but queued in
326 * zebra->ribq structure until we enter the main execution loop.
327 * The notifications from kernel will show originating PID equal
328 * to that after daemon() completes (if ever called).
329 */
330 frr_config_fork();
331
332 /* Clean up rib -- before fork (?) */
333 /* rib_weed_tables (); */
334
335 /* After we have successfully acquired the pidfile, we can be sure
336 * about being the only copy of zebra process, which is submitting
337 * changes to the FIB.
338 * Clean up zebra-originated routes. The requests will be sent to OS
339 * immediately, so originating PID in notifications from kernel
340 * will be equal to the current getpid(). To know about such routes,
341 * we have to have route_read() called before.
342 */
343 if (! keep_kernel_mode)
344 rib_sweep_route ();
345
346 /* Needed for BSD routing socket. */
347 pid = getpid ();
348
349 /* This must be done only after locking pidfile (bug #403). */
350 zebra_zserv_socket_init (zserv_path);
351
352 /* Init label manager */
353 label_manager_init (lblmgr_path);
354
355 frr_run (zebrad.master);
356
357 /* Not reached... */
358 return 0;
359 }