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