Skip to content

cli: keep the mask of inverted --filter arguments - #2182

Open
thomas0829 wants to merge 1 commit into
hardbyte:mainfrom
thomas0829:fix-cli-inverted-filter-mask
Open

thomas0829 wants to merge 1 commit into
hardbyte:mainfrom
thomas0829:fix-cli-inverted-filter-mask

Conversation

@thomas0829

Copy link
Copy Markdown

Summary of Changes

  • Clear only CAN_ERR_FLAG from the mask of an inverted --filter <can_id>~<can_mask> argument instead of keeping only that bit, so 123~7FF gives can_mask=0x7FF instead of 0.
  • Fix the expected masks in test/test_cli.py and test/test_viewer.py, which asserted the old value (0x7F0 & 0x20000000, that is, 0).
  • Add the news fragment doc/changelog.d/2181.fixed.rst.

Why

With can_mask=0, the Linux kernel stores the inverted filter as can_id & 0 and never delivers a frame, because (rx_id & 0) != 0 is never true. candump parses the same syntax with can_mask &= ~CAN_ERR_FLAG, and the --filter help text describes the != match.

Validation

  • python -c snippet from cli: inverted --filter <can_id>~<can_mask> drops the mask #2181 prints 0x20000123 0x0 on main (86e70bc) and 0x20000123 0x7ff with this change
  • pytest test/test_cli.py test/test_viewer.py: the updated test_add_bus_arguments, test_add_bus_arguments_no_prefix and test_parse_args fail on main (0 != 2032, 2047 != 0) and pass here
  • pytest: same results as on main (885 tests; the 35 that fail in this environment fail identically on main, and pass when run on their own)
  • black --check ., ruff check can examples doc, pylint can/cli.py can/logger.py can/viewer.py: clean
  • mypy --python-version 3.12 .: no new errors (the 7 in can/io/mf4.py also show on main)

Related Issues / Pull Requests

Type of Change

  • Bug fix
  • New feature
  • Documentation update
  • Refactoring
  • Other (please describe):

Checklist

  • I have followed the contribution guide.
  • I have added or updated tests as appropriate.
  • I have added or updated documentation as appropriate.
  • I have added a news fragment for towncrier.
  • All checks and tests pass (tox).

Additional Notes

Tested by parsing only; no SocketCAN device was available. Software filtering in BusABC._matches_filters does not handle CAN_INV_FILTER, which this change leaves as is.

_CanFilterAction ANDed the mask of "<can_id>~<can_mask>" with
CAN_ERR_FLAG instead of ~CAN_ERR_FLAG, so the mask was always 0 and
the inverted SocketCAN filter blocked every frame. Clear only the
error flag, as candump does.

Fixes hardbyte#2181
@zariiii9003

Copy link
Copy Markdown
Collaborator

AI Review: cli: keep the mask of inverted --filter arguments

The mask fix is correct for SocketCAN: --filter 123~7FF now installs can_mask=0x7FF, and the kernel delivers every frame except 0x123, as candump does and the --filter help text promises.

Findings

  • can/cli.py _CanFilterAction: on every interface other than SocketCAN, the filter now does the opposite of what it promises. The CLI encodes inversion as the Linux flag CAN_INV_FILTER (bit 29 of can_id). Only SocketCAN passes that bit to the kernel. BusABC._matches_filters and the hardware filters of the other backends apply the documented set_filters rule id & can_mask == can_id & can_mask and treat bit 29 as an ordinary ID bit.

    • With the old mask of 0, the filter let every frame through on these interfaces.
    • With this PR, it lets through only the excluded ID. For example, can.logger -i virtual --filter 123~7FF records only 0x123 and silently drops the traffic the user asked for.

    Fix: make inversion part of the interface-neutral filter definition instead of passing a SocketCAN kernel flag through the generic filter dict:

    • Add an optional inverted: bool key to can.typechecking.CanFilter, and document it in BusABC.set_filters and doc/bus.rst.
    • Honour it in BusABC._matches_filters: an inverted filter matches when the ID/mask comparison fails; extended still applies.
    • Translate it in the SocketCAN pack_filters to can_id | CAN_INV_FILTER, with can_mask & ~CAN_ERR_FLAG. The kernel cannot restrict an inverted filter to one frame type, so for an inverted filter that sets extended, report frames as not filtered and complete the check in Python.
    • Kvaser, Vector, SYS TEC, ETAS, IXXAT (both VCI variants), NI-CAN and Seeed Studio program filters in hardware and report frames as already filtered. When a filter is inverted, they should accept all frames in hardware and fall back to software filtering.
    • Have the CLI emit {"can_id": id, "can_mask": mask, "inverted": True}.

    Filters without the key, including hand-written bit-29 filters on SocketCAN, keep their current behaviour. Teaching BusABC._matches_filters to treat bit 29 itself as "invert" is not an alternative: it changes the documented meaning of existing filters, as MATCH_ONLY_HIGHEST in test/test_message_filtering.py shows.

  • No test covers clearing CAN_ERR_FLAG from the mask. Removing & ~0x20000000 keeps test/test_cli.py and test/test_viewer.py green. Add a case whose mask has bit 29 set (e.g. 123~200007FF) and expect the kernel mask 0x7FF, in test/test_socketcan_helpers.py once the clearing moves to pack_filters.

  • Tests for the new behaviour.

    • Add an inverted case in test/test_message_filtering.py.
    • For each backend with an existing mocked filter test (Kvaser, SYS TEC, Vector, Seeed Studio, ETAS), add a test that an inverted filter is not programmed into the hardware.
    • Add a news fragment 2181.added.rst for the new filter key.

Verdict

request changes

@DanShinde

Copy link
Copy Markdown
Contributor

The socketcan side looks right to me, candump does the same can_mask &= ~CAN_ERR_FLAG.

One thing I noticed on non socketcan interfaces though. Those use the software filter in BusABC._matches_filters, which doesn't know about CAN_INV_FILTER. I tried --filter 123~7FF on the virtual bus and sent 0x123 and 0x456:

  • main: mask 0, both frames come through (filter does nothing)
  • this branch: mask 0x7FF, only 0x123 comes through

So on those interfaces the inverted filter now acts like a normal 123:7FF match, which is the opposite of what was asked for. Before it was just ignored. Not sure if that should be fixed here (teach _matches_filters about the inverse bit) or as a follow up, but probably worth deciding before this goes in.

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.

cli: inverted --filter <can_id>~<can_mask> drops the mask

3 participants