|
| 1 | +// Copyright 2016 VMware, Inc. All Rights Reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +// The default (ESX) implementation of the VmdkCmdRunner interface. |
| 16 | +// This implementation sends synchronous commands to and receives responses |
| 17 | +// from ESX. |
| 18 | + |
| 19 | +// +build linux darwin |
| 20 | +// +build !libstorage_storage_driver libstorage_storage_driver_vmdk libstorage_storage_driver_photon |
| 21 | + |
| 22 | +package vmci |
| 23 | + |
| 24 | +import ( |
| 25 | + "encoding/json" |
| 26 | + "errors" |
| 27 | + "fmt" |
| 28 | + "os" |
| 29 | + "sync" |
| 30 | + "syscall" |
| 31 | + "time" |
| 32 | + "unsafe" |
| 33 | + |
| 34 | + log "github.com/Sirupsen/logrus" |
| 35 | +) |
| 36 | + |
| 37 | +/* |
| 38 | +#include "vmci_client.h" |
| 39 | +*/ |
| 40 | +import "C" |
| 41 | + |
| 42 | +// EsxVmdkCmd struct - empty , we use it only to implement VmdkCmdRunner |
| 43 | +// interface |
| 44 | +type EsxVmdkCmd struct { |
| 45 | + Mtx *sync.Mutex // For serialization of Run comand/response |
| 46 | +} |
| 47 | + |
| 48 | +const ( |
| 49 | + commBackendName string = "vsocket" |
| 50 | + maxRetryCount = 5 |
| 51 | + // Server side understand protocol version. If you are changing |
| 52 | + // client/server protocol we use over VMCI, PLEASE DO NOT FORGET TO CHANGE |
| 53 | + // IT FOR SERVER in file <vmdk_ops.py> ! |
| 54 | + clientProtocolVersion = "2" |
| 55 | +) |
| 56 | + |
| 57 | +// A request to be passed to ESX service |
| 58 | +type requestToVmci struct { |
| 59 | + Ops string `json:"cmd"` |
| 60 | + Details VolumeInfo `json:"details"` |
| 61 | + Version string `json:"version,omitempty"` |
| 62 | +} |
| 63 | + |
| 64 | +// VolumeInfo we get about the volume from upstairs |
| 65 | +type VolumeInfo struct { |
| 66 | + Name string `json:"Name"` |
| 67 | + Options map[string]string `json:"Opts,omitempty"` |
| 68 | +} |
| 69 | + |
| 70 | +type vmciError struct { |
| 71 | + Error string `json:",omitempty"` |
| 72 | +} |
| 73 | + |
| 74 | +// EsxPort used to connect to ESX, passed in as command line param |
| 75 | +var EsxPort = 1019 |
| 76 | + |
| 77 | +// Run command Guest VM requests on ESX via vmdkops_serv.py listening on |
| 78 | +// vSocket |
| 79 | +// * |
| 80 | +// * For each request: |
| 81 | +// * - Establishes a vSocket connection |
| 82 | +// * - Sends json string up to ESX |
| 83 | +// * - waits for reply and returns resulting JSON or an error |
| 84 | +func (vmdkCmd EsxVmdkCmd) Run( |
| 85 | + cmd string, name string, opts map[string]string) ([]byte, error) { |
| 86 | + |
| 87 | + vmdkCmd.Mtx.Lock() |
| 88 | + defer vmdkCmd.Mtx.Unlock() |
| 89 | + protocolVersion := os.Getenv("VDVS_TEST_PROTOCOL_VERSION") |
| 90 | + log.Debugf("Run get request: version=%s", protocolVersion) |
| 91 | + if protocolVersion == "" { |
| 92 | + protocolVersion = clientProtocolVersion |
| 93 | + } |
| 94 | + jsonStr, err := json.Marshal(&requestToVmci{ |
| 95 | + Ops: cmd, |
| 96 | + Details: VolumeInfo{Name: name, Options: opts}, |
| 97 | + Version: protocolVersion}) |
| 98 | + if err != nil { |
| 99 | + return nil, fmt.Errorf("Failed to marshal json: %v", err) |
| 100 | + } |
| 101 | + |
| 102 | + cmdS := C.CString(string(jsonStr)) |
| 103 | + defer C.free(unsafe.Pointer(cmdS)) |
| 104 | + |
| 105 | + beS := C.CString(commBackendName) |
| 106 | + defer C.free(unsafe.Pointer(beS)) |
| 107 | + |
| 108 | + // Get the response data in json |
| 109 | + ans := (*C.be_answer)(C.calloc(1, C.sizeof_struct_be_answer)) |
| 110 | + defer C.free(unsafe.Pointer(ans)) |
| 111 | + |
| 112 | + var ret C.be_sock_status |
| 113 | + for i := 0; i <= maxRetryCount; i++ { |
| 114 | + ret, err = C.Vmci_GetReply(C.int(EsxPort), cmdS, beS, ans) |
| 115 | + if ret == 0 { |
| 116 | + // Received no error, exit loop. |
| 117 | + // C.Vmci_GetReply indicates success/faulure by <ret> value. |
| 118 | + // Cgo interface adds <err> based on errno. We do not explicitly |
| 119 | + // reset errno in our code. Still, we do not want a stale errno |
| 120 | + // to confuse this code into thinking there was an error even when |
| 121 | + // ret==0, so explicitly declare success on <ret> value only, and |
| 122 | + break |
| 123 | + } |
| 124 | + |
| 125 | + var msg string |
| 126 | + if err != nil { |
| 127 | + var errno syscall.Errno |
| 128 | + errno = err.(syscall.Errno) |
| 129 | + msg = fmt.Sprintf("Run '%s' failed: %v (errno=%d) - %s", |
| 130 | + cmd, err, int(errno), C.GoString(&ans.errBuf[0])) |
| 131 | + if i < maxRetryCount { |
| 132 | + log.Warnf(msg + " Retrying...") |
| 133 | + time.Sleep(time.Second * 1) |
| 134 | + continue |
| 135 | + } |
| 136 | + if errno == syscall.ECONNRESET || errno == syscall.ETIMEDOUT { |
| 137 | + msg += " Cannot communicate with ESX, please refer to the " + |
| 138 | + "FAQ " + |
| 139 | + "https://github.com/vmware/docker-volume-vsphere/wiki#faq" |
| 140 | + } |
| 141 | + } else { |
| 142 | + msg = fmt.Sprintf("Internal issue: ret != 0 but errno is not "+ |
| 143 | + "set. Cancelling operation - %s ", C.GoString(&ans.errBuf[0])) |
| 144 | + } |
| 145 | + |
| 146 | + log.Warnf(msg) |
| 147 | + return nil, errors.New(msg) |
| 148 | + } |
| 149 | + |
| 150 | + response := []byte(C.GoString(ans.buf)) |
| 151 | + C.Vmci_FreeBuf(ans) |
| 152 | + |
| 153 | + err = unmarshalError(response) |
| 154 | + if err != nil && len(err.Error()) != 0 { |
| 155 | + return nil, err |
| 156 | + } |
| 157 | + // There was no error, so return the slice containing the json response |
| 158 | + return response, nil |
| 159 | +} |
| 160 | + |
| 161 | +func unmarshalError(str []byte) error { |
| 162 | + // Unmarshalling null always succeeds |
| 163 | + if string(str) == "null" { |
| 164 | + return nil |
| 165 | + } |
| 166 | + errStruct := vmciError{} |
| 167 | + err := json.Unmarshal(str, &errStruct) |
| 168 | + if err != nil { |
| 169 | + // We didn't unmarshal an error, so there is no error ;) |
| 170 | + return nil |
| 171 | + } |
| 172 | + // Return the unmarshaled error string as an `error` |
| 173 | + return errors.New(errStruct.Error) |
| 174 | +} |
0 commit comments