Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
Tune buffer size via using history record.
Browse files Browse the repository at this point in the history
  • Loading branch information
bzEq committed Dec 17, 2023
1 parent 453ca97 commit c2230f2
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ A simple and fast L4 virtual switch.

## Install
```shell
go install github.com/bzEq/bx/i3@main
GOPROXY=direct go install github.com/bzEq/bx/i3@main
```
29 changes: 22 additions & 7 deletions core/port.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package core

import (
"bufio"
"log"
"net"
"sync"
"time"
Expand Down Expand Up @@ -50,6 +51,7 @@ type RawNetPort struct {
C net.Conn
timeout time.Duration
buf []byte
nr int
}

func (self *RawNetPort) Pack(b *iovec.IoVec) error {
Expand All @@ -61,19 +63,32 @@ func (self *RawNetPort) Pack(b *iovec.IoVec) error {
}

func (self *RawNetPort) Unpack(b *iovec.IoVec) error {
if len(self.buf) < DEFAULT_UDP_BUFFER_SIZE {
self.buf = make([]byte, DEFAULT_BUFFER_SIZE)
const BUFFER_LIMIT = 1 << 20
l := len(self.buf)
log.Printf("Current buffer len: %d, Last read: %d\n", l, self.nr)
if l < self.nr {
l = self.nr * 2
}
if err := self.C.SetReadDeadline(time.Now().Add(self.timeout)); err != nil {
if l < DEFAULT_UDP_BUFFER_SIZE {
l = DEFAULT_BUFFER_SIZE
}
if l > BUFFER_LIMIT {
l = BUFFER_LIMIT
}
if l > len(self.buf) {
self.buf = make([]byte, l)
}
err := self.C.SetReadDeadline(time.Now().Add(self.timeout))
if err != nil {
return err
}
nr, err := self.C.Read(self.buf)
self.nr, err = self.C.Read(self.buf)
if err != nil {
self.nr = 0
return err
}
s := self.buf[:nr]
self.buf = self.buf[nr:]
b.Take(s)
b.Take(self.buf[:self.nr])
self.buf = self.buf[self.nr:]
return nil
}

Expand Down
2 changes: 1 addition & 1 deletion core/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (self *ProtocolWithPass) Unpack(in *bufio.Reader, b *iovec.IoVec) error {
return self.UP.Run(b)
}

const UNUSUAL_BUFFER_LENGTH_THRESHOLD = DEFAULT_BUFFER_SIZE * 2
const UNUSUAL_BUFFER_LENGTH_THRESHOLD = 64 << 20

type HTTPProtocol struct{}

Expand Down
2 changes: 0 additions & 2 deletions passes/bytes.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@ void ByteSwap(uint8_t *__restrict__ dst, const uint8_t *__restrict__ src,
const uint64_t *__restrict__ u64_src =
reinterpret_cast<const uint64_t *>(src);
const size_t m = len / n;
#pragma clang loop unroll(enable)
for (size_t i = 0; i < m; ++i)
u64_dst[i] = __builtin_bswap64(u64_src[m - 1 - i]);
#pragma clang loop unroll(enable)
for (size_t i = m * n; i < len; ++i)
dst[i] = src[i];
}
Expand Down

0 comments on commit c2230f2

Please sign in to comment.