From ba0eadda66d107fc7b22bbee96251c2ab6b52f7a Mon Sep 17 00:00:00 2001 From: Debjit Date: Wed, 29 May 2024 11:28:51 -0700 Subject: [PATCH 1/3] OutBoundNATPolicy Schema changes (#2106) Signed-off-by: Debjit Mondal Signed-off-by: Prince Pereira --- hcn/hcnpolicy.go | 9 +++++---- internal/hns/hnspolicy.go | 7 ++++--- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/hcn/hcnpolicy.go b/hcn/hcnpolicy.go index c2aa599f33..aae72b559a 100644 --- a/hcn/hcnpolicy.go +++ b/hcn/hcnpolicy.go @@ -142,10 +142,11 @@ type QosPolicySetting struct { // OutboundNatPolicySetting sets outbound Network Address Translation on an Endpoint. type OutboundNatPolicySetting struct { - VirtualIP string `json:",omitempty"` - Exceptions []string `json:",omitempty"` - Destinations []string `json:",omitempty"` - Flags NatFlags `json:",omitempty"` + VirtualIP string `json:",omitempty"` + Exceptions []string `json:",omitempty"` + Destinations []string `json:",omitempty"` + Flags NatFlags `json:",omitempty"` + MaxPortPoolUsage uint16 `json:",omitempty"` } // SDNRoutePolicySetting sets SDN Route on an Endpoint. diff --git a/internal/hns/hnspolicy.go b/internal/hns/hnspolicy.go index 84b3682184..e1c1d5287b 100644 --- a/internal/hns/hnspolicy.go +++ b/internal/hns/hnspolicy.go @@ -57,9 +57,10 @@ type PaPolicy struct { type OutboundNatPolicy struct { Policy - VIP string `json:"VIP,omitempty"` - Exceptions []string `json:"ExceptionList,omitempty"` - Destinations []string `json:",omitempty"` + VIP string `json:"VIP,omitempty"` + Exceptions []string `json:"ExceptionList,omitempty"` + Destinations []string `json:",omitempty"` + MaxPortPoolUsage uint16 `json:",omitempty"` } type ProxyPolicy struct { From a29b4b038369aaf647c6323df5874f7a9d7e3502 Mon Sep 17 00:00:00 2001 From: PRINCE PEREIRA Date: Tue, 4 Jun 2024 21:22:05 +0530 Subject: [PATCH 2/3] Changes for checking the global version for modify policy version support. (#2139) Signed-off-by: Prince Pereira --- hcn/hcn.go | 12 ++++++++++++ hcn/hcnerrors.go | 6 ++++++ hcn/hcnglobals.go | 5 +++++ hcn/hcnsupport.go | 4 ++++ 4 files changed, 27 insertions(+) diff --git a/hcn/hcn.go b/hcn/hcn.go index df3a59a78c..b6ecd75981 100644 --- a/hcn/hcn.go +++ b/hcn/hcn.go @@ -264,6 +264,18 @@ func SetPolicySupported() error { return platformDoesNotSupportError("SetPolicy") } +// ModifyLoadbalancerSupported returns an error if the HCN version does not support ModifyLoadbalancer. +func ModifyLoadbalancerSupported() error { + supported, err := GetCachedSupportedFeatures() + if err != nil { + return err + } + if supported.ModifyLoadbalancer { + return nil + } + return platformDoesNotSupportError("ModifyLoadbalancer") +} + // VxlanPortSupported returns an error if the HCN version does not support configuring the VXLAN TCP port. func VxlanPortSupported() error { supported, err := GetCachedSupportedFeatures() diff --git a/hcn/hcnerrors.go b/hcn/hcnerrors.go index ad30d320d9..c8cd009749 100644 --- a/hcn/hcnerrors.go +++ b/hcn/hcnerrors.go @@ -10,6 +10,7 @@ import ( "github.com/Microsoft/hcsshim/internal/hcserror" "github.com/Microsoft/hcsshim/internal/interop" "github.com/sirupsen/logrus" + "golang.org/x/sys/windows" ) var ( @@ -50,6 +51,7 @@ type ErrorCode uint32 const ( ERROR_NOT_FOUND = 0x490 HCN_E_PORT_ALREADY_EXISTS ErrorCode = 0x803b0013 + HCN_E_NOTIMPL ErrorCode = ErrorCode(windows.E_NOTIMPL) ) type HcnError struct { @@ -77,6 +79,10 @@ func IsPortAlreadyExistsError(err error) bool { return CheckErrorWithCode(err, HCN_E_PORT_ALREADY_EXISTS) } +func IsNotImplemented(err error) bool { + return CheckErrorWithCode(err, HCN_E_NOTIMPL) +} + func new(hr error, title string, rest string) error { err := &HcnError{} hcsError := hcserror.New(hr, title, rest) diff --git a/hcn/hcnglobals.go b/hcn/hcnglobals.go index 14903bc5e9..c0e9bc63bc 100644 --- a/hcn/hcnglobals.go +++ b/hcn/hcnglobals.go @@ -82,6 +82,11 @@ var ( //HNS 15.0 allows for NestedIpSet support NestedIpSetVersion = VersionRanges{VersionRange{MinVersion: Version{Major: 15, Minor: 0}, MaxVersion: Version{Major: math.MaxInt32, Minor: math.MaxInt32}}} + + //HNS 15.1 allows support for DisableHostPort flag. + DisableHostPortVersion = VersionRanges{VersionRange{MinVersion: Version{Major: 15, Minor: 1}, MaxVersion: Version{Major: math.MaxInt32, Minor: math.MaxInt32}}} + // HNS 15.4 allows for Modify Loadbalancer support + ModifyLoadbalancerVersion = VersionRanges{VersionRange{MinVersion: Version{Major: 15, Minor: 4}, MaxVersion: Version{Major: math.MaxInt32, Minor: math.MaxInt32}}} ) // GetGlobals returns the global properties of the HCN Service. diff --git a/hcn/hcnsupport.go b/hcn/hcnsupport.go index bacb91feda..c34d89dbec 100644 --- a/hcn/hcnsupport.go +++ b/hcn/hcnsupport.go @@ -34,6 +34,8 @@ type SupportedFeatures struct { TierAcl bool `json:"TierAcl"` NetworkACL bool `json:"NetworkACL"` NestedIpSet bool `json:"NestedIpSet"` + DisableHostPort bool `json:"DisableHostPort"` + ModifyLoadbalancer bool `json:"ModifyLoadbalancer"` } // AclFeatures are the supported ACL possibilities. @@ -111,6 +113,8 @@ func getSupportedFeatures() (SupportedFeatures, error) { features.TierAcl = isFeatureSupported(globals.Version, TierAclPolicyVersion) features.NetworkACL = isFeatureSupported(globals.Version, NetworkACLPolicyVersion) features.NestedIpSet = isFeatureSupported(globals.Version, NestedIpSetVersion) + features.DisableHostPort = isFeatureSupported(globals.Version, DisableHostPortVersion) + features.ModifyLoadbalancer = isFeatureSupported(globals.Version, ModifyLoadbalancerVersion) logrus.WithFields(logrus.Fields{ "version": fmt.Sprintf("%+v", globals.Version), From a55c80d239b6d43e28b1062cc7233d1657af4dfd Mon Sep 17 00:00:00 2001 From: PRINCE PEREIRA Date: Mon, 15 Apr 2024 08:26:12 -0700 Subject: [PATCH 3/3] Adding support for loadbalancer policy update in hns. (#2085) Signed-off-by: Prince Pereira --- hcn/hcnloadbalancer.go | 64 +++++++++++++++++++++++++++++++++++-- hcn/hcnloadbalancer_test.go | 64 +++++++++++++++++++++++++++++++++++++ hcn/hcnutils_test.go | 17 ++++++++++ 3 files changed, 143 insertions(+), 2 deletions(-) diff --git a/hcn/hcnloadbalancer.go b/hcn/hcnloadbalancer.go index 1b434b07b3..46f1104544 100644 --- a/hcn/hcnloadbalancer.go +++ b/hcn/hcnloadbalancer.go @@ -28,14 +28,14 @@ type HostComputeLoadBalancer struct { Flags LoadBalancerFlags `json:",omitempty"` // 0: None, 1: EnableDirectServerReturn } -//LoadBalancerFlags modify settings for a loadbalancer. +// LoadBalancerFlags modify settings for a loadbalancer. type LoadBalancerFlags uint32 var ( // LoadBalancerFlagsNone is the default. LoadBalancerFlagsNone LoadBalancerFlags = 0 // LoadBalancerFlagsDSR enables Direct Server Return (DSR) - LoadBalancerFlagsDSR LoadBalancerFlags = 1 + LoadBalancerFlagsDSR LoadBalancerFlags = 1 LoadBalancerFlagsIPv6 LoadBalancerFlags = 2 ) @@ -161,6 +161,49 @@ func createLoadBalancer(settings string) (*HostComputeLoadBalancer, error) { return &outputLoadBalancer, nil } +func updateLoadBalancer(loadbalancerId string, settings string) (*HostComputeLoadBalancer, error) { + loadBalancerGuid, err := guid.FromString(loadbalancerId) + if err != nil { + return nil, errInvalidLoadBalancerID + } + // Update loadBalancer. + var ( + loadBalancerHandle hcnLoadBalancer + resultBuffer *uint16 + propertiesBuffer *uint16 + ) + hr := hcnOpenLoadBalancer(&loadBalancerGuid, &loadBalancerHandle, &resultBuffer) + if err := checkForErrors("hcnOpenLoadBalancer", hr, resultBuffer); err != nil { + return nil, err + } + hr = hcnModifyLoadBalancer(loadBalancerHandle, settings, &resultBuffer) + if err := checkForErrors("hcnModifyLoadBalancer", hr, resultBuffer); err != nil { + return nil, err + } + // Query loadBalancer. + hcnQuery := defaultQuery() + query, err := json.Marshal(hcnQuery) + if err != nil { + return nil, err + } + hr = hcnQueryLoadBalancerProperties(loadBalancerHandle, string(query), &propertiesBuffer, &resultBuffer) + if err := checkForErrors("hcnQueryLoadBalancerProperties", hr, resultBuffer); err != nil { + return nil, err + } + properties := interop.ConvertAndFreeCoTaskMemString(propertiesBuffer) + // Close loadBalancer. + hr = hcnCloseLoadBalancer(loadBalancerHandle) + if err := checkForErrors("hcnCloseLoadBalancer", hr, nil); err != nil { + return nil, err + } + // Convert output to HostComputeLoadBalancer + var outputLoadBalancer HostComputeLoadBalancer + if err := json.Unmarshal([]byte(properties), &outputLoadBalancer); err != nil { + return nil, err + } + return &outputLoadBalancer, nil +} + func deleteLoadBalancer(loadBalancerId string) error { loadBalancerGuid, err := guid.FromString(loadBalancerId) if err != nil { @@ -235,6 +278,23 @@ func (loadBalancer *HostComputeLoadBalancer) Create() (*HostComputeLoadBalancer, return loadBalancer, nil } +// Update Loadbalancer. +func (loadBalancer *HostComputeLoadBalancer) Update(hnsLoadbalancerID string) (*HostComputeLoadBalancer, error) { + logrus.Debugf("hcn::HostComputeLoadBalancer::Create id=%s", hnsLoadbalancerID) + + jsonString, err := json.Marshal(loadBalancer) + if err != nil { + return nil, err + } + + logrus.Debugf("hcn::HostComputeLoadBalancer::Update JSON: %s", jsonString) + loadBalancer, hcnErr := updateLoadBalancer(hnsLoadbalancerID, string(jsonString)) + if hcnErr != nil { + return nil, hcnErr + } + return loadBalancer, nil +} + // Delete LoadBalancer. func (loadBalancer *HostComputeLoadBalancer) Delete() error { logrus.Debugf("hcn::HostComputeLoadBalancer::Delete id=%s", loadBalancer.Id) diff --git a/hcn/hcnloadbalancer_test.go b/hcn/hcnloadbalancer_test.go index c4c181c9be..9924c14b77 100644 --- a/hcn/hcnloadbalancer_test.go +++ b/hcn/hcnloadbalancer_test.go @@ -41,6 +41,70 @@ func TestCreateDeleteLoadBalancer(t *testing.T) { } } +func TestCreateUpdateDeleteLoadBalancer(t *testing.T) { + network, err := CreateTestOverlayNetwork() + if err != nil { + t.Fatal(err) + } + endpoint, err := HcnCreateTestEndpoint(network) + if err != nil { + t.Fatal(err) + } + loadBalancer, err := HcnCreateTestLoadBalancer(endpoint) + if err != nil { + t.Fatal(err) + } + jsonString, err := json.Marshal(loadBalancer) + if err != nil { + t.Fatal(err) + } + fmt.Printf("LoadBalancer JSON:\n%s \n", jsonString) + + secondEndpoint, err := HcnCreateTestEndpoint(network) + if err != nil { + t.Fatal(err) + } + + HcnLoadBalancerTestAddBackend(loadBalancer, secondEndpoint.Id) + + loadBalancer, err = loadBalancer.Update(loadBalancer.Id) + if err != nil { + t.Fatal(err) + } + + if len(loadBalancer.HostComputeEndpoints) != 2 { + t.Fatalf("Update loadBalancer with backend add failed") + } + + HcnLoadBalancerTestRemoveBackend(loadBalancer, secondEndpoint.Id) + + loadBalancer, err = loadBalancer.Update(loadBalancer.Id) + if err != nil { + t.Fatal(err) + } + + if len(loadBalancer.HostComputeEndpoints) != 1 { + t.Fatalf("Update loadBalancer with backend remove failed") + } + + err = loadBalancer.Delete() + if err != nil { + t.Fatal(err) + } + err = secondEndpoint.Delete() + if err != nil { + t.Fatal(err) + } + err = endpoint.Delete() + if err != nil { + t.Fatal(err) + } + err = network.Delete() + if err != nil { + t.Fatal(err) + } +} + func TestGetLoadBalancerById(t *testing.T) { network, err := CreateTestOverlayNetwork() if err != nil { diff --git a/hcn/hcnutils_test.go b/hcn/hcnutils_test.go index 21aff41d99..8c09493949 100644 --- a/hcn/hcnutils_test.go +++ b/hcn/hcnutils_test.go @@ -319,6 +319,23 @@ func HcnCreateTestLoadBalancer(endpoint *HostComputeEndpoint) (*HostComputeLoadB return loadBalancer.Create() } +func HcnLoadBalancerTestAddBackend(loadBalancer *HostComputeLoadBalancer, endpointId string) { + endpointIds := loadBalancer.HostComputeEndpoints + endpointIds = append(endpointIds, endpointId) + loadBalancer.HostComputeEndpoints = endpointIds +} + +func HcnLoadBalancerTestRemoveBackend(loadBalancer *HostComputeLoadBalancer, endpointId string) { + endpointIds := loadBalancer.HostComputeEndpoints + for i, v := range endpointIds { + if v == endpointId { + endpointIds = append(endpointIds[:i], endpointIds[i+1:]...) + break + } + } + loadBalancer.HostComputeEndpoints = endpointIds +} + func HcnCreateTestRemoteSubnetRoute() (*PolicyNetworkRequest, error) { rsr := RemoteSubnetRoutePolicySetting{ DestinationPrefix: "192.168.2.0/24",