-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Implement VSock secret notifier. #4565
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
Open
TingluoHuang
wants to merge
6
commits into
main
Choose a base branch
from
users/tihuang/vsocket
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,223 @@ | ||
| using System; | ||
| using System.Net; | ||
| using System.Net.Sockets; | ||
| using System.Runtime.InteropServices; | ||
| using System.Text; | ||
| using System.Threading; | ||
| using System.Threading.Channels; | ||
| using System.Threading.Tasks; | ||
| using GitHub.DistributedTask.Logging; | ||
| using GitHub.Runner.Sdk; | ||
|
|
||
| namespace GitHub.Runner.Common | ||
| { | ||
|
|
||
| [ServiceLocator(Default = typeof(VSockSecretNotifier))] | ||
| public interface IVSockSecretNotifier : IRunnerService, IAsyncDisposable | ||
| { | ||
| bool TryStartNotifier(); | ||
|
|
||
| void NotifyNewSecret(NewSecretEventArgs newSecret); | ||
| } | ||
|
|
||
| public sealed class VSockSecretNotifier : RunnerService, IVSockSecretNotifier | ||
| { | ||
| private Socket _vsock = null; | ||
|
|
||
| private CancellationTokenSource _cancellationTokenSource = null; | ||
|
|
||
| private Task _secretNotificationTask = null; | ||
|
|
||
| private Channel<byte[]> _channel = Channel.CreateUnbounded<byte[]>(new UnboundedChannelOptions() { SingleReader = true }); | ||
|
|
||
| public bool TryStartNotifier() | ||
| { | ||
| if (_vsock != null) | ||
| { | ||
| Trace.Verbose("VSocket is already connected."); | ||
| return true; | ||
| } | ||
|
|
||
| // `GITHUB_ACTIONS_RUNNER_VSOCK_CID_PORT` is expected to be in the format "CID:PORT", e.g. "2:9999". | ||
| string vsockCidPort = Environment.GetEnvironmentVariable("GITHUB_ACTIONS_RUNNER_VSOCK_CID_PORT"); | ||
| if (string.IsNullOrEmpty(vsockCidPort)) | ||
| { | ||
| Trace.Verbose("VSocket CID/Port environment variable is not set."); | ||
| return false; | ||
| } | ||
|
|
||
| string[] parts = vsockCidPort.Split(':', 2, StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries); | ||
| if (parts.Length != 2) | ||
| { | ||
| Trace.Verbose("VSocket CID/Port environment variable is not in the correct format."); | ||
| return false; | ||
| } | ||
|
|
||
| uint cid, port; | ||
| if (!uint.TryParse(parts[0], out cid) || !uint.TryParse(parts[1], out port)) | ||
| { | ||
| Trace.Verbose("VSocket CID/Port environment variable contains invalid numbers."); | ||
| return false; | ||
| } | ||
|
|
||
| Trace.Info($"Attempting to start VSocket secret notifier with CID: {cid}, Port: {port}."); | ||
| try | ||
| { | ||
| SafeSocketHandle nativeSocket = NativeSocket((int)(AddressFamily)40, (int)SocketType.Stream, 0); | ||
| if (nativeSocket.IsInvalid) | ||
| { | ||
| int error = Marshal.GetLastPInvokeError(); | ||
| nativeSocket.Dispose(); | ||
| throw new SocketException(error); | ||
| } | ||
|
|
||
| _vsock = new Socket(nativeSocket); | ||
| _vsock.Connect(new HostVsockEndPoint(cid, port)); | ||
| } | ||
|
TingluoHuang marked this conversation as resolved.
|
||
| catch (Exception ex) | ||
| { | ||
| Trace.Error($"Failed to create and connect VSocket: {ex}"); | ||
| _vsock?.Dispose(); | ||
| _vsock = null; | ||
| return false; | ||
| } | ||
|
|
||
| _cancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(HostContext.RunnerShutdownToken); | ||
| _secretNotificationTask = ProcessSecretChannel(); | ||
| Trace.Info($"VSocket secret notifier started successfully."); | ||
| return true; | ||
| } | ||
|
|
||
| public void NotifyNewSecret(NewSecretEventArgs newSecret) | ||
| { | ||
| if (_vsock == null) | ||
| { | ||
| Trace.Verbose("VSocket is not connected, skipping secret notification."); | ||
| return; | ||
| } | ||
|
|
||
| byte[] payloadBytes = Encoding.UTF8.GetBytes(StringUtil.ConvertToJson(newSecret, Newtonsoft.Json.Formatting.None)); | ||
| byte[] lengthPrefix = BitConverter.GetBytes(IPAddress.HostToNetworkOrder(payloadBytes.Length)); | ||
| byte[] fullPayload = new byte[lengthPrefix.Length + payloadBytes.Length]; | ||
| Buffer.BlockCopy(lengthPrefix, 0, fullPayload, 0, lengthPrefix.Length); | ||
| Buffer.BlockCopy(payloadBytes, 0, fullPayload, lengthPrefix.Length, payloadBytes.Length); | ||
|
|
||
| // we don't need to check return since unbounded channel will always accept the item. | ||
| _channel.Writer.TryWrite(fullPayload); | ||
| } | ||
|
|
||
| public async ValueTask DisposeAsync() | ||
| { | ||
| if (_vsock != null && _secretNotificationTask != null) | ||
| { | ||
| _cancellationTokenSource?.Cancel(); | ||
| try | ||
| { | ||
| await _secretNotificationTask; | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| Trace.Error($"Secret notification task finished with error: {ex}"); | ||
| } | ||
|
|
||
| _cancellationTokenSource?.Dispose(); | ||
| _cancellationTokenSource = null; | ||
| _vsock?.Dispose(); | ||
| _vsock = null; | ||
| } | ||
| } | ||
|
|
||
| private async Task ProcessSecretChannel() | ||
| { | ||
| try | ||
| { | ||
| while (!_cancellationTokenSource.Token.IsCancellationRequested && | ||
| await _channel.Reader.WaitToReadAsync(_cancellationTokenSource.Token)) | ||
| { | ||
| while (_channel.Reader.TryRead(out var payload)) | ||
| { | ||
| try | ||
| { | ||
| // Socket.SendAsync on a stream socket may send fewer bytes than requested, | ||
| // so keep sending until the entire payload has been written. | ||
| int totalSent = 0; | ||
| while (totalSent < payload.Length) | ||
| { | ||
| int bytesSent = await _vsock.SendAsync(payload.AsMemory(totalSent), SocketFlags.None, _cancellationTokenSource.Token); | ||
| if (bytesSent == 0) | ||
| { | ||
| throw new SocketException((int)SocketError.ConnectionReset); | ||
| } | ||
|
|
||
| totalSent += bytesSent; | ||
| } | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| Trace.Error($"Failed to notify new secret over VSocket: {ex}"); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| Trace.Error($"Failed to process secret channel: {ex}"); | ||
| } | ||
|
TingluoHuang marked this conversation as resolved.
|
||
|
|
||
| _channel.Writer.TryComplete(); | ||
| } | ||
|
|
||
| [DllImport("libc", SetLastError = true, EntryPoint = "socket")] | ||
| private static extern SafeSocketHandle NativeSocket(int domain, int type, int protocol); | ||
| } | ||
|
|
||
| internal sealed class HostVsockEndPoint : EndPoint | ||
| { | ||
| private const int SocketAddressSize = 16; | ||
| private readonly uint _cid; | ||
| private readonly uint _port; | ||
|
|
||
| public HostVsockEndPoint(uint cid, uint port) | ||
| { | ||
| _cid = cid; | ||
| _port = port; | ||
| } | ||
|
|
||
| public override AddressFamily AddressFamily => (AddressFamily)40; | ||
|
|
||
| public override SocketAddress Serialize() | ||
| { | ||
| SocketAddress socketAddress = new SocketAddress(AddressFamily.Unspecified, SocketAddressSize); | ||
| // sockaddr_vm layout: family(0-1), reserved1(2-3), port(4-7), cid(8-11) | ||
| ushort family = (ushort)AddressFamily; | ||
| socketAddress[0] = (byte)(family & 0xFF); | ||
| socketAddress[1] = (byte)((family >> 8) & 0xFF); | ||
| socketAddress[2] = 0; | ||
| socketAddress[3] = 0; | ||
| socketAddress[4] = (byte)(_port & 0xFF); | ||
| socketAddress[5] = (byte)((_port >> 8) & 0xFF); | ||
| socketAddress[6] = (byte)((_port >> 16) & 0xFF); | ||
| socketAddress[7] = (byte)((_port >> 24) & 0xFF); | ||
| socketAddress[8] = (byte)(_cid & 0xFF); | ||
| socketAddress[9] = (byte)((_cid >> 8) & 0xFF); | ||
| socketAddress[10] = (byte)((_cid >> 16) & 0xFF); | ||
| socketAddress[11] = (byte)((_cid >> 24) & 0xFF); | ||
| return socketAddress; | ||
| } | ||
|
|
||
| public override EndPoint Create(SocketAddress socketAddress) | ||
| { | ||
| uint port = (uint)socketAddress[4] | ||
| | ((uint)socketAddress[5] << 8) | ||
| | ((uint)socketAddress[6] << 16) | ||
| | ((uint)socketAddress[7] << 24); | ||
|
|
||
| uint cid = (uint)socketAddress[8] | ||
| | ((uint)socketAddress[9] << 8) | ||
| | ((uint)socketAddress[10] << 16) | ||
| | ((uint)socketAddress[11] << 24); | ||
|
|
||
| return new HostVsockEndPoint(cid, port); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.