Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added NetworkDeviceAttachmentWasDisconnected api #169

Merged
merged 7 commits into from
Nov 5, 2024

Conversation

Code-Hex
Copy link
Owner

@Code-Hex Code-Hex commented Nov 3, 2024

Which issue(s) this PR fixes:

Related #118

Additional documentation

This Pull Request adds support for attachmentWasDisconnectedWithError only on the Objective-C side. The reason it’s not provided as a Go API is that I couldn’t reproduce the invocation of this method in my environment, so I couldn’t determine the appropriate way to expose it as a Go API.

Things I tried (building example/macOS):

  • Modified the internal code to release the VZNetworkDevice and its attachment after the VM started
    • The network remained functional
  • Deleted the file descriptor passed to vz.NewFileHandleNetworkDeviceAttachment after the VM started
    • Network functionality stopped, but attachmentWasDisconnectedWithError was not executed
    • I also tried this while macOS was running, with the same result

Based on these findings, I decided to log the error content as an NSLog in Objective-C for now and have users report it if it occurs.

You can see log like below:

2024-11-03 14:28:02.354 virtualization[30280:5308173] If you see this message, please report the information about the OS (including the version) that you are running on the VM, along with the information displayed below, to https://github.com/Code-Hex/vz/issues.
Process Information:
  Name: virtualization
  macOS: Version 14.5 (Build 23F79), arm64
  networkDevice: <VZNetworkDevice: 0x6000010d86c0>
  networkDevice attachment: <VZNATNetworkDeviceAttachment: 0x600001cd80b0>
  attachmentWasDisconnectedWithError: <error content>

@Code-Hex
Copy link
Owner Author

Code-Hex commented Nov 3, 2024

Oh I got this message in CI. Let me check...

https://github.com/Code-Hex/vz/actions/runs/11648909986/job/32435851386?pr=169

It was an error that didn't seem to help. I don't know how to be helpful with regard to VZNetworkDevice.

2024-11-03 07:30:35.135 vz.test[7014:35577] If you see this message, please report the information about the OS (including the version) that you are running on the VM, along with the information displayed below, to https://github.com/Code-Hex/vz/issues.
Process Information:
  Name: vz.test
  macOS: Version 13.7 (Build 22H123), x86_64
  networkDevice: <VZNetworkDevice: 0x600000c04960>
  networkDevice attachment: (null)
  attachmentWasDisconnectedWithError: Error Domain=VZErrorDomain Code=1 "Internal Network Error." UserInfo={NSLocalizedFailure=Internal Virtualization error., NSLocalizedFailureReason=Internal Network Error.}

Edit:
same here: lucyllewy/macOS-Linux-VM-with-Rosetta#15

@Code-Hex Code-Hex force-pushed the add/attachmentWasDisconnectedWithError branch from d49cd9f to 8e9c1ff Compare November 3, 2024 05:56
@Code-Hex Code-Hex marked this pull request as draft November 3, 2024 09:46
@Code-Hex
Copy link
Owner Author

Code-Hex commented Nov 3, 2024

I'm sorry I changed this PR to draft to implement Go API 🙏

memo

2024-11-03 10:24:03.149 vz.test[5382:27203] If you see this message, please report the information about the OS (including the version) that you are running on the VM, along with the information displayed below, to https://github.com/Code-Hex/vz/issues.
Process Information:
  Name: vz.test
  macOS: Version 13.7 (Build 22H123), x86_64
  networkDevice: <VZNetworkDevice: 0x6000022889f0>
  networkDevice attachment: (null)
  attachmentWasDisconnectedWithError: Error Domain=VZErrorDomain Code=1 "Internal Network Error." UserInfo={NSLocalizedFailure=Internal Virtualization error., NSLocalizedFailureReason=Internal Network Error.}
2024-11-03 10:24:03.150 vz.test[5382:27203] Index network devices 0
2024-11-03 10:24:03.150 vz.test[5382:27203] debug is finished

@Code-Hex Code-Hex force-pushed the add/attachmentWasDisconnectedWithError branch from a7c6705 to 9f38783 Compare November 3, 2024 11:47
@Code-Hex
Copy link
Owner Author

Code-Hex commented Nov 3, 2024

The code fell into an infinite loop, as shown in the following log, when it was committed.

https://github.com/Code-Hex/vz/actions/runs/11651049051/job/32445661567

2024-11-03 17:37:14.344 vz.test[7049:36456] Index network devices 0, (null)
2024-11-03 17:37:14.344 vz.test[7049:36456] debug is finished
2024-11-03 17:37:14.345 vz.test[7049:36456] If you see this message, please report the information about the OS (including the version) that you are running on the VM, along with the information displayed below, to https://github.com/Code-Hex/vz/issues.
Process Information:
  Name: vz.test
  macOS: Version 13.7 (Build 22H123), x86_64
  networkDevice: <VZNetworkDevice: 0x600002b96bb0>
  networkDevice attachment: (null)
  attachmentWasDisconnectedWithError: Error Domain=VZErrorDomain Code=1 "Internal Network Error." UserInfo={NSLocalizedFailure=Internal Virtualization error., NSLocalizedFailureReason=Internal Network Error.}
2024-11-03 17:37:14.345 vz.test[7049:36456] Index network devices 0, (null)
2024-11-03 17:37:14.345 vz.test[7049:36456] debug is finished
2024-11-03 17:37:14.346 vz.test[7049:36456] If you see this message, please report the information about the OS (including the version) that you are running on the VM, along with the information displayed below, to https://github.com/Code-Hex/vz/issues.
Process Information:
  Name: vz.test
  macOS: Version 13.7 (Build 22H123), x86_64
  networkDevice: <VZNetworkDevice: 0x600002b96bb0>
  networkDevice attachment: (null)
  attachmentWasDisconnectedWithError: Error Domain=VZErrorDomain Code=1 "Internal Network Error." UserInfo={NSLocalizedFailure=Internal Virtualization error., NSLocalizedFailureReason=Internal Network Error.}
2024-11-03 17:37:14.346 vz.test[7049:36456] Index network devices 0, (null)
2024-11-03 17:37:14.346 vz.test[7049:36456] debug is finished
2024-11-03 17:37:14.346 vz.test[7049:36456] If you see this message, please report the information about the OS (including the version) that you are running on the VM, along with the information displayed below, to https://github.com/Code-Hex/vz/issues.
Process Information:
  Name: vz.test
  macOS: Version 13.7 (Build 22H123), x86_64
  networkDevice: <VZNetworkDevice: 0x600002b96bb0>
  networkDevice attachment: (null)
  attachmentWasDisconnectedWithError: Error Domain=VZErrorDomain Code=1 "Internal Network Error." UserInfo={NSLocalizedFailure=Internal Virtualization error., NSLocalizedFailureReason=Internal Network Error.}
2024-11-03 17:37:14.346 vz.test[7049:36456] Index network devices 0, (null)
2024-11-03 17:37:14.346 vz.test[7049:36456] debug is finished
2024-11-03 17:37:14.347 vz.test[7049:36456] If you see this message, please report the information about the OS (including the version) that you are running on the VM, along with the information displayed below, to https://github.com/Code-Hex/vz/issues.
Process Information:
  Name: vz.test
  macOS: Version 13.7 (Build 22H123), x86_64
  networkDevice: <VZNetworkDevice: 0x600002b96bb0>
  networkDevice attachment: (null)
  attachmentWasDisconnectedWithError: Error Domain=VZErrorDomain Code=1 "Internal Network Error." UserInfo={NSLocalizedFailure=Internal Virtualization error., NSLocalizedFailureReason=Internal Network Error.}
...

From these observations, we can deduce the following points:

  • Although we receive the network device, there is no way to debug it. However, we can identify if an issue has occurred in the device set in a particular position.
  • The attachment property of the network device is always nil, as documented.
  • From the error information, the only clue we have is "Internal Virtualization error."
    • However, by looking at the logs of Virtualization.framework in Console.app, we might be able to gather more detailed information, although this hasn’t been confirmed as the issue hasn’t been reproduced locally.

Based on these points, I would like to establish the following implementation plan:

  • Receive error notifications via a channel, similar to StateChangedNotify().
  • Instead of returning the NetworkDevice, return the index information.
    • This index can be useful for referring to VZVirtualMachineConfiguration.networkDevices.
      • The documentation mentions: "The network devices map to their respective configurations in a one-to-one relationship, where index i of VZVirtualMachine.networkDevices corresponds to the network device configuration at index i set on VZVirtualMachineConfiguration.networkDevices."
      • Apple Documentation on VZNetworkDevice

@Code-Hex
Copy link
Owner Author

Code-Hex commented Nov 4, 2024

I could finally observe an error.

@AkihiroSuda or @balajiv113 Could you review this PR? 🙏

CleanShot 2024-11-04 at 20 40 58@2x

2024/11/04 11:40:37 NATNetworkDeviceAttachment: Error Domain=VZErrorDomain Code=1 Description="Internal Virtualization error. Internal Network Error." UserInfo={
    NSLocalizedFailure = "Internal Virtualization error.";
    NSLocalizedFailureReason = "Internal Network Error.";
}

@Code-Hex Code-Hex marked this pull request as ready for review November 4, 2024 11:44
if err == nil {
go func() {
for disconnected := range disconnected {
log.Println(disconnected)
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

observed by this line
#169 (comment)

@Code-Hex Code-Hex changed the title added attachmentWasDisconnectedWithError delegate in internal only added NetworkDeviceAttachmentWasDisconnected api Nov 4, 2024
Comment on lines -124 to +126
nsArray := objc.NewNSArray(
C.networkDevicesVZVirtualMachineConfiguration(objc.Ptr(v)),
)
ptrs := nsArray.ToPointerSlice()
networkDevices := make([]*VirtioNetworkDeviceConfiguration, len(ptrs))
for i, ptr := range ptrs {
networkDevices[i] = newVirtioNetworkDeviceConfiguration(ptr)
}
return networkDevices
return v.networkDeviceConfiguration
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I decided to take this concise approach because it is difficult to identify the interface NetworkDeviceAttachment class in the Objective-C world and restore it in the Go world.

This also has the advantage that it is identical to the VirtioNetworkDeviceConfiguration pointer at set time.

// FindValueByIndex returns the value of the index in s,
// or -1 if not present.
func FindValueByIndex[S ~[]E, E any](s S, idx int) (v E) {
for i := range s {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this for loop really necessary?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, I will remove it.
Thank you

@@ -3,10 +3,8 @@ package sliceutil
// FindValueByIndex returns the value of the index in s,
// or -1 if not present.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't seem returning -1

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I fixed 4be99b6

@Code-Hex Code-Hex force-pushed the add/attachmentWasDisconnectedWithError branch from ad740ac to 4be99b6 Compare November 5, 2024 00:45
@Code-Hex
Copy link
Owner Author

Code-Hex commented Nov 5, 2024

I need to fix flaky test...
I checked via ssh, but macOS on CI hung (crashed?) in the first place. The macOS on the CI seems to have hung (crashed?) to begin with and is frozen, so we investigate the cause.

@Code-Hex Code-Hex merged commit d2ef438 into main Nov 5, 2024
7 of 8 checks passed
@Code-Hex Code-Hex deleted the add/attachmentWasDisconnectedWithError branch November 5, 2024 00:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants