Compare commits

..

1 Commits

Author SHA1 Message Date
Nick Garlis 497dcd17bb
Merge 9857ffe35c into 508bb1ffd4 2025-08-22 21:57:01 +00:00
3 changed files with 10 additions and 22 deletions

29
conn.go
View File

@ -232,20 +232,9 @@ func (cc *Conn) CloseLasting() error {
} }
// Flush sends all buffered commands in a single batch to nftables. // Flush sends all buffered commands in a single batch to nftables.
func (cc *Conn) Flush() error { // If an optional gen ID is provided, it will be used in the batch begin message.
return cc.flush(0) // If the gen ID is not matched by the kernel, it will return an ERESTART error.
} func (cc *Conn) Flush(genID ...uint32) error {
// FlushWithGenID sends all buffered commands in a single batch to nftables
// along with the provided gen ID. If the ruleset has changed since the gen ID
// was retrieved, an ERESTART error will be returned.
func (cc *Conn) FlushWithGenID(genID uint32) error {
return cc.flush(genID)
}
// flush sends all buffered commands in a single batch to nftables. If genID is
// non-zero, it will be included in the batch messages.
func (cc *Conn) flush(genID uint32) error {
cc.mu.Lock() cc.mu.Lock()
defer func() { defer func() {
cc.messages = nil cc.messages = nil
@ -272,7 +261,7 @@ func (cc *Conn) flush(genID uint32) error {
return err return err
} }
batch, err := batch(cc.messages, genID) batch, err := batch(cc.messages, genID...)
if err != nil { if err != nil {
return err return err
} }
@ -406,17 +395,17 @@ func (cc *Conn) marshalExpr(fam byte, e expr.Any) []byte {
return b return b
} }
// batch wraps the given messages in a batch begin and end message, and returns // Batch wraps the given messages in a batch begin and end message, and returns
// the resulting slice of netlink messages. If the genID is non-zero, it will be // the resulting slice of netlink messages. If a genID is provided, it is
// included in both batch messages. // included in both batch messages.
func batch(messages []netlinkMessage, genID uint32) ([]netlink.Message, error) { func batch(messages []netlinkMessage, genID ...uint32) ([]netlink.Message, error) {
batch := make([]netlink.Message, len(messages)+2) batch := make([]netlink.Message, len(messages)+2)
data := extraHeader(0, unix.NFNL_SUBSYS_NFTABLES) data := extraHeader(0, unix.NFNL_SUBSYS_NFTABLES)
if genID > 0 { if len(genID) > 0 && genID[0] > 0 {
attr, err := netlink.MarshalAttributes([]netlink.Attribute{ attr, err := netlink.MarshalAttributes([]netlink.Attribute{
{Type: unix.NFNL_BATCH_GENID, Data: binaryutil.BigEndian.PutUint32(genID)}, {Type: unix.NFNL_BATCH_GENID, Data: binaryutil.BigEndian.PutUint32(genID[0])},
}) })
if err != nil { if err != nil {
return nil, err return nil, err

1
gen.go
View File

@ -14,7 +14,6 @@ type Gen struct {
ProcComm string // [16]byte - max 16bytes - kernel TASK_COMM_LEN ProcComm string // [16]byte - max 16bytes - kernel TASK_COMM_LEN
} }
// Deprecated: GenMsg is an inconsistent old name for Gen. Prefer using Gen.
type GenMsg = Gen type GenMsg = Gen
const genHeaderType = netlink.HeaderType((unix.NFNL_SUBSYS_NFTABLES << 8) | unix.NFT_MSG_NEWGEN) const genHeaderType = netlink.HeaderType((unix.NFNL_SUBSYS_NFTABLES << 8) | unix.NFT_MSG_NEWGEN)

View File

@ -7493,7 +7493,7 @@ func TestFlushWithGenID(t *testing.T) {
Family: nftables.TableFamilyIPv4, Family: nftables.TableFamilyIPv4,
}) })
err = conn.FlushWithGenID(gen.ID) err = conn.Flush(gen.ID)
if err == nil || !errors.Is(err, syscall.ERESTART) { if err == nil || !errors.Is(err, syscall.ERESTART) {
t.Errorf("expected error to be ERESTART, got: %v", err) t.Errorf("expected error to be ERESTART, got: %v", err)
} }