]>
Commit | Line | Data |
---|---|---|
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 | ||
e9e2c950 RZ |
23 | #include "bfd.h" |
24 | #include "lib/version.h" | |
6ed84949 | 25 | |
e9e2c950 RZ |
26 | |
27 | /* | |
28 | * FRR related code. | |
29 | */ | |
30 | DEFINE_MGROUP(BFDD, "Bidirectional Forwarding Detection Daemon"); | |
31 | DEFINE_MTYPE(BFDD, BFDD_TMP, "short-lived temporary memory"); | |
32 | DEFINE_MTYPE(BFDD, BFDD_CONFIG, "long-lived configuration memory"); | |
33 | DEFINE_MTYPE(BFDD, BFDD_LABEL, "long-lived label memory"); | |
34 | DEFINE_MTYPE(BFDD, BFDD_CONTROL, "long-lived control socket memory"); | |
35 | DEFINE_MTYPE(BFDD, BFDD_NOTIFICATION, "short-lived control notification data"); | |
36 | ||
37 | /* Master of threads. */ | |
38 | struct thread_master *master; | |
39 | ||
40 | /* BFDd privileges */ | |
41 | static zebra_capabilities_t _caps_p[] = {ZCAP_BIND}; | |
42 | ||
43 | struct zebra_privs_t bfdd_privs = { | |
44 | #if defined(FRR_USER) && defined(FRR_GROUP) | |
45 | .user = FRR_USER, | |
46 | .group = FRR_GROUP, | |
47 | #endif | |
48 | #if defined(VTY_GROUP) | |
49 | .vty_group = VTY_GROUP, | |
50 | #endif | |
51 | .caps_p = _caps_p, | |
52 | .cap_num_p = array_size(_caps_p), | |
53 | .cap_num_i = 0, | |
54 | }; | |
55 | ||
56 | void socket_close(int *s) | |
57 | { | |
58 | if (*s <= 0) | |
59 | return; | |
60 | ||
61 | if (close(*s) != 0) | |
62 | log_error("%s: close(%d): (%d) %s", __func__, *s, errno, | |
63 | strerror(errno)); | |
64 | ||
65 | *s = -1; | |
66 | } | |
67 | ||
68 | static void sigusr1_handler(void) | |
69 | { | |
70 | zlog_rotate(); | |
71 | } | |
72 | ||
73 | static void sigterm_handler(void) | |
74 | { | |
75 | /* Signalize shutdown. */ | |
76 | frr_early_fini(); | |
77 | ||
d3af6147 RZ |
78 | /* Stop receiving message from zebra. */ |
79 | bfdd_zclient_stop(); | |
80 | ||
e9e2c950 RZ |
81 | /* Shutdown controller to avoid receiving anymore commands. */ |
82 | control_shutdown(); | |
83 | ||
84 | /* Shutdown and free all protocol related memory. */ | |
85 | bfd_shutdown(); | |
86 | ||
87 | /* Close all descriptors. */ | |
88 | socket_close(&bglobal.bg_echo); | |
89 | socket_close(&bglobal.bg_shop); | |
90 | socket_close(&bglobal.bg_mhop); | |
91 | socket_close(&bglobal.bg_shop6); | |
92 | socket_close(&bglobal.bg_mhop6); | |
e9e2c950 RZ |
93 | |
94 | /* Terminate and free() FRR related memory. */ | |
95 | frr_fini(); | |
96 | ||
97 | exit(0); | |
98 | } | |
99 | ||
100 | static struct quagga_signal_t bfd_signals[] = { | |
101 | { | |
102 | .signal = SIGUSR1, | |
103 | .handler = &sigusr1_handler, | |
104 | }, | |
105 | { | |
106 | .signal = SIGTERM, | |
107 | .handler = &sigterm_handler, | |
108 | }, | |
109 | { | |
110 | .signal = SIGINT, | |
111 | .handler = &sigterm_handler, | |
112 | }, | |
113 | }; | |
114 | ||
115 | FRR_DAEMON_INFO(bfdd, BFD, .vty_port = 2617, | |
116 | .proghelp = "Implementation of the BFD protocol.", | |
117 | .signals = bfd_signals, .n_signals = array_size(bfd_signals), | |
118 | .privs = &bfdd_privs) | |
119 | ||
120 | #define OPTION_CTLSOCK 1001 | |
121 | static struct option longopts[] = { | |
122 | {"bfdctl", required_argument, NULL, OPTION_CTLSOCK}, | |
123 | {0} | |
124 | }; | |
125 | ||
126 | ||
127 | /* | |
128 | * BFD daemon related code. | |
129 | */ | |
130 | struct bfd_global bglobal; | |
131 | ||
132 | struct bfd_diag_str_list diag_list[] = { | |
40675ea9 RZ |
133 | {.str = "control-expired", .type = BD_CONTROL_EXPIRED}, |
134 | {.str = "echo-failed", .type = BD_ECHO_FAILED}, | |
135 | {.str = "neighbor-down", .type = BD_NEIGHBOR_DOWN}, | |
136 | {.str = "forwarding-reset", .type = BD_FORWARDING_RESET}, | |
137 | {.str = "path-down", .type = BD_PATH_DOWN}, | |
138 | {.str = "concatenated-path-down", .type = BD_CONCATPATH_DOWN}, | |
139 | {.str = "administratively-down", .type = BD_ADMIN_DOWN}, | |
140 | {.str = "reverse-concat-path-down", .type = BD_REVCONCATPATH_DOWN}, | |
e9e2c950 RZ |
141 | {.str = NULL}, |
142 | }; | |
143 | ||
144 | struct bfd_state_str_list state_list[] = { | |
40675ea9 RZ |
145 | {.str = "admin-down", .type = PTM_BFD_ADM_DOWN}, |
146 | {.str = "down", .type = PTM_BFD_DOWN}, | |
147 | {.str = "init", .type = PTM_BFD_INIT}, | |
148 | {.str = "up", .type = PTM_BFD_UP}, | |
e9e2c950 RZ |
149 | {.str = NULL}, |
150 | }; | |
151 | ||
152 | ||
153 | static void bg_init(void) | |
154 | { | |
155 | TAILQ_INIT(&bglobal.bg_bcslist); | |
156 | ||
157 | bglobal.bg_shop = bp_udp_shop(); | |
158 | bglobal.bg_mhop = bp_udp_mhop(); | |
159 | bglobal.bg_shop6 = bp_udp6_shop(); | |
160 | bglobal.bg_mhop6 = bp_udp6_mhop(); | |
2f11c53f RZ |
161 | bglobal.bg_echo = bp_echo_socket(); |
162 | bglobal.bg_echov6 = bp_echov6_socket(); | |
e9e2c950 RZ |
163 | } |
164 | ||
165 | int main(int argc, char *argv[]) | |
166 | { | |
167 | const char *ctl_path = BFDD_CONTROL_SOCKET; | |
168 | int opt; | |
169 | ||
170 | frr_preinit(&bfdd_di, argc, argv); | |
171 | frr_opt_add("", longopts, | |
172 | " --bfdctl Specify bfdd control socket\n"); | |
173 | ||
174 | while (true) { | |
175 | opt = frr_getopt(argc, argv, NULL); | |
176 | if (opt == EOF) | |
177 | break; | |
178 | ||
179 | switch (opt) { | |
180 | case OPTION_CTLSOCK: | |
181 | ctl_path = optarg; | |
182 | break; | |
183 | ||
184 | default: | |
185 | frr_help_exit(1); | |
186 | break; | |
187 | } | |
188 | } | |
189 | ||
190 | #if 0 /* TODO add support for JSON configuration files. */ | |
191 | parse_config(conf); | |
192 | #endif | |
193 | ||
194 | /* Initialize logging API. */ | |
195 | log_init(1, BLOG_DEBUG, &bfdd_di); | |
196 | ||
197 | /* Initialize system sockets. */ | |
198 | bg_init(); | |
199 | ||
200 | /* Initialize control socket. */ | |
201 | control_init(ctl_path); | |
202 | ||
203 | /* Initialize FRR infrastructure. */ | |
204 | master = frr_init(); | |
205 | ||
206 | /* Initialize BFD data structures. */ | |
207 | bfd_initialize(); | |
208 | ||
d3af6147 RZ |
209 | /* Initialize zebra connection. */ |
210 | bfdd_zclient_init(&bfdd_privs); | |
211 | ||
e9e2c950 RZ |
212 | /* Add descriptors to the event loop. */ |
213 | thread_add_read(master, bfd_recv_cb, NULL, bglobal.bg_shop, | |
214 | &bglobal.bg_ev[0]); | |
215 | thread_add_read(master, bfd_recv_cb, NULL, bglobal.bg_mhop, | |
216 | &bglobal.bg_ev[1]); | |
217 | thread_add_read(master, bfd_recv_cb, NULL, bglobal.bg_shop6, | |
218 | &bglobal.bg_ev[2]); | |
219 | thread_add_read(master, bfd_recv_cb, NULL, bglobal.bg_mhop6, | |
220 | &bglobal.bg_ev[3]); | |
221 | thread_add_read(master, bfd_recv_cb, NULL, bglobal.bg_echo, | |
222 | &bglobal.bg_ev[4]); | |
2f11c53f RZ |
223 | thread_add_read(master, bfd_recv_cb, NULL, bglobal.bg_echov6, |
224 | &bglobal.bg_ev[5]); | |
e9e2c950 RZ |
225 | thread_add_read(master, control_accept, NULL, bglobal.bg_csock, |
226 | &bglobal.bg_csockev); | |
227 | ||
c2f29cf3 RZ |
228 | /* Install commands. */ |
229 | bfdd_vty_init(); | |
230 | ||
e9e2c950 RZ |
231 | /* read configuration file and daemonize */ |
232 | frr_config_fork(); | |
233 | ||
234 | frr_run(master); | |
235 | /* NOTREACHED */ | |
236 | ||
237 | return 0; | |
238 | } |