Skip to content

Commit

Permalink
Move DeliverNetworkPacket into separate goroutine
Browse files Browse the repository at this point in the history
... as it may write in the same ironwood context as the reader
if packet destination is unknown or some other condition requires
replyWithReset

Thanks to @Arceliar for hint!

Signed-off-by: Vasyl Gello <[email protected]>
  • Loading branch information
basilgello committed Jul 17, 2024
1 parent b232781 commit 393c7de
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions src/netstack/yggdrasil.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,19 @@ import (
)

type YggdrasilNIC struct {
stack *YggdrasilNetstack
ipv6rwc *ipv6rwc.ReadWriteCloser
dispatcher stack.NetworkDispatcher
readBuf []byte
writeBuf []byte
stack *YggdrasilNetstack
ipv6rwc *ipv6rwc.ReadWriteCloser
dispatcher stack.NetworkDispatcher
readBufChan chan *stack.PacketBuffer
writeBuf []byte
}

func (s *YggdrasilNetstack) NewYggdrasilNIC(ygg *core.Core) tcpip.Error {
rwc := ipv6rwc.NewReadWriteCloser(ygg)
mtu := rwc.MTU()
nic := &YggdrasilNIC{
ipv6rwc: rwc,
readBuf: make([]byte, mtu),
readBufChan: make(chan *stack.PacketBuffer, 100),
writeBuf: make([]byte, mtu),
}
if err := s.stack.CreateNIC(1, nic); err != nil {
Expand All @@ -36,16 +36,22 @@ func (s *YggdrasilNetstack) NewYggdrasilNIC(ygg *core.Core) tcpip.Error {
go func() {
var rx int
var err error
readBuf := make([]byte, mtu)
for {
rx, err = nic.ipv6rwc.Read(nic.readBuf)
rx, err = nic.ipv6rwc.Read(readBuf)
if err != nil {
log.Println(err)
break
}
pkb := stack.NewPacketBuffer(stack.PacketBufferOptions{
Payload: buffer.MakeWithData(nic.readBuf[:rx]),
Payload: buffer.MakeWithData(readBuf[:rx]),
})
go nic.dispatcher.DeliverNetworkPacket(ipv6.ProtocolNumber, pkb)
nic.readBufChan <- pkb
}
}()
go func() {
for {
nic.dispatcher.DeliverNetworkPacket(ipv6.ProtocolNumber, <- nic.readBufChan)
}
}()
_, snet, err := net.ParseCIDR("0200::/7")
Expand Down

0 comments on commit 393c7de

Please sign in to comment.