Files
Akihiro Suda da7595a69f rootless: use RootlessKit port forwarder
RootlessKit port forwarder has a lot of advantages over the slirp4netns port forwarder:

* Very high throughput.
  Benchmark result on Travis: socat: 5.2 Gbps, slirp4netns: 8.3 Gbps, RootlessKit: 27.3 Gbps
  (https://travis-ci.org/rootless-containers/rootlesskit/builds/597056377)

* Connections from the host are treated as 127.0.0.1 rather than 10.0.2.2 in the namespace.
  No UDP issue (#4586)

* No tcp_rmem issue (#4537)

* Probably works with IPv6. Even if not, it is trivial to support IPv6.  (#4311)

* Easily extensible for future support of SCTP

* Easily extensible for future support of `lxc-user-nic` SUID network

RootlessKit port forwarder has been already adopted as the default port forwarder by Rootless Docker/Moby,
and no issue has been reported AFAIK.

As the port forwarder is imported as a Go package, no `rootlesskit` binary is required for Podman.

Fix #4586
May-fix #4559
Fix #4537
May-fix #4311

See https://github.com/rootless-containers/rootlesskit/blob/v0.7.0/pkg/port/builtin/builtin.go

Signed-off-by: Akihiro Suda <akihiro.suda.cz@hco.ntt.co.jp>
2020-01-08 19:35:17 +09:00

67 lines
1.5 KiB
Go

// Package msgutil provides utility for JSON message with uint32le header
package msgutil
import (
"bytes"
"encoding/binary"
"encoding/json"
"io"
"github.com/pkg/errors"
)
const (
maxLength = 1 << 16
)
func MarshalToWriter(w io.Writer, x interface{}) (int, error) {
b, err := json.Marshal(x)
if err != nil {
return 0, err
}
if len(b) > maxLength {
return 0, errors.Errorf("bad message length: %d (max: %d)", len(b), maxLength)
}
h := make([]byte, 4)
binary.LittleEndian.PutUint32(h, uint32(len(b)))
return w.Write(append(h, b...))
}
func UnmarshalFromReader(r io.Reader, x interface{}) (int, error) {
hdr := make([]byte, 4)
n, err := r.Read(hdr)
if err != nil {
return n, err
}
if n != 4 {
return n, errors.Errorf("read %d bytes, expected 4 bytes", n)
}
bLen := binary.LittleEndian.Uint32(hdr)
if bLen > maxLength || bLen < 1 {
return n, errors.Errorf("bad message length: %d (max: %d)", bLen, maxLength)
}
b := make([]byte, bLen)
n, err = r.Read(b)
if err != nil {
return 4 + n, err
}
if n != int(bLen) {
return 4 + n, errors.Errorf("read %d bytes, expected %d bytes", n, bLen)
}
return 4 + n, json.Unmarshal(b, x)
}
func Marshal(x interface{}) ([]byte, error) {
var b bytes.Buffer
_, err := MarshalToWriter(&b, x)
return b.Bytes(), err
}
func Unmarshal(b []byte, x interface{}) error {
n, err := UnmarshalFromReader(bytes.NewReader(b), x)
if n != len(b) {
return errors.Errorf("read %d bytes, expected %d bytes", n, len(b))
}
return err
}