3 * Copyright (C) 2018 Network Device Education Foundation, Inc. ("NetDEF")
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
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.
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
26 #include "lib/version.h"
32 DEFINE_MGROUP(BFDD
, "Bidirectional Forwarding Detection Daemon");
33 DEFINE_MTYPE(BFDD
, BFDD_TMP
, "short-lived temporary memory");
34 DEFINE_MTYPE(BFDD
, BFDD_CONFIG
, "long-lived configuration memory");
35 DEFINE_MTYPE(BFDD
, BFDD_LABEL
, "long-lived label memory");
36 DEFINE_MTYPE(BFDD
, BFDD_CONTROL
, "long-lived control socket memory");
37 DEFINE_MTYPE(BFDD
, BFDD_SESSION_OBSERVER
, "Session observer");
38 DEFINE_MTYPE(BFDD
, BFDD_NOTIFICATION
, "short-lived control notification data");
40 /* Master of threads. */
41 struct thread_master
*master
;
44 static zebra_capabilities_t _caps_p
[] = {ZCAP_BIND
};
46 struct zebra_privs_t bfdd_privs
= {
47 #if defined(FRR_USER) && defined(FRR_GROUP)
51 #if defined(VTY_GROUP)
52 .vty_group
= VTY_GROUP
,
55 .cap_num_p
= array_size(_caps_p
),
59 void socket_close(int *s
)
65 log_error("%s: close(%d): (%d) %s", __func__
, *s
, errno
,
71 static void sigusr1_handler(void)
76 static void sigterm_handler(void)
78 /* Signalize shutdown. */
81 /* Stop receiving message from zebra. */
84 /* Shutdown controller to avoid receiving anymore commands. */
87 /* Shutdown and free all protocol related memory. */
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
);
97 /* Terminate and free() FRR related memory. */
103 static struct quagga_signal_t bfd_signals
[] = {
106 .handler
= &sigusr1_handler
,
110 .handler
= &sigterm_handler
,
114 .handler
= &sigterm_handler
,
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
= &bfdd_privs
)
123 #define OPTION_CTLSOCK 1001
124 static struct option longopts
[] = {
125 {"bfdctl", required_argument
, NULL
, OPTION_CTLSOCK
},
131 * BFD daemon related code.
133 struct bfd_global bglobal
;
135 struct bfd_diag_str_list diag_list
[] = {
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
},
147 struct bfd_state_str_list state_list
[] = {
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
},
156 static void bg_init(void)
158 TAILQ_INIT(&bglobal
.bg_bcslist
);
159 TAILQ_INIT(&bglobal
.bg_obslist
);
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();
165 bglobal
.bg_echo
= bp_echo_socket();
166 bglobal
.bg_echov6
= bp_echov6_socket();
169 int main(int argc
, char *argv
[])
171 const char *ctl_path
= BFDD_CONTROL_SOCKET
;
174 frr_preinit(&bfdd_di
, argc
, argv
);
175 frr_opt_add("", longopts
,
176 " --bfdctl Specify bfdd control socket\n");
179 opt
= frr_getopt(argc
, argv
, NULL
);
194 #if 0 /* TODO add support for JSON configuration files. */
198 /* Initialize logging API. */
199 log_init(1, BLOG_DEBUG
, &bfdd_di
);
201 /* Initialize system sockets. */
204 /* Initialize control socket. */
205 control_init(ctl_path
);
207 /* Initialize FRR infrastructure. */
210 /* Initialize BFD data structures. */
215 /* Initialize zebra connection. */
216 bfdd_zclient_init(&bfdd_privs
);
218 /* Add descriptors to the event loop. */
219 thread_add_read(master
, bfd_recv_cb
, NULL
, bglobal
.bg_shop
,
221 thread_add_read(master
, bfd_recv_cb
, NULL
, bglobal
.bg_mhop
,
223 thread_add_read(master
, bfd_recv_cb
, NULL
, bglobal
.bg_shop6
,
225 thread_add_read(master
, bfd_recv_cb
, NULL
, bglobal
.bg_mhop6
,
227 thread_add_read(master
, bfd_recv_cb
, NULL
, bglobal
.bg_echo
,
229 thread_add_read(master
, bfd_recv_cb
, NULL
, bglobal
.bg_echov6
,
231 thread_add_read(master
, control_accept
, NULL
, bglobal
.bg_csock
,
232 &bglobal
.bg_csockev
);
234 /* Install commands. */
237 /* read configuration file and daemonize */