Compare commits

..

2 Commits

1 changed files with 77 additions and 69 deletions

View File

@ -7669,80 +7669,88 @@ func TestNftablesCompat(t *testing.T) {
} }
func TestNftablesDeadlock(t *testing.T) { func TestNftablesDeadlock(t *testing.T) {
helperConn := func(t *testing.T, readBufSize, writeBufSize, wantRules int) (error, int) { t.Parallel()
_, newNS := nftest.OpenSystemConn(t, *enableSysTests)
conn, err := nftables.New(nftables.WithNetNSFd(int(newNS)), nftables.WithSockOptions(func(conn *netlink.Conn) error {
if err := conn.SetWriteBuffer(writeBufSize); err != nil {
return err
}
if err := conn.SetReadBuffer(readBufSize); err != nil {
return err
}
return nil
}))
if err != nil {
t.Fatalf("nftables.New() failed: %v", err)
}
defer nftest.CleanupSystemConn(t, newNS)
conn.FlushRuleset()
defer conn.FlushRuleset()
table := conn.AddTable(&nftables.Table{ tests := []struct {
Name: "test_deadlock", name string
Family: nftables.TableFamilyIPv4, readBufSize int
}) writeBufSize int
sendRules int
chain := conn.AddChain(&nftables.Chain{ wantRules int
Name: "filter", wantErr error
Table: table, }{
}) {
name: "recv",
for i := 0; i < wantRules; i++ { readBufSize: 1024,
conn.AddRule(&nftables.Rule{ writeBufSize: 1 * 1024 * 1024,
Table: table, sendRules: 2048,
Chain: chain, wantRules: 2048,
Exprs: []expr.Any{ wantErr: syscall.ENOBUFS,
&expr.Verdict{ },
Kind: expr.VerdictAccept, {
}, name: "send",
}, readBufSize: 1 * 1024 * 1024,
}) writeBufSize: 1024,
} sendRules: 2048,
wantRules: 0,
flushErr := conn.Flush() wantErr: syscall.EMSGSIZE,
},
rules, err := conn.GetRules(table, chain)
if err != nil {
t.Fatalf("conn.GetRules() failed: %v", err)
}
return flushErr, len(rules)
} }
t.Run("recv", func(t *testing.T) { for _, tt := range tests {
sendRules := 2048 t.Run(tt.name, func(t *testing.T) {
wantRules := 2048 _, newNS := nftest.OpenSystemConn(t, *enableSysTests)
conn, err := nftables.New(nftables.WithNetNSFd(int(newNS)), nftables.WithSockOptions(func(conn *netlink.Conn) error {
if err := conn.SetWriteBuffer(tt.writeBufSize); err != nil {
return err
}
if err := conn.SetReadBuffer(tt.readBufSize); err != nil {
return err
}
return nil
}))
if err != nil {
t.Fatalf("nftables.New() failed: %v", err)
}
defer nftest.CleanupSystemConn(t, newNS)
conn.FlushRuleset()
defer conn.FlushRuleset()
flushErr, rulesLen := helperConn(t, 1024, 1*1024*1024, sendRules) table := conn.AddTable(&nftables.Table{
if !errors.Is(flushErr, syscall.ENOBUFS) { Name: "test_deadlock",
t.Errorf("conn.Flush() failed: %v", flushErr) Family: nftables.TableFamilyIPv4,
} })
if got, want := rulesLen, wantRules; got != want { chain := conn.AddChain(&nftables.Chain{
t.Fatalf("got rules %d, want rules %d", got, want) Name: "filter",
} Table: table,
}) })
t.Run("send", func(t *testing.T) {
sendRules := 2048
wantRules := 0
flushErr, rulesLen := helperConn(t, 1*1024*1024, 1024, sendRules) for i := 0; i < tt.sendRules; i++ {
if !errors.Is(flushErr, syscall.EMSGSIZE) { conn.AddRule(&nftables.Rule{
t.Errorf("conn.Flush() failed: %v", flushErr) Table: table,
} Chain: chain,
Exprs: []expr.Any{
&expr.Verdict{
Kind: expr.VerdictAccept,
},
},
})
}
if got, want := rulesLen, wantRules; got != want { flushErr := conn.Flush()
t.Fatalf("got rules %d, want rules %d", got, want) rules, err := conn.GetRules(table, chain)
} if err != nil {
}) t.Fatalf("conn.GetRules() failed: %v", err)
}
if !errors.Is(flushErr, tt.wantErr) {
t.Errorf("conn.Flush() failed: %v", flushErr)
}
if got, want := len(rules), tt.wantRules; got != want {
t.Fatalf("got rules %d, want rules %d", got, want)
}
})
}
} }