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