Files
drivers/netlink/netlink.go
T
Scott Feldman 1fd8a768ae 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)
2023-12-06 20:09:39 +01:00

104 lines
2.7 KiB
Go

// L2 data link layer
package netlink
import (
"errors"
"net"
"time"
)
var (
ErrConnected = errors.New("Already connected")
ErrConnectFailed = errors.New("Connect failed")
ErrConnectTimeout = errors.New("Connect timed out")
ErrMissingSSID = errors.New("Missing WiFi SSID")
ErrAuthTypeNoGood = errors.New("Wifi authorization type not supported")
ErrConnectModeNoGood = errors.New("Connect mode not supported")
ErrNotSupported = errors.New("Not supported")
)
type Event int
// Network events
const (
// The device's network connection is now UP
EventNetUp Event = iota
// The device's network connection is now DOWN
EventNetDown
)
type ConnectMode int
// Connect modes
const (
ConnectModeSTA = iota // Connect as Wifi station (default)
ConnectModeAP // Connect as Wifi Access Point
)
type AuthType int
// Wifi authorization types. Used when setting up an access point, or
// connecting to an access point
const (
AuthTypeWPA2 = iota // WPA2 authorization (default)
AuthTypeOpen // No authorization required (open)
AuthTypeWPA // WPA authorization
AuthTypeWPA2Mixed // WPA2/WPA mixed authorization
)
const DefaultConnectTimeout = 10 * time.Second
type ConnectParams struct {
// Connect mode
ConnectMode
// SSID of Wifi AP
Ssid string
// Passphrase of Wifi AP
Passphrase string
// Wifi authorization type
AuthType
// Wifi country code as two-char string. E.g. "XX" for world-wide,
// "US" for USA, etc.
Country string
// Retries is how many attempts to connect before returning with a
// "Connect failed" error. Zero means infinite retries.
Retries int
// Timeout duration for each connection attempt. The default zero
// value means 10sec.
ConnectTimeout time.Duration
// Watchdog ticker duration. On tick, the watchdog will check for
// downed connection or hardware fault and try to recover the
// connection. Set to zero to disable watchodog.
WatchdogTimeout time.Duration
}
// Netlinker is TinyGo's OSI L2 data link layer interface. Network device
// drivers implement Netlinker to expose the device's L2 functionality.
type Netlinker interface {
// Connect device to network
NetConnect(params *ConnectParams) error
// Disconnect device from network
NetDisconnect()
// Notify to register callback for network events
NetNotify(cb func(Event))
// GetHardwareAddr returns device MAC address
GetHardwareAddr() (net.HardwareAddr, error)
// SendEth sends an Ethernet packet
// TODO describe content of pkt
SendEth(pkt []byte) error
// RecvEth callback function for receiving Ethernet pkt
// TODO describe content of pkt
RecvEthFunc(cb func(pkt []byte) error)
}