2019-01-12 04:58:27 +00:00
|
|
|
// Package hcn is a shim for the Host Compute Networking (HCN) service, which manages networking for Windows Server
|
|
|
|
// containers and Hyper-V containers. Previous to RS5, HCN was referred to as Host Networking Service (HNS).
|
|
|
|
package hcn
|
|
|
|
|
|
|
|
import (
|
2019-09-30 23:25:17 +00:00
|
|
|
"errors"
|
2019-01-12 04:58:27 +00:00
|
|
|
"fmt"
|
|
|
|
|
2019-09-30 23:25:17 +00:00
|
|
|
"github.com/Microsoft/hcsshim/internal/hcs"
|
2019-01-12 04:58:27 +00:00
|
|
|
"github.com/Microsoft/hcsshim/internal/hcserror"
|
|
|
|
"github.com/Microsoft/hcsshim/internal/interop"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
2019-09-30 23:25:17 +00:00
|
|
|
var (
|
|
|
|
errInvalidNetworkID = errors.New("invalid network ID")
|
|
|
|
errInvalidEndpointID = errors.New("invalid endpoint ID")
|
|
|
|
errInvalidNamespaceID = errors.New("invalid namespace ID")
|
|
|
|
errInvalidLoadBalancerID = errors.New("invalid load balancer ID")
|
2020-08-10 17:43:49 +00:00
|
|
|
errInvalidRouteID = errors.New("invalid route ID")
|
2019-09-30 23:25:17 +00:00
|
|
|
)
|
|
|
|
|
2019-01-12 04:58:27 +00:00
|
|
|
func checkForErrors(methodName string, hr error, resultBuffer *uint16) error {
|
|
|
|
errorFound := false
|
|
|
|
|
|
|
|
if hr != nil {
|
|
|
|
errorFound = true
|
|
|
|
}
|
|
|
|
|
|
|
|
result := ""
|
|
|
|
if resultBuffer != nil {
|
|
|
|
result = interop.ConvertAndFreeCoTaskMemString(resultBuffer)
|
|
|
|
if result != "" {
|
|
|
|
errorFound = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if errorFound {
|
2020-08-10 17:43:49 +00:00
|
|
|
returnError := new(hr, methodName, result)
|
2019-01-12 04:58:27 +00:00
|
|
|
logrus.Debugf(returnError.Error()) // HCN errors logged for debugging.
|
|
|
|
return returnError
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-08-10 17:43:49 +00:00
|
|
|
type ErrorCode uint32
|
|
|
|
|
|
|
|
// For common errors, define the error as it is in windows, so we can quickly determine it later
|
|
|
|
const (
|
|
|
|
ERROR_NOT_FOUND = 0x490
|
|
|
|
HCN_E_PORT_ALREADY_EXISTS ErrorCode = 0x803b0013
|
|
|
|
)
|
|
|
|
|
|
|
|
type HcnError struct {
|
|
|
|
*hcserror.HcsError
|
|
|
|
code ErrorCode
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *HcnError) Error() string {
|
|
|
|
return e.HcsError.Error()
|
|
|
|
}
|
|
|
|
|
|
|
|
func CheckErrorWithCode(err error, code ErrorCode) bool {
|
|
|
|
hcnError, ok := err.(*HcnError)
|
|
|
|
if ok {
|
|
|
|
return hcnError.code == code
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func IsElementNotFoundError(err error) bool {
|
|
|
|
return CheckErrorWithCode(err, ERROR_NOT_FOUND)
|
|
|
|
}
|
|
|
|
|
|
|
|
func IsPortAlreadyExistsError(err error) bool {
|
|
|
|
return CheckErrorWithCode(err, HCN_E_PORT_ALREADY_EXISTS)
|
|
|
|
}
|
|
|
|
|
|
|
|
func new(hr error, title string, rest string) error {
|
|
|
|
err := &HcnError{}
|
|
|
|
hcsError := hcserror.New(hr, title, rest)
|
|
|
|
err.HcsError = hcsError.(*hcserror.HcsError)
|
|
|
|
err.code = ErrorCode(hcserror.Win32FromError(hr))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Note that the below errors are not errors returned by hcn itself
|
|
|
|
// we wish to seperate them as they are shim usage error
|
|
|
|
//
|
|
|
|
|
2019-01-12 04:58:27 +00:00
|
|
|
// NetworkNotFoundError results from a failed seach for a network by Id or Name
|
|
|
|
type NetworkNotFoundError struct {
|
|
|
|
NetworkName string
|
|
|
|
NetworkID string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e NetworkNotFoundError) Error() string {
|
2020-08-10 17:43:49 +00:00
|
|
|
if e.NetworkName != "" {
|
|
|
|
return fmt.Sprintf("Network name %q not found", e.NetworkName)
|
2019-01-12 04:58:27 +00:00
|
|
|
}
|
2020-08-10 17:43:49 +00:00
|
|
|
return fmt.Sprintf("Network ID %q not found", e.NetworkID)
|
2019-01-12 04:58:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// EndpointNotFoundError results from a failed seach for an endpoint by Id or Name
|
|
|
|
type EndpointNotFoundError struct {
|
|
|
|
EndpointName string
|
|
|
|
EndpointID string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e EndpointNotFoundError) Error() string {
|
2020-08-10 17:43:49 +00:00
|
|
|
if e.EndpointName != "" {
|
|
|
|
return fmt.Sprintf("Endpoint name %q not found", e.EndpointName)
|
2019-01-12 04:58:27 +00:00
|
|
|
}
|
2020-08-10 17:43:49 +00:00
|
|
|
return fmt.Sprintf("Endpoint ID %q not found", e.EndpointID)
|
2019-01-12 04:58:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NamespaceNotFoundError results from a failed seach for a namsepace by Id
|
|
|
|
type NamespaceNotFoundError struct {
|
|
|
|
NamespaceID string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e NamespaceNotFoundError) Error() string {
|
2020-08-10 17:43:49 +00:00
|
|
|
return fmt.Sprintf("Namespace ID %q not found", e.NamespaceID)
|
2019-01-12 04:58:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// LoadBalancerNotFoundError results from a failed seach for a loadbalancer by Id
|
|
|
|
type LoadBalancerNotFoundError struct {
|
|
|
|
LoadBalancerId string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e LoadBalancerNotFoundError) Error() string {
|
2020-08-10 17:43:49 +00:00
|
|
|
return fmt.Sprintf("LoadBalancer %q not found", e.LoadBalancerId)
|
|
|
|
}
|
|
|
|
|
|
|
|
// RouteNotFoundError results from a failed seach for a route by Id
|
|
|
|
type RouteNotFoundError struct {
|
|
|
|
RouteId string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e RouteNotFoundError) Error() string {
|
|
|
|
return fmt.Sprintf("SDN Route %q not found", e.RouteId)
|
2019-01-12 04:58:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// IsNotFoundError returns a boolean indicating whether the error was caused by
|
|
|
|
// a resource not being found.
|
|
|
|
func IsNotFoundError(err error) bool {
|
2019-09-30 23:25:17 +00:00
|
|
|
switch pe := err.(type) {
|
2019-01-12 04:58:27 +00:00
|
|
|
case NetworkNotFoundError:
|
|
|
|
return true
|
|
|
|
case EndpointNotFoundError:
|
|
|
|
return true
|
|
|
|
case NamespaceNotFoundError:
|
|
|
|
return true
|
|
|
|
case LoadBalancerNotFoundError:
|
|
|
|
return true
|
2020-08-10 17:43:49 +00:00
|
|
|
case RouteNotFoundError:
|
|
|
|
return true
|
2019-09-30 23:25:17 +00:00
|
|
|
case *hcserror.HcsError:
|
|
|
|
return pe.Err == hcs.ErrElementNotFound
|
2019-01-12 04:58:27 +00:00
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|