2019-01-12 04:58:27 +00:00
package hcn
import (
"encoding/json"
"fmt"
2020-08-10 17:43:49 +00:00
"math"
2019-01-12 04:58:27 +00:00
"github.com/Microsoft/hcsshim/internal/hcserror"
"github.com/Microsoft/hcsshim/internal/interop"
"github.com/sirupsen/logrus"
)
// Globals are all global properties of the HCN Service.
type Globals struct {
Version Version ` json:"Version" `
}
// Version is the HCN Service version.
type Version struct {
Major int ` json:"Major" `
Minor int ` json:"Minor" `
}
2020-08-10 17:43:49 +00:00
type VersionRange struct {
MinVersion Version
MaxVersion Version
}
type VersionRanges [ ] VersionRange
2019-01-12 04:58:27 +00:00
var (
// HNSVersion1803 added ACL functionality.
2020-08-10 17:43:49 +00:00
HNSVersion1803 = VersionRanges { VersionRange { MinVersion : Version { Major : 7 , Minor : 2 } , MaxVersion : Version { Major : math . MaxInt32 , Minor : math . MaxInt32 } } }
2019-01-12 04:58:27 +00:00
// V2ApiSupport allows the use of V2 Api calls and V2 Schema.
2020-08-10 17:43:49 +00:00
V2ApiSupport = VersionRanges { VersionRange { MinVersion : Version { Major : 9 , Minor : 2 } , MaxVersion : Version { Major : math . MaxInt32 , Minor : math . MaxInt32 } } }
2019-07-10 00:29:38 +00:00
// Remote Subnet allows for Remote Subnet policies on Overlay networks
2020-08-10 17:43:49 +00:00
RemoteSubnetVersion = VersionRanges { VersionRange { MinVersion : Version { Major : 9 , Minor : 2 } , MaxVersion : Version { Major : math . MaxInt32 , Minor : math . MaxInt32 } } }
2019-07-10 00:29:38 +00:00
// A Host Route policy allows for local container to local host communication Overlay networks
2020-08-10 17:43:49 +00:00
HostRouteVersion = VersionRanges { VersionRange { MinVersion : Version { Major : 9 , Minor : 2 } , MaxVersion : Version { Major : math . MaxInt32 , Minor : math . MaxInt32 } } }
2021-06-10 19:27:00 +00:00
// HNS 9.3 through 10.0 (not included), and 10.2+ allows for Direct Server Return for loadbalancing
DSRVersion = VersionRanges {
VersionRange { MinVersion : Version { Major : 9 , Minor : 3 } , MaxVersion : Version { Major : 9 , Minor : math . MaxInt32 } } ,
VersionRange { MinVersion : Version { Major : 10 , Minor : 2 } , MaxVersion : Version { Major : math . MaxInt32 , Minor : math . MaxInt32 } } ,
}
2020-08-10 17:43:49 +00:00
// HNS 9.3 through 10.0 (not included) and, 10.4+ provide support for configuring endpoints with /32 prefixes
Slash32EndpointPrefixesVersion = VersionRanges {
VersionRange { MinVersion : Version { Major : 9 , Minor : 3 } , MaxVersion : Version { Major : 9 , Minor : math . MaxInt32 } } ,
VersionRange { MinVersion : Version { Major : 10 , Minor : 4 } , MaxVersion : Version { Major : math . MaxInt32 , Minor : math . MaxInt32 } } ,
}
// HNS 9.3 through 10.0 (not included) and, 10.4+ allow for HNS ACL Policies to support protocol 252 for VXLAN
AclSupportForProtocol252Version = VersionRanges {
2021-06-10 19:27:00 +00:00
VersionRange { MinVersion : Version { Major : 11 , Minor : 0 } , MaxVersion : Version { Major : math . MaxInt32 , Minor : math . MaxInt32 } } ,
2020-08-10 17:43:49 +00:00
}
// HNS 12.0 allows for session affinity for loadbalancing
SessionAffinityVersion = VersionRanges { VersionRange { MinVersion : Version { Major : 12 , Minor : 0 } , MaxVersion : Version { Major : math . MaxInt32 , Minor : math . MaxInt32 } } }
2021-07-20 17:59:04 +00:00
// HNS 11.10+ supports Ipv6 dual stack.
2020-08-10 17:43:49 +00:00
IPv6DualStackVersion = VersionRanges {
2021-07-20 17:59:04 +00:00
VersionRange { MinVersion : Version { Major : 11 , Minor : 10 } , MaxVersion : Version { Major : math . MaxInt32 , Minor : math . MaxInt32 } } ,
2020-08-10 17:43:49 +00:00
}
2021-06-10 19:27:00 +00:00
// HNS 13.0 allows for Set Policy support
SetPolicyVersion = VersionRanges { VersionRange { MinVersion : Version { Major : 13 , Minor : 0 } , MaxVersion : Version { Major : math . MaxInt32 , Minor : math . MaxInt32 } } }
// HNS 10.3 allows for VXLAN ports
VxlanPortVersion = VersionRanges { VersionRange { MinVersion : Version { Major : 10 , Minor : 3 } , MaxVersion : Version { Major : math . MaxInt32 , Minor : math . MaxInt32 } } }
2021-07-20 17:59:04 +00:00
//HNS 9.5 through 10.0(not included), 10.5 through 11.0(not included), 11.11 through 12.0(not included), 12.1 through 13.0(not included), 13.1+ allows for Network L4Proxy Policy support
L4ProxyPolicyVersion = VersionRanges {
VersionRange { MinVersion : Version { Major : 9 , Minor : 5 } , MaxVersion : Version { Major : 9 , Minor : math . MaxInt32 } } ,
VersionRange { MinVersion : Version { Major : 10 , Minor : 5 } , MaxVersion : Version { Major : 10 , Minor : math . MaxInt32 } } ,
VersionRange { MinVersion : Version { Major : 11 , Minor : 11 } , MaxVersion : Version { Major : 11 , Minor : math . MaxInt32 } } ,
VersionRange { MinVersion : Version { Major : 12 , Minor : 1 } , MaxVersion : Version { Major : 12 , Minor : math . MaxInt32 } } ,
VersionRange { MinVersion : Version { Major : 13 , Minor : 1 } , MaxVersion : Version { Major : math . MaxInt32 , Minor : math . MaxInt32 } } ,
}
2021-06-10 19:27:00 +00:00
//HNS 13.2 allows for L4WfpProxy Policy support
L4WfpProxyPolicyVersion = VersionRanges { VersionRange { MinVersion : Version { Major : 13 , Minor : 2 } , MaxVersion : Version { Major : math . MaxInt32 , Minor : math . MaxInt32 } } }
2021-07-20 17:59:04 +00:00
//HNS 14.0 allows for TierAcl Policy support
TierAclPolicyVersion = VersionRanges { VersionRange { MinVersion : Version { Major : 14 , Minor : 0 } , MaxVersion : Version { Major : math . MaxInt32 , Minor : math . MaxInt32 } } }
2019-01-12 04:58:27 +00:00
)
// GetGlobals returns the global properties of the HCN Service.
func GetGlobals ( ) ( * Globals , error ) {
var version Version
err := hnsCall ( "GET" , "/globals/version" , "" , & version )
if err != nil {
return nil , err
}
globals := & Globals {
Version : version ,
}
return globals , nil
}
type hnsResponse struct {
Success bool
Error string
Output json . RawMessage
}
func hnsCall ( method , path , request string , returnResponse interface { } ) error {
var responseBuffer * uint16
logrus . Debugf ( "[%s]=>[%s] Request : %s" , method , path , request )
err := _hnsCall ( method , path , request , & responseBuffer )
if err != nil {
return hcserror . New ( err , "hnsCall " , "" )
}
response := interop . ConvertAndFreeCoTaskMemString ( responseBuffer )
hnsresponse := & hnsResponse { }
if err = json . Unmarshal ( [ ] byte ( response ) , & hnsresponse ) ; err != nil {
return err
}
if ! hnsresponse . Success {
return fmt . Errorf ( "HNS failed with error : %s" , hnsresponse . Error )
}
if len ( hnsresponse . Output ) == 0 {
return nil
}
logrus . Debugf ( "Network Response : %s" , hnsresponse . Output )
err = json . Unmarshal ( hnsresponse . Output , returnResponse )
if err != nil {
return err
}
return nil
}