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