Skip to content

i/builtin: add gpio-chardev interface #15172

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

Merged
merged 14 commits into from
Mar 26, 2025

Conversation

ZeyadYasser
Copy link
Contributor

This implements the gpio-chardev interface according to the SD129 spec which is currently hidden behind an experimental feature flag until kernel improvements to the gpio-aggreagtor lands.

Also, I had to extend the systemd backend to support specifying Wants/WantedBy/After/Before directives 44562bb.

@ZeyadYasser ZeyadYasser added the Needs Samuele review Needs a review from Samuele before it can land label Mar 5, 2025
@ZeyadYasser ZeyadYasser requested review from bboozzoo and pedronis March 5, 2025 18:04
Copy link

codecov bot commented Mar 5, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 0.00%. Comparing base (d6d95f0) to head (96e9663).
Report is 47 commits behind head on master.

Additional details and impacted files
@@             Coverage Diff             @@
##           master   #15172       +/-   ##
===========================================
- Coverage   78.09%        0   -78.10%     
===========================================
  Files        1190        0     -1190     
  Lines      158458        0   -158458     
===========================================
- Hits       123746        0   -123746     
+ Misses      27017        0    -27017     
+ Partials     7695        0     -7695     
Flag Coverage Δ
unittests ?

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link

github-actions bot commented Mar 5, 2025

Wed Mar 26 18:47:33 UTC 2025
The following results are from: https://github.com/canonical/snapd/actions/runs/14079689269

Failures:

Preparing:

  • openstack:debian-sid-64
  • openstack:debian-sid-64
  • openstack:debian-sid-64
  • openstack:debian-sid-64
  • openstack:debian-sid-64
  • openstack:debian-sid-64
  • openstack:opensuse-15.6-64
  • openstack:opensuse-15.6-64
  • openstack:opensuse-15.6-64
  • google:ubuntu-25.04-64:tests/main/snap-ns-forward-compat

Executing:

  • openstack:debian-12-64:tests/main/progress
  • openstack:opensuse-tumbleweed-64:tests/main/auto-refresh-pre-download:close
  • openstack:opensuse-tumbleweed-64:tests/main/auto-refresh:parallel
  • openstack:opensuse-tumbleweed-64:tests/main/snap-refresh-hold
  • openstack:opensuse-tumbleweed-64:tests/main/auto-refresh-backoff
  • openstack:opensuse-tumbleweed-64:tests/main/auto-refresh-pre-download:restart
  • openstack:opensuse-tumbleweed-64:tests/main/auto-refresh-pre-download:ignore
  • openstack:opensuse-tumbleweed-64:tests/main/auto-refresh-gating-from-snap
  • openstack:opensuse-tumbleweed-64:tests/main/auto-refresh-pre-download:close_mid_restart
  • openstack:opensuse-tumbleweed-64:tests/main/auto-refresh:regular
  • openstack:opensuse-tumbleweed-64:tests/main/auto-refresh-gating
  • openstack:opensuse-tumbleweed-64:tests/main/auto-refresh-retry
  • openstack:opensuse-tumbleweed-64:tests/main/refresh-app-awareness-notify
  • google:ubuntu-25.04-64:tests/main/microk8s-smoke:edge
  • google:ubuntu-22.04-64:tests/main/prepare-image-classic
  • google:ubuntu-18.04-64:tests/main/preseed-lxd

Restoring:

  • openstack:opensuse-15.6-64
  • openstack:opensuse-15.6-64
  • openstack:opensuse-15.6-64
  • openstack:opensuse-tumbleweed-64:tests/main/refresh-app-awareness-notify

return lines, nil
}

func validateLines(linesAttr string) error {
Copy link
Contributor

Choose a reason for hiding this comment

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

this could be made a helper in strutil, e.g.:

// RangeSpan represents a span of numbers inside a range. A span with
// equal Start and Stop describes a span of a single number.
type RangeSpan struct {
    Start, Stop uint
}

// Range of discrete numbers represented as set of non overlapping spans.
type Range struct {
    // Spans within the range, ordered by Start.
    Spans []RangeSpan 
}

// ParseRange parses a range represented as a string. The entries are 
// joining them with a comma: n[,m] or as a range: n-m or a combination
// of both, assuming the ranges do not overlap, e.g.: n,m,x-y.
func ParseRange(in str, expectedMax uint) (Range, error) {
    ...
}

and then you can reuse it in the helper. Separately I'd add something to the gadget package, or a subpackage there to convert range to a sequence of lines, which should be quite trivial at this point.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, this now could easily be shared with the helper command, about converting a range into a sequence of lines, I don't think we really need this as the kernel in fact accepts this exact format.

@ZeyadYasser ZeyadYasser requested a review from bboozzoo March 7, 2025 10:22
@ZeyadYasser ZeyadYasser added the Needs security review Can only be merged once security gave a :+1: label Mar 10, 2025
Copy link
Collaborator

@pedronis pedronis left a comment

Choose a reason for hiding this comment

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

this looks reasonable to me

strutil/range.go Outdated
}

func parseRangeSpan(in string) (RangeSpan, error) {
hasNegativeStart := strings.HasPrefix(in, "-")
Copy link
Collaborator

Choose a reason for hiding this comment

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

do we need the negative range support?

Copy link
Contributor

Choose a reason for hiding this comment

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

not at all, I discussed dropping it with @ZeyadYasser

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Switched to using uints

@ZeyadYasser ZeyadYasser requested a review from bboozzoo March 12, 2025 11:07
Copy link
Contributor

@bboozzoo bboozzoo left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Collaborator

@pedronis pedronis left a comment

Choose a reason for hiding this comment

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

thanks, couple of small things, question for @bboozzoo

strutil/range.go Outdated
// ParseRange parses a range represented as a string. The entries are joining
// them with a comma: n[,m] or as a range: n-m or a combination of both, assuming
// the ranges are non-negative and do not overlap, e.g.: n,m,x-y.
func ParseRange(in string) (Range, error) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

nitpick: s/in/input/ or s/in/s/

strutil/range.go Outdated
// Parse range e.g. 2-5
tokens := strings.SplitN(in, "-", 2)
if len(tokens) != 2 {
return RangeSpan{}, fmt.Errorf("invalid range span %q", in)
Copy link
Collaborator

Choose a reason for hiding this comment

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

this error is not tested

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This check is not reachable since input that doesn't contain - will have already been parsed as a single number, and doing SplitN on a string that has - will always have exactly two tokens.

The check is redundant, but good to have in case SplitN was swapped to Split for any reason or by mistake.

return nil
}

// XXX: What should be the limit on max range.
Copy link
Collaborator

Choose a reason for hiding this comment

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

@bboozzoo any suggestion here? is there a limit in the kernel? otherwise we should just remove the XXX

Copy link
Contributor

Choose a reason for hiding this comment

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

we can set it to 512, which would still be very high and unlikely to ever be reached in practice.

@ZeyadYasser ZeyadYasser force-pushed the gpiod-chardev-interface branch from 71b8a46 to 7c6db0b Compare March 13, 2025 12:47
@ZeyadYasser
Copy link
Contributor Author

I had to rebase to resolve conflicts

@ZeyadYasser ZeyadYasser requested a review from pedronis March 13, 2025 12:47
Copy link
Collaborator

@pedronis pedronis left a comment

Choose a reason for hiding this comment

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

thanks

@ZeyadYasser ZeyadYasser requested a review from zyga March 19, 2025 12:00
Copy link
Contributor

@zyga zyga left a comment

Choose a reason for hiding this comment

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

Thanks for the improvements. I did a longer pass.

I have some small fixes/improvements to the range code and some questions and comments to the interface code. Please see inline.

"strings"
)

// RangeSpan represents a span of numbers inside a range. A span with
Copy link
Contributor

Choose a reason for hiding this comment

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

Nitpick. I tried to make sense of the naming of RangeSpan and Range but I keep feeling those are off. Could this be just Span and the other one Spans? Feel free to ignore.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't have strong opinions on this, @bboozzoo @pedronis what do you think?

@ZeyadYasser ZeyadYasser requested a review from zyga March 21, 2025 10:46
"github.com/snapcore/snapd/strutil"
)

// TODO: Snapd should validate the correctness of slot declarations
Copy link
Contributor

Choose a reason for hiding this comment

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

What's the plan to implement this after this PR lands?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Contributor

@zyga zyga left a comment

Choose a reason for hiding this comment

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

Mostly LGTM, my main remaining question is the TODO up top in gpio_chardev.go. I have some concerns about reexec on old snapd. Deferring my vote until those two are answered.

Copy link
Contributor

@zyga zyga left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Contributor

@jslarraz jslarraz left a comment

Choose a reason for hiding this comment

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

LGTM

@ZeyadYasser
Copy link
Contributor Author

For awareness, I found a stupid bug where the slot name was used as the plug name for the symlink generation, I fixed it in b80228a and updated the unit tests to catch it.

This implements the gpio-chardev interface (spec SD129) which
is currently hidden behind an experimental feature flag until
kernel improvements to the gpio-aggreagtor lands.

Signed-off-by: Zeyad Gouda <[email protected]>
- strutil: fix range helper typo s/Intersets/Intersects
- strutil: only support positive numbers in ParseRange
- strutil: sort range spans in ParseRange
- i/builtin/gpio-chardev: error message improvements
- i/builtin/gpio-chardev: remove unnesaccery non-negative validation

Signed-off-by: Zeyad Gouda <[email protected]>
@ZeyadYasser ZeyadYasser force-pushed the gpiod-chardev-interface branch from b80228a to 96e9663 Compare March 26, 2025 09:20
@ernestl ernestl merged commit 1c897a0 into canonical:master Mar 26, 2025
70 of 75 checks passed
maykathm pushed a commit to maykathm/snapd that referenced this pull request Mar 28, 2025
* i/systemd: support Wants/WantedBy/After/Before directives

Signed-off-by: Zeyad Gouda <[email protected]>

* i/builtin: add gpio-chardev interface

This implements the gpio-chardev interface (spec SD129) which
is currently hidden behind an experimental feature flag until
kernel improvements to the gpio-aggreagtor lands.

Signed-off-by: Zeyad Gouda <[email protected]>

* strutil: add range parsing helper

Signed-off-by: Zeyad Gouda <[email protected]>

* fixup! i/builtin: use strutil.ParseRange helper

Signed-off-by: Zeyad Gouda <[email protected]>

* fixup! i/builtin: add source-chip validation

Signed-off-by: Zeyad Gouda <[email protected]>

* fixup! many: address review comments

- strutil: fix range helper typo s/Intersets/Intersects
- strutil: only support positive numbers in ParseRange
- strutil: sort range spans in ParseRange
- i/builtin/gpio-chardev: error message improvements
- i/builtin/gpio-chardev: remove unnesaccery non-negative validation

Signed-off-by: Zeyad Gouda <[email protected]>

* fixup! strutil: typo fix and test coverage improvement

Signed-off-by: Zeyad Gouda <[email protected]>

* fixup! i/builtin: use dirs.DistroLibExecDir instead of fixed path

Signed-off-by: Zeyad Gouda <[email protected]>

* fixup! i/builtin: fix release info mocking order

Signed-off-by: Zeyad Gouda <[email protected]>

* tests: exclude gpio-chardev interface from document-interfaces-url

Signed-off-by: Zeyad Gouda <[email protected]>

* fixup! address review comments

Signed-off-by: Zeyad Gouda <[email protected]>

* fixup! strutil: simplify Range into a slice of RangeSpan

Signed-off-by: Zeyad Gouda <[email protected]>

* fixup! many: address review comments

Signed-off-by: Zeyad Gouda <[email protected]>

* fixup! i/builtin: fix typo of plug name using slot name

Signed-off-by: Zeyad Gouda <[email protected]>

---------

Signed-off-by: Zeyad Gouda <[email protected]>
maykathm pushed a commit to maykathm/snapd that referenced this pull request Apr 8, 2025
* i/systemd: support Wants/WantedBy/After/Before directives

Signed-off-by: Zeyad Gouda <[email protected]>

* i/builtin: add gpio-chardev interface

This implements the gpio-chardev interface (spec SD129) which
is currently hidden behind an experimental feature flag until
kernel improvements to the gpio-aggreagtor lands.

Signed-off-by: Zeyad Gouda <[email protected]>

* strutil: add range parsing helper

Signed-off-by: Zeyad Gouda <[email protected]>

* fixup! i/builtin: use strutil.ParseRange helper

Signed-off-by: Zeyad Gouda <[email protected]>

* fixup! i/builtin: add source-chip validation

Signed-off-by: Zeyad Gouda <[email protected]>

* fixup! many: address review comments

- strutil: fix range helper typo s/Intersets/Intersects
- strutil: only support positive numbers in ParseRange
- strutil: sort range spans in ParseRange
- i/builtin/gpio-chardev: error message improvements
- i/builtin/gpio-chardev: remove unnesaccery non-negative validation

Signed-off-by: Zeyad Gouda <[email protected]>

* fixup! strutil: typo fix and test coverage improvement

Signed-off-by: Zeyad Gouda <[email protected]>

* fixup! i/builtin: use dirs.DistroLibExecDir instead of fixed path

Signed-off-by: Zeyad Gouda <[email protected]>

* fixup! i/builtin: fix release info mocking order

Signed-off-by: Zeyad Gouda <[email protected]>

* tests: exclude gpio-chardev interface from document-interfaces-url

Signed-off-by: Zeyad Gouda <[email protected]>

* fixup! address review comments

Signed-off-by: Zeyad Gouda <[email protected]>

* fixup! strutil: simplify Range into a slice of RangeSpan

Signed-off-by: Zeyad Gouda <[email protected]>

* fixup! many: address review comments

Signed-off-by: Zeyad Gouda <[email protected]>

* fixup! i/builtin: fix typo of plug name using slot name

Signed-off-by: Zeyad Gouda <[email protected]>

---------

Signed-off-by: Zeyad Gouda <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs Samuele review Needs a review from Samuele before it can land Needs security review Can only be merged once security gave a :+1:
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants