Skip to content

sonic-mgmt-common: port to libyang3 #157

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

bradh352
Copy link

@bradh352 bradh352 commented Feb 19, 2025

SONiC Libyang3 upgrade Tracking ticket sonic-net/sonic-buildimage#22385

This PR depends on the other PRs mentioned in the above tracking ticket.

The API changes between libyang1 and libyang3 are drastic.

The cvl test cases are currently passing all tests.

This depends on libyang3's devel branch plus this rejected patch:

Or the libyang3 package built via sonic-buildimage which is based on v3.7.8 + all the necessary patches, part of sonic-net/sonic-buildimage#21679

Fixes sonic-net/sonic-buildimage#22385

@mssonicbld
Copy link

/azp run

@bradh352 bradh352 marked this pull request as draft February 19, 2025 15:18
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@mssonicbld
Copy link

/azp run

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@mssonicbld
Copy link

/azp run

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@mssonicbld
Copy link

/azp run

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@mssonicbld
Copy link

/azp run

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@mssonicbld
Copy link

/azp run

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@mssonicbld
Copy link

/azp run

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@mssonicbld
Copy link

/azp run

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@mssonicbld
Copy link

/azp run

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@mssonicbld
Copy link

/azp run

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@bradh352
Copy link
Author

@dutta-partha probably going to need your help on this as you are the original author. I've got the port to libyang3 compiling at least, but I'm not entirely sure how to test it or debug it. I'm sure there are issues here.

@ganglyu I've seen you make some commits here too, maybe you can help?

I think out of all the libyang3 porting this is the one that is the most complex and that I know the least about.

@ganglyu
Copy link
Contributor

ganglyu commented Feb 19, 2025

@bradh352
This pipeline downloads libyang*.deb from the sonic-buildimage.vs master pipeline artifacts. However, since your changes have not been merged into the master branch, the libyang*3.*.deb file is not available.

    - task: DownloadPipelineArtifact@2
      inputs:
        source: specific
        project: build
        pipeline: 142
        artifact: sonic-buildimage.vs
        runVersion: 'latestFromBranch'
        runBranch: 'refs/heads/$(BUILD_BRANCH)'
        patterns: |
            target/debs/bookworm/libyang*.deb
            target/python-wheels/bookworm/sonic_yang_models*.whl
      displayName: "Download sonic buildimage"

@ganglyu
Copy link
Contributor

ganglyu commented Feb 19, 2025

You can follow the pipeline to setup your environment and run unit test.
https://github.com/sonic-net/sonic-mgmt-common/blob/master/azure-pipelines.yml

@bradh352
Copy link
Author

@ganglyu

@bradh352 This pipeline downloads libyang*.deb from the sonic-buildimage.vs master pipeline artifacts. However, since your changes have not been merged into the master branch, the libyang_3._.deb file is not available.

Right, I'm aware of that. This PR is expected to fail in the SONiC CI system (azure pipelines) until the dependent PRs are merged.

I was hoping someone more familiar with sonic-mgmt-common could help with testing and validation of this particular PR. Its by far the most complex port to libyang3 out of all the various sub-projects.

@ganglyu
Copy link
Contributor

ganglyu commented Feb 20, 2025

@sachinholla would you please help?

@mssonicbld
Copy link

/azp run

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@mssonicbld
Copy link

/azp run

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@mssonicbld
Copy link

/azp run

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@mssonicbld
Copy link

/azp run

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@bradh352 bradh352 force-pushed the bradh352/libyang3 branch from 02ce756 to 4a72c1f Compare March 4, 2025 00:09
@mssonicbld
Copy link

/azp run

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@bradh352 bradh352 force-pushed the bradh352/libyang3 branch from 4a72c1f to 3fad970 Compare March 4, 2025 00:11
@mssonicbld
Copy link

/azp run

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@bradh352 bradh352 force-pushed the bradh352/libyang3 branch from 3fad970 to 36e287b Compare March 4, 2025 00:13
@mssonicbld
Copy link

/azp run

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@bradh352 bradh352 force-pushed the bradh352/libyang3 branch from 36e287b to 10f9078 Compare March 4, 2025 00:15
@mssonicbld
Copy link

/azp run

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@bradh352 bradh352 marked this pull request as ready for review March 4, 2025 00:18
Copy link
Contributor

@sachinholla sachinholla left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @bradh352, all tests are passing on my development vm too.. Changes look good to me. Will ask cvl maintainers to take a look.

Seeing a few compiler warnings though:

In file included from _cgo_export.c:4:
util.go: In function ‘customLogCb’:
util.go:30:65: warning: passing argument 3 of ‘customLogCallback’ discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]
util.go:26:62: note: expected ‘char *’ but argument is of type ‘const char *’
# github.com/Azure/sonic-mgmt-common/cvl/internal/util
../cvl/internal/util/util.go: In function ‘customLogCb’:
../cvl/internal/util/util.go:30:72: warning: passing argument 3 of ‘customLogCallback’ discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]
   30 |         customLogCallback(level, (char*)msg, (char*)data_path?data_path:schema_path);
      |                                              ~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
../cvl/internal/util/util.go:26:62: note: expected ‘char *’ but argument is of type ‘const char *’
   26 | extern void customLogCallback(LY_LOG_LEVEL, char* msg, char* path);
      |                                                        ~~~~~~^~~~

@bradh352
Copy link
Author

bradh352 commented Mar 4, 2025

Hi @bradh352, all tests are passing on my development vm too.. Changes look good to me. Will ask cvl maintainers to take a look.

Seeing a few compiler warnings though:

In file included from _cgo_export.c:4:
util.go: In function ‘customLogCb’:
util.go:30:65: warning: passing argument 3 of ‘customLogCallback’ discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]
util.go:26:62: note: expected ‘char *’ but argument is of type ‘const char *’
# github.com/Azure/sonic-mgmt-common/cvl/internal/util
../cvl/internal/util/util.go: In function ‘customLogCb’:
../cvl/internal/util/util.go:30:72: warning: passing argument 3 of ‘customLogCallback’ discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]
   30 |         customLogCallback(level, (char*)msg, (char*)data_path?data_path:schema_path);
      |                                              ~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
../cvl/internal/util/util.go:26:62: note: expected ‘char *’ but argument is of type ‘const char *’
   26 | extern void customLogCallback(LY_LOG_LEVEL, char* msg, char* path);
      |                                                        ~~~~~~^~~~

These warnings are from the original code and not something newly introduced. Its due to the Golang<->C (cgo) stuff, where Go doesn't know about the const qualifier in C. If you really want me to address it I can probably look into it, here's a reference to the issue:
https://stackoverflow.com/questions/32938558/cgo-cant-find-way-to-use-callbacks-with-const-char-argument

@bradh352
Copy link
Author

bradh352 commented Mar 6, 2025

@sachinholla any other comments on this review? If not, could you approve it?

@faraazbrcm
Copy link
Contributor

faraazbrcm commented Mar 6, 2025

@bradh352 Code changes look good to me.
I wanted to ensure SET/GET going fine using gNMI and RESTCONF interfaces. I will run some some tests next week and update you.

@bradh352
Copy link
Author

bradh352 commented Mar 6, 2025

let me know if you need an image to test and for what platform. I've got a tree with all 17 PRs in this series that I test with here: https://github.com/bradh352/sonic-buildimage/commits/bradh352/libyang3/

@sachinholla
Copy link
Contributor

In file included from _cgo_export.c:4:
util.go: In function ‘customLogCb’:
util.go:30:65: warning: passing argument 3 of ‘customLogCallback’ discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]
util.go:26:62: note: expected ‘char *’ but argument is of type ‘const char *’
# github.com/Azure/sonic-mgmt-common/cvl/internal/util
../cvl/internal/util/util.go: In function ‘customLogCb’:
../cvl/internal/util/util.go:30:72: warning: passing argument 3 of ‘customLogCallback’ discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]
   30 |         customLogCallback(level, (char*)msg, (char*)data_path?data_path:schema_path);
      |                                              ~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
../cvl/internal/util/util.go:26:62: note: expected ‘char *’ but argument is of type ‘const char *’
   26 | extern void customLogCallback(LY_LOG_LEVEL, char* msg, char* path);
      |                                                        ~~~~~~^~~~

These warnings are from the original code and not something newly introduced. Its due to the Golang<->C (cgo) stuff, where Go doesn't know about the const qualifier in C.

@bradh352 -- i don't see this warning with old code. Let us fix it as it shows up during compilation & test runs of all other cvl dependent components. Cgo handles const qualifier if you define a typedef with it. Following works for me:

diff --git a/cvl/internal/util/util.go b/cvl/internal/util/util.go
index a1664f2..9c357b8 100644
--- a/cvl/internal/util/util.go
+++ b/cvl/internal/util/util.go
@@ -23,7 +23,8 @@ package util
 #cgo LDFLAGS: -lyang
 #include <libyang/libyang.h>

-extern void customLogCallback(LY_LOG_LEVEL, char* msg, char* path);
+typedef const char const_char_t;
+extern void customLogCallback(LY_LOG_LEVEL, const_char_t* msg, const_char_t* path);

 static void customLogCb(LY_LOG_LEVEL level, const char* msg, const char* data_path, const char * schema_path, uint64_t line) {
        (void)line;
@@ -188,7 +189,7 @@ func IsTraceSet() bool {
 changing libyang's global log setting */

 //export customLogCallback
-func customLogCallback(level C.LY_LOG_LEVEL, msg *C.char, path *C.char) {
+func customLogCallback(level C.LY_LOG_LEVEL, msg *C.const_char_t, path *C.const_char_t) {
        if level == C.LY_LLERR {
                CVL_LEVEL_LOG(WARNING, "[libyang Error] %s (path: %s)", C.GoString(msg), C.GoString(path))
        } else {

@bradh352
Copy link
Author

In file included from _cgo_export.c:4:
util.go: In function ‘customLogCb’:
util.go:30:65: warning: passing argument 3 of ‘customLogCallback’ discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]
util.go:26:62: note: expected ‘char *’ but argument is of type ‘const char *’
# github.com/Azure/sonic-mgmt-common/cvl/internal/util
../cvl/internal/util/util.go: In function ‘customLogCb’:
../cvl/internal/util/util.go:30:72: warning: passing argument 3 of ‘customLogCallback’ discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]
   30 |         customLogCallback(level, (char*)msg, (char*)data_path?data_path:schema_path);
      |                                              ~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
../cvl/internal/util/util.go:26:62: note: expected ‘char *’ but argument is of type ‘const char *’
   26 | extern void customLogCallback(LY_LOG_LEVEL, char* msg, char* path);
      |                                                        ~~~~~~^~~~

These warnings are from the original code and not something newly introduced. Its due to the Golang<->C (cgo) stuff, where Go doesn't know about the const qualifier in C.

@bradh352 -- i don't see this warning with old code. Let us fix it as it shows up during compilation & test runs of all other cvl dependent components. Cgo handles const qualifier if you define a typedef with it. Following works for me:

diff --git a/cvl/internal/util/util.go b/cvl/internal/util/util.go
index a1664f2..9c357b8 100644
--- a/cvl/internal/util/util.go
+++ b/cvl/internal/util/util.go
@@ -23,7 +23,8 @@ package util
 #cgo LDFLAGS: -lyang
 #include <libyang/libyang.h>

-extern void customLogCallback(LY_LOG_LEVEL, char* msg, char* path);
+typedef const char const_char_t;
+extern void customLogCallback(LY_LOG_LEVEL, const_char_t* msg, const_char_t* path);

 static void customLogCb(LY_LOG_LEVEL level, const char* msg, const char* data_path, const char * schema_path, uint64_t line) {
        (void)line;
@@ -188,7 +189,7 @@ func IsTraceSet() bool {
 changing libyang's global log setting */

 //export customLogCallback
-func customLogCallback(level C.LY_LOG_LEVEL, msg *C.char, path *C.char) {
+func customLogCallback(level C.LY_LOG_LEVEL, msg *C.const_char_t, path *C.const_char_t) {
        if level == C.LY_LLERR {
                CVL_LEVEL_LOG(WARNING, "[libyang Error] %s (path: %s)", C.GoString(msg), C.GoString(path))
        } else {

Thanks for letting me know how to resolve that with cgo, i'll go ahead and apply that patch. Traveling at the moment, but I'll get to it in the next day or two.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Enhancement: Upgrade to libyang3
5 participants