Skip to content

expr: support shift and boolean ops in Bitwise - #375

Open
saphalpdyl wants to merge 2 commits into
google:mainfrom
saphalpdyl:feat/add-bitshift-support-to-bitwise
Open

saphalpdyl wants to merge 2 commits into
google:mainfrom
saphalpdyl:feat/add-bitshift-support-to-bitwise

Conversation

@saphalpdyl

Copy link
Copy Markdown

expr.Bitwise only supported the original mask/xor form (dreg = (sreg & mask) ^ xor). Currently, the kernel's bitwise expression also supports shift (LSHIFT, RSHIFT) and boolean operations (AND, OR, XOR) selected through NFTA_BITWISE_OP.

nft uses these for common rules like ip6 dscp which compiles to [ bitwise reg x = ( reg x >> <shiftAmt> ) ], so rules like meta mark set ip6 dscp map @somemap was not possible with this library.

CHANGES

  • expr.Bitwise has three more attributes .Op, .Data, and .SourceRegister2.
  • New type BitwiseOp matching enum nft_bitwise_ops. source
  • marshalData sends exactly the attributes required for a given operation. .SourceRegister2 for AND & OR. .Data for LSHIFT, RSHIFT, and XOR. And, .Op operation != BitwiseOpMaskXor.
  • respective changes to unmarshal.

Backwards compatibility

.Op's zero value is mask/xor, and for it no new attributes are sent. The message payload is the same as before for mask/xor operations. Existing tests pass.

NOTE

NFTA_BITWISE_OP, NFTA_BITWISE_DATA, NFTA_BITWISE_SREG2 are defined locally since they are not yet in golang.org/x/sys/unix.

Respective tests have been added to bitwise_test.go and nftables_test.go to test non-xor/mask operations.

Add .Op, .Data, and .SourceRegister2 to expr.Bitwise so the kernel's NFTA_BITWISE_OP operations (lshift, rshift, and, or, xor) can be built and decoded. Attributes are sent selectively based on the operation.

NFTA_BITWISE_OP, NFTA_BITWISE_DATA, NFTA_BITWISE_SREG2 are defined locally since they are not yet in golang.org/x/sys/unix.

Signed-off-by: saphalpdyl <git@saphal.me>
@google-cla

google-cla Bot commented Oct 23, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

…itwise.marshalData

Signed-off-by: saphalpdyl <git@saphal.me>
@saphalpdyl

Copy link
Copy Markdown
Author

A PR to support the missing constants is up in https://go.dev/cl/854465.

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.

1 participant