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