]> git.proxmox.com Git - mirror_frr.git/blob - bfdd/bfdd.c
lib: rewrite zlog lock-free & TLS-buffered
[mirror_frr.git] / bfdd / bfdd.c
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
23 #include "filter.h"
24
25 #include "bfd.h"
26 #include "bfdd_nb.h"
27 #include "lib/version.h"
28 #include "lib/command.h"
29
30
31 /*
32 * FRR related code.
33 */
34 DEFINE_MGROUP(BFDD, "Bidirectional Forwarding Detection Daemon")
35 DEFINE_MTYPE(BFDD, BFDD_CONTROL, "long-lived control socket memory")
36 DEFINE_MTYPE(BFDD, BFDD_NOTIFICATION, "short-lived control notification data")
37
38 /* Master of threads. */
39 struct thread_master *master;
40
41 /* BFDd privileges */
42 static zebra_capabilities_t _caps_p[] = {ZCAP_BIND, ZCAP_SYS_ADMIN, ZCAP_NET_RAW};
43
44 /* BFD daemon information. */
45 static struct frr_daemon_info bfdd_di;
46
47 void socket_close(int *s)
48 {
49 if (*s <= 0)
50 return;
51
52 if (close(*s) != 0)
53 log_error("%s: close(%d): (%d) %s", __func__, *s, errno,
54 strerror(errno));
55
56 *s = -1;
57 }
58
59 static void sigusr1_handler(void)
60 {
61 zlog_rotate();
62 }
63
64 static void sigterm_handler(void)
65 {
66 /* Signalize shutdown. */
67 frr_early_fini();
68
69 /* Stop receiving message from zebra. */
70 bfdd_zclient_stop();
71
72 /* Shutdown controller to avoid receiving anymore commands. */
73 control_shutdown();
74
75 /* Shutdown and free all protocol related memory. */
76 bfd_shutdown();
77
78 bfd_vrf_terminate();
79
80 /* Terminate and free() FRR related memory. */
81 frr_fini();
82
83 exit(0);
84 }
85
86 static void sighup_handler(void)
87 {
88 zlog_info("SIGHUP received");
89
90 /* Reload config file. */
91 vty_read_config(NULL, bfdd_di.config_file, config_default);
92 }
93
94 static struct quagga_signal_t bfd_signals[] = {
95 {
96 .signal = SIGUSR1,
97 .handler = &sigusr1_handler,
98 },
99 {
100 .signal = SIGTERM,
101 .handler = &sigterm_handler,
102 },
103 {
104 .signal = SIGINT,
105 .handler = &sigterm_handler,
106 },
107 {
108 .signal = SIGHUP,
109 .handler = &sighup_handler,
110 },
111 };
112
113 static const struct frr_yang_module_info *const bfdd_yang_modules[] = {
114 &frr_interface_info,
115 &frr_bfdd_info,
116 };
117
118 FRR_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 = &bglobal.bfdd_privs,
122 .yang_modules = bfdd_yang_modules,
123 .n_yang_modules = array_size(bfdd_yang_modules))
124
125 #define OPTION_CTLSOCK 1001
126 static const struct option longopts[] = {
127 {"bfdctl", required_argument, NULL, OPTION_CTLSOCK},
128 {0}
129 };
130
131
132 /*
133 * BFD daemon related code.
134 */
135 struct bfd_global bglobal;
136
137 const struct bfd_diag_str_list diag_list[] = {
138 {.str = "control-expired", .type = BD_CONTROL_EXPIRED},
139 {.str = "echo-failed", .type = BD_ECHO_FAILED},
140 {.str = "neighbor-down", .type = BD_NEIGHBOR_DOWN},
141 {.str = "forwarding-reset", .type = BD_FORWARDING_RESET},
142 {.str = "path-down", .type = BD_PATH_DOWN},
143 {.str = "concatenated-path-down", .type = BD_CONCATPATH_DOWN},
144 {.str = "administratively-down", .type = BD_ADMIN_DOWN},
145 {.str = "reverse-concat-path-down", .type = BD_REVCONCATPATH_DOWN},
146 {.str = NULL},
147 };
148
149 const struct bfd_state_str_list state_list[] = {
150 {.str = "admin-down", .type = PTM_BFD_ADM_DOWN},
151 {.str = "down", .type = PTM_BFD_DOWN},
152 {.str = "init", .type = PTM_BFD_INIT},
153 {.str = "up", .type = PTM_BFD_UP},
154 {.str = NULL},
155 };
156
157
158 static void bg_init(void)
159 {
160 struct zebra_privs_t bfdd_privs = {
161 #if defined(FRR_USER) && defined(FRR_GROUP)
162 .user = FRR_USER,
163 .group = FRR_GROUP,
164 #endif
165 #if defined(VTY_GROUP)
166 .vty_group = VTY_GROUP,
167 #endif
168 .caps_p = _caps_p,
169 .cap_num_p = array_size(_caps_p),
170 .cap_num_i = 0,
171 };
172
173 TAILQ_INIT(&bglobal.bg_bcslist);
174 TAILQ_INIT(&bglobal.bg_obslist);
175
176 memcpy(&bglobal.bfdd_privs, &bfdd_privs,
177 sizeof(bfdd_privs));
178 }
179
180 int main(int argc, char *argv[])
181 {
182 char ctl_path[512];
183 bool ctlsockused = false;
184 int opt;
185
186 /* Initialize system sockets. */
187 bg_init();
188
189 frr_preinit(&bfdd_di, argc, argv);
190 frr_opt_add("", longopts,
191 " --bfdctl Specify bfdd control socket\n");
192
193 snprintf(ctl_path, sizeof(ctl_path), BFDD_CONTROL_SOCKET,
194 "", "");
195 while (true) {
196 opt = frr_getopt(argc, argv, NULL);
197 if (opt == EOF)
198 break;
199
200 switch (opt) {
201 case OPTION_CTLSOCK:
202 strlcpy(ctl_path, optarg, sizeof(ctl_path));
203 ctlsockused = true;
204 break;
205
206 default:
207 frr_help_exit(1);
208 break;
209 }
210 }
211
212 if (bfdd_di.pathspace && !ctlsockused)
213 snprintf(ctl_path, sizeof(ctl_path), BFDD_CONTROL_SOCKET,
214 "/", bfdd_di.pathspace);
215
216 #if 0 /* TODO add support for JSON configuration files. */
217 parse_config(conf);
218 #endif
219
220 /* Initialize FRR infrastructure. */
221 master = frr_init();
222
223 /* Initialize control socket. */
224 control_init(ctl_path);
225
226 /* Initialize BFD data structures. */
227 bfd_initialize();
228
229 bfd_vrf_init();
230
231 access_list_init();
232
233 /* Initialize zebra connection. */
234 bfdd_zclient_init(&bglobal.bfdd_privs);
235
236 thread_add_read(master, control_accept, NULL, bglobal.bg_csock,
237 &bglobal.bg_csockev);
238
239 /* Install commands. */
240 bfdd_vty_init();
241
242 /* read configuration file and daemonize */
243 frr_config_fork();
244
245 frr_run(master);
246 /* NOTREACHED */
247
248 return 0;
249 }