Skip to content

Commit

Permalink
Merge pull request #218 from hermitkim1/replace-the-package-for-tun
Browse files Browse the repository at this point in the history
Configure TUN by `syscall` instead of `water` tun package
  • Loading branch information
hermitkim1 authored Jan 19, 2022
2 parents 588ddef + c48a0c7 commit 9970a8e
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 57 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/build-on-pull-request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:

strategy:
matrix:
go-version: [ 1.16 ]
go-version: [ 1.17 ]
os: [ ubuntu-18.04 ] #macos-latest, windows-latest
runs-on: ${{ matrix.os }}

Expand All @@ -30,7 +30,7 @@ jobs:
# optionally downloading and caching a version of Go by version and adding to PATH
# registering problem matchers for error output
# This step uses GitHub's setup-go: https://github.com/actions/setup-go
- name: Set up Go 1.16.x
- name: Set up Go ${{ matrix.go-version }}
uses: actions/setup-go@v2
with:
go-version: ${{ matrix.go-version }}
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/lint-on-push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ jobs:

strategy:
matrix:
go-version: [ 1.16 ]
go-version: [ 1.17 ]
os: [ ubuntu-18.04 ] #macos-latest, windows-latest
runs-on: ${{ matrix.os }}

steps:
- name: Set up Go 1.16.x
- name: Set up Go ${{ matrix.go-version }}
uses: actions/setup-go@v2
with:
go-version: ${{ matrix.go-version }}
Expand Down
6 changes: 2 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ module github.com/cloud-barista/cb-larva
go 1.15

require (
github.com/buger/jsonparser v1.1.1
github.com/cloud-barista/cb-log v0.4.0
github.com/go-ping/ping v0.0.0-20210506233800-ff8be3320020
github.com/go-resty/resty/v2 v2.7.0
Expand All @@ -26,10 +25,9 @@ require (
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.7.0 // indirect
go.uber.org/zap v1.19.0 // indirect
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519 // indirect
golang.org/x/net v0.0.0-20211029224645-99673261e6eb
golang.org/x/sys v0.0.0-20210910150752-751e447fb3d0 // indirect
golang.org/x/net v0.0.0-20220114011407-0dd24b26b47d
golang.org/x/text v0.3.7 // indirect
golang.zx2c4.com/wireguard v0.0.0-20220114151043-114a3db91850
google.golang.org/genproto v0.0.0-20211018162055-cf77aa76bad2
google.golang.org/grpc v1.40.0
google.golang.org/protobuf v1.27.1
Expand Down
20 changes: 13 additions & 7 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@ github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZx
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
github.com/buger/jsonparser v1.1.1 h1:2PnMjfWD7wBILjqQbt530v576A/cAbQvEW9gGIpYMUs=
github.com/buger/jsonparser v1.1.1/go.mod h1:6RYKKt7H4d4+iWqouImQ9R2FZql3VbhNgx27UK13J/0=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
Expand Down Expand Up @@ -289,8 +287,8 @@ golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8U
golang.org/x/crypto v0.0.0-20190605123033-f99c8df09eb5/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519 h1:7I4JAnoQBe7ZtJcBaYHi5UtiO8tQHbUSXxL+pnGRANg=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.0.0-20211108221036-ceb1ce70b4fa h1:idItI2DDfCokpg0N51B2VtiLdJ4vAuXC9fnCb2gACo4=
golang.org/x/crypto v0.0.0-20211108221036-ceb1ce70b4fa/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20190510132918-efd6b22b2522/go.mod h1:ZjyILWgesfNpC6sMxTJOJm9Kp84zZh5NQWvqDGG3Qr8=
Expand Down Expand Up @@ -357,8 +355,10 @@ golang.org/x/net v0.0.0-20200904194848-62affa334b73/go.mod h1:/O7V0waA8r7cgGh81R
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
golang.org/x/net v0.0.0-20211029224645-99673261e6eb h1:pirldcYWx7rx7kE5r+9WsOXPXK0+WH5+uZ7uPmJ44uM=
golang.org/x/net v0.0.0-20211029224645-99673261e6eb/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20211111083644-e5c967477495/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20220114011407-0dd24b26b47d h1:1n1fc535VhN8SYtD4cDUyNlfpAF2ROMM9+11equK3hs=
golang.org/x/net v0.0.0-20220114011407-0dd24b26b47d/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
Expand Down Expand Up @@ -421,8 +421,9 @@ golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210603081109-ebe580a85c40/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210910150752-751e447fb3d0 h1:xrCZDmdtoloIiooiA9q0OQb9r8HejIHYoHGhGCe1pGg=
golang.org/x/sys v0.0.0-20210910150752-751e447fb3d0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211103235746-7861aae1554b/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211110154304-99a53858aa08 h1:WecRHqgE09JBkh/584XIE6PMz5KKE/vER4izNUi30AQ=
golang.org/x/sys v0.0.0-20211110154304-99a53858aa08/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
Expand Down Expand Up @@ -487,6 +488,11 @@ golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8T
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.zx2c4.com/go118/netip v0.0.0-20211111135330-a4a02eeacf9d/go.mod h1:5yyfuiqVIJ7t+3MqrpTQ+QqRkMWiESiyDvPNvKYCecg=
golang.zx2c4.com/wintun v0.0.0-20211104114900-415007cec224 h1:Ug9qvr1myri/zFN6xL17LSCBGFDnphBBhzmILHsM5TY=
golang.zx2c4.com/wintun v0.0.0-20211104114900-415007cec224/go.mod h1:deeaetjYA+DHMHg+sMSMI58GrEteJUUzzw7en6TJQcI=
golang.zx2c4.com/wireguard v0.0.0-20220114151043-114a3db91850 h1:5eMhIjXyTR2W9CmrN65ZHd+tUj82Tq0tMlWpDlFUobo=
golang.zx2c4.com/wireguard v0.0.0-20220114151043-114a3db91850/go.mod h1:TjUWrnD5ATh7bFvmm/ALEJZQ4ivKbETb6pmyj1vUoNI=
google.golang.org/api v0.4.0/go.mod h1:8k5glujaEP+g9n7WNsDg8QP6cUVNI86fCNMcbazEtwE=
google.golang.org/api v0.7.0/go.mod h1:WtwebWUNSVBH/HAw79HIFXZNqEvBhG+Ra+ax0hx3E3M=
google.golang.org/api v0.8.0/go.mod h1:o4eAsZoiT+ibD93RtjEohWalFOjRDx6CVaqeizhEnKg=
Expand Down
2 changes: 2 additions & 0 deletions poc-cb-net/cmd/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,8 @@ func pingTest(outVal *model.InterHostNetworkStatus, wg *sync.WaitGroup, trialCou
CBLogger.Debug("Start.........")
defer wg.Done()

CBLogger.Tracef("Ping to %s", outVal.DestinationIP)

pinger, err := ping.NewPinger(outVal.DestinationIP)
pinger.SetPrivileged(true)
if err != nil {
Expand Down
108 changes: 66 additions & 42 deletions poc-cb-net/internal/cb-network/cb-network.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,22 @@ import (
"errors"
"fmt"
"io/ioutil"
"log"
"net"
"net/http"
"os"
"os/exec"
"path/filepath"
"strings"
"sync"
"syscall"
"unsafe"

model "github.com/cloud-barista/cb-larva/poc-cb-net/internal/cb-network/model"
"github.com/cloud-barista/cb-larva/poc-cb-net/internal/file"
nethelper "github.com/cloud-barista/cb-larva/poc-cb-net/internal/network-helper"
cblog "github.com/cloud-barista/cb-log"
"github.com/sirupsen/logrus"
"github.com/songgao/water"
"golang.org/x/net/ipv4"
)

Expand Down Expand Up @@ -64,6 +66,12 @@ func init() {
fmt.Println("End......... init() of cb-network.go")
}

type ifReq struct {
Name [0x10]byte
Flags uint16
pad [0x28 - 0x10 - 2]byte
}

// CBNetwork represents a network for the multi-cloud
type CBNetwork struct {
// Variables for the cb-network
Expand All @@ -74,14 +82,14 @@ type CBNetwork struct {
// TBD

// Variables for the cb-network agents
HostID string // HostID in a cloud adaptive network
HostPublicIP string // Inquired public IP of VM/Host
HostPrivateIPv4Networks []string // Inquired private IPv4 networks of VM/Host (e.g. ["192.168.10.4/24", ...])
Interface *water.Interface // Assigned cbnet0 IP from the controller
name string // Name of a network interface, e.g., cbnet0
port int // Port used for tunneling
isInterfaceConfigured bool // Status if a network interface is configured or not
notificationChannel chan bool // Channel to notify the status of a network interface
HostID string // HostID in a cloud adaptive network
HostPublicIP string // Inquired public IP of VM/Host
HostPrivateIPv4Networks []string // Inquired private IPv4 networks of VM/Host (e.g. ["192.168.10.4/24", ...])
Interface *os.File // Assigned cbnet0 IP from the controller
name string // Name of a network interface, e.g., cbnet0
port int // Port used for tunneling
isInterfaceConfigured bool // Status if a network interface is configured or not
notificationChannel chan bool // Channel to notify the status of a network interface

//listenConnection *net.UDPConn // Connection for encapsulation and decapsulation
//NetworkInterfaces []model.NetworkInterface // Deprecated
Expand Down Expand Up @@ -294,34 +302,49 @@ func (cbnetwork *CBNetwork) DecodeAndSetNetworkingRule(value []byte) {
func (cbnetwork *CBNetwork) configureCBNetworkInterface() error {
CBLogger.Debug("Start.........")

// Open TUN device
fd, err := syscall.Open("/dev/net/tun", os.O_RDWR|syscall.O_NONBLOCK, 0)
if err != nil {
log.Fatal(err)
}
fdInt := uintptr(fd)

// Setup a file descriptor
var flags uint16 = syscall.IFF_NO_PI
flags |= syscall.IFF_TUN

// Create an interface
var req ifReq

req.Flags = flags
copy(req.Name[:], cbnetwork.name)

_, _, errno := syscall.Syscall(syscall.SYS_IOCTL, fdInt, uintptr(syscall.TUNSETIFF), uintptr(unsafe.Pointer(&req)))
if errno != 0 {
return err
}

createdIFName := strings.Trim(string(req.Name[:]), "\x00")
CBLogger.Tracef("createdInterfaceName: %s\n", createdIFName)
CBLogger.Info("Interface allocated:", cbnetwork.name)

// Open TUN Interface
tunFd := os.NewFile(fdInt, "tun")
cbnetwork.Interface = tunFd

// Get HostIPv4Network
idx := cbnetwork.NetworkingRules.GetIndexOfPublicIP(cbnetwork.HostPublicIP)
if idx < 0 || idx >= len(cbnetwork.NetworkingRules.HostID) {
return errors.New("index out of range")
}
localNetwork := cbnetwork.NetworkingRules.HostIPv4Network[idx]

// localIP := flag.String("local", localNetwork, "Local tun interface IP/MASK like 192.168.3.3⁄24")
// if *localIP == "" {
// flag.Usage()
// CBLogger.Fatal("local ip is not specified")
// }

iface, err := water.New(water.Config{
DeviceType: water.TUN,
PlatformSpecificParams: water.PlatformSpecificParams{Name: cbnetwork.name},
})
if nil != err {
CBLogger.Fatal("Unable to allocate TUN interface:", err)
}
CBLogger.Info("Interface allocated:", iface.Name())

cbnetwork.Interface = iface
CBLogger.Trace("=== cb-network.HostIPv4Network: ", cbnetwork.Interface)
CBLogger.Trace("=== cb-network.HostIPv4Network: ", localNetwork)

// Set interface parameters
cbnetwork.runIP("link", "set", "dev", cbnetwork.Interface.Name(), "mtu", MTU)
cbnetwork.runIP("addr", "add", localNetwork, "dev", cbnetwork.Interface.Name())
cbnetwork.runIP("link", "set", "dev", cbnetwork.Interface.Name(), "up")
cbnetwork.runIP("link", "set", "dev", cbnetwork.name, "mtu", MTU)
cbnetwork.runIP("addr", "add", localNetwork, "dev", cbnetwork.name)
cbnetwork.runIP("link", "set", "dev", cbnetwork.name, "up")

CBLogger.Debug("End.........")
return nil
Expand Down Expand Up @@ -396,7 +419,7 @@ func (cbnetwork *CBNetwork) runTunneling() {

// Parse header
header, _ := ipv4.ParseHeader(buf[:n])
CBLogger.Tracef("Header received: %+v", header)
CBLogger.Tracef("[Decapsulation] Header: %+v", header)

//fmt.Printf("Received %d bytes from %v: %+v", n, addr, header)

Expand All @@ -419,34 +442,35 @@ func (cbnetwork *CBNetwork) runTunneling() {
// Read packet from HostIPv4Network interface "cbnet0"
//fmt.Println("=== *cbnetwork.HostIPv4Network: ", *cbnetwork.HostIPv4Network)
//fmt.Println("=== cbnetwork.HostIPv4Network: ",cbnetwork.HostIPv4Network)
plen, err := cbnetwork.Interface.Read(packet)
plen, err := cbnetwork.Interface.Read(packet[:])
if err != nil {
CBLogger.Error("Error Read() in encapsulation:", err)
return
}

// Parse header
header, _ := ipv4.ParseHeader(packet[:plen])
CBLogger.Tracef("Sending to remote: %+v (%+v)", header, err)
CBLogger.Tracef("[Encapsulation] Header: %+v", header)

// Search and change destination (Public IP of target VM)
idx := cbnetwork.NetworkingRules.GetIndexOfCBNetIP(header.Dst.String())

var remoteIP string
if idx != -1 {
remoteIP = cbnetwork.NetworkingRules.PublicIPAddress[idx]
}

// Resolve remote addr
remoteAddr, err := net.ResolveUDPAddr("udp", fmt.Sprintf("%s:%v", remoteIP, cbnetwork.port))
if nil != err {
CBLogger.Fatal("Unable to resolve remote addr:", err)
}
// Resolve remote addr
remoteAddr, err := net.ResolveUDPAddr("udp", fmt.Sprintf("%s:%v", remoteIP, cbnetwork.port))
CBLogger.Tracef("Remote Endpoint: %+v", remoteAddr)
if nil != err {
CBLogger.Fatal("Unable to resolve remote addr:", err)
}

// Send packet
nWriteToUDP, errWriteToUDP := lstnConn.WriteToUDP(packet[:plen], remoteAddr)
if errWriteToUDP != nil || nWriteToUDP == 0 {
CBLogger.Errorf("Error(%d len): %s", nWriteToUDP, errWriteToUDP)
// Send packet
nWriteToUDP, errWriteToUDP := lstnConn.WriteToUDP(packet[:plen], remoteAddr)
if errWriteToUDP != nil || nWriteToUDP == 0 {
CBLogger.Errorf("Error(%d len): %s", nWriteToUDP, errWriteToUDP)
}
}
}

Expand Down

0 comments on commit 9970a8e

Please sign in to comment.