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
24 #include "lib/version.h"
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");
37 /* Master of threads. */
38 struct thread_master
*master
;
41 static zebra_capabilities_t _caps_p
[] = {ZCAP_BIND
};
43 struct zebra_privs_t bfdd_privs
= {
44 #if defined(FRR_USER) && defined(FRR_GROUP)
48 #if defined(VTY_GROUP)
49 .vty_group
= VTY_GROUP
,
52 .cap_num_p
= array_size(_caps_p
),
56 void socket_close(int *s
)
62 log_error("%s: close(%d): (%d) %s", __func__
, *s
, errno
,
68 static void sigusr1_handler(void)
73 static void sigterm_handler(void)
75 /* Signalize shutdown. */
78 /* Stop receiving message from zebra. */
81 /* Shutdown controller to avoid receiving anymore commands. */
84 /* Shutdown and free all protocol related memory. */
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
);
93 socket_close(&bglobal
.bg_vxlan
);
95 /* Terminate and free() FRR related memory. */
101 static struct quagga_signal_t bfd_signals
[] = {
104 .handler
= &sigusr1_handler
,
108 .handler
= &sigterm_handler
,
112 .handler
= &sigterm_handler
,
116 FRR_DAEMON_INFO(bfdd
, BFD
, .vty_port
= 2617,
117 .proghelp
= "Implementation of the BFD protocol.",
118 .signals
= bfd_signals
, .n_signals
= array_size(bfd_signals
),
119 .privs
= &bfdd_privs
)
121 #define OPTION_CTLSOCK 1001
122 static struct option longopts
[] = {
123 {"bfdctl", required_argument
, NULL
, OPTION_CTLSOCK
},
129 * BFD daemon related code.
131 struct bfd_global bglobal
;
133 struct bfd_diag_str_list diag_list
[] = {
134 {.str
= "NeighDown", .type
= BFD_DIAGNEIGHDOWN
},
135 {.str
= "DetectTime", .type
= BFD_DIAGDETECTTIME
},
136 {.str
= "AdminDown", .type
= BFD_DIAGADMINDOWN
},
140 struct bfd_state_str_list state_list
[] = {
141 {.str
= "AdminDown", .type
= PTM_BFD_ADM_DOWN
},
142 {.str
= "Down", .type
= PTM_BFD_DOWN
},
143 {.str
= "Init", .type
= PTM_BFD_INIT
},
144 {.str
= "Up", .type
= PTM_BFD_UP
},
149 static void bg_init(void)
151 TAILQ_INIT(&bglobal
.bg_bcslist
);
153 bglobal
.bg_shop
= bp_udp_shop();
154 bglobal
.bg_mhop
= bp_udp_mhop();
155 bglobal
.bg_shop6
= bp_udp6_shop();
156 bglobal
.bg_mhop6
= bp_udp6_mhop();
157 bglobal
.bg_echo
= ptm_bfd_echo_sock_init();
158 bglobal
.bg_vxlan
= ptm_bfd_vxlan_sock_init();
161 int main(int argc
, char *argv
[])
163 const char *ctl_path
= BFDD_CONTROL_SOCKET
;
166 frr_preinit(&bfdd_di
, argc
, argv
);
167 frr_opt_add("", longopts
,
168 " --bfdctl Specify bfdd control socket\n");
171 opt
= frr_getopt(argc
, argv
, NULL
);
186 #if 0 /* TODO add support for JSON configuration files. */
190 /* Initialize logging API. */
191 log_init(1, BLOG_DEBUG
, &bfdd_di
);
193 /* Initialize system sockets. */
196 /* Initialize control socket. */
197 control_init(ctl_path
);
199 /* Initialize FRR infrastructure. */
202 /* Initialize BFD data structures. */
205 /* Initialize zebra connection. */
206 bfdd_zclient_init(&bfdd_privs
);
208 /* Add descriptors to the event loop. */
209 thread_add_read(master
, bfd_recv_cb
, NULL
, bglobal
.bg_shop
,
211 thread_add_read(master
, bfd_recv_cb
, NULL
, bglobal
.bg_mhop
,
213 thread_add_read(master
, bfd_recv_cb
, NULL
, bglobal
.bg_shop6
,
215 thread_add_read(master
, bfd_recv_cb
, NULL
, bglobal
.bg_mhop6
,
217 thread_add_read(master
, bfd_recv_cb
, NULL
, bglobal
.bg_echo
,
219 #if 0 /* TODO VxLAN support. */
220 thread_add_read(master
, bfd_recv_cb
, NULL
, bglobal
.bg_vxlan
,
223 thread_add_read(master
, control_accept
, NULL
, bglobal
.bg_csock
,
224 &bglobal
.bg_csockev
);
226 /* Install commands. */
229 /* read configuration file and daemonize */