Files
drivers/netdev
Scott Feldman e7931c6a22 wifinina: fix concurrency issues with multiple sockets
wifinina driver was not handling concurrent socket connections
correctly.  When trying to make multiple socket connections, the sockfd
returned by the first Socket() call would be the same sockfd returned by
subsequent Socket() calls.  The problem is the sockfd returned by
Socket() isn't used until Connect() (or Appect()).  This would result in
Connect() trying to use the same sockfd for multiple connections,
causing wifinina fw to lock up.

The solution in this PR is to create a new sockfd space managed by the
driver that gives the app a unique, safe sockfd for each connection.
The real underlying sock fd returned by fw is set on Connect() (or
Accept()), and mapped back to the apps sockfd using a map:

    sockets map[int]*Socket // keyed by sockfd

Where Socket has a reference to the fw sock:

    type Socket struct {
            protocol        int
            clientConnected bool
            laddr           netip.AddrPort // Set in Bind()
            raddr           netip.AddrPort // Set in Connect()
            sock                           // Device socket, as returned from w.getSocket()
    }
2024-01-22 13:32:57 -08:00
..

Table of Contents

Netdever

TinyGo's network device driver model comprises two Go interfaces: Netdever and Netlinker. This README covers Netdever.

The Netdever interface describes an L4/L3 network interface modeled after the BSD sockets. A netdev is a concrete implementation of a Netdever. See Netlinker for the L2 network interface.

A netdev can:

  • Send and receive L4/L3 packets
  • Resolve DNS lookups
  • Get/set the device's IP address

TinyGo network drivers implement the Netdever interface, providing a BSD sockets interface to TinyGo's "net" package. net.Conn implementations (TCPConn, UDPConn, and TLSConn) use the netdev socket. For example, net.DialTCP, which returns a net.TCPConn, calls netdev.Socket() and netdev.Connect():

func DialTCP(network string, laddr, raddr *TCPAddr) (*TCPConn, error) {

        fd, _ := netdev.Socket(netdev.AF_INET, netdev.SOCK_STREAM, netdev.IPPROTO_TCP)

        netdev.Connect(fd, "", raddr.IP, raddr.Port)

        return &TCPConn{
                fd:    fd,
                laddr: laddr,
                raddr: raddr,
        }, nil
}

Setting Netdev

Before the app can use TinyGo's "net" package, the app must set the netdev using UseNetdev(). This binds the "net" package to the netdev driver. For example, setting the wifinina driver as the netdev:

	nina := wifinina.New(&cfg)
	netdev.UseNetdev(nina)

Netdev Driver Notes

See the wifinina and rtl8720dn for examples of netdev drivers. Here are some notes for netdev drivers.

Locking

Multiple goroutines may invoke methods on a net.Conn simultaneously, and since the net package translates net.Conn calls into netdev socket calls, it follows that multiple goroutines may invoke socket calls, so locking is required to keep socket calls from stepping on one another.

Don't hold a lock while Time.Sleep()ing waiting for a long hardware operation to finish. Unlocking while sleeping let's other goroutines make progress.

Sockfd

The netdev BSD socket interface uses a socket fd (int) to represent a socket connection (end-point). Each fd maps 1:1 to a net.Conn maps. The number of fds available is a hardware limitation. Wifinina, for example, can hand out 10 fds, representing 10 active sockets.

Testing

The netdev driver should minimally run all of the example/net examples.