From e34c7218d0daa8830f1a74c574a53d1576337154 Mon Sep 17 00:00:00 2001 From: lubronzhan Date: Fri, 30 Jun 2023 14:29:50 -0700 Subject: [PATCH] govc: Add CLI device protocol support for vm.network.add Closes: #3167 --- govc/USAGE.md | 7 +++++++ govc/flags/network.go | 14 ++++++++++++++ govc/test/network.bats | 22 +++++++++++++++++++++- 3 files changed, 42 insertions(+), 1 deletion(-) diff --git a/govc/USAGE.md b/govc/USAGE.md index fc574d232..11093c590 100644 --- a/govc/USAGE.md +++ b/govc/USAGE.md @@ -1419,6 +1419,7 @@ Options: -net= Network [GOVC_NETWORK] -net.adapter=e1000 Network adapter type -net.address= Network hardware address + -net.protocol= Network device protocol. Applicable to vmxnet3vrdma. Default to 'rocev2' -vm= Virtual machine [GOVC_VM] ``` @@ -3470,6 +3471,7 @@ Options: -net= Network [GOVC_NETWORK] -net.adapter=e1000 Network adapter type -net.address= Network hardware address + -net.protocol= Network device protocol. Applicable to vmxnet3vrdma. Default to 'rocev2' -pool= Resource pool [GOVC_RESOURCE_POOL] ``` @@ -5700,6 +5702,7 @@ Options: -net= Network [GOVC_NETWORK] -net.adapter=e1000 Network adapter type -net.address= Network hardware address + -net.protocol= Network device protocol. Applicable to vmxnet3vrdma. Default to 'rocev2' -on=true Power on VM -pool= Resource pool [GOVC_RESOURCE_POOL] -snapshot= Snapshot name to clone from @@ -5771,6 +5774,7 @@ Options: -net= Network [GOVC_NETWORK] -net.adapter=e1000 Network adapter type -net.address= Network hardware address + -net.protocol= Network device protocol. Applicable to vmxnet3vrdma. Default to 'rocev2' -on=true Power on VM -pool= Resource pool [GOVC_RESOURCE_POOL] -version= ESXi hardware version [5.0|5.5|6.0|6.5|6.7|7.0] @@ -5985,6 +5989,7 @@ Options: -net= Network [GOVC_NETWORK] -net.adapter=e1000 Network adapter type -net.address= Network hardware address + -net.protocol= Network device protocol. Applicable to vmxnet3vrdma. Default to 'rocev2' -pool= Resource pool [GOVC_RESOURCE_POOL] -vm= Virtual machine [GOVC_VM] ``` @@ -6137,6 +6142,7 @@ Options: -net= Network [GOVC_NETWORK] -net.adapter=e1000 Network adapter type -net.address= Network hardware address + -net.protocol= Network device protocol. Applicable to vmxnet3vrdma. Default to 'rocev2' -vm= Virtual machine [GOVC_VM] ``` @@ -6159,6 +6165,7 @@ Options: -net= Network [GOVC_NETWORK] -net.adapter=e1000 Network adapter type -net.address= Network hardware address + -net.protocol= Network device protocol. Applicable to vmxnet3vrdma. Default to 'rocev2' -vm= Virtual machine [GOVC_VM] ``` diff --git a/govc/flags/network.go b/govc/flags/network.go index 8e4ec1e90..5e187303c 100644 --- a/govc/flags/network.go +++ b/govc/flags/network.go @@ -36,6 +36,7 @@ type NetworkFlag struct { adapter string address string isset bool + proto string } var networkFlagKey = flagKey("network") @@ -62,6 +63,7 @@ func (flag *NetworkFlag) Register(ctx context.Context, f *flag.FlagSet) { f.Var(flag, "net", usage) f.StringVar(&flag.adapter, "net.adapter", "e1000", "Network adapter type") f.StringVar(&flag.address, "net.address", "", "Network hardware address") + f.StringVar(&flag.proto, "net.protocol", "", fmt.Sprintf("Network device protocol. Applicable to vmxnet3vrdma. Default to '%s'", string(types.VirtualVmxnet3VrdmaOptionDeviceProtocolsRocev2))) }) } @@ -121,6 +123,18 @@ func (flag *NetworkFlag) Device() (types.BaseVirtualDevice, error) { return nil, err } + if a, ok := device.(*types.VirtualVmxnet3Vrdma); ok { + if flag.proto != "" { + if flag.proto != string(types.VirtualVmxnet3VrdmaOptionDeviceProtocolsRocev2) && + flag.proto != string(types.VirtualVmxnet3VrdmaOptionDeviceProtocolsRocev1) { + return nil, fmt.Errorf("invalid device protocol '%s'", flag.proto) + } + a.DeviceProtocol = flag.proto + } + } else if flag.proto != "" { + return nil, fmt.Errorf("device protocol is only supported for vmxnet3vrdma at the moment") + } + if flag.address == "-" { card := device.(types.BaseVirtualEthernetCard).GetVirtualEthernetCard() card.AddressType = string(types.VirtualEthernetCardMacTypeGenerated) diff --git a/govc/test/network.bats b/govc/test/network.bats index 424f261f7..0e1ffd825 100755 --- a/govc/test/network.bats +++ b/govc/test/network.bats @@ -140,7 +140,7 @@ load test_helper } @test "network adapter" { - vcsim_env -esx + vcsim_env vm=$(new_id) run govc vm.create -on=false -net.adapter=enoent $vm @@ -164,6 +164,26 @@ load test_helper # validate each NIC has a unique MAC macs=$(govc device.info -vm "$vm" -json ethernet-* | jq -r .Devices[].macAddress | uniq | wc -l) assert_equal 2 "$macs" + + # validate -net.protocol. VM Network not compatible with vmxnet3vrdma, so create on dvgp under existing DVS0 + run govc dvs.portgroup.add -dvs DVS0 -type ephemeral NSX-dvpg + assert_success + + # add a valid vmxnet3vrdma adapter with valid protocal + run govc vm.network.add -vm $vm -net.adapter vmxnet3vrdma -net "DVS0/NSX-dvpg" -net.protocol=rocev2 + assert_success + + # add a valid vmxnet3vrdma adapter with valid protocal + run govc vm.network.add -vm $vm -net.adapter vmxnet3vrdma -net "DVS0/NSX-dvpg" -net.protocol=rocev1 + assert_success + + # invalid value for -net.protocol + run govc vm.network.add -vm $vm -net.adapter vmxnet3vrdma -net "DVS0/NSX-dvpg" -net.protocol=what + assert_failure "govc: invalid device protocol 'what'" + + # invalid combination for -net.adapter and -net.protocol + run govc vm.network.add -vm $vm -net.adapter e1000e -net "DVS0/NSX-dvpg" -net.protocol=rocev2 + assert_failure "govc: device protocol is only supported for vmxnet3vrdma at the moment" } @test "network flag required" {