]>
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>
33 DEFINE_MTYPE_STATIC(BFDD
, BFDD_LABEL
, "long-lived label memory")
48 static int parse_config_json(struct json_object
*jo
, bpc_handle h
, void *arg
);
49 static int parse_list(struct json_object
*jo
, enum peer_list_type plt
,
50 bpc_handle h
, void *arg
);
51 static int parse_peer_config(struct json_object
*jo
, struct bfd_peer_cfg
*bpc
);
52 static int parse_peer_label_config(struct json_object
*jo
,
53 struct bfd_peer_cfg
*bpc
);
55 static int config_add(struct bfd_peer_cfg
*bpc
, void *arg
);
56 static int config_del(struct bfd_peer_cfg
*bpc
, void *arg
);
58 static int json_object_add_peer(struct json_object
*jo
, struct bfd_session
*bs
);
64 static int config_add(struct bfd_peer_cfg
*bpc
,
65 void *arg
__attribute__((unused
)))
67 return ptm_bfd_sess_new(bpc
) == NULL
;
70 static int config_del(struct bfd_peer_cfg
*bpc
,
71 void *arg
__attribute__((unused
)))
73 return ptm_bfd_sess_del(bpc
) != 0;
76 static int parse_config_json(struct json_object
*jo
, bpc_handle h
, void *arg
)
78 const char *key
, *sval
;
79 struct json_object
*jo_val
;
80 struct json_object_iterator joi
, join
;
83 JSON_FOREACH (jo
, joi
, join
) {
84 key
= json_object_iter_peek_name(&joi
);
85 jo_val
= json_object_iter_peek_value(&joi
);
87 if (strcmp(key
, "ipv4") == 0) {
88 error
+= parse_list(jo_val
, PLT_IPV4
, h
, arg
);
89 } else if (strcmp(key
, "ipv6") == 0) {
90 error
+= parse_list(jo_val
, PLT_IPV6
, h
, arg
);
91 } else if (strcmp(key
, "label") == 0) {
92 error
+= parse_list(jo_val
, PLT_LABEL
, h
, arg
);
94 sval
= json_object_get_string(jo_val
);
95 log_warning("%s:%d invalid configuration: %s", __func__
,
102 * Our callers never call free() on json_object and only expect
103 * the return value, so lets free() it here.
110 int parse_config(const char *fname
)
112 struct json_object
*jo
;
114 jo
= json_object_from_file(fname
);
118 return parse_config_json(jo
, config_add
, NULL
);
121 static int parse_list(struct json_object
*jo
, enum peer_list_type plt
,
122 bpc_handle h
, void *arg
)
124 struct json_object
*jo_val
;
125 struct bfd_peer_cfg bpc
;
127 int error
= 0, result
;
129 allen
= json_object_array_length(jo
);
130 for (idx
= 0; idx
< allen
; idx
++) {
131 jo_val
= json_object_array_get_idx(jo
, idx
);
134 memset(&bpc
, 0, sizeof(bpc
));
135 bpc
.bpc_detectmultiplier
= BFD_DEFDETECTMULT
;
136 bpc
.bpc_recvinterval
= BFD_DEFREQUIREDMINRX
;
137 bpc
.bpc_txinterval
= BFD_DEFDESIREDMINTX
;
138 bpc
.bpc_echointerval
= BFD_DEF_REQ_MIN_ECHO
;
142 log_debug("ipv4 peers %d:", allen
);
146 log_debug("ipv6 peers %d:", allen
);
147 bpc
.bpc_ipv4
= false;
150 log_debug("label peers %d:", allen
);
151 if (parse_peer_label_config(jo_val
, &bpc
) != 0) {
159 log_error("%s:%d: unsupported peer type", __func__
,
164 result
= parse_peer_config(jo_val
, &bpc
);
167 error
+= (h(&bpc
, arg
) != 0);
173 static int parse_peer_config(struct json_object
*jo
, struct bfd_peer_cfg
*bpc
)
175 const char *key
, *sval
;
176 struct json_object
*jo_val
;
177 struct json_object_iterator joi
, join
;
178 int family_type
= (bpc
->bpc_ipv4
) ? AF_INET
: AF_INET6
;
181 log_debug("\tpeer: %s", bpc
->bpc_ipv4
? "ipv4" : "ipv6");
183 JSON_FOREACH (jo
, joi
, join
) {
184 key
= json_object_iter_peek_name(&joi
);
185 jo_val
= json_object_iter_peek_value(&joi
);
187 if (strcmp(key
, "multihop") == 0) {
188 bpc
->bpc_mhop
= json_object_get_boolean(jo_val
);
189 log_debug("\tmultihop: %s",
190 bpc
->bpc_mhop
? "true" : "false");
191 } else if (strcmp(key
, "peer-address") == 0) {
192 sval
= json_object_get_string(jo_val
);
193 if (strtosa(sval
, &bpc
->bpc_peer
) != 0
194 || bpc
->bpc_peer
.sa_sin
.sin_family
!= family_type
) {
196 "%s:%d failed to parse peer-address '%s'",
197 __func__
, __LINE__
, sval
);
200 log_debug("\tpeer-address: %s", sval
);
201 } else if (strcmp(key
, "local-address") == 0) {
202 sval
= json_object_get_string(jo_val
);
203 if (strtosa(sval
, &bpc
->bpc_local
) != 0
204 || bpc
->bpc_local
.sa_sin
.sin_family
207 "%s:%d failed to parse local-address '%s'",
208 __func__
, __LINE__
, sval
);
211 log_debug("\tlocal-address: %s", sval
);
212 } else if (strcmp(key
, "local-interface") == 0) {
213 bpc
->bpc_has_localif
= true;
214 sval
= json_object_get_string(jo_val
);
215 if (strlcpy(bpc
->bpc_localif
, sval
,
216 sizeof(bpc
->bpc_localif
))
217 > sizeof(bpc
->bpc_localif
)) {
218 log_debug("\tlocal-interface: %s (truncated)");
221 log_debug("\tlocal-interface: %s", sval
);
223 } else if (strcmp(key
, "vrf-name") == 0) {
224 bpc
->bpc_has_vrfname
= true;
225 sval
= json_object_get_string(jo_val
);
226 if (strlcpy(bpc
->bpc_vrfname
, sval
,
227 sizeof(bpc
->bpc_vrfname
))
228 > sizeof(bpc
->bpc_vrfname
)) {
229 log_debug("\tvrf-name: %s (truncated)", sval
);
232 log_debug("\tvrf-name: %s", sval
);
234 } else if (strcmp(key
, "detect-multiplier") == 0) {
235 bpc
->bpc_detectmultiplier
=
236 json_object_get_int64(jo_val
);
237 bpc
->bpc_has_detectmultiplier
= true;
238 log_debug("\tdetect-multiplier: %llu",
239 bpc
->bpc_detectmultiplier
);
240 } else if (strcmp(key
, "receive-interval") == 0) {
241 bpc
->bpc_recvinterval
= json_object_get_int64(jo_val
);
242 bpc
->bpc_has_recvinterval
= true;
243 log_debug("\treceive-interval: %llu",
244 bpc
->bpc_recvinterval
);
245 } else if (strcmp(key
, "transmit-interval") == 0) {
246 bpc
->bpc_txinterval
= json_object_get_int64(jo_val
);
247 bpc
->bpc_has_txinterval
= true;
248 log_debug("\ttransmit-interval: %llu",
249 bpc
->bpc_txinterval
);
250 } else if (strcmp(key
, "echo-interval") == 0) {
251 bpc
->bpc_echointerval
= json_object_get_int64(jo_val
);
252 bpc
->bpc_has_echointerval
= true;
253 log_debug("\techo-interval: %llu",
254 bpc
->bpc_echointerval
);
255 } else if (strcmp(key
, "create-only") == 0) {
256 bpc
->bpc_createonly
= json_object_get_boolean(jo_val
);
257 log_debug("\tcreate-only: %s",
258 bpc
->bpc_createonly
? "true" : "false");
259 } else if (strcmp(key
, "shutdown") == 0) {
260 bpc
->bpc_shutdown
= json_object_get_boolean(jo_val
);
261 log_debug("\tshutdown: %s",
262 bpc
->bpc_shutdown
? "true" : "false");
263 } else if (strcmp(key
, "echo-mode") == 0) {
264 bpc
->bpc_echo
= json_object_get_boolean(jo_val
);
265 log_debug("\techo-mode: %s",
266 bpc
->bpc_echo
? "true" : "false");
267 } else if (strcmp(key
, "label") == 0) {
268 bpc
->bpc_has_label
= true;
269 sval
= json_object_get_string(jo_val
);
270 if (strlcpy(bpc
->bpc_label
, sval
,
271 sizeof(bpc
->bpc_label
))
272 > sizeof(bpc
->bpc_label
)) {
273 log_debug("\tlabel: %s (truncated)", sval
);
276 log_debug("\tlabel: %s", sval
);
279 sval
= json_object_get_string(jo_val
);
280 log_warning("%s:%d invalid configuration: '%s: %s'",
281 __func__
, __LINE__
, key
, sval
);
286 if (bpc
->bpc_peer
.sa_sin
.sin_family
== 0) {
287 log_debug("%s:%d no peer address provided", __func__
, __LINE__
);
294 static int parse_peer_label_config(struct json_object
*jo
,
295 struct bfd_peer_cfg
*bpc
)
297 struct peer_label
*pl
;
298 struct json_object
*label
;
301 /* Get label and translate it to BFD daemon key. */
302 if (!json_object_object_get_ex(jo
, "label", &label
))
305 sval
= json_object_get_string(label
);
311 log_debug("\tpeer-label: %s", sval
);
313 /* Translate the label into BFD address keys. */
314 bs_to_bpc(pl
->pl_bs
, bpc
);
321 * Control socket JSON parsing.
323 int config_request_add(const char *jsonstr
)
325 struct json_object
*jo
;
327 jo
= json_tokener_parse(jsonstr
);
331 return parse_config_json(jo
, config_add
, NULL
);
334 int config_request_del(const char *jsonstr
)
336 struct json_object
*jo
;
338 jo
= json_tokener_parse(jsonstr
);
342 return parse_config_json(jo
, config_del
, NULL
);
345 char *config_response(const char *status
, const char *error
)
347 struct json_object
*resp
, *jo
;
350 resp
= json_object_new_object();
354 /* Add 'status' response key. */
355 jo
= json_object_new_string(status
);
357 json_object_put(resp
);
361 json_object_object_add(resp
, "status", jo
);
363 /* Add 'error' response key. */
365 jo
= json_object_new_string(error
);
367 json_object_put(resp
);
371 json_object_object_add(resp
, "error", jo
);
374 /* Generate JSON response. */
376 MTYPE_BFDD_NOTIFICATION
,
377 json_object_to_json_string_ext(resp
, BFDD_JSON_CONV_OPTIONS
));
378 json_object_put(resp
);
383 char *config_notify(struct bfd_session
*bs
)
385 struct json_object
*resp
;
389 resp
= json_object_new_object();
393 json_object_string_add(resp
, "op", BCM_NOTIFY_PEER_STATUS
);
395 json_object_add_peer(resp
, bs
);
397 /* Add status information */
398 json_object_int_add(resp
, "id", bs
->discrs
.my_discr
);
399 json_object_int_add(resp
, "remote-id", bs
->discrs
.my_discr
);
401 switch (bs
->ses_state
) {
403 json_object_string_add(resp
, "state", "up");
405 now
= monotime(NULL
);
406 json_object_int_add(resp
, "uptime", now
- bs
->uptime
.tv_sec
);
408 case PTM_BFD_ADM_DOWN
:
409 json_object_string_add(resp
, "state", "adm-down");
412 json_object_string_add(resp
, "state", "down");
414 now
= monotime(NULL
);
415 json_object_int_add(resp
, "downtime",
416 now
- bs
->downtime
.tv_sec
);
419 json_object_string_add(resp
, "state", "init");
423 json_object_string_add(resp
, "state", "unknown");
427 json_object_int_add(resp
, "diagnostics", bs
->local_diag
);
428 json_object_int_add(resp
, "remote-diagnostics", bs
->remote_diag
);
430 /* Generate JSON response. */
432 MTYPE_BFDD_NOTIFICATION
,
433 json_object_to_json_string_ext(resp
, BFDD_JSON_CONV_OPTIONS
));
434 json_object_put(resp
);
439 char *config_notify_config(const char *op
, struct bfd_session
*bs
)
441 struct json_object
*resp
;
444 resp
= json_object_new_object();
448 json_object_string_add(resp
, "op", op
);
450 json_object_add_peer(resp
, bs
);
452 /* On peer deletion we don't need to add any additional information. */
453 if (strcmp(op
, BCM_NOTIFY_CONFIG_DELETE
) == 0)
456 json_object_int_add(resp
, "detect-multiplier", bs
->detect_mult
);
457 json_object_int_add(resp
, "receive-interval",
458 bs
->timers
.required_min_rx
/ 1000);
459 json_object_int_add(resp
, "transmit-interval",
460 bs
->timers
.desired_min_tx
/ 1000);
461 json_object_int_add(resp
, "echo-interval",
462 bs
->timers
.required_min_echo
/ 1000);
464 json_object_int_add(resp
, "remote-detect-multiplier",
465 bs
->remote_detect_mult
);
466 json_object_int_add(resp
, "remote-receive-interval",
467 bs
->remote_timers
.required_min_rx
/ 1000);
468 json_object_int_add(resp
, "remote-transmit-interval",
469 bs
->remote_timers
.desired_min_tx
/ 1000);
470 json_object_int_add(resp
, "remote-echo-interval",
471 bs
->remote_timers
.required_min_echo
/ 1000);
473 if (BFD_CHECK_FLAG(bs
->flags
, BFD_SESS_FLAG_ECHO
))
474 json_object_boolean_true_add(resp
, "echo-mode");
476 json_object_boolean_false_add(resp
, "echo-mode");
478 if (BFD_CHECK_FLAG(bs
->flags
, BFD_SESS_FLAG_SHUTDOWN
))
479 json_object_boolean_true_add(resp
, "shutdown");
481 json_object_boolean_false_add(resp
, "shutdown");
484 /* Generate JSON response. */
486 MTYPE_BFDD_NOTIFICATION
,
487 json_object_to_json_string_ext(resp
, BFDD_JSON_CONV_OPTIONS
));
488 json_object_put(resp
);
493 int config_notify_request(struct bfd_control_socket
*bcs
, const char *jsonstr
,
496 struct json_object
*jo
;
498 jo
= json_tokener_parse(jsonstr
);
502 return parse_config_json(jo
, bh
, bcs
);
505 static int json_object_add_peer(struct json_object
*jo
, struct bfd_session
*bs
)
507 char addr_buf
[INET6_ADDRSTRLEN
];
509 /* Add peer 'key' information. */
510 if (BFD_CHECK_FLAG(bs
->flags
, BFD_SESS_FLAG_IPV6
))
511 json_object_boolean_true_add(jo
, "ipv6");
513 json_object_boolean_false_add(jo
, "ipv6");
515 if (BFD_CHECK_FLAG(bs
->flags
, BFD_SESS_FLAG_MH
)) {
516 json_object_boolean_true_add(jo
, "multihop");
517 json_object_string_add(jo
, "peer-address",
518 inet_ntop(bs
->key
.family
, &bs
->key
.peer
,
519 addr_buf
, sizeof(addr_buf
)));
520 json_object_string_add(jo
, "local-address",
521 inet_ntop(bs
->key
.family
, &bs
->key
.local
,
522 addr_buf
, sizeof(addr_buf
)));
523 if (bs
->key
.vrfname
[0])
524 json_object_string_add(jo
, "vrf-name", bs
->key
.vrfname
);
526 json_object_boolean_false_add(jo
, "multihop");
527 json_object_string_add(jo
, "peer-address",
528 inet_ntop(bs
->key
.family
, &bs
->key
.peer
,
529 addr_buf
, sizeof(addr_buf
)));
530 if (memcmp(&bs
->key
.local
, &zero_addr
, sizeof(bs
->key
.local
)))
531 json_object_string_add(
533 inet_ntop(bs
->key
.family
, &bs
->key
.local
,
534 addr_buf
, sizeof(addr_buf
)));
535 if (bs
->key
.ifname
[0])
536 json_object_string_add(jo
, "local-interface",
541 json_object_string_add(jo
, "label", bs
->pl
->pl_label
);
550 struct peer_label
*pl_find(const char *label
)
552 struct peer_label
*pl
;
554 TAILQ_FOREACH (pl
, &bglobal
.bg_pllist
, pl_entry
) {
555 if (strcmp(pl
->pl_label
, label
) != 0)
564 struct peer_label
*pl_new(const char *label
, struct bfd_session
*bs
)
566 struct peer_label
*pl
;
568 pl
= XCALLOC(MTYPE_BFDD_LABEL
, sizeof(*pl
));
570 if (strlcpy(pl
->pl_label
, label
, sizeof(pl
->pl_label
))
571 > sizeof(pl
->pl_label
))
572 log_warning("%s:%d: label was truncated", __func__
, __LINE__
);
577 TAILQ_INSERT_HEAD(&bglobal
.bg_pllist
, pl
, pl_entry
);
582 void pl_free(struct peer_label
*pl
)
587 /* Remove the pointer back. */
588 pl
->pl_bs
->pl
= NULL
;
590 TAILQ_REMOVE(&bglobal
.bg_pllist
, pl
, pl_entry
);
591 XFREE(MTYPE_BFDD_LABEL
, pl
);