Skip to content

Commit

Permalink
govc: Add CLI device protocol support for vm.network.add
Browse files Browse the repository at this point in the history
Closes: 3167
  • Loading branch information
lubronzhan committed Jul 1, 2023
1 parent 801db44 commit 716e25a
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
7 changes: 7 additions & 0 deletions govc/USAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]
```

Expand Down Expand Up @@ -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]
```

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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]
```
Expand Down Expand Up @@ -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]
```

Expand All @@ -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]
```

Expand Down
14 changes: 14 additions & 0 deletions govc/flags/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type NetworkFlag struct {
adapter string
address string
isset bool
proto string
}

var networkFlagKey = flagKey("network")
Expand All @@ -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)))
})
}

Expand Down Expand Up @@ -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 suppported for vmxnet3vrdma at the moment")
}

if flag.address == "-" {
card := device.(types.BaseVirtualEthernetCard).GetVirtualEthernetCard()
card.AddressType = string(types.VirtualEthernetCardMacTypeGenerated)
Expand Down
22 changes: 21 additions & 1 deletion govc/test/network.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 suppported for vmxnet3vrdma at the moment"
}

@test "network flag required" {
Expand Down

0 comments on commit 716e25a

Please sign in to comment.