Skip to content

Commit 52de58e

Browse files
authored
Merge pull request FRRouting#20259 from LabNConsulting/chopps/libyang4
lib: adapt to libyang4 NBC API changes
2 parents 2ffd2bf + 51d073b commit 52de58e

File tree

17 files changed

+242
-56
lines changed

17 files changed

+242
-56
lines changed

configure.ac

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2086,6 +2086,20 @@ dnl ---------------
20862086
PKG_CHECK_MODULES([LIBYANG], [libyang >= 2.1.128], , [
20872087
AC_MSG_ERROR([m4_normalize([libyang >= 2.1.128 is required, and was not found on your system.
20882088
Please consult doc/developer/building-libyang.rst for instructions on installing or building libyang.])])])
2089+
2090+
LIBYANG_MAJOR=$(pkg-config --modversion libyang | cut -f1 -d.)
2091+
if test "$LIBYANG_MAJOR" -ge 4; then
2092+
dnl NOTE: binary encoding (LYD_LYB) is no longer supported in v4,
2093+
dnl so v3 may be more efficient for certain use cases
2094+
AC_MSG_WARN([m4_normalize([
2095+
Libyang v4 is supported; however libyang v3 use is still suggested. Libyang
2096+
v4 has changed its binary encoding (LYD_LYB) format to a very restricted
2097+
subset of users which does not include FRR. This mean FRR will always use
2098+
JSON or XML even when passing intermediate results internally.
2099+
])
2100+
])
2101+
fi
2102+
20892103
ac_cflags_save="$CFLAGS"
20902104
CFLAGS="$CFLAGS $LIBYANG_CFLAGS"
20912105
AC_CHECK_MEMBER([struct lyd_node.priv], [], [

doc/developer/logging.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,14 @@ General utility formats
444444
(This is a GNU extension not specific to FRR. FRR guarantees it is
445445
available on all systems in printfrr, though BSDs support it in printf too.)
446446
447+
.. frrfmt:: %pSA (const char **)
448+
449+
([S]tring [A]rray.) Like ``%s``, but prints an array of strings (char *)
450+
until a NULL pointer is seen.
451+
452+
If a length is specified (``%*pSA`` or ``%.*pSA``), it is the number of
453+
strings in the array to print.
454+
447455
.. frrfmt:: %pSQ (char *)
448456
449457
([S]tring [Q]uote.) Like ``%s``, but produce a quoted string. Options:

lib/if.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1762,8 +1762,8 @@ static enum nb_error lib_interface_state_if_index_get(const struct nb_node *nb_n
17621762
const struct interface *ifp = list_entry;
17631763
int32_t value = ifp->ifindex;
17641764

1765-
if (lyd_new_term_bin(parent, snode->module, snode->name, &value, sizeof(value),
1766-
LYD_NEW_PATH_UPDATE, NULL))
1765+
if (yang_new_term_bin(parent, snode->module, snode->name, &value, sizeof(value),
1766+
LYD_NEW_PATH_UPDATE, NULL))
17671767
return NB_ERR_RESOURCE;
17681768
return NB_OK;
17691769
}
@@ -1778,8 +1778,8 @@ static enum nb_error lib_interface_state_mtu_get(const struct nb_node *nb_node,
17781778
const struct interface *ifp = list_entry;
17791779
uint32_t value = ifp->mtu;
17801780

1781-
if (lyd_new_term_bin(parent, snode->module, snode->name, &value, sizeof(value),
1782-
LYD_NEW_PATH_UPDATE, NULL))
1781+
if (yang_new_term_bin(parent, snode->module, snode->name, &value, sizeof(value),
1782+
LYD_NEW_PATH_UPDATE, NULL))
17831783
return NB_ERR_RESOURCE;
17841784
return NB_OK;
17851785
}
@@ -1794,8 +1794,8 @@ static enum nb_error lib_interface_state_speed_get(const struct nb_node *nb_node
17941794
const struct interface *ifp = list_entry;
17951795
uint32_t value = ifp->speed;
17961796

1797-
if (lyd_new_term_bin(parent, snode->module, snode->name, &value, sizeof(value),
1798-
LYD_NEW_PATH_UPDATE, NULL))
1797+
if (yang_new_term_bin(parent, snode->module, snode->name, &value, sizeof(value),
1798+
LYD_NEW_PATH_UPDATE, NULL))
17991799
return NB_ERR_RESOURCE;
18001800
return NB_OK;
18011801
}
@@ -1810,8 +1810,8 @@ static enum nb_error lib_interface_state_metric_get(const struct nb_node *nb_nod
18101810
const struct interface *ifp = list_entry;
18111811
uint32_t value = ifp->metric;
18121812

1813-
if (lyd_new_term_bin(parent, snode->module, snode->name, &value, sizeof(value),
1814-
LYD_NEW_PATH_UPDATE, NULL))
1813+
if (yang_new_term_bin(parent, snode->module, snode->name, &value, sizeof(value),
1814+
LYD_NEW_PATH_UPDATE, NULL))
18151815
return NB_ERR_RESOURCE;
18161816
return NB_OK;
18171817
}

lib/mgmt_be_client.c

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -848,17 +848,15 @@ static void be_client_handle_rpc(struct mgmt_be_client *client, uint64_t txn_id,
848848
* It is especially needed for actions, because their parents
849849
* may hold necessary information.
850850
*/
851-
err = lyd_new_path2(NULL, ly_native_ctx, xpath, NULL, 0, 0, 0,
852-
NULL, &input);
851+
err = yang_new_path2(NULL, ly_native_ctx, xpath, NULL, 0, 0, 0, NULL, &input);
853852
}
854853
if (err) {
855854
be_client_send_error(client, txn_id, rpc_msg->req_id, false, -EINVAL,
856855
"Can't parse RPC data for: %s", xpath);
857856
return;
858857
}
859858

860-
err = lyd_new_path2(NULL, ly_native_ctx, xpath, NULL, 0, 0, 0, NULL,
861-
&output);
859+
err = yang_new_path2(NULL, ly_native_ctx, xpath, NULL, 0, 0, 0, NULL, &output);
862860
if (err) {
863861
lyd_free_all(input);
864862
be_client_send_error(client, txn_id, rpc_msg->req_id, false,
@@ -1223,8 +1221,8 @@ static enum nb_error clients_client_state_candidate_config_version_get(
12231221
else
12241222
value = __be_client->running_config->version;
12251223

1226-
if (lyd_new_term_bin(parent, snode->module, snode->name, &value, sizeof(value),
1227-
LYD_NEW_PATH_UPDATE, NULL))
1224+
if (yang_new_term_bin(parent, snode->module, snode->name, &value, sizeof(value),
1225+
LYD_NEW_PATH_UPDATE, NULL))
12281226
return NB_ERR_RESOURCE;
12291227

12301228
return NB_OK;
@@ -1240,8 +1238,8 @@ static enum nb_error clients_client_state_running_config_version_get(const struc
12401238
const struct lysc_node *snode = nb_node->snode;
12411239
uint64_t value = __be_client->running_config->version;
12421240

1243-
if (lyd_new_term_bin(parent, snode->module, snode->name, &value, sizeof(value),
1244-
LYD_NEW_PATH_UPDATE, NULL))
1241+
if (yang_new_term_bin(parent, snode->module, snode->name, &value, sizeof(value),
1242+
LYD_NEW_PATH_UPDATE, NULL))
12451243
return NB_ERR_RESOURCE;
12461244

12471245
return NB_OK;

lib/northbound.c

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -723,8 +723,8 @@ static LY_ERR dnode_create(struct nb_config *candidate, const char *xpath, const
723723
struct lyd_node *dnode;
724724
LY_ERR err;
725725

726-
err = lyd_new_path2(candidate->dnode, ly_native_ctx, xpath, value, 0, 0, options, NULL,
727-
&dnode);
726+
err = yang_new_path2(candidate->dnode, ly_native_ctx, xpath, value, 0, 0, options, NULL,
727+
&dnode);
728728
if (err) {
729729
flog_warn(EC_LIB_LIBYANG, "%s: lyd_new_path(%s) failed: %d",
730730
__func__, xpath, err);
@@ -892,8 +892,8 @@ static int nb_candidate_edit_tree_add(struct nb_config *candidate,
892892
* merged later with candidate.
893893
*/
894894
if (parent_xpath) {
895-
err = lyd_new_path2(NULL, ly_native_ctx, parent_xpath, NULL, 0,
896-
0, 0, &tree, &parent);
895+
err = yang_new_path2(NULL, ly_native_ctx, parent_xpath, NULL, 0, 0, 0, &tree,
896+
&parent);
897897
if (err) {
898898
yang_print_errors(ly_native_ctx, errmsg, errmsg_len);
899899
ret = NB_ERR;
@@ -2301,7 +2301,12 @@ bool nb_cb_operation_is_valid(enum nb_cb_operation operation,
23012301
if (sleaf->when)
23022302
return true;
23032303
if (CHECK_FLAG(sleaf->flags, LYS_MAND_TRUE)
2304-
|| sleaf->dflt)
2304+
#if (LY_VERSION_MAJOR < 4)
2305+
|| sleaf->dflt
2306+
#else
2307+
|| sleaf->dflt.str
2308+
#endif
2309+
)
23052310
return false;
23062311
break;
23072312
case LYS_CONTAINER:

lib/northbound_cli.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -296,8 +296,7 @@ int nb_cli_rpc(struct vty *vty, const char *xpath, struct lyd_node **output_p)
296296
}
297297

298298
/* create input tree */
299-
err = lyd_new_path2(NULL, ly_native_ctx, xpath, NULL, 0, 0, 0, NULL,
300-
&input);
299+
err = yang_new_path2(NULL, ly_native_ctx, xpath, NULL, 0, 0, 0, NULL, &input);
301300
assert(err == LY_SUCCESS);
302301

303302
for (size_t i = 0; i < vty->num_rpc_params; i++) {
@@ -318,8 +317,7 @@ int nb_cli_rpc(struct vty *vty, const char *xpath, struct lyd_node **output_p)
318317
assert(err == LY_SUCCESS);
319318

320319
/* create output tree root for population in the callback */
321-
err = lyd_new_path2(NULL, ly_native_ctx, xpath, NULL, 0, 0, 0, NULL,
322-
&output);
320+
err = yang_new_path2(NULL, ly_native_ctx, xpath, NULL, 0, 0, 0, NULL, &output);
323321
assert(err == LY_SUCCESS);
324322

325323
ret = nb_callback_rpc(nb_node, xpath, input, output, errmsg,

lib/northbound_grpc.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1032,8 +1032,8 @@ grpc::Status HandleUnaryExecute(
10321032
"Unknown data path");
10331033

10341034
// Create input data tree.
1035-
err = lyd_new_path2(NULL, ly_native_ctx, xpath, NULL, 0,
1036-
(LYD_ANYDATA_VALUETYPE)0, 0, NULL, &input_tree);
1035+
err = yang_new_path2(NULL, ly_native_ctx, xpath, NULL, 0, (LYD_ANYDATA_VALUETYPE)0, 0,
1036+
NULL, &input_tree);
10371037
if (err != LY_SUCCESS) {
10381038
return grpc::Status(grpc::StatusCode::INVALID_ARGUMENT,
10391039
"Invalid data path");
@@ -1061,8 +1061,8 @@ grpc::Status HandleUnaryExecute(
10611061
}
10621062

10631063
// Create output data tree.
1064-
err = lyd_new_path2(NULL, ly_native_ctx, xpath, NULL, 0,
1065-
(LYD_ANYDATA_VALUETYPE)0, 0, NULL, &output_tree);
1064+
err = yang_new_path2(NULL, ly_native_ctx, xpath, NULL, 0, (LYD_ANYDATA_VALUETYPE)0, 0,
1065+
NULL, &output_tree);
10661066
if (err != LY_SUCCESS) {
10671067
lyd_free_tree(input_tree);
10681068
return grpc::Status(grpc::StatusCode::INVALID_ARGUMENT,

lib/northbound_oper.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -442,8 +442,8 @@ static enum nb_error nb_op_xpath_to_trunk(const char *xpath_in, char **xpath_out
442442

443443
ly_temp_log_options(&llopts);
444444
for (;;) {
445-
err = lyd_new_path2(NULL, ly_native_ctx, xpath, NULL, 0, 0,
446-
LYD_NEW_PATH_UPDATE, NULL, trunk);
445+
err = yang_new_path2(NULL, ly_native_ctx, xpath, NULL, 0, 0, LYD_NEW_PATH_UPDATE,
446+
NULL, trunk);
447447
if (err == LY_SUCCESS)
448448
break;
449449

@@ -2405,8 +2405,8 @@ enum nb_error nb_oper_uint64_get(const struct nb_node *nb_node, const void *pare
24052405
size_t size;
24062406

24072407
valuep = _adjust_ptr(lsnode, (const char *)&ubigval, &size);
2408-
if (lyd_new_term_bin(parent, snode->module, snode->name, valuep, size, LYD_NEW_PATH_UPDATE,
2409-
NULL))
2408+
if (yang_new_term_bin(parent, snode->module, snode->name, valuep, size,
2409+
LYD_NEW_PATH_UPDATE, NULL))
24102410
return NB_ERR_RESOURCE;
24112411
return NB_OK;
24122412
}
@@ -2423,8 +2423,8 @@ enum nb_error nb_oper_uint32_get(const struct nb_node *nb_node, const void *pare
24232423
size_t size;
24242424

24252425
valuep = _adjust_ptr(lsnode, (const char *)&ubigval, &size);
2426-
if (lyd_new_term_bin(parent, snode->module, snode->name, valuep, size, LYD_NEW_PATH_UPDATE,
2427-
NULL))
2426+
if (yang_new_term_bin(parent, snode->module, snode->name, valuep, size,
2427+
LYD_NEW_PATH_UPDATE, NULL))
24282428
return NB_ERR_RESOURCE;
24292429
return NB_OK;
24302430
}

lib/printfrr.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,7 @@ struct va_format {
265265
};
266266

267267
#ifdef _FRR_ATTRIBUTE_PRINTFRR
268+
// clang-format off
268269
#pragma FRR printfrr_ext "%pFB" (struct fbuf *)
269270
#pragma FRR printfrr_ext "%pVA" (struct va_format *)
270271

@@ -277,10 +278,12 @@ struct va_format {
277278

278279
#pragma FRR printfrr_ext "%pSE" (char *)
279280
#pragma FRR printfrr_ext "%pSQ" (char *)
281+
#pragma FRR printfrr_ext "%pSA" (const char **)
280282

281283
#pragma FRR printfrr_ext "%pTS" (struct timespec *)
282284
#pragma FRR printfrr_ext "%pTV" (struct timeval *)
283285
#pragma FRR printfrr_ext "%pTT" (time_t *)
286+
// clang-format on
284287
#endif
285288

286289
/* when using non-ISO-C compatible extension specifiers... */

lib/strformat.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,27 @@ static ssize_t printfrr_quote(struct fbuf *buf, struct printfrr_eargs *ea,
262262
return ret;
263263
}
264264

265+
printfrr_ext_autoreg_p("SA", printfrr_str_array);
266+
static ssize_t printfrr_str_array(struct fbuf *buf, struct printfrr_eargs *ea, const void *vptr)
267+
{
268+
ssize_t len = printfrr_ext_len(ea);
269+
char const *const *sarr = vptr;
270+
ssize_t olen = 0;
271+
int i;
272+
273+
if (!sarr)
274+
return bputs(buf, "");
275+
276+
for (i = 0; len < 0 || i < len; i++) {
277+
if (!sarr[i])
278+
break;
279+
if (i > 0)
280+
olen += bputch(buf, ',');
281+
olen += bputs(buf, sarr[i]);
282+
}
283+
return olen;
284+
}
285+
265286
static ssize_t printfrr_abstime(struct fbuf *buf, struct printfrr_eargs *ea,
266287
const struct timespec *ts, unsigned int flags);
267288
static ssize_t printfrr_reltime(struct fbuf *buf, struct printfrr_eargs *ea,

0 commit comments

Comments
 (0)