Skip to content

Commit

Permalink
Add GSO support
Browse files Browse the repository at this point in the history
  • Loading branch information
nekohasekai committed Dec 9, 2023
1 parent bf6cade commit 6cc1612
Show file tree
Hide file tree
Showing 14 changed files with 979 additions and 361 deletions.
17 changes: 10 additions & 7 deletions stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@ type Stack interface {
type StackOptions struct {
Context context.Context
Tun Tun
Name string
MTU uint32
Inet4Address []netip.Prefix
Inet6Address []netip.Prefix
TunOptions Options
EndpointIndependentNat bool
UDPTimeout int64
Handler Handler
Expand All @@ -31,13 +28,21 @@ type StackOptions struct {
InterfaceFinder control.InterfaceFinder
}

func (o *StackOptions) BufferSize() uint32 {
if o.TunOptions.GSO {
return o.TunOptions.GSOMaxSize
} else {
return o.TunOptions.MTU
}
}

func NewStack(
stack string,
options StackOptions,
) (Stack, error) {
switch stack {
case "":
if WithGVisor {
if WithGVisor && !options.TunOptions.GSO {
return NewMixed(options)
} else {
return NewSystem(options)
Expand All @@ -48,8 +53,6 @@ func NewStack(
return NewMixed(options)
case "system":
return NewSystem(options)
case "lwip":
return NewLWIP(options)
default:
return nil, E.New("unknown stack: ", stack)
}
Expand Down
6 changes: 2 additions & 4 deletions stack_gvisor.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ const defaultNIC tcpip.NICID = 1
type GVisor struct {
ctx context.Context
tun GVisorTun
tunMtu uint32
endpointIndependentNat bool
udpTimeout int64
broadcastAddr netip.Addr
Expand All @@ -57,10 +56,9 @@ func NewGVisor(
gStack := &GVisor{
ctx: options.Context,
tun: gTun,
tunMtu: options.MTU,
endpointIndependentNat: options.EndpointIndependentNat,
udpTimeout: options.UDPTimeout,
broadcastAddr: BroadcastAddr(options.Inet4Address),
broadcastAddr: BroadcastAddr(options.TunOptions.Inet4Address),
handler: options.Handler,
logger: options.Logger,
}
Expand All @@ -72,7 +70,7 @@ func (t *GVisor) Start() error {
if err != nil {
return err
}
linkEndpoint = &LinkEndpointFilter{linkEndpoint, t.broadcastAddr, t.tun.CreateVectorisedWriter()}
linkEndpoint = &LinkEndpointFilter{linkEndpoint, t.broadcastAddr, bufio.NewVectorisedWriter(t.tun)}
ipStack, err := newGVisorStack(linkEndpoint)
if err != nil {
return err
Expand Down
144 changes: 0 additions & 144 deletions stack_lwip.go

This file was deleted.

11 changes: 0 additions & 11 deletions stack_lwip_stub.go

This file was deleted.

27 changes: 14 additions & 13 deletions stack_mixed.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func NewMixed(
}
return &Mixed{
System: system.(*System),
writer: options.Tun.CreateVectorisedWriter(),
writer: bufio.NewVectorisedWriter(options.Tun),
endpointIndependentNat: options.EndpointIndependentNat,
}, nil
}
Expand Down Expand Up @@ -95,7 +95,7 @@ func (m *Mixed) tunLoop() {
m.wintunLoop(winTun)
return
}
packetBuffer := make([]byte, m.mtu+PacketOffset)
packetBuffer := make([]byte, m.bufferSize+m.tun.FrontHeadroom())
for {
n, err := m.tun.Read(packetBuffer)
if err != nil {
Expand All @@ -104,12 +104,13 @@ func (m *Mixed) tunLoop() {
if n < clashtcpip.IPv4PacketMinLength {
continue
}
packet := packetBuffer[PacketOffset:n]
rawPacket := packetBuffer[:n]
packet := packetBuffer[m.tun.FrontHeadroom():n]
switch ipVersion := packet[0] >> 4; ipVersion {
case 4:
err = m.processIPv4(packet)
err = m.processIPv4(rawPacket, packet)
case 6:
err = m.processIPv6(packet)
err = m.processIPv6(rawPacket, packet)
default:
err = E.New("ip: unknown version: ", ipVersion)
}
Expand All @@ -131,9 +132,9 @@ func (m *Mixed) wintunLoop(winTun WinTun) {
}
switch ipVersion := packet[0] >> 4; ipVersion {
case 4:
err = m.processIPv4(packet)
err = m.processIPv4(packet, packet)
case 6:
err = m.processIPv6(packet)
err = m.processIPv6(packet, packet)
default:
err = E.New("ip: unknown version: ", ipVersion)
}
Expand All @@ -144,14 +145,14 @@ func (m *Mixed) wintunLoop(winTun WinTun) {
}
}

func (m *Mixed) processIPv4(packet clashtcpip.IPv4Packet) error {
func (m *Mixed) processIPv4(rawPacket []byte, packet clashtcpip.IPv4Packet) error {
destination := packet.DestinationIP()
if destination == m.broadcastAddr || !destination.IsGlobalUnicast() {
return common.Error(m.tun.Write(packet))
}
switch packet.Protocol() {
case clashtcpip.TCP:
return m.processIPv4TCP(packet, packet.Payload())
return m.processIPv4TCP(rawPacket, packet, packet.Payload())
case clashtcpip.UDP:
pkt := stack.NewPacketBuffer(stack.PacketBufferOptions{
Payload: buffer.MakeWithData(packet),
Expand All @@ -160,19 +161,19 @@ func (m *Mixed) processIPv4(packet clashtcpip.IPv4Packet) error {
pkt.DecRef()
return nil
case clashtcpip.ICMP:
return m.processIPv4ICMP(packet, packet.Payload())
return m.processIPv4ICMP(rawPacket, packet, packet.Payload())
default:
return common.Error(m.tun.Write(packet))
}
}

func (m *Mixed) processIPv6(packet clashtcpip.IPv6Packet) error {
func (m *Mixed) processIPv6(rawPacket []byte, packet clashtcpip.IPv6Packet) error {
if !packet.DestinationIP().IsGlobalUnicast() {
return common.Error(m.tun.Write(packet))
}
switch packet.Protocol() {
case clashtcpip.TCP:
return m.processIPv6TCP(packet, packet.Payload())
return m.processIPv6TCP(rawPacket, packet, packet.Payload())
case clashtcpip.UDP:
pkt := stack.NewPacketBuffer(stack.PacketBufferOptions{
Payload: buffer.MakeWithData(packet),
Expand All @@ -181,7 +182,7 @@ func (m *Mixed) processIPv6(packet clashtcpip.IPv6Packet) error {
pkt.DecRef()
return nil
case clashtcpip.ICMPv6:
return m.processIPv6ICMP(packet, packet.Payload())
return m.processIPv6ICMP(rawPacket, packet, packet.Payload())
default:
return common.Error(m.tun.Write(packet))
}
Expand Down
Loading

0 comments on commit 6cc1612

Please sign in to comment.