]>
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 zlog_warn("%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_echorecvinterval
= BFD_DEF_REQ_MIN_ECHO_RX
;
139 bpc
.bpc_echotxinterval
= BFD_DEF_DES_MIN_ECHO_TX
;
143 zlog_debug("ipv4 peers %d:", allen
);
147 zlog_debug("ipv6 peers %d:", allen
);
148 bpc
.bpc_ipv4
= false;
151 zlog_debug("label peers %d:", allen
);
152 if (parse_peer_label_config(jo_val
, &bpc
) != 0) {
160 zlog_err("%s:%d: unsupported peer type", __func__
,
165 result
= parse_peer_config(jo_val
, &bpc
);
168 error
+= (h(&bpc
, arg
) != 0);
174 static int parse_peer_config(struct json_object
*jo
, struct bfd_peer_cfg
*bpc
)
176 const char *key
, *sval
;
177 struct json_object
*jo_val
;
178 struct json_object_iterator joi
, join
;
179 int family_type
= (bpc
->bpc_ipv4
) ? AF_INET
: AF_INET6
;
182 zlog_debug(" peer: %s", bpc
->bpc_ipv4
? "ipv4" : "ipv6");
184 JSON_FOREACH (jo
, joi
, join
) {
185 key
= json_object_iter_peek_name(&joi
);
186 jo_val
= json_object_iter_peek_value(&joi
);
188 if (strcmp(key
, "multihop") == 0) {
189 bpc
->bpc_mhop
= json_object_get_boolean(jo_val
);
190 zlog_debug(" multihop: %s",
191 bpc
->bpc_mhop
? "true" : "false");
192 } else if (strcmp(key
, "peer-address") == 0) {
193 sval
= json_object_get_string(jo_val
);
194 if (strtosa(sval
, &bpc
->bpc_peer
) != 0
195 || bpc
->bpc_peer
.sa_sin
.sin_family
!= family_type
) {
197 "%s:%d failed to parse peer-address '%s'",
198 __func__
, __LINE__
, sval
);
201 zlog_debug(" peer-address: %s", sval
);
202 } else if (strcmp(key
, "local-address") == 0) {
203 sval
= json_object_get_string(jo_val
);
204 if (strtosa(sval
, &bpc
->bpc_local
) != 0
205 || bpc
->bpc_local
.sa_sin
.sin_family
208 "%s:%d failed to parse local-address '%s'",
209 __func__
, __LINE__
, sval
);
212 zlog_debug(" local-address: %s", sval
);
213 } else if (strcmp(key
, "local-interface") == 0) {
214 bpc
->bpc_has_localif
= true;
215 sval
= json_object_get_string(jo_val
);
216 if (strlcpy(bpc
->bpc_localif
, sval
,
217 sizeof(bpc
->bpc_localif
))
218 > sizeof(bpc
->bpc_localif
)) {
220 " local-interface: %s (truncated)",
224 zlog_debug(" local-interface: %s", sval
);
226 } else if (strcmp(key
, "vrf-name") == 0) {
227 bpc
->bpc_has_vrfname
= true;
228 sval
= json_object_get_string(jo_val
);
229 if (strlcpy(bpc
->bpc_vrfname
, sval
,
230 sizeof(bpc
->bpc_vrfname
))
231 > sizeof(bpc
->bpc_vrfname
)) {
232 zlog_debug(" vrf-name: %s (truncated)",
236 zlog_debug(" vrf-name: %s", sval
);
238 } else if (strcmp(key
, "detect-multiplier") == 0) {
239 bpc
->bpc_detectmultiplier
=
240 json_object_get_int64(jo_val
);
241 bpc
->bpc_has_detectmultiplier
= true;
242 zlog_debug(" detect-multiplier: %u",
243 bpc
->bpc_detectmultiplier
);
244 } else if (strcmp(key
, "receive-interval") == 0) {
245 bpc
->bpc_recvinterval
= json_object_get_int64(jo_val
);
246 bpc
->bpc_has_recvinterval
= true;
247 zlog_debug(" receive-interval: %" PRIu64
,
248 bpc
->bpc_recvinterval
);
249 } else if (strcmp(key
, "transmit-interval") == 0) {
250 bpc
->bpc_txinterval
= json_object_get_int64(jo_val
);
251 bpc
->bpc_has_txinterval
= true;
252 zlog_debug(" transmit-interval: %" PRIu64
,
253 bpc
->bpc_txinterval
);
254 } else if (strcmp(key
, "echo-receive-interval") == 0) {
255 bpc
->bpc_echorecvinterval
= json_object_get_int64(jo_val
);
256 bpc
->bpc_has_echorecvinterval
= true;
257 zlog_debug(" echo-receive-interval: %" PRIu64
,
258 bpc
->bpc_echorecvinterval
);
259 } else if (strcmp(key
, "echo-transmit-interval") == 0) {
260 bpc
->bpc_echotxinterval
= json_object_get_int64(jo_val
);
261 bpc
->bpc_has_echotxinterval
= true;
262 zlog_debug(" echo-transmit-interval: %" PRIu64
,
263 bpc
->bpc_echotxinterval
);
264 } else if (strcmp(key
, "create-only") == 0) {
265 bpc
->bpc_createonly
= json_object_get_boolean(jo_val
);
266 zlog_debug(" create-only: %s",
267 bpc
->bpc_createonly
? "true" : "false");
268 } else if (strcmp(key
, "shutdown") == 0) {
269 bpc
->bpc_shutdown
= json_object_get_boolean(jo_val
);
270 zlog_debug(" shutdown: %s",
271 bpc
->bpc_shutdown
? "true" : "false");
272 } else if (strcmp(key
, "echo-mode") == 0) {
273 bpc
->bpc_echo
= json_object_get_boolean(jo_val
);
274 zlog_debug(" echo-mode: %s",
275 bpc
->bpc_echo
? "true" : "false");
276 } else if (strcmp(key
, "label") == 0) {
277 bpc
->bpc_has_label
= true;
278 sval
= json_object_get_string(jo_val
);
279 if (strlcpy(bpc
->bpc_label
, sval
,
280 sizeof(bpc
->bpc_label
))
281 > sizeof(bpc
->bpc_label
)) {
282 zlog_debug(" label: %s (truncated)",
286 zlog_debug(" label: %s", sval
);
289 sval
= json_object_get_string(jo_val
);
290 zlog_warn("%s:%d invalid configuration: '%s: %s'",
291 __func__
, __LINE__
, key
, sval
);
296 if (bpc
->bpc_peer
.sa_sin
.sin_family
== 0) {
297 zlog_debug("%s:%d no peer address provided", __func__
,
305 static int parse_peer_label_config(struct json_object
*jo
,
306 struct bfd_peer_cfg
*bpc
)
308 struct peer_label
*pl
;
309 struct json_object
*label
;
312 /* Get label and translate it to BFD daemon key. */
313 if (!json_object_object_get_ex(jo
, "label", &label
))
316 sval
= json_object_get_string(label
);
322 zlog_debug(" peer-label: %s", sval
);
324 /* Translate the label into BFD address keys. */
325 bs_to_bpc(pl
->pl_bs
, bpc
);
332 * Control socket JSON parsing.
334 int config_request_add(const char *jsonstr
)
336 struct json_object
*jo
;
338 jo
= json_tokener_parse(jsonstr
);
342 return parse_config_json(jo
, config_add
, NULL
);
345 int config_request_del(const char *jsonstr
)
347 struct json_object
*jo
;
349 jo
= json_tokener_parse(jsonstr
);
353 return parse_config_json(jo
, config_del
, NULL
);
356 char *config_response(const char *status
, const char *error
)
358 struct json_object
*resp
, *jo
;
361 resp
= json_object_new_object();
365 /* Add 'status' response key. */
366 jo
= json_object_new_string(status
);
368 json_object_put(resp
);
372 json_object_object_add(resp
, "status", jo
);
374 /* Add 'error' response key. */
376 jo
= json_object_new_string(error
);
378 json_object_put(resp
);
382 json_object_object_add(resp
, "error", jo
);
385 /* Generate JSON response. */
387 MTYPE_BFDD_NOTIFICATION
,
388 json_object_to_json_string_ext(resp
, BFDD_JSON_CONV_OPTIONS
));
389 json_object_put(resp
);
394 char *config_notify(struct bfd_session
*bs
)
396 struct json_object
*resp
;
400 resp
= json_object_new_object();
404 json_object_string_add(resp
, "op", BCM_NOTIFY_PEER_STATUS
);
406 json_object_add_peer(resp
, bs
);
408 /* Add status information */
409 json_object_int_add(resp
, "id", bs
->discrs
.my_discr
);
410 json_object_int_add(resp
, "remote-id", bs
->discrs
.my_discr
);
412 switch (bs
->ses_state
) {
414 json_object_string_add(resp
, "state", "up");
416 now
= monotime(NULL
);
417 json_object_int_add(resp
, "uptime", now
- bs
->uptime
.tv_sec
);
419 case PTM_BFD_ADM_DOWN
:
420 json_object_string_add(resp
, "state", "adm-down");
423 json_object_string_add(resp
, "state", "down");
425 now
= monotime(NULL
);
426 json_object_int_add(resp
, "downtime",
427 now
- bs
->downtime
.tv_sec
);
430 json_object_string_add(resp
, "state", "init");
434 json_object_string_add(resp
, "state", "unknown");
438 json_object_int_add(resp
, "diagnostics", bs
->local_diag
);
439 json_object_int_add(resp
, "remote-diagnostics", bs
->remote_diag
);
441 /* Generate JSON response. */
443 MTYPE_BFDD_NOTIFICATION
,
444 json_object_to_json_string_ext(resp
, BFDD_JSON_CONV_OPTIONS
));
445 json_object_put(resp
);
450 char *config_notify_config(const char *op
, struct bfd_session
*bs
)
452 struct json_object
*resp
;
455 resp
= json_object_new_object();
459 json_object_string_add(resp
, "op", op
);
461 json_object_add_peer(resp
, bs
);
463 /* On peer deletion we don't need to add any additional information. */
464 if (strcmp(op
, BCM_NOTIFY_CONFIG_DELETE
) == 0)
467 json_object_int_add(resp
, "detect-multiplier", bs
->detect_mult
);
468 json_object_int_add(resp
, "receive-interval",
469 bs
->timers
.required_min_rx
/ 1000);
470 json_object_int_add(resp
, "transmit-interval",
471 bs
->timers
.desired_min_tx
/ 1000);
472 json_object_int_add(resp
, "echo-receive-interval",
473 bs
->timers
.required_min_echo_rx
/ 1000);
474 json_object_int_add(resp
, "echo-transmit-interval",
475 bs
->timers
.desired_min_echo_tx
/ 1000);
477 json_object_int_add(resp
, "remote-detect-multiplier",
478 bs
->remote_detect_mult
);
479 json_object_int_add(resp
, "remote-receive-interval",
480 bs
->remote_timers
.required_min_rx
/ 1000);
481 json_object_int_add(resp
, "remote-transmit-interval",
482 bs
->remote_timers
.desired_min_tx
/ 1000);
483 json_object_int_add(resp
, "remote-echo-receive-interval",
484 bs
->remote_timers
.required_min_echo
/ 1000);
486 if (CHECK_FLAG(bs
->flags
, BFD_SESS_FLAG_ECHO
))
487 json_object_boolean_true_add(resp
, "echo-mode");
489 json_object_boolean_false_add(resp
, "echo-mode");
491 if (CHECK_FLAG(bs
->flags
, BFD_SESS_FLAG_SHUTDOWN
))
492 json_object_boolean_true_add(resp
, "shutdown");
494 json_object_boolean_false_add(resp
, "shutdown");
497 /* Generate JSON response. */
499 MTYPE_BFDD_NOTIFICATION
,
500 json_object_to_json_string_ext(resp
, BFDD_JSON_CONV_OPTIONS
));
501 json_object_put(resp
);
506 int config_notify_request(struct bfd_control_socket
*bcs
, const char *jsonstr
,
509 struct json_object
*jo
;
511 jo
= json_tokener_parse(jsonstr
);
515 return parse_config_json(jo
, bh
, bcs
);
518 static int json_object_add_peer(struct json_object
*jo
, struct bfd_session
*bs
)
520 char addr_buf
[INET6_ADDRSTRLEN
];
522 /* Add peer 'key' information. */
523 if (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 (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 inet_ntop(bs
->key
.family
, &bs
->key
.peer
,
532 addr_buf
, sizeof(addr_buf
)));
533 json_object_string_add(jo
, "local-address",
534 inet_ntop(bs
->key
.family
, &bs
->key
.local
,
535 addr_buf
, sizeof(addr_buf
)));
536 if (bs
->key
.vrfname
[0])
537 json_object_string_add(jo
, "vrf-name", bs
->key
.vrfname
);
539 json_object_boolean_false_add(jo
, "multihop");
540 json_object_string_add(jo
, "peer-address",
541 inet_ntop(bs
->key
.family
, &bs
->key
.peer
,
542 addr_buf
, sizeof(addr_buf
)));
543 if (memcmp(&bs
->key
.local
, &zero_addr
, sizeof(bs
->key
.local
)))
544 json_object_string_add(
546 inet_ntop(bs
->key
.family
, &bs
->key
.local
,
547 addr_buf
, sizeof(addr_buf
)));
548 if (bs
->key
.ifname
[0])
549 json_object_string_add(jo
, "local-interface",
554 json_object_string_add(jo
, "label", bs
->pl
->pl_label
);
563 struct peer_label
*pl_find(const char *label
)
565 struct peer_label
*pl
;
567 TAILQ_FOREACH (pl
, &bglobal
.bg_pllist
, pl_entry
) {
568 if (strcmp(pl
->pl_label
, label
) != 0)
577 struct peer_label
*pl_new(const char *label
, struct bfd_session
*bs
)
579 struct peer_label
*pl
;
581 pl
= XCALLOC(MTYPE_BFDD_LABEL
, sizeof(*pl
));
583 if (strlcpy(pl
->pl_label
, label
, sizeof(pl
->pl_label
))
584 > sizeof(pl
->pl_label
))
585 zlog_warn("%s:%d: label was truncated", __func__
, __LINE__
);
590 TAILQ_INSERT_HEAD(&bglobal
.bg_pllist
, pl
, pl_entry
);
595 void pl_free(struct peer_label
*pl
)
600 /* Remove the pointer back. */
601 pl
->pl_bs
->pl
= NULL
;
603 TAILQ_REMOVE(&bglobal
.bg_pllist
, pl
, pl_entry
);
604 XFREE(MTYPE_BFDD_LABEL
, pl
);