Skip to content

hook: accept interface names in ifindex= option#508

Open
Bodlux wants to merge 1 commit intofacebook:mainfrom
Bodlux:feat/ifindex-accept-interface-names
Open

hook: accept interface names in ifindex= option#508
Bodlux wants to merge 1 commit intofacebook:mainfrom
Bodlux:feat/ifindex-accept-interface-names

Conversation

@Bodlux
Copy link
Copy Markdown

@Bodlux Bodlux commented Apr 15, 2026

Use bf_if_index_from_str() to parse the ifindex= hook option value, which accepts both numeric indices and interface names (e.g. eth0). This is consistent with how the REDIRECT verdict already resolves interface references.

Closes #409

@Bodlux Bodlux requested a review from qdeslandes as a code owner April 15, 2026 07:22
@meta-cla
Copy link
Copy Markdown

meta-cla Bot commented Apr 15, 2026

Hi @Bodlux!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks!

@meta-cla meta-cla Bot added the cla signed label Apr 15, 2026
@qdeslandes
Copy link
Copy Markdown
Contributor

Hi, thanks for your first contribution! ifindex is more suited for an interface index (number). I would suggest introducing iface= which would allow either an interface index or an interface name.

Copy link
Copy Markdown
Contributor

@qdeslandes qdeslandes left a comment

Choose a reason for hiding this comment

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

Repeating my comment:
Hi, thanks for your first contribution! ifindex is more suited for an interface index (number). I would suggest introducing iface= which would allow either an interface index or an interface name.

Also, could you get rid of the comment to trigger the CLA check.

@Bodlux
Copy link
Copy Markdown
Author

Bodlux commented Apr 15, 2026

Thanks for the suggestion. I've renamed the option from ifindex= to iface= so it accepts either an interface name or index.

Copy link
Copy Markdown
Contributor

@qdeslandes qdeslandes left a comment

Choose a reason for hiding this comment

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

This change is incomplete, as it's only modifying the code and not the doc. Additionally, renaming an existing option will break use cases. An alternative would be to create a new iface option which handles interface index and interface name, then make ifindex as deprecated in the doc.

Add a new hook option `iface=` that accepts either an interface name
(resolved via `if_nametoindex`) or a numeric interface index. The
existing `ifindex=` option is kept for backward compatibility but is
now marked as deprecated: it continues to accept numeric indices only,
and emits a runtime warning pointing users to `iface=`.

`iface=` and `ifindex=` share the same `BF_HOOKOPTS_IFINDEX` slot, so
either one satisfies the XDP/TC requirement and specifying both in the
same chain is rejected as a duplicate.

The on-wire (msgpack) serialization key remains `ifindex`, so existing
serialized chains continue to load unchanged.

Docs, unit tests and e2e duplicate-detection tests are updated.

Relates to facebook#409.
@Bodlux Bodlux force-pushed the feat/ifindex-accept-interface-names branch from 8d8081d to 55647eb Compare April 16, 2026 12:29
@Bodlux
Copy link
Copy Markdown
Author

Bodlux commented Apr 16, 2026

Thanks for the detailed feedback, @qdeslandes. I've reworked the PR along the lines you suggested and force-pushed a single, clean commit.

Summary of the new approach:

  • ifindex= is preserved. It still accepts only a numeric index, so no existing ruleset, script or serialized chain breaks. It now emits a deprecation warning pointing users to iface=.
  • A new iface= option is introduced. It accepts either an interface name (resolved via if_nametoindex, reusing the existing bf_if_index_from_str() helper from src/libbpfilter/if.c) or a numeric interface index.
  • Both options share the same BF_HOOKOPTS_IFINDEX slot in used_opts. This means either one satisfies the XDP/TC requirement, and specifying both in the same chain (or either one twice) is rejected as a duplicate.
  • The on-wire msgpack key stays "ifindex", so the serialization format is unchanged.
  • doc/usage/bfcli.rst: the hook option table now documents iface=$INTERFACE as the preferred option and flags ifindex=$IFINDEX as deprecated.
  • Added a new hookopts_parse_iface unit test covering numeric input, interface name (lo), invalid names, duplicate detection, and the ifindex= + iface= cross-duplicate case.
  • Added e2e duplicate-detection cases in tests/e2e/cli/hookopts.sh for iface=,iface= and ifindex=,iface=.
  • History squashed to a single commit (the retrigger-CLA commit is gone).

Let me know if you'd like any further adjustments.

Copy link
Copy Markdown
Contributor

@qdeslandes qdeslandes left a comment

Choose a reason for hiding this comment

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

Small changes, otherwise LGTM.

Comment thread src/libbpfilter/hook.c
Comment on lines +430 to +441
/* Alias table: alternative names that map to an existing bf_hookopts_type but
* use a different parser. The `type` field determines which bit in `used_opts`
* is set, so aliases share duplicate-detection and flavor
* requirement/support checks with their primary entry. */
static struct bf_hookopts_ops _bf_hookopts_alias_ops[] = {
{.name = "iface",
.type = BF_HOOKOPTS_IFINDEX,
.required_by = 0,
.supported_by = BF_FLAGS(BF_FLAVOR_XDP, BF_FLAVOR_TC),
.parse = _bf_hookopts_iface_parse,
.dump = _bf_hookopts_ifindex_dump},
};
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

No need for a dedicated table here, this entry should be in _bf_hookopts_ops, and you should add a check for incompatibility in their respective parsers:

  • _bf_hookopts_iface_parse: error if ifindex is defined, and error if iface is already defined
  • _bf_hookopts_ifindex_parse: error if iface is defined, and error if ifindex is already defined

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Replace ifindex= hook option with iface=

2 participants