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