]> git.proxmox.com Git - mirror_frr.git/blame - zebra/main.c
Merge pull request #10621 from donaldsharp/cov_fun
[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
b72aae2e 74/* Allow non-frr entities to delete frr routes */
6baf7bb8
DS
75int allow_delete = 0;
76
d4644d41
DS
77int graceful_restart;
78
6b093863
DS
79bool v6_rr_semantics = false;
80
c34b6b57 81#ifdef HAVE_NETLINK
82/* Receive buffer size for netlink socket */
d7c0a89a 83uint32_t nl_rcvbufsize = 4194304;
c34b6b57 84#endif /* HAVE_NETLINK */
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
d62a17ae 105zebra_capabilities_t _caps_p[] = {
9d303b37 106 ZCAP_NET_ADMIN, ZCAP_SYS_ADMIN, ZCAP_NET_RAW,
edd7c245 107};
108
109/* zebra privileges to run with */
d62a17ae 110struct zebra_privs_t zserv_privs = {
b2f36157 111#if defined(FRR_USER) && defined(FRR_GROUP)
d62a17ae 112 .user = FRR_USER,
113 .group = FRR_GROUP,
edd7c245 114#endif
115#ifdef VTY_GROUP
d62a17ae 116 .vty_group = VTY_GROUP,
edd7c245 117#endif
d62a17ae 118 .caps_p = _caps_p,
119 .cap_num_p = array_size(_caps_p),
120 .cap_num_i = 0};
edd7c245 121
718e3744 122/* SIGHUP handler. */
d62a17ae 123static void sighup(void)
718e3744 124{
d62a17ae 125 zlog_info("SIGHUP received");
718e3744 126
d62a17ae 127 /* Reload of config file. */
128 ;
718e3744 129}
130
131/* SIGINT handler. */
d62a17ae 132static void sigint(void)
718e3744 133{
d62a17ae 134 struct vrf *vrf;
135 struct zebra_vrf *zvrf;
f3e33b69
QY
136 struct listnode *ln, *nn;
137 struct zserv *client;
ff2460d5
MS
138 static bool sigint_done;
139
140 if (sigint_done)
141 return;
142
143 sigint_done = true;
fe18ee2d 144
d62a17ae 145 zlog_notice("Terminating on signal");
718e3744 146
2fc69f03
MS
147 atomic_store_explicit(&zrouter.in_shutdown, true,
148 memory_order_relaxed);
149
d7fc0e67
DS
150 /* send RA lifetime of 0 before stopping. rfc4861/6.2.5 */
151 rtadv_stop_ra_all();
152
03951374 153 frr_early_fini();
718e3744 154
9bb02389
MS
155 /* Stop the opaque module pthread */
156 zebra_opaque_stop();
157
4dfd7a02
MS
158 zebra_dplane_pre_finish();
159
b9e6727a
S
160 /* Clean up GR related info. */
161 zebra_gr_stale_client_cleanup(zrouter.stale_client_list);
162 list_delete_all_node(zrouter.stale_client_list);
163
9bb02389 164 /* Clean up zapi clients and server module */
161e9ab7 165 for (ALL_LIST_ELEMENTS(zrouter.client_list, ln, nn, client))
f3e33b69
QY
166 zserv_close_client(client);
167
41674562 168 zserv_close();
161e9ab7 169 list_delete_all_node(zrouter.client_list);
41674562 170
9bb02389
MS
171 /* Once all the zclients are cleaned up, clean up the opaque module */
172 zebra_opaque_finish();
173
f88bd20c 174 zebra_ptm_finish();
d62a17ae 175
b1b07ef5
DS
176 if (retain_mode) {
177 zebra_nhg_mark_keep();
a2addae8 178 RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name) {
d62a17ae 179 zvrf = vrf->info;
180 if (zvrf)
181 SET_FLAG(zvrf->flags, ZEBRA_VRF_RETAIN);
182 }
b1b07ef5 183 }
3e0372d2 184
695b279a
MS
185 if (zrouter.lsp_process_q)
186 work_queue_free_and_null(&zrouter.lsp_process_q);
187
d62a17ae 188 vrf_terminate();
189
2961d060 190 ns_walk_func(zebra_ns_early_shutdown, NULL, NULL);
e27dec3c 191 zebra_ns_notify_close();
d62a17ae 192
193 access_list_reset();
194 prefix_list_reset();
a2665e38 195 /*
196 * zebra_routemap_finish will
197 * 1 set rmap upd timer to 0 so that rmap update wont be scheduled again
198 * 2 Put off the rmap update thread
199 * 3 route_map_finish
200 */
201 zebra_routemap_finish();
03951374 202
161e9ab7 203 list_delete(&zrouter.client_list);
4dfd7a02 204
ff2460d5
MS
205 /* Indicate that all new dplane work has been enqueued. When that
206 * work is complete, the dataplane will enqueue an event
207 * with the 'finalize' function.
208 */
1d11b21f 209 zebra_dplane_finish();
4dfd7a02
MS
210}
211
ff2460d5
MS
212/*
213 * Final shutdown step for the zebra main thread. This is run after all
214 * async update processing has completed.
215 */
4dfd7a02
MS
216int zebra_finalize(struct thread *dummy)
217{
218 zlog_info("Zebra final shutdown");
219
62b8bb7a 220 /* Final shutdown of ns resources */
2961d060 221 ns_walk_func(zebra_ns_final_shutdown, NULL, NULL);
62b8bb7a 222
4dfd7a02
MS
223 /* Stop dplane thread and finish any cleanup */
224 zebra_dplane_shutdown();
1d11b21f 225
89272910
DS
226 zebra_router_terminate();
227
03951374 228 frr_fini();
d62a17ae 229 exit(0);
718e3744 230}
231
232/* SIGUSR1 handler. */
d62a17ae 233static void sigusr1(void)
718e3744 234{
d62a17ae 235 zlog_rotate();
718e3744 236}
237
7cc91e67 238struct frr_signal_t zebra_signals[] = {
d62a17ae 239 {
240 .signal = SIGHUP,
241 .handler = &sighup,
242 },
243 {
244 .signal = SIGUSR1,
245 .handler = &sigusr1,
246 },
247 {
248 .signal = SIGINT,
249 .handler = &sigint,
250 },
251 {
252 .signal = SIGTERM,
253 .handler = &sigint,
254 },
2d75d052 255};
b72ede27 256
0d8c7a26 257static const struct frr_yang_module_info *const zebra_yang_modules[] = {
c2aab693 258 &frr_filter_info,
a4bed468 259 &frr_interface_info,
91835f1f 260 &frr_route_map_info,
b87fa24d 261 &frr_zebra_info,
6fd8972a 262 &frr_vrf_info,
755100ac 263 &frr_routing_info,
e71627cb 264 &frr_zebra_route_map_info,
8fcdd0d6
RW
265};
266
d62a17ae 267FRR_DAEMON_INFO(
268 zebra, ZEBRA, .vty_port = ZEBRA_VTY_PORT, .flags = FRR_NO_ZCLIENT,
4f04a76b 269
d62a17ae 270 .proghelp =
3efd0893 271 "Daemon which manages kernel routing table management and\nredistribution between different routing protocols.",
4f04a76b 272
d62a17ae 273 .signals = zebra_signals, .n_signals = array_size(zebra_signals),
4f04a76b 274
8fcdd0d6
RW
275 .privs = &zserv_privs,
276
277 .yang_modules = zebra_yang_modules,
80413c20
DL
278 .n_yang_modules = array_size(zebra_yang_modules),
279);
4f04a76b 280
718e3744 281/* Main startup routine. */
d62a17ae 282int main(int argc, char **argv)
718e3744 283{
d62a17ae 284 // int batch_mode = 0;
285 char *zserv_path = NULL;
689f5a8c
DL
286 struct sockaddr_storage dummy;
287 socklen_t dummylen;
e4876266
DS
288 bool asic_offload = false;
289 bool notify_on_ack = true;
fea12efb 290
d4644d41 291 graceful_restart = 0;
78dd30b2
PG
292 vrf_configure_backend(VRF_BACKEND_VRF_LITE);
293
d62a17ae 294 frr_preinit(&zebra_di, argc, argv);
718e3744 295
d62a17ae 296 frr_opt_add(
ac2cb9bf 297 "baz:e:rK:"
4f04a76b 298#ifdef HAVE_NETLINK
78dd30b2 299 "s:n"
411314ed 300#endif
d62a17ae 301 ,
302 longopts,
d4644d41
DS
303 " -b, --batch Runs in batch mode\n"
304 " -a, --allow_delete Allow other processes to delete zebra routes\n"
305 " -z, --socket Set path of zebra socket\n"
306 " -e, --ecmp Specify ECMP to use.\n"
d4644d41 307 " -r, --retain When program terminates, retain added route by zebra.\n"
d4644d41 308 " -K, --graceful_restart Graceful restart at the kernel level, timer in seconds for expiration\n"
e4876266 309 " -A, --asic-offload FRR is interacting with an asic underneath the linux kernel\n"
4f04a76b 310#ifdef HAVE_NETLINK
d4644d41
DS
311 " -n, --vrfwnetns Use NetNS as VRF backend\n"
312 " -s, --nl-bufsize Set netlink receive buffer size\n"
313 " --v6-rr-semantics Use v6 RR semantics\n"
4f04a76b 314#endif /* HAVE_NETLINK */
6b093863 315 );
d62a17ae 316
317 while (1) {
318 int opt = frr_getopt(argc, argv, NULL);
319
320 if (opt == EOF)
321 break;
322
323 switch (opt) {
324 case 0:
325 break;
326 case 'b':
327 // batch_mode = 1;
328 break;
329 case 'a':
330 allow_delete = 1;
331 break;
1e03d6bc
QY
332 case 'e': {
333 unsigned long int parsed_multipath =
334 strtoul(optarg, NULL, 10);
335 if (parsed_multipath == 0
336 || parsed_multipath > MULTIPATH_NUM
337 || parsed_multipath > UINT32_MAX) {
af4c2728 338 flog_err(
e914ccbe 339 EC_ZEBRA_BAD_MULTIPATH_NUM,
1e03d6bc 340 "Multipath Number specified must be less than %u and greater than 0",
d62a17ae 341 MULTIPATH_NUM);
342 return 1;
343 }
1e03d6bc 344 zrouter.multipath_num = parsed_multipath;
d62a17ae 345 break;
1e03d6bc 346 }
d62a17ae 347 case 'z':
348 zserv_path = optarg;
689f5a8c
DL
349 if (!frr_zclient_addr(&dummy, &dummylen, optarg)) {
350 fprintf(stderr,
351 "Invalid zserv socket path: %s\n",
352 optarg);
353 exit(1);
354 }
d62a17ae 355 break;
d62a17ae 356 case 'r':
357 retain_mode = 1;
358 break;
d4644d41 359 case 'K':
d4644d41
DS
360 graceful_restart = atoi(optarg);
361 break;
c34b6b57 362#ifdef HAVE_NETLINK
d62a17ae 363 case 's':
364 nl_rcvbufsize = atoi(optarg);
365 break;
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();
402#if defined(HAVE_RTADV)
403 rtadv_cmd_init();
36735ed9 404#endif
d62a17ae 405/* PTM socket */
244c1cdc 406#ifdef ZEBRA_PTM_SUPPORT
d62a17ae 407 zebra_ptm_init();
244c1cdc 408#endif
718e3744 409
d62a17ae 410 zebra_mpls_init();
411 zebra_mpls_vty_init();
2dd0d726 412 zebra_pw_vty_init();
4c0ec639 413 zebra_pbr_init();
9bb02389 414 zebra_opaque_init();
31f937fb 415 zebra_srte_init();
6e68a084 416 zebra_srv6_init();
6c0a7c09 417 zebra_srv6_vty_init();
7758e3f3 418
31f937fb
SM
419 /* For debug purpose. */
420 /* SET_FLAG (zebra_debug_event, ZEBRA_DEBUG_EVENT); */
718e3744 421
d62a17ae 422 /* Process the configuration file. Among other configuration
9d303b37
DL
423 * directives we can meet those installing static routes. Such
424 * requests will not be executed immediately, but queued in
425 * zebra->ribq structure until we enter the main execution loop.
426 * The notifications from kernel will show originating PID equal
427 * to that after daemon() completes (if ever called).
428 */
d62a17ae 429 frr_config_fork();
718e3744 430
d62a17ae 431 /* After we have successfully acquired the pidfile, we can be sure
9d303b37
DL
432 * about being the only copy of zebra process, which is submitting
433 * changes to the FIB.
434 * Clean up zebra-originated routes. The requests will be sent to OS
435 * immediately, so originating PID in notifications from kernel
436 * will be equal to the current getpid(). To know about such routes,
437 * we have to have route_read() called before.
438 */
d4644d41 439 zrouter.startup_time = monotime(NULL);
c3343a75
DS
440 thread_add_timer(zrouter.master, rib_sweep_route, NULL,
441 graceful_restart, &zrouter.sweeper);
91b7351d 442
d62a17ae 443 /* Needed for BSD routing socket. */
444 pid = getpid();
718e3744 445
e5a60d82
MS
446 /* Start dataplane system */
447 zebra_dplane_start();
448
9bb02389
MS
449 /* Start the ted module, before zserv */
450 zebra_opaque_start();
451
21ccc0cf
QY
452 /* Start Zebra API server */
453 zserv_start(zserv_path);
97be79f9 454
d62a17ae 455 /* Init label manager */
e11d7c96 456 label_manager_init();
fea12efb 457
453844ab
QY
458 /* RNH init */
459 zebra_rnh_init();
89272910 460
27627f9a
KA
461 /* Config handler Init */
462 zebra_evpn_init();
463
5ad4c39c
QY
464 /* Error init */
465 zebra_error_init();
453844ab 466
3801e764 467 frr_run(zrouter.master);
718e3744 468
d62a17ae 469 /* Not reached... */
470 return 0;
718e3744 471}