]> git.proxmox.com Git - mirror_frr.git/blob - zebra/main.c
Merge pull request #2273 from qlyoung/remove-retain-mode
[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
55 #define ZEBRA_PTM_SUPPORT
56
57 /* Zebra instance */
58 struct zebra_t zebrad = {
59 .rtm_table_default = 0,
60 .packets_to_process = ZEBRA_ZAPI_PACKETS_TO_PROCESS,
61 };
62
63 /* process id. */
64 pid_t pid;
65
66 /* Pacify zclient.o in libfrr, which expects this variable. */
67 struct thread_master *master;
68
69 /* Route retain mode flag. */
70 int retain_mode = 0;
71
72 /* Allow non-quagga entities to delete quagga routes */
73 int allow_delete = 0;
74
75 /* Don't delete kernel route. */
76 int keep_kernel_mode = 0;
77
78 bool v6_rr_semantics = false;
79
80 #ifdef HAVE_NETLINK
81 /* Receive buffer size for netlink socket */
82 uint32_t nl_rcvbufsize = 4194304;
83 #endif /* HAVE_NETLINK */
84
85 #define OPTION_V6_RR_SEMANTICS 2000
86 /* Command line options. */
87 struct 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'},
95 #ifdef HAVE_NETLINK
96 {"vrfwnetns", no_argument, NULL, 'n'},
97 {"nl-bufsize", required_argument, NULL, 's'},
98 {"v6-rr-semantics", no_argument, NULL, OPTION_V6_RR_SEMANTICS},
99 #endif /* HAVE_NETLINK */
100 {0}};
101
102 zebra_capabilities_t _caps_p[] = {
103 ZCAP_NET_ADMIN, ZCAP_SYS_ADMIN, ZCAP_NET_RAW,
104 };
105
106 /* zebra privileges to run with */
107 struct zebra_privs_t zserv_privs = {
108 #if defined(FRR_USER) && defined(FRR_GROUP)
109 .user = FRR_USER,
110 .group = FRR_GROUP,
111 #endif
112 #ifdef VTY_GROUP
113 .vty_group = VTY_GROUP,
114 #endif
115 .caps_p = _caps_p,
116 .cap_num_p = array_size(_caps_p),
117 .cap_num_i = 0};
118
119 unsigned int multipath_num = MULTIPATH_NUM;
120
121 /* SIGHUP handler. */
122 static void sighup(void)
123 {
124 zlog_info("SIGHUP received");
125
126 /* Reload of config file. */
127 ;
128 }
129
130 /* SIGINT handler. */
131 static void sigint(void)
132 {
133 struct vrf *vrf;
134 struct zebra_vrf *zvrf;
135
136 zlog_notice("Terminating on signal");
137
138 frr_early_fini();
139
140 list_delete_all_node(zebrad.client_list);
141 zebra_ptm_finish();
142
143 if (retain_mode)
144 RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name) {
145 zvrf = vrf->info;
146 if (zvrf)
147 SET_FLAG(zvrf->flags, ZEBRA_VRF_RETAIN);
148 }
149 if (zebrad.lsp_process_q)
150 work_queue_free_and_null(&zebrad.lsp_process_q);
151 vrf_terminate();
152
153 ns_walk_func(zebra_ns_disabled);
154 zebra_ns_notify_close();
155
156 access_list_reset();
157 prefix_list_reset();
158 route_map_finish();
159
160 list_delete_and_null(&zebrad.client_list);
161 work_queue_free_and_null(&zebrad.ribq);
162 meta_queue_free(zebrad.mq);
163
164 frr_fini();
165 exit(0);
166 }
167
168 /* SIGUSR1 handler. */
169 static void sigusr1(void)
170 {
171 zlog_rotate();
172 }
173
174 struct 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 },
191 };
192
193 FRR_DAEMON_INFO(
194 zebra, ZEBRA, .vty_port = ZEBRA_VTY_PORT, .flags = FRR_NO_ZCLIENT,
195
196 .proghelp =
197 "Daemon which manages kernel routing table management "
198 "and\nredistribution between different routing protocols.",
199
200 .signals = zebra_signals, .n_signals = array_size(zebra_signals),
201
202 .privs = &zserv_privs, )
203
204 /* Main startup routine. */
205 int main(int argc, char **argv)
206 {
207 // int batch_mode = 0;
208 char *zserv_path = NULL;
209 /* Socket to external label manager */
210 char *lblmgr_path = NULL;
211 struct sockaddr_storage dummy;
212 socklen_t dummylen;
213 #if defined(HANDLE_ZAPI_FUZZING)
214 char *fuzzing = NULL;
215 #endif
216
217 vrf_configure_backend(VRF_BACKEND_VRF_LITE);
218 logicalrouter_configure_backend(LOGICALROUTER_BACKEND_NETNS);
219
220 frr_preinit(&zebra_di, argc, argv);
221
222 frr_opt_add(
223 "bakz:e:l:r"
224 #ifdef HAVE_NETLINK
225 "s:n"
226 #endif
227 #if defined(HANDLE_ZAPI_FUZZING)
228 "c:"
229 #endif
230 ,
231 longopts,
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"
239 #ifdef HAVE_NETLINK
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"
243 #endif /* HAVE_NETLINK */
244 #if defined(HANDLE_ZAPI_FUZZING)
245 " -c <file> Bypass normal startup use this file for tetsting of zapi"
246 #endif
247 );
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;
279 if (!frr_zclient_addr(&dummy, &dummylen, optarg)) {
280 fprintf(stderr,
281 "Invalid zserv socket path: %s\n",
282 optarg);
283 exit(1);
284 }
285 break;
286 case 'l':
287 lblmgr_path = optarg;
288 break;
289 case 'r':
290 retain_mode = 1;
291 break;
292 #ifdef HAVE_NETLINK
293 case 's':
294 nl_rcvbufsize = atoi(optarg);
295 break;
296 case 'n':
297 vrf_configure_backend(VRF_BACKEND_NETNS);
298 logicalrouter_configure_backend(
299 LOGICALROUTER_BACKEND_OFF);
300 break;
301 case OPTION_V6_RR_SEMANTICS:
302 v6_rr_semantics = true;
303 break;
304 #endif /* HAVE_NETLINK */
305 #if defined(HANDLE_ZAPI_FUZZING)
306 case 'c':
307 fuzzing = optarg;
308 break;
309 #endif
310 default:
311 frr_help_exit(1);
312 break;
313 }
314 }
315
316 vty_config_lockless();
317 zebrad.master = frr_init();
318
319 /* Zebra related initialize. */
320 zserv_init();
321 rib_init();
322 zebra_if_init();
323 zebra_debug_init();
324 router_id_cmd_init();
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();
332 access_list_init();
333 prefix_list_init();
334 #if defined(HAVE_RTADV)
335 rtadv_cmd_init();
336 #endif
337 /* PTM socket */
338 #ifdef ZEBRA_PTM_SUPPORT
339 zebra_ptm_init();
340 #endif
341
342 zebra_mpls_init();
343 zebra_mpls_vty_init();
344 zebra_pw_vty_init();
345
346 /* For debug purpose. */
347 /* SET_FLAG (zebra_debug_event, ZEBRA_DEBUG_EVENT); */
348
349 #if defined(HANDLE_ZAPI_FUZZING)
350 if (fuzzing) {
351 zserv_read_file(fuzzing);
352 exit(0);
353 }
354 #endif
355
356 /* Process the configuration file. Among other configuration
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 */
363 frr_config_fork();
364
365 /* After we have successfully acquired the pidfile, we can be sure
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 */
373 if (!keep_kernel_mode)
374 rib_sweep_route();
375
376 /* Needed for BSD routing socket. */
377 pid = getpid();
378
379 /* This must be done only after locking pidfile (bug #403). */
380 zebra_zserv_socket_init(zserv_path);
381
382 /* Init label manager */
383 label_manager_init(lblmgr_path);
384
385 /* RNH init */
386 zebra_rnh_init();
387
388 frr_run(zebrad.master);
389
390 /* Not reached... */
391 return 0;
392 }