From ee7c5793bb61c7f81e5d6b575c6131df38e2b8fa Mon Sep 17 00:00:00 2001 From: Sushant Sharma Date: Mon, 1 Feb 2016 22:05:06 -0800 Subject: [PATCH] MultiCA: Work in progress --- .gitignore | 5 ++ src/core/helper.go | 130 ------------------------------------- src/core/linux_syscalls.go | 4 ++ src/core/logic.go | 114 ++++++++++++++++++++++++++++++++ src/core/utils.go | 27 ++++++++ 5 files changed, 150 insertions(+), 130 deletions(-) delete mode 100644 src/core/helper.go create mode 100644 src/core/linux_syscalls.go create mode 100644 src/core/logic.go create mode 100644 src/core/utils.go diff --git a/.gitignore b/.gitignore index 5550f1bbd..6988142be 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,7 @@ README.md~ src/main/main +bin/ +pkg/ +src/9fans.net/ +src/github.com/ +src/golang.org/ diff --git a/src/core/helper.go b/src/core/helper.go deleted file mode 100644 index 3e30567a6..000000000 --- a/src/core/helper.go +++ /dev/null @@ -1,130 +0,0 @@ -// Copyright Microsoft Corp. -// All rights reserved. - -package azure - -import ( - "net" - "fmt" - "strings" -) - -func printHostInterfaces() { - hostInterfaces, err := net.Interfaces() - if err != nil { - fmt.Println("Azure Driver: Got error while retrieving interfaces") - } else { - fmt.Println("Azure Driver: Found following Interfaces in default name space") - for _, hostInterface := range hostInterfaces { - - addresses, ok := hostInterface.Addrs() - if ok == nil && len(addresses) > 0 { - fmt.Println("\t", hostInterface.Name, hostInterface.Index, hostInterface.Flags, hostInterface.HardwareAddr, hostInterface.Flags, hostInterface.MTU, addresses[0].String()) - } else { - fmt.Println("\t", hostInterface.Name, hostInterface.Index, hostInterface.Flags, hostInterface.HardwareAddr, hostInterface.Flags, hostInterface.MTU) - } - } - } -} - -type interfaceDetails struct { - Address net.IPNet - AddressIPV6 net.IPNet - MacAddress net.HardwareAddr - ID int - SrcName string - DstPrefix string - GatewayIPv4 net.IP -} - -func GetInterfaceToAttach(interfaceNameToAttach string, ipAddressToAttach string)(net.IPNet, net.IPNet, net.HardwareAddr, int, string, string, net.IP, string){ - - printHostInterfaces() - - fmt.Println("Request came for", ipAddressToAttach) - var selectedInterface net.Interface - selected := false - - hostInterfaces, err := net.Interfaces() - if err != nil { - ermsg := "Azure: Got error while retrieving interfaces" - return net.IPNet{}, net.IPNet{}, nil, -1, "", "", net.IP{}, ermsg - } else { - fmt.Println("Azure: Going to select an interface for container") - for _, hostInterface := range hostInterfaces { - - addresses, ok := hostInterface.Addrs() - flag := hostInterface.Flags & net.FlagBroadcast - loopbackFlag := hostInterface.Flags & net.FlagLoopback - canBeSelected := ok == nil && - // interface is configured with some ip address - len(addresses) > 0 && - // interface supports broadcast access capability - flag == net.FlagBroadcast && - // interface is not a loopback interface - loopbackFlag != net.FlagLoopback // && - //strings.Contains(hostInterface.Name, "veth") - - if(ipAddressToAttach == ""){ - if canBeSelected && interfaceNameToAttach != "" { - isThisSameAsRequested := hostInterface.Name == interfaceNameToAttach - canBeSelected = canBeSelected && isThisSameAsRequested - } - if canBeSelected { - selectedInterface = hostInterface - selected = true - } - }else{ - if canBeSelected { - doesThisInterfaceHaveSameIPAsRequested := false - addrs, _ := hostInterface.Addrs() - for _, addr := range addrs{ - address := addr.String() - if(strings.Split(address, "/")[0] == ipAddressToAttach){ - doesThisInterfaceHaveSameIPAsRequested = true - break - } - } - canBeSelected = canBeSelected && doesThisInterfaceHaveSameIPAsRequested - } - if canBeSelected { - selectedInterface = hostInterface - selected = true - } - } - } - } - - if !selected { - ermsg := "Azure: Interface Not Found Error. " + - "It is possible that none of the interfaces is configured properly, " + - "or none of configured interfaces match the selection criteria." - return net.IPNet{}, net.IPNet{}, nil, -1, "", "", net.IP{}, ermsg - } else { - fmt.Println("Selected interface: ", selectedInterface.Name) - } - - addresses, _ := selectedInterface.Addrs() - address := addresses[0].String() - ipv4, ipv4Net, _ := net.ParseCIDR(address) - ipv4Net.IP = ipv4 - bytes := strings.Split(address, ".") - gateway := bytes[0] + "." + bytes[1] + "." + bytes[2] + ".1" - gatewayIpv4 := net.ParseIP(gateway) - srcName := selectedInterface.Name - macAddress, _ := net.ParseMAC(selectedInterface.HardwareAddr.String()) - - fmt.Println("Azure: Interface ip/netmask: ", - ipv4Net.IP.String(), "/", ipv4Net.Mask.String()) - fmt.Println("Azure: Gateway IP: ", gatewayIpv4.String()) - - retval := &interfaceDetails{ - Address: *ipv4Net, - MacAddress: macAddress, - SrcName: srcName, - DstPrefix: srcName + "eth", - GatewayIPv4: gatewayIpv4, - } - fmt.Println("Azure: Successfully selected interface ", retval) - return *ipv4Net, net.IPNet{}, macAddress, -1, srcName, srcName + "eth", gatewayIpv4, "" -} diff --git a/src/core/linux_syscalls.go b/src/core/linux_syscalls.go new file mode 100644 index 000000000..212221683 --- /dev/null +++ b/src/core/linux_syscalls.go @@ -0,0 +1,4 @@ +// Copyright Microsoft Corp. +// All rights reserved. + +package azure diff --git a/src/core/logic.go b/src/core/logic.go new file mode 100644 index 000000000..222075815 --- /dev/null +++ b/src/core/logic.go @@ -0,0 +1,114 @@ +// Copyright Microsoft Corp. +// All rights reserved. + +package azure + +import ( + "fmt" + "net" + "strings" +) + +type interfaceDetails struct { + Address net.IPNet + AddressIPV6 net.IPNet + MacAddress net.HardwareAddr + ID int + SrcName string + DstPrefix string + GatewayIPv4 net.IP +} + +// GetInterfaceToAttach is a function that contains the logic to create/select +// the interface that will be attached to the container +func GetInterfaceToAttach(interfaceNameToAttach string, ipAddressToAttach string) (net.IPNet, net.IPNet, net.HardwareAddr, int, string, string, net.IP, string) { + + printHostInterfaces() + + fmt.Println("Request came for", ipAddressToAttach) + var selectedInterface net.Interface + selected := false + + hostInterfaces, err := net.Interfaces() + if err != nil { + ermsg := "Azure: Got error while retrieving interfaces" + return net.IPNet{}, net.IPNet{}, nil, -1, "", "", net.IP{}, ermsg + } + + fmt.Println("Azure: Going to select an interface for container") + for _, hostInterface := range hostInterfaces { + + addresses, ok := hostInterface.Addrs() + flag := hostInterface.Flags & net.FlagBroadcast + loopbackFlag := hostInterface.Flags & net.FlagLoopback + canBeSelected := ok == nil && + // interface is configured with some ip address + len(addresses) > 0 && + // interface supports broadcast access capability + flag == net.FlagBroadcast && + // interface is not a loopback interface + loopbackFlag != net.FlagLoopback // && + //strings.Contains(hostInterface.Name, "veth") + + if ipAddressToAttach == "" { + if canBeSelected && interfaceNameToAttach != "" { + isThisSameAsRequested := hostInterface.Name == interfaceNameToAttach + canBeSelected = canBeSelected && isThisSameAsRequested + } + if canBeSelected { + selectedInterface = hostInterface + selected = true + } + } else { + if canBeSelected { + doesThisInterfaceHaveSameIPAsRequested := false + addrs, _ := hostInterface.Addrs() + for _, addr := range addrs { + address := addr.String() + if strings.Split(address, "/")[0] == ipAddressToAttach { + doesThisInterfaceHaveSameIPAsRequested = true + break + } + } + canBeSelected = canBeSelected && doesThisInterfaceHaveSameIPAsRequested + } + if canBeSelected { + selectedInterface = hostInterface + selected = true + } + } + } + + if !selected { + ermsg := "Azure: Interface Not Found Error. " + + "It is possible that none of the interfaces is configured properly, " + + "or none of configured interfaces match the selection criteria." + return net.IPNet{}, net.IPNet{}, nil, -1, "", "", net.IP{}, ermsg + } + + fmt.Println("Selected interface: ", selectedInterface.Name) + + addresses, _ := selectedInterface.Addrs() + address := addresses[0].String() + ipv4, ipv4Net, _ := net.ParseCIDR(address) + ipv4Net.IP = ipv4 + bytes := strings.Split(address, ".") + gateway := bytes[0] + "." + bytes[1] + "." + bytes[2] + ".1" + gatewayIpv4 := net.ParseIP(gateway) + srcName := selectedInterface.Name + macAddress, _ := net.ParseMAC(selectedInterface.HardwareAddr.String()) + + fmt.Println("Azure: Interface ip/netmask: ", + ipv4Net.IP.String(), "/", ipv4Net.Mask.String()) + fmt.Println("Azure: Gateway IP: ", gatewayIpv4.String()) + + retval := &interfaceDetails{ + Address: *ipv4Net, + MacAddress: macAddress, + SrcName: srcName, + DstPrefix: srcName + "eth", + GatewayIPv4: gatewayIpv4, + } + fmt.Println("Azure: Successfully selected interface ", retval) + return *ipv4Net, net.IPNet{}, macAddress, -1, srcName, srcName + "eth", gatewayIpv4, "" +} diff --git a/src/core/utils.go b/src/core/utils.go new file mode 100644 index 000000000..96305eacd --- /dev/null +++ b/src/core/utils.go @@ -0,0 +1,27 @@ +// Copyright Microsoft Corp. +// All rights reserved. + +package azure + +import ( + "fmt" + "net" +) + +func printHostInterfaces() { + hostInterfaces, err := net.Interfaces() + if err != nil { + fmt.Println("Azure Driver: Got error while retrieving interfaces") + } else { + fmt.Println("Azure Driver: Found following Interfaces in default name space") + for _, hostInterface := range hostInterfaces { + + addresses, ok := hostInterface.Addrs() + if ok == nil && len(addresses) > 0 { + fmt.Println("\t", hostInterface.Name, hostInterface.Index, hostInterface.Flags, hostInterface.HardwareAddr, hostInterface.Flags, hostInterface.MTU, addresses[0].String()) + } else { + fmt.Println("\t", hostInterface.Name, hostInterface.Index, hostInterface.Flags, hostInterface.HardwareAddr, hostInterface.Flags, hostInterface.MTU) + } + } + } +}