Page MenuHomeFreeBSD

lib/flua: Add bindings for libifconfig
Needs ReviewPublic

Authored by freqlabs on Jun 25 2020, 11:08 AM.
Tags
None
Referenced Files
Unknown Object (File)
Sat, Mar 23, 6:31 AM
Unknown Object (File)
Sun, Mar 10, 1:58 PM
Unknown Object (File)
Thu, Mar 7, 6:37 PM
Unknown Object (File)
Dec 22 2023, 11:16 PM
Unknown Object (File)
Dec 10 2023, 12:09 AM
Unknown Object (File)
Dec 4 2023, 10:11 AM
Unknown Object (File)
Nov 7 2023, 1:20 AM
Unknown Object (File)
Nov 5 2023, 7:00 PM

Details

Reviewers
kevans
emaste
kp
Group Reviewers
manpages
Summary

Add Lua bindings for libifconfig to flua.

Throwing this on phab to see if anyone has a use case for it. Otherwise, I don't have plans to commit it yet. libifconfig really should have more features.

Test Plan
#!/usr/libexec/flua

ifcfg = require('ifconfig').open()
ucl = require('ucl')

ifaces = ifcfg:foreach_iface(function(_, iface, ifs)
    local name = iface:name()
    ifs[name] = {
        media = ifcfg:get_media(name),
        status = ifcfg:get_status(name),
        capabilities = ifcfg:get_capability(name),
        groups = ifcfg:get_groups(name),
        metric = ifcfg:get_metric(name),
        mtu = ifcfg:get_mtu(name),
        nd6 = ifcfg:get_nd6(name),
        lagg_status = ifcfg:get_lagg_status(name),
        laggdev = ifcfg:get_laggport_laggdev(name),
        addresses = ifcfg:foreach_ifaddr(iface, function(_, addr, addrs)
            table.insert(addrs, ifcfg:addr_info(addr))
            return addrs
        end, {})
    }
    return ifs
end, {})

function yaml(obj)
    print(ucl.to_format(obj, "yaml"))
end

yaml(ifaces)

ifcfg:get_status("usb")
print(ifcfg:error())

Diff Detail

Repository
rS FreeBSD src repository - subversion
Lint
Lint Skipped
Unit
Tests Skipped

Event Timeline

Tossing kp in the mix for the giggles of it

Perhaps useful for network testing purposes?

I can see some potential for automation and orchestration.

Without immediate users for this it's probably a bit early to commit this, but I like the notion.
If nothing else it gives a bit more sample code of how we can wrap things in Lua, and I could also see use cases for base-system scripting where we may want something other than shell to do things in.

Thanks for the feedback. Definitely early days for all this. I'm continuing the work over on gitlab to avoid too much phabricator spam for the time being :)
https://gitlab.com/freqlabs/lua-libifconfig

linimon retitled this revision from flua: Add bindings for libifconfig to /usr/libexec/flua: Add bindings for libifconfig.Jun 26 2020, 7:57 AM
freqlabs edited the summary of this revision. (Show Details)

Updated diff to include man page, example code, build as a lua module instead of embedded in flua.
Diff is based on flua-libjail going in first.

There is still quite a bit of functionality missing from libifconfig that makes this feel poorly fleshed out.

From the first look the man page should be fine. Did you run igor and mandoc -Tlint against it?

freqlabs retitled this revision from /usr/libexec/flua: Add bindings for libifconfig to lib/flua: Add bindings for libifconfig.

Incorporated changes to libifconfig.

Avoid tracking stack index manually when we know the position relative to the top. We're usually using the index to set a field on a table immediately under a value on the top of the stack, so the index can be specified as -2 rather than the absolute index.

Fixed empty media options table. I forgot to pop the option names off the stack and into the table after pushing the string.

Further refinement of the media table format: rather than setting "<unknown type>", "<unknown subtype>", or "<unknown mode>", just leave the field out of the table and let the caller decide if/how they want to display the nil field. This idea stems from mode only really being a thing for wifi, and seeing "mode": "<unknown mode>" on every Ethernet interface for example is not ideal.

Note to self: update the documentation for the mode table format in the man page, too.

In D25447#609400, @gbe wrote:

From the first look the man page should be fine. Did you run igor and mandoc -Tlint against it?

Yes, apart from the date they both come back clean.

Changed the format of lagg ports to a table keyed by the interface name, so rather than

"ports": [
    {
        "lacp_state": [
            "ACTIVITY",
            "AGGREGATION",
            "SYNC",
            "COLLECTING",
            "DISTRIBUTING"
        ],
        "flags": [
            "ACTIVE",
            "COLLECTING",
            "DISTRIBUTING"
        ],
        "laggport": "igb0"
    },
    {
        "lacp_state": [
            "ACTIVITY",
            "AGGREGATION",
            "SYNC",
            "COLLECTING",
            "DISTRIBUTING"
        ],
        "flags": [
            "ACTIVE",
            "COLLECTING",
            "DISTRIBUTING"
        ],
        "laggport": "igb1"
    }
],

we now have

"ports": {
    "igb0": {
        "lacp_state": [
            "ACTIVITY",
            "AGGREGATION",
            "SYNC",
            "COLLECTING",
            "DISTRIBUTING"
        ],
        "flags": [
            "ACTIVE",
            "COLLECTING",
            "DISTRIBUTING"
        ]
    },
    "igb1": {
        "lacp_state": [
            "ACTIVITY",
            "AGGREGATION",
            "SYNC",
            "COLLECTING",
            "DISTRIBUTING"
        ],
        "flags": [
            "ACTIVE",
            "COLLECTING",
            "DISTRIBUTING"
        ]
    }
},

Updated the man page to reflect recent changes to table formats.