]> git.proxmox.com Git - mirror_frr.git/blame - bfdd/bfdd.c
rmap: Modify cli helper text for `match_ipv6_next_hop_type_cmd`
[mirror_frr.git] / bfdd / bfdd.c
CommitLineData
e9e2c950
RZ
1/*
2 * BFD daemon code
3 * Copyright (C) 2018 Network Device Education Foundation, Inc. ("NetDEF")
4 *
5 * FRR is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2, or (at your option) any
8 * later version.
9 *
10 * FRR is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with FRR; see the file COPYING. If not, write to the Free
17 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
18 * 02111-1307, USA.
19 */
20
21#include <zebra.h>
22
4feb4d9d
DS
23#include "filter.h"
24
e9e2c950
RZ
25#include "bfd.h"
26#include "lib/version.h"
6ed84949 27
e9e2c950
RZ
28
29/*
30 * FRR related code.
31 */
32DEFINE_MGROUP(BFDD, "Bidirectional Forwarding Detection Daemon");
33DEFINE_MTYPE(BFDD, BFDD_TMP, "short-lived temporary memory");
34DEFINE_MTYPE(BFDD, BFDD_CONFIG, "long-lived configuration memory");
35DEFINE_MTYPE(BFDD, BFDD_LABEL, "long-lived label memory");
36DEFINE_MTYPE(BFDD, BFDD_CONTROL, "long-lived control socket memory");
d245e522 37DEFINE_MTYPE(BFDD, BFDD_SESSION_OBSERVER, "Session observer");
e9e2c950
RZ
38DEFINE_MTYPE(BFDD, BFDD_NOTIFICATION, "short-lived control notification data");
39
40/* Master of threads. */
41struct thread_master *master;
42
43/* BFDd privileges */
44static zebra_capabilities_t _caps_p[] = {ZCAP_BIND};
45
46struct zebra_privs_t bfdd_privs = {
47#if defined(FRR_USER) && defined(FRR_GROUP)
48 .user = FRR_USER,
49 .group = FRR_GROUP,
50#endif
51#if defined(VTY_GROUP)
52 .vty_group = VTY_GROUP,
53#endif
54 .caps_p = _caps_p,
55 .cap_num_p = array_size(_caps_p),
56 .cap_num_i = 0,
57};
58
59void socket_close(int *s)
60{
61 if (*s <= 0)
62 return;
63
64 if (close(*s) != 0)
65 log_error("%s: close(%d): (%d) %s", __func__, *s, errno,
66 strerror(errno));
67
68 *s = -1;
69}
70
71static void sigusr1_handler(void)
72{
73 zlog_rotate();
74}
75
76static void sigterm_handler(void)
77{
78 /* Signalize shutdown. */
79 frr_early_fini();
80
d3af6147
RZ
81 /* Stop receiving message from zebra. */
82 bfdd_zclient_stop();
83
e9e2c950
RZ
84 /* Shutdown controller to avoid receiving anymore commands. */
85 control_shutdown();
86
87 /* Shutdown and free all protocol related memory. */
88 bfd_shutdown();
89
90 /* Close all descriptors. */
91 socket_close(&bglobal.bg_echo);
92 socket_close(&bglobal.bg_shop);
93 socket_close(&bglobal.bg_mhop);
94 socket_close(&bglobal.bg_shop6);
95 socket_close(&bglobal.bg_mhop6);
e9e2c950
RZ
96
97 /* Terminate and free() FRR related memory. */
98 frr_fini();
99
100 exit(0);
101}
102
103static struct quagga_signal_t bfd_signals[] = {
104 {
105 .signal = SIGUSR1,
106 .handler = &sigusr1_handler,
107 },
108 {
109 .signal = SIGTERM,
110 .handler = &sigterm_handler,
111 },
112 {
113 .signal = SIGINT,
114 .handler = &sigterm_handler,
115 },
116};
117
118FRR_DAEMON_INFO(bfdd, BFD, .vty_port = 2617,
119 .proghelp = "Implementation of the BFD protocol.",
120 .signals = bfd_signals, .n_signals = array_size(bfd_signals),
121 .privs = &bfdd_privs)
122
123#define OPTION_CTLSOCK 1001
124static struct option longopts[] = {
125 {"bfdctl", required_argument, NULL, OPTION_CTLSOCK},
126 {0}
127};
128
129
130/*
131 * BFD daemon related code.
132 */
133struct bfd_global bglobal;
134
135struct bfd_diag_str_list diag_list[] = {
40675ea9
RZ
136 {.str = "control-expired", .type = BD_CONTROL_EXPIRED},
137 {.str = "echo-failed", .type = BD_ECHO_FAILED},
138 {.str = "neighbor-down", .type = BD_NEIGHBOR_DOWN},
139 {.str = "forwarding-reset", .type = BD_FORWARDING_RESET},
140 {.str = "path-down", .type = BD_PATH_DOWN},
141 {.str = "concatenated-path-down", .type = BD_CONCATPATH_DOWN},
142 {.str = "administratively-down", .type = BD_ADMIN_DOWN},
143 {.str = "reverse-concat-path-down", .type = BD_REVCONCATPATH_DOWN},
e9e2c950
RZ
144 {.str = NULL},
145};
146
147struct bfd_state_str_list state_list[] = {
40675ea9
RZ
148 {.str = "admin-down", .type = PTM_BFD_ADM_DOWN},
149 {.str = "down", .type = PTM_BFD_DOWN},
150 {.str = "init", .type = PTM_BFD_INIT},
151 {.str = "up", .type = PTM_BFD_UP},
e9e2c950
RZ
152 {.str = NULL},
153};
154
155
156static void bg_init(void)
157{
158 TAILQ_INIT(&bglobal.bg_bcslist);
d245e522 159 TAILQ_INIT(&bglobal.bg_obslist);
e9e2c950
RZ
160
161 bglobal.bg_shop = bp_udp_shop();
162 bglobal.bg_mhop = bp_udp_mhop();
163 bglobal.bg_shop6 = bp_udp6_shop();
164 bglobal.bg_mhop6 = bp_udp6_mhop();
2f11c53f
RZ
165 bglobal.bg_echo = bp_echo_socket();
166 bglobal.bg_echov6 = bp_echov6_socket();
e9e2c950
RZ
167}
168
169int main(int argc, char *argv[])
170{
171 const char *ctl_path = BFDD_CONTROL_SOCKET;
172 int opt;
173
174 frr_preinit(&bfdd_di, argc, argv);
175 frr_opt_add("", longopts,
176 " --bfdctl Specify bfdd control socket\n");
177
178 while (true) {
179 opt = frr_getopt(argc, argv, NULL);
180 if (opt == EOF)
181 break;
182
183 switch (opt) {
184 case OPTION_CTLSOCK:
185 ctl_path = optarg;
186 break;
187
188 default:
189 frr_help_exit(1);
190 break;
191 }
192 }
193
194#if 0 /* TODO add support for JSON configuration files. */
195 parse_config(conf);
196#endif
197
198 /* Initialize logging API. */
199 log_init(1, BLOG_DEBUG, &bfdd_di);
200
201 /* Initialize system sockets. */
202 bg_init();
203
204 /* Initialize control socket. */
205 control_init(ctl_path);
206
207 /* Initialize FRR infrastructure. */
208 master = frr_init();
209
210 /* Initialize BFD data structures. */
211 bfd_initialize();
212
4feb4d9d
DS
213 access_list_init();
214
d3af6147
RZ
215 /* Initialize zebra connection. */
216 bfdd_zclient_init(&bfdd_privs);
217
e9e2c950
RZ
218 /* Add descriptors to the event loop. */
219 thread_add_read(master, bfd_recv_cb, NULL, bglobal.bg_shop,
220 &bglobal.bg_ev[0]);
221 thread_add_read(master, bfd_recv_cb, NULL, bglobal.bg_mhop,
222 &bglobal.bg_ev[1]);
223 thread_add_read(master, bfd_recv_cb, NULL, bglobal.bg_shop6,
224 &bglobal.bg_ev[2]);
225 thread_add_read(master, bfd_recv_cb, NULL, bglobal.bg_mhop6,
226 &bglobal.bg_ev[3]);
227 thread_add_read(master, bfd_recv_cb, NULL, bglobal.bg_echo,
228 &bglobal.bg_ev[4]);
2f11c53f
RZ
229 thread_add_read(master, bfd_recv_cb, NULL, bglobal.bg_echov6,
230 &bglobal.bg_ev[5]);
e9e2c950
RZ
231 thread_add_read(master, control_accept, NULL, bglobal.bg_csock,
232 &bglobal.bg_csockev);
233
c2f29cf3
RZ
234 /* Install commands. */
235 bfdd_vty_init();
236
e9e2c950
RZ
237 /* read configuration file and daemonize */
238 frr_config_fork();
239
240 frr_run(master);
241 /* NOTREACHED */
242
243 return 0;
244}