mirror of
https://github.com/linuxkit/linuxkit.git
synced 2025-07-23 19:05:37 +00:00
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:
parent
403ce4e696
commit
e4f405be26
@ -8,6 +8,7 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"libproxy"
|
"libproxy"
|
||||||
"strings"
|
"strings"
|
||||||
|
"vsock"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
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)
|
log.Printf("Failed to open /port/%s/ctl: %#v\n", name, err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
me, err := getMyAddress()
|
_, err = ctl.WriteString(fmt.Sprintf("%s:%d:%d", name, vsock.VSOCK_CID_SELF, vSockPortOffset + port))
|
||||||
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))
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Failed to open /port/%s/ctl: %#v\n", name, err)
|
log.Printf("Failed to open /port/%s/ctl: %#v\n", name, err)
|
||||||
return err
|
return err
|
||||||
@ -84,28 +80,3 @@ func unexposePort(host net.Addr) {
|
|||||||
log.Printf("Failed to remove /port/%s: %#v\n", name, err)
|
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
|
|
||||||
}
|
|
||||||
|
@ -10,6 +10,7 @@ import (
|
|||||||
"syscall"
|
"syscall"
|
||||||
|
|
||||||
"libproxy"
|
"libproxy"
|
||||||
|
"vsock"
|
||||||
)
|
)
|
||||||
|
|
||||||
// sendError signals the error to the parent and quits the process.
|
// sendError signals the error to the parent and quits the process.
|
||||||
@ -28,6 +29,9 @@ func sendOK() {
|
|||||||
f.Close()
|
f.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Map dynamic ports onto vsock ports over this offset
|
||||||
|
var vSockPortOffset = 0x10000
|
||||||
|
|
||||||
// From docker/libnetwork/portmapper/proxy.go:
|
// From docker/libnetwork/portmapper/proxy.go:
|
||||||
|
|
||||||
// parseHostContainerAddrs parses the flags passed on reexec to create the TCP or UDP
|
// 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 {
|
switch *proto {
|
||||||
case "tcp":
|
case "tcp":
|
||||||
host = &net.TCPAddr{IP: net.ParseIP(*hostIP), Port: *hostPort}
|
port = vSockPortOffset + *hostPort
|
||||||
|
host = &vsock.VsockAddr{Port: uint(port)}
|
||||||
port = *hostPort
|
port = *hostPort
|
||||||
container = &net.TCPAddr{IP: net.ParseIP(*containerIP), Port: *containerPort}
|
container = &net.TCPAddr{IP: net.ParseIP(*containerIP), Port: *containerPort}
|
||||||
case "udp":
|
case "udp":
|
||||||
|
5
alpine/packages/proxy/vendor/vsock/vsock.go
vendored
5
alpine/packages/proxy/vendor/vsock/vsock.go
vendored
@ -26,8 +26,9 @@ int accept_vm(int fd, struct sockaddr_vm *sa_vm, socklen_t *sa_vm_len) {
|
|||||||
import "C"
|
import "C"
|
||||||
|
|
||||||
const (
|
const (
|
||||||
AF_VSOCK = 40
|
AF_VSOCK = 40
|
||||||
VSOCK_CID_ANY = 4294967295 /* 2^32-1 */
|
VSOCK_CID_ANY = 4294967295 /* 2^32-1 */
|
||||||
|
VSOCK_CID_SELF = 3
|
||||||
)
|
)
|
||||||
|
|
||||||
// Listen returns a net.Listener which can accept connections on the given
|
// Listen returns a net.Listener which can accept connections on the given
|
||||||
|
Loading…
Reference in New Issue
Block a user