//go:build !windows

package link

import (
	"fmt"
	"sync/atomic"
	"testing"

	"github.com/go-quicktest/qt"
	"github.com/jsimonetti/rtnetlink/v2"
	"github.com/jsimonetti/rtnetlink/v2/driver"

	"github.com/cilium/ebpf"
	"github.com/cilium/ebpf/internal/testutils"
	"github.com/cilium/ebpf/internal/unix"
)

func TestAttachNetkit(t *testing.T) {
	testutils.SkipOnOldKernel(t, "6.7", "Netkit Device")

	ns := testutils.NewNetNS(t)

	prog := mustLoadProgram(t, ebpf.SchedCLS, ebpf.AttachNetkitPrimary, "")
	link, _ := mustAttachNetkit(t, prog, ebpf.AttachNetkitPrimary, ns)

	testLink(t, link, prog)
}

func TestNetkitAnchor(t *testing.T) {
	testutils.SkipOnOldKernel(t, "6.7", "Netkit Device")

	a := mustLoadProgram(t, ebpf.SchedCLS, ebpf.AttachNetkitPrimary, "")
	b := mustLoadProgram(t, ebpf.SchedCLS, ebpf.AttachNetkitPrimary, "")

	ns := testutils.NewNetNS(t)

	linkA, ifIndex := mustAttachNetkit(t, a, ebpf.AttachNetkitPrimary, ns)

	programInfo, err := a.Info()
	qt.Assert(t, qt.IsNil(err))
	programID, _ := programInfo.ID()

	linkInfo, err := linkA.Info()
	qt.Assert(t, qt.IsNil(err))
	linkID := linkInfo.ID

	for _, anchor := range []Anchor{
		Head(),
		Tail(),
		BeforeProgram(a),
		BeforeProgramByID(programID),
		AfterLink(linkA),
		AfterLinkByID(linkID),
	} {
		t.Run(fmt.Sprintf("%T", anchor), func(t *testing.T) {
			var linkB Link
			qt.Assert(t, qt.IsNil(ns.Do(func() (err error) {
				linkB, err = AttachNetkit(NetkitOptions{
					Program:   b,
					Attach:    ebpf.AttachNetkitPrimary,
					Interface: ifIndex,
					Anchor:    anchor,
				})
				return err
			})))
			qt.Assert(t, qt.IsNil(linkB.Close()))
		})
	}
}

// The last ifindex we created.
var prevIfindex atomic.Uint32

func init() { prevIfindex.Store(1000 - 1) }

func mustAttachNetkit(tb testing.TB, prog *ebpf.Program, attachType ebpf.AttachType, ns *testutils.NetNS) (Link, int) {
	var conn *rtnetlink.Conn
	qt.Assert(tb, qt.IsNil(ns.Do(func() (err error) {
		conn, err = rtnetlink.Dial(nil)
		return err
	})))
	tb.Cleanup(func() {
		qt.Assert(tb, qt.IsNil(conn.Close()))
	})

	ifIndex := prevIfindex.Add(1)

	layer2 := driver.NetkitModeL2
	blackhole := driver.NetkitPolicyDrop
	qt.Assert(tb, qt.IsNil(conn.Link.New(&rtnetlink.LinkMessage{
		Family: unix.AF_UNSPEC,
		Index:  ifIndex,
		Flags:  unix.IFF_UP,
		Change: unix.IFF_UP,
		Attributes: &rtnetlink.LinkAttributes{
			Info: &rtnetlink.LinkInfo{
				Kind: "netkit",
				Data: &driver.Netkit{
					Mode:       &layer2,
					PeerPolicy: &blackhole,
				},
			},
		},
	})))
	tb.Cleanup(func() {
		qt.Assert(tb, qt.IsNil(conn.Link.Delete(uint32(ifIndex))))
	})

	var link Link
	qt.Assert(tb, qt.IsNil(ns.Do(func() (err error) {
		link, err = AttachNetkit(NetkitOptions{
			Program:   prog,
			Attach:    attachType,
			Interface: int(ifIndex),
		})
		return err
	})))
	tb.Cleanup(func() {
		qt.Assert(tb, qt.IsNil(link.Close()))
	})

	return link, int(ifIndex)
}
