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

@thomas0829 thomas0829 commented Dec 4, 2026 •

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. On SocketCAN the kernel now inverts the filter as intended.
  • CAN_INV_FILTER is only understood by SocketCAN. For every other interface, create_bus_from_namespace sets the mask of such a filter to 0 and logs a warning, so the filter lets every frame through exactly as on main (no behaviour change there).
  • Fix the expected masks in test/test_cli.py and test/test_viewer.py, which asserted the old value, and add tests for both cases.
  • 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.

Software filtering in BusABC._matches_filters and the hardware filters of other backends do not know CAN_INV_FILTER. Passing the corrected mask to them would make --filter 123~7FF accept only 0x123 (reported in review), so this PR keeps them at their current behaviour. A generic inverted filter key honoured by every backend would be a separate follow-up.

Validation

  • pytest (CPython 3.13, all extras): 998 tests, 0 failures, 99 skipped
  • New tests in test/test_cli.py fail without the interface check: SocketCAN gets the inverted mask, other interfaces get mask 0 (interface given as argument or via CAN_INTERFACE), and on a virtual bus --filter 123~7FF receives both 0x123 and 0x456 as on main; 123~200007FF gives mask 0x7FF
  • 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 and with mocked/virtual buses only; no SocketCAN device was available.

@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.

_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.

CAN_INV_FILTER is only understood by SocketCAN. For any other
interface, create_bus_from_namespace now sets the mask of such a
filter to 0, so the filter lets every frame through as before
instead of matching only the excluded ID.

Fixes hardbyte#2181
@thomas0829
thomas0829 force-pushed the fix-cli-inverted-filter-mask branch from 1cc7a60 to 2a3758a Compare December 5, 2026 17:43
@thomas0829

Copy link
Copy Markdown
Author

Thank you both for the careful review, and thanks to @DanShinde for checking the virtual bus.

You are right that passing the corrected mask to interfaces other than SocketCAN would make --filter 123~7FF accept only 0x123. I have narrowed this PR to the SocketCAN fix (2a3758a):

  • On SocketCAN the inverted filter keeps its mask (123~7FF gives can_id=0x20000123, can_mask=0x7FF), so the kernel inverts it as candump does.
  • On every other interface, create_bus_from_namespace sets the mask of such a filter to 0 and logs a warning, so the filter lets everything through exactly as on main. There is no regression there. The interface is taken from --interface or, if that is not given, from the usual config sources.
  • New tests cover the SocketCAN mask, 123~200007FF giving mask 0x7FF, the mask-0 fallback, and a virtual bus with --filter 123~7FF receiving both 0x123 and 0x456.

Results: full pytest run has 998 tests and 0 failures. black, ruff and pylint are clean, and mypy reports only the existing can/io/mf4.py errors. The PR description is updated.

The interface-agnostic version from the review (an inverted key in CanFilter, handling in BusABC._matches_filters, pack_filters in SocketCAN, and the hardware backends falling back to software filtering) is a larger change, so I would prefer to do it as a separate follow-up PR. Would you welcome that? If yes, I will open it once this one is merged.

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