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