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