From 1fd8a768ae33bfbe5ee97b6369c1e925a36fc276 Mon Sep 17 00:00:00 2001 From: Scott Feldman Date: Fri, 9 Jun 2023 14:50:30 -0700 Subject: [PATCH] netlink: add L2 bridge/bond/vlan intermediate drivers This adds three new L2 (intermediate) drivers for bridge/bond/vlan. Theses drivers are incomplete but illustrate how to stack L2 drivers. Here are some examples of how these driver would stack with the cy243439 driver: Bridge: bridge two cyw43 devices together, connecting the two LANs: cyw43_0 := cyw43439.NewDevice(...) cyw43_1 := cyw43439.NewDevice(...) bridge := NewBridge([]netlink.Netlinker{cyw43_0, cyw43_1}) stack := tcpip.NewStack(bridge) netdev.UseNetdev(stack) Bond: bond two cyw43 devices together, creating one logical device. The first physical device is primary, the second is backup (fail-over) device: cyw43_0 := cyw43439.NewDevice(...) // primary cyw43_1 := cyw43439.NewDevice(...) // secondary (backup) bond := NewBond([]netlink.Netlinker{cyw43_0, cyw43_1}) stack := tcpip.NewStack(bond) netdev.UseNetdev(stack) Vlan: add tagged VLAN ID=100 to cyw43: cyw43 := cyw43439.NewDevice(...) vlan100 := NewVlan(100, cyw43) stack := tcpip.NewStack(vlan100) netdev.UseNetdev(stack) --- netlink/bond.go | 15 +++++++++++++++ netlink/bridge.go | 39 +++++++++++++++++++++++++++++++++++++++ netlink/netlink.go | 2 +- netlink/vlan.go | 41 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 96 insertions(+), 1 deletion(-) create mode 100644 netlink/bond.go create mode 100644 netlink/bridge.go create mode 100644 netlink/vlan.go diff --git a/netlink/bond.go b/netlink/bond.go new file mode 100644 index 0000000..8f583ca --- /dev/null +++ b/netlink/bond.go @@ -0,0 +1,15 @@ +package netlink + +// Bond aggregates links as one +type Bond struct { + *Bridge +} + +func NewBond(links []Netlinker) *Bond { + return &Bond{Bridge: NewBridge(links)} +} + +func (b *Bond) SendEth(pkt []byte) error { + // Send Ethernet pkt to active link(s) + return nil +} diff --git a/netlink/bridge.go b/netlink/bridge.go new file mode 100644 index 0000000..ae564ed --- /dev/null +++ b/netlink/bridge.go @@ -0,0 +1,39 @@ +package netlink + +import ( + "net" +) + +// Bridge connects links into an Ethernet (L2) broadcast domain +type Bridge struct { + ip net.IP + links []Netlinker + recvEth func([]byte) error +} + +func NewBridge(links []Netlinker) *Bridge { + return &Bridge{links: links} +} + +func (b *Bridge) NetConnect(params *ConnectParams) error { + return nil +} + +func (b *Bridge) NetDisconnect() { +} + +func (b *Bridge) NetNotify(cb func(Event)) { +} + +func (b *Bridge) GetHardwareAddr() (net.HardwareAddr, error) { + return net.HardwareAddr{}, nil +} + +func (b *Bridge) SendEth(pkt []byte) error { + // L2 Forward ethernet pkt to zero of more []links + return nil +} + +func (b *Bridge) RecvEthFunc(cb func(pkt []byte) error) { + b.recvEth = cb +} diff --git a/netlink/netlink.go b/netlink/netlink.go index 025dc56..35ae3d5 100644 --- a/netlink/netlink.go +++ b/netlink/netlink.go @@ -99,5 +99,5 @@ type Netlinker interface { // RecvEth callback function for receiving Ethernet pkt // TODO describe content of pkt - RecvEthFunc(func(pkt []byte) error) + RecvEthFunc(cb func(pkt []byte) error) } diff --git a/netlink/vlan.go b/netlink/vlan.go new file mode 100644 index 0000000..8669f23 --- /dev/null +++ b/netlink/vlan.go @@ -0,0 +1,41 @@ +package netlink + +import ( + "net" +) + +// Vlan is a virtual LAN +type Vlan struct { + ip net.IP + // Vlan ID + id uint16 + link Netlinker + recvEth func([]byte) error +} + +func NewVlan(id uint16, link Netlinker) *Vlan { + return &Vlan{id: id, link: link} +} + +func (v *Vlan) NetConnect(params *ConnectParams) error { + return nil +} + +func (v *Vlan) NetDisconnect() { +} + +func (v *Vlan) NetNotify(cb func(Event)) { +} + +func (v *Vlan) GetHardwareAddr() (net.HardwareAddr, error) { + return net.HardwareAddr{}, nil +} + +func (v *Vlan) SendEth(pkt []byte) error { + // Prepend VLAN hdr to pkt and send on link + return nil +} + +func (v *Vlan) RecvEthFunc(cb func(pkt []byte) error) { + v.recvEth = cb +}