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