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