github.com/vmware/govmomi@v0.37.2/toolbox/backdoor.go (about)

     1  /*
     2  Copyright (c) 2017 VMware, Inc. All Rights Reserved.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package toolbox
    18  
    19  import (
    20  	"errors"
    21  
    22  	"github.com/vmware/vmw-guestinfo/message"
    23  	"github.com/vmware/vmw-guestinfo/vmcheck"
    24  )
    25  
    26  const (
    27  	rpciProtocol uint32 = 0x49435052
    28  	tcloProtocol uint32 = 0x4f4c4354
    29  )
    30  
    31  var (
    32  	ErrNotVirtualWorld = errors.New("not in a virtual world")
    33  )
    34  
    35  type backdoorChannel struct {
    36  	protocol uint32
    37  
    38  	*message.Channel
    39  }
    40  
    41  func (b *backdoorChannel) Start() error {
    42  	if !vmcheck.IsVirtualCPU() {
    43  		return ErrNotVirtualWorld
    44  	}
    45  
    46  	channel, err := message.NewChannel(b.protocol)
    47  	if err != nil {
    48  		return err
    49  	}
    50  
    51  	b.Channel = channel
    52  
    53  	return nil
    54  }
    55  
    56  func (b *backdoorChannel) Stop() error {
    57  	if b.Channel == nil {
    58  		return nil
    59  	}
    60  
    61  	err := b.Channel.Close()
    62  
    63  	b.Channel = nil
    64  
    65  	return err
    66  }
    67  
    68  // NewBackdoorChannelOut creates a Channel for use with the RPCI protocol
    69  func NewBackdoorChannelOut() Channel {
    70  	return &backdoorChannel{
    71  		protocol: rpciProtocol,
    72  	}
    73  }
    74  
    75  // NewBackdoorChannelIn creates a Channel for use with the TCLO protocol
    76  func NewBackdoorChannelIn() Channel {
    77  	return &backdoorChannel{
    78  		protocol: tcloProtocol,
    79  	}
    80  }