]> git.proxmox.com Git - mirror_frr.git/blame - zebra/main.c
Merge pull request #2262 from donaldsharp/v6_replace_semantics
[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"
4a1ab8e4 29#include "zebra_memory.h"
fc7948fa 30#include "memory_vty.h"
718e3744 31#include "prefix.h"
32#include "log.h"
7514fb77 33#include "plist.h"
edd7c245 34#include "privs.h"
2d75d052 35#include "sigevent.h"
b72ede27 36#include "vrf.h"
736d41ad 37#include "logicalrouter.h"
4f04a76b 38#include "libfrr.h"
bf094f69 39#include "routemap.h"
718e3744 40
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"
244c1cdc
DS
54
55#define ZEBRA_PTM_SUPPORT
718e3744 56
b21b19c5 57/* Zebra instance */
d62a17ae 58struct zebra_t zebrad = {
59 .rtm_table_default = 0,
a37ef435 60 .packets_to_process = ZEBRA_ZAPI_PACKETS_TO_PROCESS,
b21b19c5 61};
718e3744 62
63/* process id. */
718e3744 64pid_t pid;
65
55c72803 66/* Pacify zclient.o in libfrr, which expects this variable. */
87efd646 67struct thread_master *master;
68
718e3744 69/* Route retain mode flag. */
70int retain_mode = 0;
71
6baf7bb8
DS
72/* Allow non-quagga entities to delete quagga routes */
73int allow_delete = 0;
74
718e3744 75/* Don't delete kernel route. */
76int keep_kernel_mode = 0;
77
6b093863
DS
78bool v6_rr_semantics = false;
79
c34b6b57 80#ifdef HAVE_NETLINK
81/* Receive buffer size for netlink socket */
d7c0a89a 82uint32_t nl_rcvbufsize = 4194304;
c34b6b57 83#endif /* HAVE_NETLINK */
84
6b093863 85#define OPTION_V6_RR_SEMANTICS 2000
718e3744 86/* Command line options. */
6b093863
DS
87struct option longopts[] = {
88 {"batch", no_argument, NULL, 'b'},
89 {"allow_delete", no_argument, NULL, 'a'},
90 {"keep_kernel", no_argument, NULL, 'k'},
91 {"socket", required_argument, NULL, 'z'},
92 {"ecmp", required_argument, NULL, 'e'},
93 {"label_socket", no_argument, NULL, 'l'},
94 {"retain", no_argument, NULL, 'r'},
c34b6b57 95#ifdef HAVE_NETLINK
6b093863
DS
96 {"vrfwnetns", no_argument, NULL, 'n'},
97 {"nl-bufsize", required_argument, NULL, 's'},
98 {"v6-rr-semantics", no_argument, NULL, OPTION_V6_RR_SEMANTICS},
c34b6b57 99#endif /* HAVE_NETLINK */
6b093863 100 {0}};
718e3744 101
d62a17ae 102zebra_capabilities_t _caps_p[] = {
9d303b37 103 ZCAP_NET_ADMIN, ZCAP_SYS_ADMIN, ZCAP_NET_RAW,
edd7c245 104};
105
106/* zebra privileges to run with */
d62a17ae 107struct zebra_privs_t zserv_privs = {
b2f36157 108#if defined(FRR_USER) && defined(FRR_GROUP)
d62a17ae 109 .user = FRR_USER,
110 .group = FRR_GROUP,
edd7c245 111#endif
112#ifdef VTY_GROUP
d62a17ae 113 .vty_group = VTY_GROUP,
edd7c245 114#endif
d62a17ae 115 .caps_p = _caps_p,
116 .cap_num_p = array_size(_caps_p),
117 .cap_num_i = 0};
edd7c245 118
37fe7731
DS
119unsigned int multipath_num = MULTIPATH_NUM;
120
718e3744 121/* SIGHUP handler. */
d62a17ae 122static void sighup(void)
718e3744 123{
d62a17ae 124 zlog_info("SIGHUP received");
718e3744 125
d62a17ae 126 /* Reload of config file. */
127 ;
718e3744 128}
129
130/* SIGINT handler. */
d62a17ae 131static void sigint(void)
718e3744 132{
d62a17ae 133 struct vrf *vrf;
134 struct zebra_vrf *zvrf;
fe18ee2d 135
d62a17ae 136 zlog_notice("Terminating on signal");
718e3744 137
03951374 138 frr_early_fini();
718e3744 139
d62a17ae 140 list_delete_all_node(zebrad.client_list);
f88bd20c 141 zebra_ptm_finish();
d62a17ae 142
143 if (retain_mode)
a2addae8 144 RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name) {
d62a17ae 145 zvrf = vrf->info;
146 if (zvrf)
147 SET_FLAG(zvrf->flags, ZEBRA_VRF_RETAIN);
148 }
40289934 149 if (zebrad.lsp_process_q)
e208c8f9 150 work_queue_free_and_null(&zebrad.lsp_process_q);
d62a17ae 151 vrf_terminate();
152
ff705b15 153 ns_walk_func(zebra_ns_disabled);
e27dec3c 154 zebra_ns_notify_close();
d62a17ae 155
156 access_list_reset();
157 prefix_list_reset();
158 route_map_finish();
03951374 159
affe9e99 160 list_delete_and_null(&zebrad.client_list);
e208c8f9 161 work_queue_free_and_null(&zebrad.ribq);
d62a17ae 162 meta_queue_free(zebrad.mq);
d62a17ae 163
03951374 164 frr_fini();
d62a17ae 165 exit(0);
718e3744 166}
167
168/* SIGUSR1 handler. */
d62a17ae 169static void sigusr1(void)
718e3744 170{
d62a17ae 171 zlog_rotate();
718e3744 172}
173
d62a17ae 174struct quagga_signal_t zebra_signals[] = {
175 {
176 .signal = SIGHUP,
177 .handler = &sighup,
178 },
179 {
180 .signal = SIGUSR1,
181 .handler = &sigusr1,
182 },
183 {
184 .signal = SIGINT,
185 .handler = &sigint,
186 },
187 {
188 .signal = SIGTERM,
189 .handler = &sigint,
190 },
2d75d052 191};
b72ede27 192
d62a17ae 193FRR_DAEMON_INFO(
194 zebra, ZEBRA, .vty_port = ZEBRA_VTY_PORT, .flags = FRR_NO_ZCLIENT,
4f04a76b 195
d62a17ae 196 .proghelp =
197 "Daemon which manages kernel routing table management "
4f04a76b
DL
198 "and\nredistribution between different routing protocols.",
199
d62a17ae 200 .signals = zebra_signals, .n_signals = array_size(zebra_signals),
4f04a76b 201
d62a17ae 202 .privs = &zserv_privs, )
4f04a76b 203
718e3744 204/* Main startup routine. */
d62a17ae 205int main(int argc, char **argv)
718e3744 206{
d62a17ae 207 // int batch_mode = 0;
208 char *zserv_path = NULL;
209 /* Socket to external label manager */
210 char *lblmgr_path = NULL;
689f5a8c
DL
211 struct sockaddr_storage dummy;
212 socklen_t dummylen;
411314ed
DS
213#if defined(HANDLE_ZAPI_FUZZING)
214 char *fuzzing = NULL;
215#endif
fea12efb 216
78dd30b2 217 vrf_configure_backend(VRF_BACKEND_VRF_LITE);
996c9314 218 logicalrouter_configure_backend(LOGICALROUTER_BACKEND_NETNS);
78dd30b2 219
d62a17ae 220 frr_preinit(&zebra_di, argc, argv);
718e3744 221
d62a17ae 222 frr_opt_add(
223 "bakz:e:l:r"
4f04a76b 224#ifdef HAVE_NETLINK
78dd30b2 225 "s:n"
411314ed
DS
226#endif
227#if defined(HANDLE_ZAPI_FUZZING)
228 "c:"
c05795b1 229#endif
d62a17ae 230 ,
231 longopts,
6b093863
DS
232 " -b, --batch Runs in batch mode\n"
233 " -a, --allow_delete Allow other processes to delete zebra routes\n"
234 " -z, --socket Set path of zebra socket\n"
235 " -e, --ecmp Specify ECMP to use.\n"
236 " -l, --label_socket Socket to external label manager\n"
237 " -k, --keep_kernel Don't delete old routes which installed by zebra.\n"
238 " -r, --retain When program terminates, retain added route by zebra.\n"
4f04a76b 239#ifdef HAVE_NETLINK
6b093863
DS
240 " -n, --vrfwnetns Set VRF with NetNS\n"
241 " -s, --nl-bufsize Set netlink receive buffer size\n"
242 " --v6-rr-semantics Use v6 RR semantics\n"
4f04a76b 243#endif /* HAVE_NETLINK */
411314ed 244#if defined(HANDLE_ZAPI_FUZZING)
6b093863 245 " -c <file> Bypass normal startup use this file for tetsting of zapi"
411314ed 246#endif
6b093863 247 );
d62a17ae 248
249 while (1) {
250 int opt = frr_getopt(argc, argv, NULL);
251
252 if (opt == EOF)
253 break;
254
255 switch (opt) {
256 case 0:
257 break;
258 case 'b':
259 // batch_mode = 1;
260 break;
261 case 'a':
262 allow_delete = 1;
263 break;
264 case 'k':
265 keep_kernel_mode = 1;
266 break;
267 case 'e':
268 multipath_num = atoi(optarg);
269 if (multipath_num > MULTIPATH_NUM
270 || multipath_num <= 0) {
271 zlog_err(
272 "Multipath Number specified must be less than %d and greater than 0",
273 MULTIPATH_NUM);
274 return 1;
275 }
276 break;
277 case 'z':
278 zserv_path = optarg;
689f5a8c
DL
279 if (!frr_zclient_addr(&dummy, &dummylen, optarg)) {
280 fprintf(stderr,
281 "Invalid zserv socket path: %s\n",
282 optarg);
283 exit(1);
284 }
d62a17ae 285 break;
286 case 'l':
287 lblmgr_path = optarg;
288 break;
289 case 'r':
290 retain_mode = 1;
291 break;
c34b6b57 292#ifdef HAVE_NETLINK
d62a17ae 293 case 's':
294 nl_rcvbufsize = atoi(optarg);
295 break;
78dd30b2
PG
296 case 'n':
297 vrf_configure_backend(VRF_BACKEND_NETNS);
736d41ad 298 logicalrouter_configure_backend(
996c9314 299 LOGICALROUTER_BACKEND_OFF);
78dd30b2 300 break;
6b093863
DS
301 case OPTION_V6_RR_SEMANTICS:
302 v6_rr_semantics = true;
303 break;
c34b6b57 304#endif /* HAVE_NETLINK */
411314ed
DS
305#if defined(HANDLE_ZAPI_FUZZING)
306 case 'c':
307 fuzzing = optarg;
308 break;
309#endif
d62a17ae 310 default:
311 frr_help_exit(1);
312 break;
313 }
718e3744 314 }
d62a17ae 315
316 vty_config_lockless();
317 zebrad.master = frr_init();
318
319 /* Zebra related initialize. */
5f145fb8 320 zserv_init();
d62a17ae 321 rib_init();
322 zebra_if_init();
323 zebra_debug_init();
324 router_id_cmd_init();
f84fc2c9
DS
325
326 /*
327 * Initialize NS( and implicitly the VRF module), and make kernel
328 * routing socket. */
329 zebra_ns_init();
330
331 zebra_vty_init();
d62a17ae 332 access_list_init();
333 prefix_list_init();
334#if defined(HAVE_RTADV)
335 rtadv_cmd_init();
36735ed9 336#endif
d62a17ae 337/* PTM socket */
244c1cdc 338#ifdef ZEBRA_PTM_SUPPORT
d62a17ae 339 zebra_ptm_init();
244c1cdc 340#endif
718e3744 341
d62a17ae 342 zebra_mpls_init();
343 zebra_mpls_vty_init();
2dd0d726 344 zebra_pw_vty_init();
7758e3f3 345
996c9314
LB
346/* For debug purpose. */
347/* SET_FLAG (zebra_debug_event, ZEBRA_DEBUG_EVENT); */
718e3744 348
411314ed
DS
349#if defined(HANDLE_ZAPI_FUZZING)
350 if (fuzzing) {
351 zserv_read_file(fuzzing);
352 exit(0);
353 }
354#endif
355
d62a17ae 356 /* Process the configuration file. Among other configuration
9d303b37
DL
357 * directives we can meet those installing static routes. Such
358 * requests will not be executed immediately, but queued in
359 * zebra->ribq structure until we enter the main execution loop.
360 * The notifications from kernel will show originating PID equal
361 * to that after daemon() completes (if ever called).
362 */
d62a17ae 363 frr_config_fork();
718e3744 364
d62a17ae 365 /* After we have successfully acquired the pidfile, we can be sure
9d303b37
DL
366 * about being the only copy of zebra process, which is submitting
367 * changes to the FIB.
368 * Clean up zebra-originated routes. The requests will be sent to OS
369 * immediately, so originating PID in notifications from kernel
370 * will be equal to the current getpid(). To know about such routes,
371 * we have to have route_read() called before.
372 */
d62a17ae 373 if (!keep_kernel_mode)
374 rib_sweep_route();
91b7351d 375
d62a17ae 376 /* Needed for BSD routing socket. */
377 pid = getpid();
718e3744 378
d62a17ae 379 /* This must be done only after locking pidfile (bug #403). */
380 zebra_zserv_socket_init(zserv_path);
97be79f9 381
d62a17ae 382 /* Init label manager */
383 label_manager_init(lblmgr_path);
fea12efb 384
453844ab
QY
385 /* RNH init */
386 zebra_rnh_init();
387
d62a17ae 388 frr_run(zebrad.master);
718e3744 389
d62a17ae 390 /* Not reached... */
391 return 0;
718e3744 392}