]> git.proxmox.com Git - mirror_frr.git/blob - zebra/main.c
Merge pull request #11210 from anlancs/fix/zebra-leak-vtp
[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 "prefix.h"
30 #include "log.h"
31 #include "plist.h"
32 #include "privs.h"
33 #include "sigevent.h"
34 #include "vrf.h"
35 #include "libfrr.h"
36 #include "routemap.h"
37 #include "routing_nb.h"
38
39 #include "zebra/zebra_router.h"
40 #include "zebra/zebra_errors.h"
41 #include "zebra/rib.h"
42 #include "zebra/zserv.h"
43 #include "zebra/debug.h"
44 #include "zebra/router-id.h"
45 #include "zebra/irdp.h"
46 #include "zebra/rtadv.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 #include "zebra/label_manager.h"
52 #include "zebra/zebra_netns_notify.h"
53 #include "zebra/zebra_rnh.h"
54 #include "zebra/zebra_pbr.h"
55 #include "zebra/zebra_vxlan.h"
56 #include "zebra/zebra_routemap.h"
57 #include "zebra/zebra_nb.h"
58 #include "zebra/zebra_opaque.h"
59 #include "zebra/zebra_srte.h"
60 #include "zebra/zebra_srv6.h"
61 #include "zebra/zebra_srv6_vty.h"
62
63 #define ZEBRA_PTM_SUPPORT
64
65 /* process id. */
66 pid_t pid;
67
68 /* Pacify zclient.o in libfrr, which expects this variable. */
69 struct thread_master *master;
70
71 /* Route retain mode flag. */
72 int retain_mode = 0;
73
74 /* Allow non-frr entities to delete frr routes */
75 int allow_delete = 0;
76
77 int graceful_restart;
78
79 bool v6_rr_semantics = false;
80
81 /* Receive buffer size for kernel control sockets */
82 #define RCVBUFSIZE_MIN 4194304
83 #ifdef HAVE_NETLINK
84 uint32_t rcvbufsize = RCVBUFSIZE_MIN;
85 #else
86 uint32_t rcvbufsize = 128 * 1024;
87 #endif
88
89 #define OPTION_V6_RR_SEMANTICS 2000
90 #define OPTION_ASIC_OFFLOAD 2001
91
92 /* Command line options. */
93 const struct option longopts[] = {
94 {"batch", no_argument, NULL, 'b'},
95 {"allow_delete", no_argument, NULL, 'a'},
96 {"socket", required_argument, NULL, 'z'},
97 {"ecmp", required_argument, NULL, 'e'},
98 {"retain", no_argument, NULL, 'r'},
99 {"graceful_restart", required_argument, NULL, 'K'},
100 {"asic-offload", optional_argument, NULL, OPTION_ASIC_OFFLOAD},
101 #ifdef HAVE_NETLINK
102 {"vrfwnetns", no_argument, NULL, 'n'},
103 {"nl-bufsize", required_argument, NULL, 's'},
104 {"v6-rr-semantics", no_argument, NULL, OPTION_V6_RR_SEMANTICS},
105 #endif /* HAVE_NETLINK */
106 {0}};
107
108 zebra_capabilities_t _caps_p[] = {
109 ZCAP_NET_ADMIN, ZCAP_SYS_ADMIN, ZCAP_NET_RAW,
110 };
111
112 /* zebra privileges to run with */
113 struct zebra_privs_t zserv_privs = {
114 #if defined(FRR_USER) && defined(FRR_GROUP)
115 .user = FRR_USER,
116 .group = FRR_GROUP,
117 #endif
118 #ifdef VTY_GROUP
119 .vty_group = VTY_GROUP,
120 #endif
121 .caps_p = _caps_p,
122 .cap_num_p = array_size(_caps_p),
123 .cap_num_i = 0};
124
125 /* SIGHUP handler. */
126 static void sighup(void)
127 {
128 zlog_info("SIGHUP received");
129
130 /* Reload of config file. */
131 ;
132 }
133
134 /* SIGINT handler. */
135 static void sigint(void)
136 {
137 struct vrf *vrf;
138 struct zebra_vrf *zvrf;
139 struct listnode *ln, *nn;
140 struct zserv *client;
141 static bool sigint_done;
142
143 if (sigint_done)
144 return;
145
146 sigint_done = true;
147
148 zlog_notice("Terminating on signal");
149
150 atomic_store_explicit(&zrouter.in_shutdown, true,
151 memory_order_relaxed);
152
153 /* send RA lifetime of 0 before stopping. rfc4861/6.2.5 */
154 rtadv_stop_ra_all();
155
156 frr_early_fini();
157
158 /* Stop the opaque module pthread */
159 zebra_opaque_stop();
160
161 zebra_dplane_pre_finish();
162
163 /* Clean up GR related info. */
164 zebra_gr_stale_client_cleanup(zrouter.stale_client_list);
165 list_delete_all_node(zrouter.stale_client_list);
166
167 /* Clean up zapi clients and server module */
168 for (ALL_LIST_ELEMENTS(zrouter.client_list, ln, nn, client))
169 zserv_close_client(client);
170
171 zserv_close();
172 list_delete_all_node(zrouter.client_list);
173
174 /* Once all the zclients are cleaned up, clean up the opaque module */
175 zebra_opaque_finish();
176
177 zebra_ptm_finish();
178
179 if (retain_mode) {
180 zebra_nhg_mark_keep();
181 RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name) {
182 zvrf = vrf->info;
183 if (zvrf)
184 SET_FLAG(zvrf->flags, ZEBRA_VRF_RETAIN);
185 }
186 }
187
188 if (zrouter.lsp_process_q)
189 work_queue_free_and_null(&zrouter.lsp_process_q);
190
191 vrf_terminate();
192
193 ns_walk_func(zebra_ns_early_shutdown, NULL, NULL);
194 zebra_ns_notify_close();
195
196 access_list_reset();
197 prefix_list_reset();
198 /*
199 * zebra_routemap_finish will
200 * 1 set rmap upd timer to 0 so that rmap update wont be scheduled again
201 * 2 Put off the rmap update thread
202 * 3 route_map_finish
203 */
204 zebra_routemap_finish();
205
206 list_delete(&zrouter.client_list);
207
208 /* Indicate that all new dplane work has been enqueued. When that
209 * work is complete, the dataplane will enqueue an event
210 * with the 'finalize' function.
211 */
212 zebra_dplane_finish();
213 }
214
215 /*
216 * Final shutdown step for the zebra main thread. This is run after all
217 * async update processing has completed.
218 */
219 void zebra_finalize(struct thread *dummy)
220 {
221 zlog_info("Zebra final shutdown");
222
223 /* Final shutdown of ns resources */
224 ns_walk_func(zebra_ns_final_shutdown, NULL, NULL);
225
226 /* Stop dplane thread and finish any cleanup */
227 zebra_dplane_shutdown();
228
229 zebra_router_terminate();
230
231 frr_fini();
232 exit(0);
233 }
234
235 /* SIGUSR1 handler. */
236 static void sigusr1(void)
237 {
238 zlog_rotate();
239 }
240
241 struct frr_signal_t zebra_signals[] = {
242 {
243 .signal = SIGHUP,
244 .handler = &sighup,
245 },
246 {
247 .signal = SIGUSR1,
248 .handler = &sigusr1,
249 },
250 {
251 .signal = SIGINT,
252 .handler = &sigint,
253 },
254 {
255 .signal = SIGTERM,
256 .handler = &sigint,
257 },
258 };
259
260 static const struct frr_yang_module_info *const zebra_yang_modules[] = {
261 &frr_filter_info,
262 &frr_interface_info,
263 &frr_route_map_info,
264 &frr_zebra_info,
265 &frr_vrf_info,
266 &frr_routing_info,
267 &frr_zebra_route_map_info,
268 };
269
270 FRR_DAEMON_INFO(
271 zebra, ZEBRA, .vty_port = ZEBRA_VTY_PORT, .flags = FRR_NO_ZCLIENT,
272
273 .proghelp =
274 "Daemon which manages kernel routing table management and\nredistribution between different routing protocols.",
275
276 .signals = zebra_signals, .n_signals = array_size(zebra_signals),
277
278 .privs = &zserv_privs,
279
280 .yang_modules = zebra_yang_modules,
281 .n_yang_modules = array_size(zebra_yang_modules),
282 );
283
284 /* Main startup routine. */
285 int main(int argc, char **argv)
286 {
287 // int batch_mode = 0;
288 char *zserv_path = NULL;
289 struct sockaddr_storage dummy;
290 socklen_t dummylen;
291 bool asic_offload = false;
292 bool notify_on_ack = true;
293
294 graceful_restart = 0;
295 vrf_configure_backend(VRF_BACKEND_VRF_LITE);
296
297 frr_preinit(&zebra_di, argc, argv);
298
299 frr_opt_add(
300 "baz:e:rK:s:"
301 #ifdef HAVE_NETLINK
302 "n"
303 #endif
304 ,
305 longopts,
306 " -b, --batch Runs in batch mode\n"
307 " -a, --allow_delete Allow other processes to delete zebra routes\n"
308 " -z, --socket Set path of zebra socket\n"
309 " -e, --ecmp Specify ECMP to use.\n"
310 " -r, --retain When program terminates, retain added route by zebra.\n"
311 " -K, --graceful_restart Graceful restart at the kernel level, timer in seconds for expiration\n"
312 " -A, --asic-offload FRR is interacting with an asic underneath the linux kernel\n"
313 #ifdef HAVE_NETLINK
314 " -s, --nl-bufsize Set netlink receive buffer size\n"
315 " -n, --vrfwnetns Use NetNS as VRF backend\n"
316 " --v6-rr-semantics Use v6 RR semantics\n"
317 #else
318 " -s, Set kernel socket receive buffer size\n"
319 #endif /* HAVE_NETLINK */
320 );
321
322 while (1) {
323 int opt = frr_getopt(argc, argv, NULL);
324
325 if (opt == EOF)
326 break;
327
328 switch (opt) {
329 case 0:
330 break;
331 case 'b':
332 // batch_mode = 1;
333 break;
334 case 'a':
335 allow_delete = 1;
336 break;
337 case 'e': {
338 unsigned long int parsed_multipath =
339 strtoul(optarg, NULL, 10);
340 if (parsed_multipath == 0
341 || parsed_multipath > MULTIPATH_NUM
342 || parsed_multipath > UINT32_MAX) {
343 flog_err(
344 EC_ZEBRA_BAD_MULTIPATH_NUM,
345 "Multipath Number specified must be less than %u and greater than 0",
346 MULTIPATH_NUM);
347 return 1;
348 }
349 zrouter.multipath_num = parsed_multipath;
350 break;
351 }
352 case 'z':
353 zserv_path = optarg;
354 if (!frr_zclient_addr(&dummy, &dummylen, optarg)) {
355 fprintf(stderr,
356 "Invalid zserv socket path: %s\n",
357 optarg);
358 exit(1);
359 }
360 break;
361 case 'r':
362 retain_mode = 1;
363 break;
364 case 'K':
365 graceful_restart = atoi(optarg);
366 break;
367 case 's':
368 rcvbufsize = atoi(optarg);
369 if (rcvbufsize < RCVBUFSIZE_MIN)
370 fprintf(stderr,
371 "Rcvbufsize is smaller than recommended value: %d\n",
372 RCVBUFSIZE_MIN);
373 break;
374 #ifdef HAVE_NETLINK
375 case 'n':
376 vrf_configure_backend(VRF_BACKEND_NETNS);
377 break;
378 case OPTION_V6_RR_SEMANTICS:
379 v6_rr_semantics = true;
380 break;
381 case OPTION_ASIC_OFFLOAD:
382 if (!strcmp(optarg, "notify_on_offload"))
383 notify_on_ack = false;
384 if (!strcmp(optarg, "notify_on_ack"))
385 notify_on_ack = true;
386 asic_offload = true;
387 break;
388 #endif /* HAVE_NETLINK */
389 default:
390 frr_help_exit(1);
391 }
392 }
393
394 zrouter.master = frr_init();
395
396 /* Zebra related initialize. */
397 zebra_router_init(asic_offload, notify_on_ack);
398 zserv_init();
399 rib_init();
400 zebra_if_init();
401 zebra_debug_init();
402
403 /*
404 * Initialize NS( and implicitly the VRF module), and make kernel
405 * routing socket. */
406 zebra_ns_init();
407 router_id_cmd_init();
408 zebra_vty_init();
409 access_list_init();
410 prefix_list_init();
411 #if defined(HAVE_RTADV)
412 rtadv_cmd_init();
413 #endif
414 /* PTM socket */
415 #ifdef ZEBRA_PTM_SUPPORT
416 zebra_ptm_init();
417 #endif
418
419 zebra_mpls_init();
420 zebra_mpls_vty_init();
421 zebra_pw_vty_init();
422 zebra_pbr_init();
423 zebra_opaque_init();
424 zebra_srte_init();
425 zebra_srv6_init();
426 zebra_srv6_vty_init();
427
428 /* For debug purpose. */
429 /* SET_FLAG (zebra_debug_event, ZEBRA_DEBUG_EVENT); */
430
431 /* Process the configuration file. Among other configuration
432 * directives we can meet those installing static routes. Such
433 * requests will not be executed immediately, but queued in
434 * zebra->ribq structure until we enter the main execution loop.
435 * The notifications from kernel will show originating PID equal
436 * to that after daemon() completes (if ever called).
437 */
438 frr_config_fork();
439
440 /* After we have successfully acquired the pidfile, we can be sure
441 * about being the only copy of zebra process, which is submitting
442 * changes to the FIB.
443 * Clean up zebra-originated routes. The requests will be sent to OS
444 * immediately, so originating PID in notifications from kernel
445 * will be equal to the current getpid(). To know about such routes,
446 * we have to have route_read() called before.
447 */
448 zrouter.startup_time = monotime(NULL);
449 thread_add_timer(zrouter.master, rib_sweep_route, NULL,
450 graceful_restart, &zrouter.sweeper);
451
452 /* Needed for BSD routing socket. */
453 pid = getpid();
454
455 /* Start dataplane system */
456 zebra_dplane_start();
457
458 /* Start the ted module, before zserv */
459 zebra_opaque_start();
460
461 /* Start Zebra API server */
462 zserv_start(zserv_path);
463
464 /* Init label manager */
465 label_manager_init();
466
467 /* RNH init */
468 zebra_rnh_init();
469
470 /* Config handler Init */
471 zebra_evpn_init();
472
473 /* Error init */
474 zebra_error_init();
475
476 frr_run(zrouter.master);
477
478 /* Not reached... */
479 return 0;
480 }