mirror of
https://github.com/containers/gvisor-tap-vsock.git
synced 2025-12-24 13:29:22 +08:00
Inverted
This commit is contained in:
@@ -4,6 +4,5 @@ COPY . .
|
||||
RUN make
|
||||
|
||||
FROM centos
|
||||
RUN yum install -y net-tools iproute
|
||||
COPY --from=build /go/src/app/bin/vm .
|
||||
CMD ["/vm", "-logtostderr"]
|
||||
|
||||
5
go.mod
5
go.mod
@@ -3,6 +3,7 @@ module github.com/guillaumerose/gvisor-tap-vsock
|
||||
go 1.13
|
||||
|
||||
require (
|
||||
github.com/dustin/go-humanize v1.0.0
|
||||
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b
|
||||
github.com/google/gopacket v1.1.16
|
||||
github.com/google/tcpproxy v0.0.0-20200125044825-b6bb9b5b8252
|
||||
@@ -12,7 +13,9 @@ require (
|
||||
github.com/pkg/errors v0.9.1
|
||||
github.com/songgao/packets v0.0.0-20160404182456-549a10cd4091
|
||||
github.com/songgao/water v0.0.0-20200317203138-2b4b6d7c09d8
|
||||
golang.org/x/sys v0.0.0-20200724161237-0e2f3a69832c // indirect
|
||||
github.com/vishvananda/netlink v1.1.0
|
||||
github.com/vishvananda/netns v0.0.0-20200728191858-db3c7e526aae // indirect
|
||||
golang.org/x/sys v0.0.0-20200831180312-196b9ba8737a // indirect
|
||||
gvisor.dev/gvisor v0.0.0-20200727050644-5e9c2950a570
|
||||
)
|
||||
|
||||
|
||||
11
go.sum
11
go.sum
@@ -40,6 +40,8 @@ github.com/docker/go-connections v0.3.0/go.mod h1:Gbd7IOopHjR8Iph03tsViu4nIes5Xh
|
||||
github.com/docker/go-events v0.0.0-20190806004212-e31b211e4f1c/go.mod h1:Uw6UezgYA44ePAFQYUehOuCzmy5zmg/+nl2ZfMWGkpA=
|
||||
github.com/docker/go-units v0.4.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk=
|
||||
github.com/dustin/go-humanize v0.0.0-20171111073723-bb3d318650d4/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
|
||||
github.com/dustin/go-humanize v1.0.0 h1:VSnTsYCnlFHaM2/igO1h6X3HA71jcobQuxemgkq4zYo=
|
||||
github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
|
||||
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
|
||||
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
|
||||
github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98=
|
||||
@@ -153,7 +155,13 @@ github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81P
|
||||
github.com/syndtr/gocapability v0.0.0-20180916011248-d98352740cb2/go.mod h1:hkRG7XYTFWNJGYcbNJQlaLq0fg1yr4J4t/NcTQtrfww=
|
||||
github.com/urfave/cli v0.0.0-20171014202726-7bc6a0acffa5/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA=
|
||||
github.com/vishvananda/netlink v1.0.1-0.20190930145447-2ec5bdc52b86/go.mod h1:+SR5DhBJrl6ZM7CoCKvpw5BKroDKQ+PJqOg65H/2ktk=
|
||||
github.com/vishvananda/netlink v1.1.0 h1:1iyaYNBLmP6L0220aDnYQpo1QEV4t4hJ+xEEhhJH8j0=
|
||||
github.com/vishvananda/netlink v1.1.0/go.mod h1:cTgwzPIzzgDAYoQrMm0EdrjRUBkTqKYppBueQtXaqoE=
|
||||
github.com/vishvananda/netns v0.0.0-20191106174202-0a2b9b5464df/go.mod h1:JP3t17pCcGlemwknint6hfoeCVQrEMVwxRLRjXpq+BU=
|
||||
github.com/vishvananda/netns v0.0.0-20200520041808-52d707b772fe h1:mjAZxE1nh8yvuwhGHpdDqdhtNu2dgbpk93TwoXuk5so=
|
||||
github.com/vishvananda/netns v0.0.0-20200520041808-52d707b772fe/go.mod h1:DD4vA1DwXk04H54A1oHXtwZmA0grkVMdPxx/VGLCah0=
|
||||
github.com/vishvananda/netns v0.0.0-20200728191858-db3c7e526aae h1:4hwBBUfQCFe3Cym0ZtKyq7L16eZUtYKs+BaHDN6mAns=
|
||||
github.com/vishvananda/netns v0.0.0-20200728191858-db3c7e526aae/go.mod h1:DD4vA1DwXk04H54A1oHXtwZmA0grkVMdPxx/VGLCah0=
|
||||
github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
|
||||
go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU=
|
||||
go.opencensus.io v0.22.0/go.mod h1:+kGneAE2xo2IficOXnaByMWTGM9T73dGwxeWcUqIpI8=
|
||||
@@ -226,6 +234,7 @@ golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7w
|
||||
golang.org/x/sys v0.0.0-20190502145724-3ef323f4f1fd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20190507160741-ecd444e8653b/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20190606165138-5da285871e9c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20190606203320-7fc4e5ec1444/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20190624142023-c5567b49c5d0/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20190924154521-2837fb4f24fe/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
@@ -240,6 +249,8 @@ golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7w
|
||||
golang.org/x/sys v0.0.0-20200501145240-bc7a7d42d5c3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20200724161237-0e2f3a69832c h1:UIcGWL6/wpCfyGuJnRFJRurA+yj8RrW7Q6x2YMCXt6c=
|
||||
golang.org/x/sys v0.0.0-20200724161237-0e2f3a69832c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20200831180312-196b9ba8737a h1:i47hUS795cOydZI4AwJQCKXOr4BvxzvikwDoDtHhP2Y=
|
||||
golang.org/x/sys v0.0.0-20200831180312-196b9ba8737a/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"io"
|
||||
"net"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
|
||||
log "github.com/golang/glog"
|
||||
"github.com/google/gopacket"
|
||||
@@ -18,6 +19,9 @@ import (
|
||||
)
|
||||
|
||||
type TapLinkEndpoint struct {
|
||||
Sent uint64
|
||||
Received uint64
|
||||
|
||||
Conn net.Conn
|
||||
Debug bool
|
||||
Mac tcpip.LinkAddress
|
||||
@@ -114,6 +118,8 @@ func (e *TapLinkEndpoint) writeSockets(hdr buffer.Prependable, payload buffer.Ve
|
||||
e.Conn.Close()
|
||||
return err
|
||||
}
|
||||
|
||||
atomic.AddUint64(&e.Sent, uint64(hdr.UsedLength()+payload.Size()))
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -164,6 +170,8 @@ func rx(conn net.Conn, e *TapLinkEndpoint) error {
|
||||
if e.dispatcher == nil {
|
||||
continue
|
||||
}
|
||||
|
||||
atomic.AddUint64(&e.Received, uint64(size))
|
||||
e.dispatcher.DeliverNetworkPacket(
|
||||
eth.SourceAddress(),
|
||||
eth.DestinationAddress(),
|
||||
|
||||
@@ -10,6 +10,8 @@ import (
|
||||
"runtime"
|
||||
"time"
|
||||
|
||||
"github.com/dustin/go-humanize"
|
||||
|
||||
log "github.com/golang/glog"
|
||||
"github.com/linuxkit/virtsock/pkg/hvsock"
|
||||
mdlayhervsock "github.com/mdlayher/vsock"
|
||||
@@ -89,10 +91,9 @@ func run(debug bool, mtu int) error {
|
||||
|
||||
go func() {
|
||||
for {
|
||||
fmt.Printf("%v packets sent, %v packets received\n", stack.Stats().IP.PacketsSent.Value(), stack.Stats().IP.PacketsReceived.Value())
|
||||
fmt.Printf("%v sent to the VM, %v received from the VM\n", humanize.Bytes(tapEndpoint.Sent), humanize.Bytes(tapEndpoint.Received))
|
||||
time.Sleep(5 * time.Second)
|
||||
}
|
||||
|
||||
}()
|
||||
return tapEndpoint.AcceptOne()
|
||||
}
|
||||
|
||||
88
vm/main.go
88
vm/main.go
@@ -6,8 +6,6 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"os/exec"
|
||||
"os/user"
|
||||
|
||||
log "github.com/golang/glog"
|
||||
"github.com/google/gopacket"
|
||||
@@ -16,6 +14,7 @@ import (
|
||||
"github.com/pkg/errors"
|
||||
"github.com/songgao/packets/ethernet"
|
||||
"github.com/songgao/water"
|
||||
"github.com/vishvananda/netlink"
|
||||
"gvisor.dev/gvisor/pkg/tcpip/header"
|
||||
)
|
||||
|
||||
@@ -72,48 +71,61 @@ func handle(conn net.Conn) error {
|
||||
go tx(conn, tap, errCh)
|
||||
go rx(conn, tap, errCh)
|
||||
|
||||
user, err := user.Current()
|
||||
cleanup, err := linkUp()
|
||||
defer cleanup()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
commands := []string{
|
||||
"ip addr add 192.168.127.2/24 dev O_O",
|
||||
"ip link set dev O_O up",
|
||||
"route del default gw 192.168.130.1",
|
||||
"route add default gw 192.168.127.1 dev O_O",
|
||||
"ifconfig O_O mtu 1500 up",
|
||||
}
|
||||
defer func() {
|
||||
command := exec.Command("sudo", "/bin/sh", "-c", "route add default gw 192.168.130.1 dev ens3")
|
||||
if user.Uid == "0" {
|
||||
command = exec.Command("/bin/sh", "-c", "route add default gw 192.168.130.1 dev ens3")
|
||||
}
|
||||
out, err := command.CombinedOutput()
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
return
|
||||
}
|
||||
log.Info(out)
|
||||
}()
|
||||
for _, command := range commands {
|
||||
log.Infof("Running %s", command)
|
||||
cmd := exec.Command("sudo", "/bin/sh", "-c", command)
|
||||
if user.Uid == "0" {
|
||||
cmd = exec.Command("/bin/sh", "-c", command)
|
||||
}
|
||||
out, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
continue
|
||||
}
|
||||
if len(out) > 0 {
|
||||
log.Info(out)
|
||||
}
|
||||
}
|
||||
|
||||
return <-errCh
|
||||
}
|
||||
|
||||
func linkUp() (func(), error) {
|
||||
link, err := netlink.LinkByName("O_O")
|
||||
if err != nil {
|
||||
return func() {}, err
|
||||
}
|
||||
newDefaultRoute := netlink.Route{
|
||||
LinkIndex: link.Attrs().Index,
|
||||
Gw: net.ParseIP("192.168.127.1"),
|
||||
}
|
||||
var defaultRoute *netlink.Route
|
||||
routes, err := netlink.RouteList(nil, netlink.FAMILY_V4)
|
||||
for _, r := range routes {
|
||||
if r.Dst == nil {
|
||||
defaultRoute = &r
|
||||
break
|
||||
}
|
||||
}
|
||||
if defaultRoute == nil {
|
||||
return func() {}, errors.New("no default gateway found")
|
||||
}
|
||||
cleanup := func() {
|
||||
if err := netlink.RouteAdd(defaultRoute); err != nil {
|
||||
log.Errorf("cannot restore old default gateway: %v", err)
|
||||
}
|
||||
if err := netlink.RouteDel(&newDefaultRoute); err != nil {
|
||||
log.Errorf("cannot remove new default gateway: %v", err)
|
||||
}
|
||||
}
|
||||
addr, err := netlink.ParseAddr("192.168.127.2/24")
|
||||
if err != nil {
|
||||
return cleanup, err
|
||||
}
|
||||
if err := netlink.AddrAdd(link, addr); err != nil {
|
||||
return cleanup, errors.Wrap(err, "cannot add address")
|
||||
}
|
||||
if err := netlink.LinkSetUp(link); err != nil {
|
||||
return cleanup, errors.Wrap(err, "cannot set link up")
|
||||
}
|
||||
if err := netlink.RouteDel(defaultRoute); err != nil {
|
||||
return cleanup, errors.Wrap(err, "cannot remove old default gateway")
|
||||
}
|
||||
if err := netlink.RouteAdd(&newDefaultRoute); err != nil {
|
||||
return cleanup, errors.Wrap(err, "cannot add new default gateway")
|
||||
}
|
||||
return cleanup, nil
|
||||
}
|
||||
|
||||
func rx(conn net.Conn, tap *water.Interface, errCh chan error) {
|
||||
log.Info("waiting for packets...")
|
||||
var frame ethernet.Frame
|
||||
|
||||
Reference in New Issue
Block a user