]> git.proxmox.com Git - mirror_frr.git/blob - bgpd/bgp_main.c
Merge pull request #8992 from opensourcerouting/workflow-knobs
[mirror_frr.git] / bgpd / bgp_main.c
1 /* Main routine of bgpd.
2 * Copyright (C) 1996, 97, 98, 1999 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 <pthread.h>
24 #include "vector.h"
25 #include "command.h"
26 #include "getopt.h"
27 #include "thread.h"
28 #include <lib/version.h>
29 #include "memory.h"
30 #include "prefix.h"
31 #include "log.h"
32 #include "privs.h"
33 #include "sigevent.h"
34 #include "zclient.h"
35 #include "routemap.h"
36 #include "filter.h"
37 #include "plist.h"
38 #include "stream.h"
39 #include "queue.h"
40 #include "vrf.h"
41 #include "bfd.h"
42 #include "libfrr.h"
43 #include "ns.h"
44
45 #include "bgpd/bgpd.h"
46 #include "bgpd/bgp_attr.h"
47 #include "bgpd/bgp_route.h"
48 #include "bgpd/bgp_mplsvpn.h"
49 #include "bgpd/bgp_aspath.h"
50 #include "bgpd/bgp_dump.h"
51 #include "bgpd/bgp_route.h"
52 #include "bgpd/bgp_nexthop.h"
53 #include "bgpd/bgp_regex.h"
54 #include "bgpd/bgp_clist.h"
55 #include "bgpd/bgp_debug.h"
56 #include "bgpd/bgp_errors.h"
57 #include "bgpd/bgp_filter.h"
58 #include "bgpd/bgp_zebra.h"
59 #include "bgpd/bgp_packet.h"
60 #include "bgpd/bgp_keepalives.h"
61 #include "bgpd/bgp_network.h"
62 #include "bgpd/bgp_errors.h"
63 #include "bgpd/bgp_script.h"
64 #include "lib/routing_nb.h"
65 #include "bgpd/bgp_nb.h"
66 #include "bgpd/bgp_evpn_mh.h"
67 #include "bgpd/bgp_nht.h"
68 #include "bgpd/bgp_routemap_nb.h"
69 #include "bgpd/bgp_community_alias.h"
70
71 #ifdef ENABLE_BGP_VNC
72 #include "bgpd/rfapi/rfapi_backend.h"
73 #endif
74
75 /* bgpd options, we use GNU getopt library. */
76 static const struct option longopts[] = {
77 {"bgp_port", required_argument, NULL, 'p'},
78 {"listenon", required_argument, NULL, 'l'},
79 {"no_kernel", no_argument, NULL, 'n'},
80 {"skip_runas", no_argument, NULL, 'S'},
81 {"ecmp", required_argument, NULL, 'e'},
82 {"int_num", required_argument, NULL, 'I'},
83 {"no_zebra", no_argument, NULL, 'Z'},
84 {"socket_size", required_argument, NULL, 's'},
85 {0}};
86
87 /* signal definitions */
88 void sighup(void);
89 void sigint(void);
90 void sigusr1(void);
91
92 static void bgp_exit(int);
93 static void bgp_vrf_terminate(void);
94
95 static struct quagga_signal_t bgp_signals[] = {
96 {
97 .signal = SIGHUP,
98 .handler = &sighup,
99 },
100 {
101 .signal = SIGUSR1,
102 .handler = &sigusr1,
103 },
104 {
105 .signal = SIGINT,
106 .handler = &sigint,
107 },
108 {
109 .signal = SIGTERM,
110 .handler = &sigint,
111 },
112 };
113
114 /* privileges */
115 static zebra_capabilities_t _caps_p[] = {ZCAP_BIND, ZCAP_NET_RAW,
116 ZCAP_NET_ADMIN, ZCAP_SYS_ADMIN};
117
118 struct zebra_privs_t bgpd_privs = {
119 #if defined(FRR_USER) && defined(FRR_GROUP)
120 .user = FRR_USER,
121 .group = FRR_GROUP,
122 #endif
123 #ifdef VTY_GROUP
124 .vty_group = VTY_GROUP,
125 #endif
126 .caps_p = _caps_p,
127 .cap_num_p = array_size(_caps_p),
128 .cap_num_i = 0,
129 };
130
131 static struct frr_daemon_info bgpd_di;
132
133 /* SIGHUP handler. */
134 void sighup(void)
135 {
136 zlog_info("SIGHUP received, ignoring");
137
138 return;
139
140 /*
141 * This is turned off for the moment. There is all
142 * sorts of config turned off by bgp_terminate
143 * that is not setup properly again in bgp_reset.
144 * I see no easy way to do this nor do I see that
145 * this is a desirable way to reload config
146 * given the yang work.
147 */
148 /* Terminate all thread. */
149 /*
150 * bgp_terminate();
151 * bgp_reset();
152 * zlog_info("bgpd restarting!");
153
154 * Reload config file.
155 * vty_read_config(NULL, bgpd_di.config_file, config_default);
156 */
157 /* Try to return to normal operation. */
158 }
159
160 /* SIGINT handler. */
161 __attribute__((__noreturn__)) void sigint(void)
162 {
163 zlog_notice("Terminating on signal");
164 assert(bm->terminating == false);
165 bm->terminating = true; /* global flag that shutting down */
166
167 /* Disable BFD events to avoid wasting processing. */
168 bfd_protocol_integration_set_shutdown(true);
169
170 bgp_terminate();
171
172 bgp_exit(0);
173
174 exit(0);
175 }
176
177 /* SIGUSR1 handler. */
178 void sigusr1(void)
179 {
180 zlog_rotate();
181 }
182
183 /*
184 Try to free up allocations we know about so that diagnostic tools such as
185 valgrind are able to better illuminate leaks.
186
187 Zebra route removal and protocol teardown are not meant to be done here.
188 For example, "retain_mode" may be set.
189 */
190 static __attribute__((__noreturn__)) void bgp_exit(int status)
191 {
192 struct bgp *bgp, *bgp_default, *bgp_evpn;
193 struct listnode *node, *nnode;
194
195 /* it only makes sense for this to be called on a clean exit */
196 assert(status == 0);
197
198 frr_early_fini();
199
200 bgp_close();
201
202 bgp_default = bgp_get_default();
203 bgp_evpn = bgp_get_evpn();
204
205 /* reverse bgp_master_init */
206 for (ALL_LIST_ELEMENTS(bm->bgp, node, nnode, bgp)) {
207 if (bgp_default == bgp || bgp_evpn == bgp)
208 continue;
209 bgp_delete(bgp);
210 }
211 if (bgp_evpn && bgp_evpn != bgp_default)
212 bgp_delete(bgp_evpn);
213 if (bgp_default)
214 bgp_delete(bgp_default);
215
216 bgp_evpn_mh_finish();
217 bgp_l3nhg_finish();
218
219 /* reverse bgp_dump_init */
220 bgp_dump_finish();
221
222 /* BGP community aliases */
223 bgp_community_alias_finish();
224
225 /* reverse bgp_route_init */
226 bgp_route_finish();
227
228 /* cleanup route maps */
229 bgp_route_map_terminate();
230
231 /* reverse bgp_attr_init */
232 bgp_attr_finish();
233
234 /* stop pthreads */
235 bgp_pthreads_finish();
236
237 /* reverse access_list_init */
238 access_list_add_hook(NULL);
239 access_list_delete_hook(NULL);
240 access_list_reset();
241
242 /* reverse bgp_filter_init */
243 as_list_add_hook(NULL);
244 as_list_delete_hook(NULL);
245 bgp_filter_reset();
246
247 /* reverse prefix_list_init */
248 prefix_list_add_hook(NULL);
249 prefix_list_delete_hook(NULL);
250 prefix_list_reset();
251
252 /* reverse community_list_init */
253 community_list_terminate(bgp_clist);
254
255 bgp_vrf_terminate();
256 #ifdef ENABLE_BGP_VNC
257 vnc_zebra_destroy();
258 #endif
259 bgp_zebra_destroy();
260
261 bf_free(bm->rd_idspace);
262 list_delete(&bm->bgp);
263 list_delete(&bm->addresses);
264
265 bgp_lp_finish();
266
267 memset(bm, 0, sizeof(*bm));
268
269 frr_fini();
270 exit(status);
271 }
272
273 static int bgp_vrf_new(struct vrf *vrf)
274 {
275 if (BGP_DEBUG(zebra, ZEBRA))
276 zlog_debug("VRF Created: %s(%u)", vrf->name, vrf->vrf_id);
277
278 return 0;
279 }
280
281 static int bgp_vrf_delete(struct vrf *vrf)
282 {
283 if (BGP_DEBUG(zebra, ZEBRA))
284 zlog_debug("VRF Deletion: %s(%u)", vrf->name, vrf->vrf_id);
285
286 return 0;
287 }
288
289 static int bgp_vrf_enable(struct vrf *vrf)
290 {
291 struct bgp *bgp;
292 vrf_id_t old_vrf_id;
293
294 if (BGP_DEBUG(zebra, ZEBRA))
295 zlog_debug("VRF enable add %s id %u", vrf->name, vrf->vrf_id);
296
297 bgp = bgp_lookup_by_name(vrf->name);
298 if (bgp && bgp->vrf_id != vrf->vrf_id) {
299 if (bgp->name && strmatch(vrf->name, VRF_DEFAULT_NAME)) {
300 XFREE(MTYPE_BGP, bgp->name);
301 XFREE(MTYPE_BGP, bgp->name_pretty);
302 bgp->name_pretty = XSTRDUP(MTYPE_BGP, "VRF default");
303 bgp->inst_type = BGP_INSTANCE_TYPE_DEFAULT;
304 #ifdef ENABLE_BGP_VNC
305 if (!bgp->rfapi) {
306 bgp->rfapi = bgp_rfapi_new(bgp);
307 assert(bgp->rfapi);
308 assert(bgp->rfapi_cfg);
309 }
310 #endif /* ENABLE_BGP_VNC */
311 }
312 old_vrf_id = bgp->vrf_id;
313 /* We have instance configured, link to VRF and make it "up". */
314 bgp_vrf_link(bgp, vrf);
315
316 bgp_handle_socket(bgp, vrf, old_vrf_id, true);
317 bgp_instance_up(bgp);
318 vpn_leak_zebra_vrf_label_update(bgp, AFI_IP);
319 vpn_leak_zebra_vrf_label_update(bgp, AFI_IP6);
320 vpn_leak_zebra_vrf_sid_update(bgp, AFI_IP);
321 vpn_leak_zebra_vrf_sid_update(bgp, AFI_IP6);
322 vpn_leak_postchange(BGP_VPN_POLICY_DIR_TOVPN, AFI_IP,
323 bgp_get_default(), bgp);
324 vpn_leak_postchange(BGP_VPN_POLICY_DIR_FROMVPN, AFI_IP,
325 bgp_get_default(), bgp);
326 vpn_leak_postchange(BGP_VPN_POLICY_DIR_TOVPN, AFI_IP6,
327 bgp_get_default(), bgp);
328 vpn_leak_postchange(BGP_VPN_POLICY_DIR_FROMVPN, AFI_IP6,
329 bgp_get_default(), bgp);
330 }
331
332 return 0;
333 }
334
335 static int bgp_vrf_disable(struct vrf *vrf)
336 {
337 struct bgp *bgp;
338
339 if (vrf->vrf_id == VRF_DEFAULT)
340 return 0;
341
342 if (BGP_DEBUG(zebra, ZEBRA))
343 zlog_debug("VRF disable %s id %d", vrf->name, vrf->vrf_id);
344
345 bgp = bgp_lookup_by_name(vrf->name);
346 if (bgp) {
347
348 vpn_leak_zebra_vrf_label_withdraw(bgp, AFI_IP);
349 vpn_leak_zebra_vrf_label_withdraw(bgp, AFI_IP6);
350 vpn_leak_prechange(BGP_VPN_POLICY_DIR_TOVPN, AFI_IP,
351 bgp_get_default(), bgp);
352 vpn_leak_prechange(BGP_VPN_POLICY_DIR_FROMVPN, AFI_IP,
353 bgp_get_default(), bgp);
354 vpn_leak_prechange(BGP_VPN_POLICY_DIR_TOVPN, AFI_IP6,
355 bgp_get_default(), bgp);
356 vpn_leak_prechange(BGP_VPN_POLICY_DIR_FROMVPN, AFI_IP6,
357 bgp_get_default(), bgp);
358
359 bgp_handle_socket(bgp, vrf, VRF_UNKNOWN, false);
360 /* We have instance configured, unlink from VRF and make it
361 * "down". */
362 bgp_instance_down(bgp);
363 bgp_vrf_unlink(bgp, vrf);
364 }
365
366 /* Note: This is a callback, the VRF will be deleted by the caller. */
367 return 0;
368 }
369
370 static void bgp_vrf_init(void)
371 {
372 vrf_init(bgp_vrf_new, bgp_vrf_enable, bgp_vrf_disable,
373 bgp_vrf_delete, bgp_vrf_enable);
374 }
375
376 static void bgp_vrf_terminate(void)
377 {
378 vrf_terminate();
379 }
380
381 static const struct frr_yang_module_info *const bgpd_yang_modules[] = {
382 &frr_bgp_info,
383 &frr_filter_info,
384 &frr_interface_info,
385 &frr_route_map_info,
386 &frr_routing_info,
387 &frr_vrf_info,
388 &frr_bgp_route_map_info,
389 };
390
391 FRR_DAEMON_INFO(bgpd, BGP, .vty_port = BGP_VTY_PORT,
392
393 .proghelp = "Implementation of the BGP routing protocol.",
394
395 .signals = bgp_signals, .n_signals = array_size(bgp_signals),
396
397 .privs = &bgpd_privs, .yang_modules = bgpd_yang_modules,
398 .n_yang_modules = array_size(bgpd_yang_modules),
399 );
400
401 #define DEPRECATED_OPTIONS ""
402
403 /* Main routine of bgpd. Treatment of argument and start bgp finite
404 state machine is handled at here. */
405 int main(int argc, char **argv)
406 {
407 int opt;
408 int tmp_port;
409
410 int bgp_port = BGP_PORT_DEFAULT;
411 struct list *addresses = list_new();
412 int no_fib_flag = 0;
413 int no_zebra_flag = 0;
414 int skip_runas = 0;
415 int instance = 0;
416 int buffer_size = BGP_SOCKET_SNDBUF_SIZE;
417 char *address;
418 struct listnode *node;
419
420 addresses->cmp = (int (*)(void *, void *))strcmp;
421
422 frr_preinit(&bgpd_di, argc, argv);
423 frr_opt_add(
424 "p:l:SnZe:I:s:" DEPRECATED_OPTIONS, longopts,
425 " -p, --bgp_port Set BGP listen port number (0 means do not listen).\n"
426 " -l, --listenon Listen on specified address (implies -n)\n"
427 " -n, --no_kernel Do not install route to kernel.\n"
428 " -Z, --no_zebra Do not communicate with Zebra.\n"
429 " -S, --skip_runas Skip capabilities checks, and changing user and group IDs.\n"
430 " -e, --ecmp Specify ECMP to use.\n"
431 " -I, --int_num Set instance number (label-manager)\n"
432 " -s, --socket_size Set BGP peer socket send buffer size\n");
433
434 /* Command line argument treatment. */
435 while (1) {
436 opt = frr_getopt(argc, argv, 0);
437
438 if (opt && opt < 128 && strchr(DEPRECATED_OPTIONS, opt)) {
439 fprintf(stderr,
440 "The -%c option no longer exists.\nPlease refer to the manual.\n",
441 opt);
442 continue;
443 }
444
445 if (opt == EOF)
446 break;
447
448 switch (opt) {
449 case 0:
450 break;
451 case 'p':
452 tmp_port = atoi(optarg);
453 if (tmp_port < 0 || tmp_port > 0xffff)
454 bgp_port = BGP_PORT_DEFAULT;
455 else
456 bgp_port = tmp_port;
457 break;
458 case 'e': {
459 unsigned long int parsed_multipath =
460 strtoul(optarg, NULL, 10);
461 if (parsed_multipath == 0
462 || parsed_multipath > MULTIPATH_NUM
463 || parsed_multipath > UINT_MAX) {
464 flog_err(
465 EC_BGP_MULTIPATH,
466 "Multipath Number specified must be less than %u and greater than 0",
467 MULTIPATH_NUM);
468 return 1;
469 }
470 multipath_num = parsed_multipath;
471 break;
472 }
473 case 'l':
474 listnode_add_sort_nodup(addresses, optarg);
475 /* listenon implies -n */
476 /* fallthru */
477 case 'n':
478 no_fib_flag = 1;
479 break;
480 case 'Z':
481 no_zebra_flag = 1;
482 break;
483 case 'S':
484 skip_runas = 1;
485 break;
486 case 'I':
487 instance = atoi(optarg);
488 if (instance > (unsigned short)-1)
489 zlog_err("Instance %i out of range (0..%u)",
490 instance, (unsigned short)-1);
491 break;
492 case 's':
493 buffer_size = atoi(optarg);
494 break;
495 default:
496 frr_help_exit(1);
497 break;
498 }
499 }
500 if (skip_runas)
501 memset(&bgpd_privs, 0, sizeof(bgpd_privs));
502
503 /* BGP master init. */
504 bgp_master_init(frr_init(), buffer_size, addresses);
505 bm->port = bgp_port;
506 if (bgp_port == 0)
507 bgp_option_set(BGP_OPT_NO_LISTEN);
508 if (no_fib_flag || no_zebra_flag)
509 bgp_option_set(BGP_OPT_NO_FIB);
510 if (no_zebra_flag)
511 bgp_option_set(BGP_OPT_NO_ZEBRA);
512 bgp_error_init();
513 /* Initializations. */
514 bgp_vrf_init();
515
516 #ifdef HAVE_SCRIPTING
517 bgp_script_init();
518 #endif
519
520 hook_register(routing_conf_event,
521 routing_control_plane_protocols_name_validate);
522
523
524 /* BGP related initialization. */
525 bgp_init((unsigned short)instance);
526
527 if (list_isempty(bm->addresses)) {
528 snprintf(bgpd_di.startinfo, sizeof(bgpd_di.startinfo),
529 ", bgp@<all>:%d", bm->port);
530 } else {
531 for (ALL_LIST_ELEMENTS_RO(bm->addresses, node, address))
532 snprintf(bgpd_di.startinfo + strlen(bgpd_di.startinfo),
533 sizeof(bgpd_di.startinfo)
534 - strlen(bgpd_di.startinfo),
535 ", bgp@%s:%d", address, bm->port);
536 }
537
538 frr_config_fork();
539 /* must be called after fork() */
540 bgp_gr_apply_running_config();
541 bgp_pthreads_run();
542 frr_run(bm->master);
543
544 /* Not reached. */
545 return 0;
546 }