proxy: request a vsock connection rather than a TCP/IP one

Note this means we don't need to figure out our local IP address.

Signed-off-by: David Scott <dave.scott@docker.com>
This commit is contained in:
David Scott 2016-04-14 16:27:59 +01:00
parent 403ce4e696
commit e4f405be26
3 changed files with 11 additions and 34 deletions

View File

@ -8,6 +8,7 @@ import (
"os"
"libproxy"
"strings"
"vsock"
)
func main() {
@ -42,12 +43,7 @@ func exposePort(host net.Addr, port int) error {
log.Printf("Failed to open /port/%s/ctl: %#v\n", name, err)
return err
}
me, err := getMyAddress()
if err != nil {
log.Printf("Failed to determine my local address: %#v\n", err)
return err
}
_, err = ctl.WriteString(fmt.Sprintf("%s:%s:%d", name, me, port))
_, err = ctl.WriteString(fmt.Sprintf("%s:%d:%d", name, vsock.VSOCK_CID_SELF, vSockPortOffset + port))
if err != nil {
log.Printf("Failed to open /port/%s/ctl: %#v\n", name, err)
return err
@ -84,28 +80,3 @@ func unexposePort(host net.Addr) {
log.Printf("Failed to remove /port/%s: %#v\n", name, err)
}
}
var myAddress string
// getMyAddress returns a string representing my address from the host's
// point of view. For now this is an IP address but it soon should be a vsock
// port.
func getMyAddress() (string, error) {
if myAddress != "" {
return myAddress, nil
}
d, err := os.Open("/port/docker")
if err != nil {
return "", err
}
defer d.Close()
bytes := make([]byte, 100)
count, err := d.Read(bytes)
if err != nil {
return "", err
}
s := string(bytes)[0:count]
bits := strings.Split(s, ":")
myAddress = bits[2]
return myAddress, nil
}

View File

@ -10,6 +10,7 @@ import (
"syscall"
"libproxy"
"vsock"
)
// sendError signals the error to the parent and quits the process.
@ -28,6 +29,9 @@ func sendOK() {
f.Close()
}
// Map dynamic ports onto vsock ports over this offset
var vSockPortOffset = 0x10000
// From docker/libnetwork/portmapper/proxy.go:
// parseHostContainerAddrs parses the flags passed on reexec to create the TCP or UDP
@ -45,7 +49,8 @@ func parseHostContainerAddrs() (host net.Addr, port int, container net.Addr) {
switch *proto {
case "tcp":
host = &net.TCPAddr{IP: net.ParseIP(*hostIP), Port: *hostPort}
port = vSockPortOffset + *hostPort
host = &vsock.VsockAddr{Port: uint(port)}
port = *hostPort
container = &net.TCPAddr{IP: net.ParseIP(*containerIP), Port: *containerPort}
case "udp":

View File

@ -26,8 +26,9 @@ int accept_vm(int fd, struct sockaddr_vm *sa_vm, socklen_t *sa_vm_len) {
import "C"
const (
AF_VSOCK = 40
VSOCK_CID_ANY = 4294967295 /* 2^32-1 */
AF_VSOCK = 40
VSOCK_CID_ANY = 4294967295 /* 2^32-1 */
VSOCK_CID_SELF = 3
)
// Listen returns a net.Listener which can accept connections on the given