1 /*
2  * BFD daemon northbound implementation.
3  *
4  * Copyright (C) 2019 Network Device Education Foundation, Inc. ("NetDEF")
5  *                    Rafael Zalamena
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20  * 02110-1301 USA.
21  */
22 
23 #include <zebra.h>
24 
25 #include "lib/log.h"
26 #include "lib/northbound.h"
27 
28 #include "bfd.h"
29 #include "bfdd_nb.h"
30 
31 /*
32  * XPath: /frr-bfdd:bfdd/bfd/sessions/single-hop
33  */
34 const void *
bfdd_bfd_sessions_single_hop_get_next(struct nb_cb_get_next_args * args)35 bfdd_bfd_sessions_single_hop_get_next(struct nb_cb_get_next_args *args)
36 {
37 	return bfd_session_next(args->list_entry, false);
38 }
39 
bfdd_bfd_sessions_single_hop_get_keys(struct nb_cb_get_keys_args * args)40 int bfdd_bfd_sessions_single_hop_get_keys(struct nb_cb_get_keys_args *args)
41 {
42 	const struct bfd_session *bs = args->list_entry;
43 	char dstbuf[INET6_ADDRSTRLEN];
44 
45 	inet_ntop(bs->key.family, &bs->key.peer, dstbuf, sizeof(dstbuf));
46 
47 	args->keys->num = 3;
48 	strlcpy(args->keys->key[0], dstbuf, sizeof(args->keys->key[0]));
49 	strlcpy(args->keys->key[1], bs->key.ifname, sizeof(args->keys->key[1]));
50 	strlcpy(args->keys->key[2], bs->key.vrfname,
51 		sizeof(args->keys->key[2]));
52 
53 	return NB_OK;
54 }
55 
56 const void *
bfdd_bfd_sessions_single_hop_lookup_entry(struct nb_cb_lookup_entry_args * args)57 bfdd_bfd_sessions_single_hop_lookup_entry(struct nb_cb_lookup_entry_args *args)
58 {
59 	const char *dest_addr = args->keys->key[0];
60 	const char *ifname = args->keys->key[1];
61 	const char *vrf = args->keys->key[2];
62 	struct sockaddr_any psa, lsa;
63 	struct bfd_key bk;
64 
65 	strtosa(dest_addr, &psa);
66 	memset(&lsa, 0, sizeof(lsa));
67 	gen_bfd_key(&bk, &psa, &lsa, false, ifname, vrf);
68 
69 	return bfd_key_lookup(bk);
70 }
71 
72 /*
73  * XPath: /frr-bfdd:bfdd/bfd/sessions/single-hop/stats/local-discriminator
74  */
75 struct yang_data *
bfdd_bfd_sessions_single_hop_stats_local_discriminator_get_elem(struct nb_cb_get_elem_args * args)76 bfdd_bfd_sessions_single_hop_stats_local_discriminator_get_elem(
77 	struct nb_cb_get_elem_args *args)
78 {
79 	const struct bfd_session *bs = args->list_entry;
80 
81 	return yang_data_new_uint32(args->xpath, bs->discrs.my_discr);
82 }
83 
84 /*
85  * XPath: /frr-bfdd:bfdd/bfd/sessions/single-hop/stats/local-state
86  */
bfdd_bfd_sessions_single_hop_stats_local_state_get_elem(struct nb_cb_get_elem_args * args)87 struct yang_data *bfdd_bfd_sessions_single_hop_stats_local_state_get_elem(
88 	struct nb_cb_get_elem_args *args)
89 {
90 	const struct bfd_session *bs = args->list_entry;
91 
92 	return yang_data_new_enum(args->xpath, bs->ses_state);
93 }
94 
95 /*
96  * XPath: /frr-bfdd:bfdd/bfd/sessions/single-hop/stats/local-diagnostic
97  */
bfdd_bfd_sessions_single_hop_stats_local_diagnostic_get_elem(struct nb_cb_get_elem_args * args)98 struct yang_data *bfdd_bfd_sessions_single_hop_stats_local_diagnostic_get_elem(
99 	struct nb_cb_get_elem_args *args)
100 {
101 	const struct bfd_session *bs = args->list_entry;
102 
103 	return yang_data_new_enum(args->xpath, bs->local_diag);
104 }
105 
106 /*
107  * XPath: /frr-bfdd:bfdd/bfd/sessions/single-hop/stats/local-multiplier
108  */
bfdd_bfd_sessions_single_hop_stats_local_multiplier_get_elem(struct nb_cb_get_elem_args * args)109 struct yang_data *bfdd_bfd_sessions_single_hop_stats_local_multiplier_get_elem(
110 	struct nb_cb_get_elem_args *args)
111 {
112 	const struct bfd_session *bs = args->list_entry;
113 
114 	return yang_data_new_int8(args->xpath, bs->detect_mult);
115 }
116 
117 /*
118  * XPath: /frr-bfdd:bfdd/bfd/sessions/single-hop/stats/remote-discriminator
119  */
120 struct yang_data *
bfdd_bfd_sessions_single_hop_stats_remote_discriminator_get_elem(struct nb_cb_get_elem_args * args)121 bfdd_bfd_sessions_single_hop_stats_remote_discriminator_get_elem(
122 	struct nb_cb_get_elem_args *args)
123 {
124 	const struct bfd_session *bs = args->list_entry;
125 
126 	if (bs->discrs.remote_discr == 0)
127 		return NULL;
128 
129 	return yang_data_new_uint32(args->xpath, bs->discrs.remote_discr);
130 }
131 
132 /*
133  * XPath: /frr-bfdd:bfdd/bfd/sessions/single-hop/stats/remote-state
134  */
bfdd_bfd_sessions_single_hop_stats_remote_state_get_elem(struct nb_cb_get_elem_args * args)135 struct yang_data *bfdd_bfd_sessions_single_hop_stats_remote_state_get_elem(
136 	struct nb_cb_get_elem_args *args)
137 {
138 	const struct bfd_session *bs = args->list_entry;
139 
140 	return yang_data_new_enum(args->xpath, bs->ses_state);
141 }
142 
143 /*
144  * XPath: /frr-bfdd:bfdd/bfd/sessions/single-hop/stats/remote-diagnostic
145  */
bfdd_bfd_sessions_single_hop_stats_remote_diagnostic_get_elem(struct nb_cb_get_elem_args * args)146 struct yang_data *bfdd_bfd_sessions_single_hop_stats_remote_diagnostic_get_elem(
147 	struct nb_cb_get_elem_args *args)
148 {
149 	const struct bfd_session *bs = args->list_entry;
150 
151 	return yang_data_new_enum(args->xpath, bs->remote_diag);
152 }
153 
154 /*
155  * XPath: /frr-bfdd:bfdd/bfd/sessions/single-hop/stats/remote-multiplier
156  */
bfdd_bfd_sessions_single_hop_stats_remote_multiplier_get_elem(struct nb_cb_get_elem_args * args)157 struct yang_data *bfdd_bfd_sessions_single_hop_stats_remote_multiplier_get_elem(
158 	struct nb_cb_get_elem_args *args)
159 {
160 	const struct bfd_session *bs = args->list_entry;
161 
162 	return yang_data_new_int8(args->xpath, bs->remote_detect_mult);
163 }
164 
165 /*
166  * XPath:
167  * /frr-bfdd:bfdd/bfd/sessions/single-hop/stats/negotiated-transmission-interval
168  */
169 struct yang_data *
bfdd_bfd_sessions_single_hop_stats_negotiated_transmission_interval_get_elem(struct nb_cb_get_elem_args * args)170 bfdd_bfd_sessions_single_hop_stats_negotiated_transmission_interval_get_elem(
171 	struct nb_cb_get_elem_args *args)
172 {
173 	const struct bfd_session *bs = args->list_entry;
174 
175 	return yang_data_new_uint32(args->xpath,
176 				    bs->remote_timers.desired_min_tx);
177 }
178 
179 /*
180  * XPath:
181  * /frr-bfdd:bfdd/bfd/sessions/single-hop/stats/negotiated-receive-interval
182  */
183 struct yang_data *
bfdd_bfd_sessions_single_hop_stats_negotiated_receive_interval_get_elem(struct nb_cb_get_elem_args * args)184 bfdd_bfd_sessions_single_hop_stats_negotiated_receive_interval_get_elem(
185 	struct nb_cb_get_elem_args *args)
186 {
187 	const struct bfd_session *bs = args->list_entry;
188 
189 	return yang_data_new_uint32(args->xpath,
190 				    bs->remote_timers.required_min_rx);
191 }
192 
193 /*
194  * XPath: /frr-bfdd:bfdd/bfd/sessions/single-hop/stats/detection-mode
195  */
bfdd_bfd_sessions_single_hop_stats_detection_mode_get_elem(struct nb_cb_get_elem_args * args)196 struct yang_data *bfdd_bfd_sessions_single_hop_stats_detection_mode_get_elem(
197 	struct nb_cb_get_elem_args *args)
198 {
199 	const struct bfd_session *bs = args->list_entry;
200 	int detection_mode;
201 
202 	/*
203 	 * Detection mode:
204 	 *   1. Async with echo
205 	 *   2. Async without echo
206 	 *   3. Demand with echo
207 	 *   4. Demand without echo
208 	 *
209 	 * TODO: support demand mode.
210 	 */
211 	if (CHECK_FLAG(bs->flags, BFD_SESS_FLAG_ECHO))
212 		detection_mode = 1;
213 	else
214 		detection_mode = 2;
215 
216 	return yang_data_new_enum(args->xpath, detection_mode);
217 }
218 
219 /*
220  * XPath: /frr-bfdd:bfdd/bfd/sessions/single-hop/stats/last-down-time
221  */
bfdd_bfd_sessions_single_hop_stats_last_down_time_get_elem(struct nb_cb_get_elem_args * args)222 struct yang_data *bfdd_bfd_sessions_single_hop_stats_last_down_time_get_elem(
223 	struct nb_cb_get_elem_args *args)
224 {
225 	/*
226 	 * TODO: implement me.
227 	 *
228 	 * No yang support for time elements yet.
229 	 */
230 	return NULL;
231 }
232 
233 /*
234  * XPath: /frr-bfdd:bfdd/bfd/sessions/single-hop/stats/last-up-time
235  */
bfdd_bfd_sessions_single_hop_stats_last_up_time_get_elem(struct nb_cb_get_elem_args * args)236 struct yang_data *bfdd_bfd_sessions_single_hop_stats_last_up_time_get_elem(
237 	struct nb_cb_get_elem_args *args)
238 {
239 	/*
240 	 * TODO: implement me.
241 	 *
242 	 * No yang support for time elements yet.
243 	 */
244 	return NULL;
245 }
246 
247 /*
248  * XPath: /frr-bfdd:bfdd/bfd/sessions/single-hop/stats/session-down-count
249  */
250 struct yang_data *
bfdd_bfd_sessions_single_hop_stats_session_down_count_get_elem(struct nb_cb_get_elem_args * args)251 bfdd_bfd_sessions_single_hop_stats_session_down_count_get_elem(
252 	struct nb_cb_get_elem_args *args)
253 {
254 	const struct bfd_session *bs = args->list_entry;
255 
256 	return yang_data_new_uint64(args->xpath, bs->stats.session_down);
257 }
258 
259 /*
260  * XPath: /frr-bfdd:bfdd/bfd/sessions/single-hop/stats/session-up-count
261  */
bfdd_bfd_sessions_single_hop_stats_session_up_count_get_elem(struct nb_cb_get_elem_args * args)262 struct yang_data *bfdd_bfd_sessions_single_hop_stats_session_up_count_get_elem(
263 	struct nb_cb_get_elem_args *args)
264 {
265 	const struct bfd_session *bs = args->list_entry;
266 
267 	return yang_data_new_uint64(args->xpath, bs->stats.session_up);
268 }
269 
270 /*
271  * XPath:
272  * /frr-bfdd:bfdd/bfd/sessions/single-hop/stats/control-packet-input-count
273  */
274 struct yang_data *
bfdd_bfd_sessions_single_hop_stats_control_packet_input_count_get_elem(struct nb_cb_get_elem_args * args)275 bfdd_bfd_sessions_single_hop_stats_control_packet_input_count_get_elem(
276 	struct nb_cb_get_elem_args *args)
277 {
278 	const struct bfd_session *bs = args->list_entry;
279 
280 	return yang_data_new_uint64(args->xpath, bs->stats.rx_ctrl_pkt);
281 }
282 
283 /*
284  * XPath:
285  * /frr-bfdd:bfdd/bfd/sessions/single-hop/stats/control-packet-output-count
286  */
287 struct yang_data *
bfdd_bfd_sessions_single_hop_stats_control_packet_output_count_get_elem(struct nb_cb_get_elem_args * args)288 bfdd_bfd_sessions_single_hop_stats_control_packet_output_count_get_elem(
289 	struct nb_cb_get_elem_args *args)
290 {
291 	const struct bfd_session *bs = args->list_entry;
292 
293 	return yang_data_new_uint64(args->xpath, bs->stats.tx_ctrl_pkt);
294 }
295 
296 /*
297  * XPath:
298  * /frr-bfdd:bfdd/bfd/sessions/single-hop/stats/negotiated-echo-transmission-interval
299  */
300 struct yang_data *
bfdd_bfd_sessions_single_hop_stats_negotiated_echo_transmission_interval_get_elem(struct nb_cb_get_elem_args * args)301 bfdd_bfd_sessions_single_hop_stats_negotiated_echo_transmission_interval_get_elem(
302 	struct nb_cb_get_elem_args *args)
303 {
304 	const struct bfd_session *bs = args->list_entry;
305 
306 	return yang_data_new_uint32(args->xpath,
307 				    bs->remote_timers.required_min_echo);
308 }
309 
310 /*
311  * XPath: /frr-bfdd:bfdd/bfd/sessions/single-hop/stats/echo-packet-input-count
312  */
313 struct yang_data *
bfdd_bfd_sessions_single_hop_stats_echo_packet_input_count_get_elem(struct nb_cb_get_elem_args * args)314 bfdd_bfd_sessions_single_hop_stats_echo_packet_input_count_get_elem(
315 	struct nb_cb_get_elem_args *args)
316 {
317 	const struct bfd_session *bs = args->list_entry;
318 
319 	return yang_data_new_uint64(args->xpath, bs->stats.rx_echo_pkt);
320 }
321 
322 /*
323  * XPath: /frr-bfdd:bfdd/bfd/sessions/single-hop/stats/echo-packet-output-count
324  */
325 struct yang_data *
bfdd_bfd_sessions_single_hop_stats_echo_packet_output_count_get_elem(struct nb_cb_get_elem_args * args)326 bfdd_bfd_sessions_single_hop_stats_echo_packet_output_count_get_elem(
327 	struct nb_cb_get_elem_args *args)
328 {
329 	const struct bfd_session *bs = args->list_entry;
330 
331 	return yang_data_new_uint64(args->xpath, bs->stats.tx_echo_pkt);
332 }
333 
334 /*
335  * XPath: /frr-bfdd:bfdd/bfd/sessions/multi-hop
336  */
337 const void *
bfdd_bfd_sessions_multi_hop_get_next(struct nb_cb_get_next_args * args)338 bfdd_bfd_sessions_multi_hop_get_next(struct nb_cb_get_next_args *args)
339 {
340 	return bfd_session_next(args->list_entry, true);
341 }
342 
bfdd_bfd_sessions_multi_hop_get_keys(struct nb_cb_get_keys_args * args)343 int bfdd_bfd_sessions_multi_hop_get_keys(struct nb_cb_get_keys_args *args)
344 {
345 	const struct bfd_session *bs = args->list_entry;
346 	char dstbuf[INET6_ADDRSTRLEN], srcbuf[INET6_ADDRSTRLEN];
347 
348 	inet_ntop(bs->key.family, &bs->key.peer, dstbuf, sizeof(dstbuf));
349 	inet_ntop(bs->key.family, &bs->key.local, srcbuf, sizeof(srcbuf));
350 
351 	args->keys->num = 4;
352 	strlcpy(args->keys->key[0], srcbuf, sizeof(args->keys->key[0]));
353 	strlcpy(args->keys->key[1], dstbuf, sizeof(args->keys->key[1]));
354 	strlcpy(args->keys->key[2], bs->key.ifname, sizeof(args->keys->key[2]));
355 	strlcpy(args->keys->key[3], bs->key.vrfname,
356 		sizeof(args->keys->key[3]));
357 
358 	return NB_OK;
359 }
360 
361 const void *
bfdd_bfd_sessions_multi_hop_lookup_entry(struct nb_cb_lookup_entry_args * args)362 bfdd_bfd_sessions_multi_hop_lookup_entry(struct nb_cb_lookup_entry_args *args)
363 {
364 	const char *source_addr = args->keys->key[0];
365 	const char *dest_addr = args->keys->key[1];
366 	const char *ifname = args->keys->key[2];
367 	const char *vrf = args->keys->key[3];
368 	struct sockaddr_any psa, lsa;
369 	struct bfd_key bk;
370 
371 	strtosa(dest_addr, &psa);
372 	strtosa(source_addr, &lsa);
373 	gen_bfd_key(&bk, &psa, &lsa, true, ifname, vrf);
374 
375 	return bfd_key_lookup(bk);
376 }
377