]>
git.proxmox.com Git - mirror_frr.git/blob - bfdd/config.c
1 /*********************************************************************
2 * Copyright 2017-2018 Network Device Education Foundation, Inc. ("NetDEF")
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the Free
6 * Software Foundation; either version 2 of the License, or (at your option)
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * You should have received a copy of the GNU General Public License along
15 * with this program; see the file COPYING; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 * config.c: implements the BFD daemon configuration handling.
22 * Rafael Zalamena <rzalamena@opensourcerouting.org>
46 static int parse_config_json(struct json_object
*jo
, bpc_handle h
, void *arg
);
47 static int parse_list(struct json_object
*jo
, enum peer_list_type plt
,
48 bpc_handle h
, void *arg
);
49 static int parse_peer_config(struct json_object
*jo
, struct bfd_peer_cfg
*bpc
);
50 static int parse_peer_label_config(struct json_object
*jo
,
51 struct bfd_peer_cfg
*bpc
);
53 static int config_add(struct bfd_peer_cfg
*bpc
, void *arg
);
54 static int config_del(struct bfd_peer_cfg
*bpc
, void *arg
);
56 static int json_object_add_peer(struct json_object
*jo
, struct bfd_session
*bs
);
62 static int config_add(struct bfd_peer_cfg
*bpc
,
63 void *arg
__attribute__((unused
)))
65 return ptm_bfd_sess_new(bpc
) == NULL
;
68 static int config_del(struct bfd_peer_cfg
*bpc
,
69 void *arg
__attribute__((unused
)))
71 return ptm_bfd_ses_del(bpc
) != 0;
74 static int parse_config_json(struct json_object
*jo
, bpc_handle h
, void *arg
)
76 const char *key
, *sval
;
77 struct json_object
*jo_val
;
78 struct json_object_iterator joi
, join
;
81 JSON_FOREACH (jo
, joi
, join
) {
82 key
= json_object_iter_peek_name(&joi
);
83 jo_val
= json_object_iter_peek_value(&joi
);
85 if (strcmp(key
, "ipv4") == 0) {
86 error
+= parse_list(jo_val
, PLT_IPV4
, h
, arg
);
87 } else if (strcmp(key
, "ipv6") == 0) {
88 error
+= parse_list(jo_val
, PLT_IPV6
, h
, arg
);
89 } else if (strcmp(key
, "label") == 0) {
90 error
+= parse_list(jo_val
, PLT_LABEL
, h
, arg
);
92 sval
= json_object_get_string(jo_val
);
93 log_warning("%s:%d invalid configuration: %s", __func__
,
100 * Our callers never call free() on json_object and only expect
101 * the return value, so lets free() it here.
108 int parse_config(const char *fname
)
110 struct json_object
*jo
;
112 jo
= json_object_from_file(fname
);
116 return parse_config_json(jo
, config_add
, NULL
);
119 static int parse_list(struct json_object
*jo
, enum peer_list_type plt
,
120 bpc_handle h
, void *arg
)
122 struct json_object
*jo_val
;
123 struct bfd_peer_cfg bpc
;
125 int error
= 0, result
;
127 allen
= json_object_array_length(jo
);
128 for (idx
= 0; idx
< allen
; idx
++) {
129 jo_val
= json_object_array_get_idx(jo
, idx
);
132 memset(&bpc
, 0, sizeof(bpc
));
133 bpc
.bpc_detectmultiplier
= BFD_DEFDETECTMULT
;
134 bpc
.bpc_recvinterval
= BFD_DEFREQUIREDMINRX
;
135 bpc
.bpc_txinterval
= BFD_DEFDESIREDMINTX
;
136 bpc
.bpc_echointerval
= BFD_DEF_REQ_MIN_ECHO
;
140 log_debug("ipv4 peers %d:", allen
);
144 log_debug("ipv6 peers %d:", allen
);
145 bpc
.bpc_ipv4
= false;
148 log_debug("label peers %d:", allen
);
149 if (parse_peer_label_config(jo_val
, &bpc
) != 0) {
157 log_error("%s:%d: unsupported peer type", __func__
,
162 result
= parse_peer_config(jo_val
, &bpc
);
165 error
+= (h(&bpc
, arg
) != 0);
171 static int parse_peer_config(struct json_object
*jo
, struct bfd_peer_cfg
*bpc
)
173 const char *key
, *sval
;
174 struct json_object
*jo_val
;
175 struct json_object_iterator joi
, join
;
176 int family_type
= (bpc
->bpc_ipv4
) ? AF_INET
: AF_INET6
;
179 log_debug("\tpeer: %s", bpc
->bpc_ipv4
? "ipv4" : "ipv6");
181 JSON_FOREACH (jo
, joi
, join
) {
182 key
= json_object_iter_peek_name(&joi
);
183 jo_val
= json_object_iter_peek_value(&joi
);
185 if (strcmp(key
, "multihop") == 0) {
186 bpc
->bpc_mhop
= json_object_get_boolean(jo_val
);
187 log_debug("\tmultihop: %s",
188 bpc
->bpc_mhop
? "true" : "false");
189 } else if (strcmp(key
, "peer-address") == 0) {
190 sval
= json_object_get_string(jo_val
);
191 if (strtosa(sval
, &bpc
->bpc_peer
) != 0
192 || bpc
->bpc_peer
.sa_sin
.sin_family
!= family_type
) {
194 "%s:%d failed to parse peer-address '%s'",
195 __func__
, __LINE__
, sval
);
198 log_debug("\tpeer-address: %s", sval
);
199 } else if (strcmp(key
, "local-address") == 0) {
200 sval
= json_object_get_string(jo_val
);
201 if (strtosa(sval
, &bpc
->bpc_local
) != 0
202 || bpc
->bpc_local
.sa_sin
.sin_family
205 "%s:%d failed to parse local-address '%s'",
206 __func__
, __LINE__
, sval
);
209 log_debug("\tlocal-address: %s", sval
);
210 } else if (strcmp(key
, "local-interface") == 0) {
211 bpc
->bpc_has_localif
= true;
212 sval
= json_object_get_string(jo_val
);
213 if (strlcpy(bpc
->bpc_localif
, sval
,
214 sizeof(bpc
->bpc_localif
))
215 > sizeof(bpc
->bpc_localif
)) {
216 log_debug("\tlocal-interface: %s (truncated)");
219 log_debug("\tlocal-interface: %s", sval
);
221 } else if (strcmp(key
, "vrf-name") == 0) {
222 bpc
->bpc_has_vrfname
= true;
223 sval
= json_object_get_string(jo_val
);
224 if (strlcpy(bpc
->bpc_vrfname
, sval
,
225 sizeof(bpc
->bpc_vrfname
))
226 > sizeof(bpc
->bpc_vrfname
)) {
227 log_debug("\tvrf-name: %s (truncated)", sval
);
230 log_debug("\tvrf-name: %s", sval
);
232 } else if (strcmp(key
, "detect-multiplier") == 0) {
233 bpc
->bpc_detectmultiplier
=
234 json_object_get_int64(jo_val
);
235 bpc
->bpc_has_detectmultiplier
= true;
236 log_debug("\tdetect-multiplier: %llu",
237 bpc
->bpc_detectmultiplier
);
238 } else if (strcmp(key
, "receive-interval") == 0) {
239 bpc
->bpc_recvinterval
= json_object_get_int64(jo_val
);
240 bpc
->bpc_has_recvinterval
= true;
241 log_debug("\treceive-interval: %llu",
242 bpc
->bpc_recvinterval
);
243 } else if (strcmp(key
, "transmit-interval") == 0) {
244 bpc
->bpc_txinterval
= json_object_get_int64(jo_val
);
245 bpc
->bpc_has_txinterval
= true;
246 log_debug("\ttransmit-interval: %llu",
247 bpc
->bpc_txinterval
);
248 } else if (strcmp(key
, "echo-interval") == 0) {
249 bpc
->bpc_echointerval
= json_object_get_int64(jo_val
);
250 bpc
->bpc_has_echointerval
= true;
251 log_debug("\techo-interval: %llu",
252 bpc
->bpc_echointerval
);
253 } else if (strcmp(key
, "create-only") == 0) {
254 bpc
->bpc_createonly
= json_object_get_boolean(jo_val
);
255 log_debug("\tcreate-only: %s",
256 bpc
->bpc_createonly
? "true" : "false");
257 } else if (strcmp(key
, "shutdown") == 0) {
258 bpc
->bpc_shutdown
= json_object_get_boolean(jo_val
);
259 log_debug("\tshutdown: %s",
260 bpc
->bpc_shutdown
? "true" : "false");
261 } else if (strcmp(key
, "echo-mode") == 0) {
262 bpc
->bpc_echo
= json_object_get_boolean(jo_val
);
263 log_debug("\techo-mode: %s",
264 bpc
->bpc_echo
? "true" : "false");
265 } else if (strcmp(key
, "label") == 0) {
266 bpc
->bpc_has_label
= true;
267 sval
= json_object_get_string(jo_val
);
268 if (strlcpy(bpc
->bpc_label
, sval
,
269 sizeof(bpc
->bpc_label
))
270 > sizeof(bpc
->bpc_label
)) {
271 log_debug("\tlabel: %s (truncated)", sval
);
274 log_debug("\tlabel: %s", sval
);
277 sval
= json_object_get_string(jo_val
);
278 log_warning("%s:%d invalid configuration: '%s: %s'",
279 __func__
, __LINE__
, key
, sval
);
284 if (bpc
->bpc_peer
.sa_sin
.sin_family
== 0) {
285 log_debug("%s:%d no peer address provided", __func__
, __LINE__
);
292 static int parse_peer_label_config(struct json_object
*jo
,
293 struct bfd_peer_cfg
*bpc
)
295 struct peer_label
*pl
;
296 struct json_object
*label
;
299 /* Get label and translate it to BFD daemon key. */
300 if (!json_object_object_get_ex(jo
, "label", &label
))
303 sval
= json_object_get_string(label
);
309 log_debug("\tpeer-label: %s", sval
);
311 /* Translate the label into BFD address keys. */
312 bpc
->bpc_ipv4
= !BFD_CHECK_FLAG(pl
->pl_bs
->flags
, BFD_SESS_FLAG_IPV6
);
313 bpc
->bpc_mhop
= BFD_CHECK_FLAG(pl
->pl_bs
->flags
, BFD_SESS_FLAG_MH
);
315 bpc
->bpc_peer
= pl
->pl_bs
->mhop
.peer
;
316 bpc
->bpc_local
= pl
->pl_bs
->mhop
.local
;
317 if (pl
->pl_bs
->mhop
.vrfid
!= VRF_DEFAULT
) {
318 bpc
->bpc_has_vrfname
= true;
319 strlcpy(bpc
->bpc_vrfname
, pl
->pl_bs
->vrf
->name
,
320 sizeof(bpc
->bpc_vrfname
));
323 bpc
->bpc_peer
= pl
->pl_bs
->shop
.peer
;
324 if (pl
->pl_bs
->ifname
[0]) {
325 bpc
->bpc_has_localif
= true;
326 strlcpy(bpc
->bpc_localif
, pl
->pl_bs
->ifname
,
327 sizeof(bpc
->bpc_localif
));
336 * Control socket JSON parsing.
338 int config_request_add(const char *jsonstr
)
340 struct json_object
*jo
;
342 jo
= json_tokener_parse(jsonstr
);
346 return parse_config_json(jo
, config_add
, NULL
);
349 int config_request_del(const char *jsonstr
)
351 struct json_object
*jo
;
353 jo
= json_tokener_parse(jsonstr
);
357 return parse_config_json(jo
, config_del
, NULL
);
360 char *config_response(const char *status
, const char *error
)
362 struct json_object
*resp
, *jo
;
365 resp
= json_object_new_object();
369 /* Add 'status' response key. */
370 jo
= json_object_new_string(status
);
372 json_object_put(resp
);
376 json_object_object_add(resp
, "status", jo
);
378 /* Add 'error' response key. */
380 jo
= json_object_new_string(error
);
382 json_object_put(resp
);
386 json_object_object_add(resp
, "error", jo
);
389 /* Generate JSON response. */
391 MTYPE_BFDD_NOTIFICATION
,
392 json_object_to_json_string_ext(resp
, BFDD_JSON_CONV_OPTIONS
));
393 json_object_put(resp
);
398 char *config_notify(struct bfd_session
*bs
)
400 struct json_object
*resp
;
404 resp
= json_object_new_object();
408 json_object_string_add(resp
, "op", BCM_NOTIFY_PEER_STATUS
);
410 json_object_add_peer(resp
, bs
);
412 /* Add status information */
413 json_object_int_add(resp
, "id", bs
->discrs
.my_discr
);
414 json_object_int_add(resp
, "remote-id", bs
->discrs
.my_discr
);
416 switch (bs
->ses_state
) {
418 json_object_string_add(resp
, "state", "up");
420 now
= monotime(NULL
);
421 json_object_int_add(resp
, "uptime", now
- bs
->uptime
.tv_sec
);
423 case PTM_BFD_ADM_DOWN
:
424 json_object_string_add(resp
, "state", "adm-down");
427 json_object_string_add(resp
, "state", "down");
429 now
= monotime(NULL
);
430 json_object_int_add(resp
, "downtime",
431 now
- bs
->downtime
.tv_sec
);
434 json_object_string_add(resp
, "state", "init");
438 json_object_string_add(resp
, "state", "unknown");
442 json_object_int_add(resp
, "diagnostics", bs
->local_diag
);
443 json_object_int_add(resp
, "remote-diagnostics", bs
->remote_diag
);
445 /* Generate JSON response. */
447 MTYPE_BFDD_NOTIFICATION
,
448 json_object_to_json_string_ext(resp
, BFDD_JSON_CONV_OPTIONS
));
449 json_object_put(resp
);
454 char *config_notify_config(const char *op
, struct bfd_session
*bs
)
456 struct json_object
*resp
;
459 resp
= json_object_new_object();
463 json_object_string_add(resp
, "op", op
);
465 json_object_add_peer(resp
, bs
);
467 /* On peer deletion we don't need to add any additional information. */
468 if (strcmp(op
, BCM_NOTIFY_CONFIG_DELETE
) == 0)
471 json_object_int_add(resp
, "detect-multiplier", bs
->detect_mult
);
472 json_object_int_add(resp
, "receive-interval",
473 bs
->timers
.required_min_rx
/ 1000);
474 json_object_int_add(resp
, "transmit-interval",
475 bs
->timers
.desired_min_tx
/ 1000);
476 json_object_int_add(resp
, "echo-interval",
477 bs
->timers
.required_min_echo
/ 1000);
479 json_object_int_add(resp
, "remote-detect-multiplier",
480 bs
->remote_detect_mult
);
481 json_object_int_add(resp
, "remote-receive-interval",
482 bs
->remote_timers
.required_min_rx
/ 1000);
483 json_object_int_add(resp
, "remote-transmit-interval",
484 bs
->remote_timers
.desired_min_tx
/ 1000);
485 json_object_int_add(resp
, "remote-echo-interval",
486 bs
->remote_timers
.required_min_echo
/ 1000);
488 if (BFD_CHECK_FLAG(bs
->flags
, BFD_SESS_FLAG_ECHO
))
489 json_object_boolean_true_add(resp
, "echo-mode");
491 json_object_boolean_false_add(resp
, "echo-mode");
493 if (BFD_CHECK_FLAG(bs
->flags
, BFD_SESS_FLAG_SHUTDOWN
))
494 json_object_boolean_true_add(resp
, "shutdown");
496 json_object_boolean_false_add(resp
, "shutdown");
499 /* Generate JSON response. */
501 MTYPE_BFDD_NOTIFICATION
,
502 json_object_to_json_string_ext(resp
, BFDD_JSON_CONV_OPTIONS
));
503 json_object_put(resp
);
508 int config_notify_request(struct bfd_control_socket
*bcs
, const char *jsonstr
,
511 struct json_object
*jo
;
513 jo
= json_tokener_parse(jsonstr
);
517 return parse_config_json(jo
, bh
, bcs
);
520 static int json_object_add_peer(struct json_object
*jo
, struct bfd_session
*bs
)
522 /* Add peer 'key' information. */
523 if (BFD_CHECK_FLAG(bs
->flags
, BFD_SESS_FLAG_IPV6
))
524 json_object_boolean_true_add(jo
, "ipv6");
526 json_object_boolean_false_add(jo
, "ipv6");
528 if (BFD_CHECK_FLAG(bs
->flags
, BFD_SESS_FLAG_MH
)) {
529 json_object_boolean_true_add(jo
, "multihop");
530 json_object_string_add(jo
, "peer-address",
531 satostr(&bs
->mhop
.peer
));
532 json_object_string_add(jo
, "local-address",
533 satostr(&bs
->mhop
.local
));
535 json_object_string_add(jo
, "vrf-name", bs
->vrfname
);
537 json_object_boolean_false_add(jo
, "multihop");
538 json_object_string_add(jo
, "peer-address",
539 satostr(&bs
->shop
.peer
));
540 if (bs
->local_address
.sa_sin
.sin_family
!= AF_UNSPEC
)
541 json_object_string_add(jo
, "local-address",
542 satostr(&bs
->local_address
));
544 json_object_string_add(jo
, "local-interface",
549 json_object_string_add(jo
, "label", bs
->pl
->pl_label
);
558 struct peer_label
*pl_find(const char *label
)
560 struct peer_label
*pl
;
562 TAILQ_FOREACH (pl
, &bglobal
.bg_pllist
, pl_entry
) {
563 if (strcmp(pl
->pl_label
, label
) != 0)
572 struct peer_label
*pl_new(const char *label
, struct bfd_session
*bs
)
574 struct peer_label
*pl
;
576 pl
= XCALLOC(MTYPE_BFDD_LABEL
, sizeof(*pl
));
580 if (strlcpy(pl
->pl_label
, label
, sizeof(pl
->pl_label
))
581 > sizeof(pl
->pl_label
))
582 log_warning("%s:%d: label was truncated", __func__
, __LINE__
);
587 TAILQ_INSERT_HEAD(&bglobal
.bg_pllist
, pl
, pl_entry
);
592 void pl_free(struct peer_label
*pl
)
597 /* Remove the pointer back. */
598 pl
->pl_bs
->pl
= NULL
;
600 TAILQ_REMOVE(&bglobal
.bg_pllist
, pl
, pl_entry
);
601 XFREE(MTYPE_BFDD_LABEL
, pl
);