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