-
Notifications
You must be signed in to change notification settings - Fork 10k
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
Initial port of MsQuic transport #15375
Merged
Merged
Changes from 13 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
790512f
Code port
jkotalik 7118d77
Features
jkotalik 74005c5
Remove copy task for now
jkotalik b91dfdc
ref assemblies
jkotalik d94e916
project reference
jkotalik c1106d5
Feedback and sample
jkotalik 9e8f3dd
Just the API for now
jkotalik 9a3de3f
oops
jkotalik 2b30243
Internals
jkotalik a3df14d
ref assemblies
jkotalik 0c8e585
Some feedback
jkotalik 30df2d6
different enums for different platforms
jkotalik 4ab3bad
New constants file
jkotalik 2169112
New file
jkotalik 142b36e
Remove translation
jkotalik f27e1b0
layout to remove warning
jkotalik 366f9e8
Update locations
jkotalik 55d73ac
Update refs
jkotalik bf67055
Update enums
jkotalik 5ee9554
Feedback
jkotalik 6b00048
Ideas
jkotalik 445153a
Feedback
jkotalik 002b42f
Update src/Servers/Kestrel/Transport.MsQuic/src/Internal/MsQuicStatus…
jkotalik a274fa0
Update src/Servers/Kestrel/Transport.MsQuic/src/Internal/MsQuicConsta…
jkotalik 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
There are no files selected for viewing
This file contains 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 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
186 changes: 186 additions & 0 deletions
186
src/Servers/Kestrel/Transport.MsQuic/Internal/MsQuicApi.cs
This file contains 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,186 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Runtime.InteropServices; | ||
|
||
namespace Microsoft.AspNetCore.Server.Kestrel.Transport.MsQuic.Internal | ||
{ | ||
internal class MsQuicApi : IDisposable | ||
{ | ||
private bool _disposed = false; | ||
|
||
private IntPtr _registrationContext; | ||
|
||
internal unsafe MsQuicApi() | ||
{ | ||
var status = (uint)NativeMethods.MsQuicOpen(version: 1, out var registration); | ||
QuicStatusException.ThrowIfFailed(status); | ||
|
||
NativeRegistration = *registration; | ||
|
||
RegistrationOpenDelegate = | ||
Marshal.GetDelegateForFunctionPointer<NativeMethods.RegistrationOpenDelegate>( | ||
NativeRegistration.RegistrationOpen); | ||
RegistrationCloseDelegate = | ||
Marshal.GetDelegateForFunctionPointer<NativeMethods.RegistrationCloseDelegate>( | ||
NativeRegistration.RegistrationClose); | ||
|
||
SecConfigCreateDelegate = | ||
Marshal.GetDelegateForFunctionPointer<NativeMethods.SecConfigCreateDelegate>( | ||
NativeRegistration.SecConfigCreate); | ||
SecConfigDeleteDelegate = | ||
Marshal.GetDelegateForFunctionPointer<NativeMethods.SecConfigDeleteDelegate>( | ||
NativeRegistration.SecConfigDelete); | ||
|
||
SessionOpenDelegate = | ||
Marshal.GetDelegateForFunctionPointer<NativeMethods.SessionOpenDelegate>( | ||
NativeRegistration.SessionOpen); | ||
SessionCloseDelegate = | ||
Marshal.GetDelegateForFunctionPointer<NativeMethods.SessionCloseDelegate>( | ||
NativeRegistration.SessionClose); | ||
SessionShutdownDelegate = | ||
Marshal.GetDelegateForFunctionPointer<NativeMethods.SessionShutdownDelegate>( | ||
NativeRegistration.SessionShutdown); | ||
|
||
ListenerOpenDelegate = | ||
Marshal.GetDelegateForFunctionPointer<NativeMethods.ListenerOpenDelegate>( | ||
NativeRegistration.ListenerOpen); | ||
ListenerCloseDelegate = | ||
Marshal.GetDelegateForFunctionPointer<NativeMethods.ListenerCloseDelegate>( | ||
NativeRegistration.ListenerClose); | ||
ListenerStartDelegate = | ||
Marshal.GetDelegateForFunctionPointer<NativeMethods.ListenerStartDelegate>( | ||
NativeRegistration.ListenerStart); | ||
ListenerStopDelegate = | ||
Marshal.GetDelegateForFunctionPointer<NativeMethods.ListenerStopDelegate>( | ||
NativeRegistration.ListenerStop); | ||
|
||
ConnectionOpenDelegate = | ||
Marshal.GetDelegateForFunctionPointer<NativeMethods.ConnectionOpenDelegate>( | ||
NativeRegistration.ConnectionOpen); | ||
ConnectionCloseDelegate = | ||
Marshal.GetDelegateForFunctionPointer<NativeMethods.ConnectionCloseDelegate>( | ||
NativeRegistration.ConnectionClose); | ||
ConnectionShutdownDelegate = | ||
Marshal.GetDelegateForFunctionPointer<NativeMethods.ConnectionShutdownDelegate>( | ||
NativeRegistration.ConnectionShutdown); | ||
ConnectionStartDelegate = | ||
Marshal.GetDelegateForFunctionPointer<NativeMethods.ConnectionStartDelegate>( | ||
NativeRegistration.ConnectionStart); | ||
|
||
StreamOpenDelegate = | ||
Marshal.GetDelegateForFunctionPointer<NativeMethods.StreamOpenDelegate>( | ||
NativeRegistration.StreamOpen); | ||
StreamCloseDelegate = | ||
Marshal.GetDelegateForFunctionPointer<NativeMethods.StreamCloseDelegate>( | ||
NativeRegistration.StreamClose); | ||
StreamStartDelegate = | ||
Marshal.GetDelegateForFunctionPointer<NativeMethods.StreamStartDelegate>( | ||
NativeRegistration.StreamStart); | ||
StreamShutdownDelegate = | ||
Marshal.GetDelegateForFunctionPointer<NativeMethods.StreamShutdownDelegate>( | ||
NativeRegistration.StreamShutdown); | ||
StreamSendDelegate = | ||
Marshal.GetDelegateForFunctionPointer<NativeMethods.StreamSendDelegate>( | ||
NativeRegistration.StreamSend); | ||
|
||
SetContextDelegate = | ||
Marshal.GetDelegateForFunctionPointer<NativeMethods.SetContextDelegate>( | ||
NativeRegistration.SetContext); | ||
GetContextDelegate = | ||
Marshal.GetDelegateForFunctionPointer<NativeMethods.GetContextDelegate>( | ||
NativeRegistration.GetContext); | ||
SetCallbackHandlerDelegate = | ||
Marshal.GetDelegateForFunctionPointer<NativeMethods.SetCallbackHandlerDelegate>( | ||
NativeRegistration.SetCallbackHandler); | ||
|
||
SetParamDelegate = | ||
Marshal.GetDelegateForFunctionPointer<NativeMethods.SetParamDelegate>( | ||
NativeRegistration.SetParam); | ||
GetParamDelegate = | ||
Marshal.GetDelegateForFunctionPointer<NativeMethods.GetParamDelegate>( | ||
NativeRegistration.GetParam); | ||
} | ||
|
||
internal NativeMethods.NativeApi NativeRegistration { get; private set; } | ||
|
||
internal NativeMethods.RegistrationOpenDelegate RegistrationOpenDelegate { get; private set; } | ||
internal NativeMethods.RegistrationCloseDelegate RegistrationCloseDelegate { get; private set; } | ||
|
||
internal NativeMethods.SecConfigCreateDelegate SecConfigCreateDelegate { get; private set; } | ||
internal NativeMethods.SecConfigCreateCompleteDelegate SecConfigCreateCompleteDelegate { get; private set; } | ||
internal NativeMethods.SecConfigDeleteDelegate SecConfigDeleteDelegate { get; private set; } | ||
|
||
internal NativeMethods.SessionOpenDelegate SessionOpenDelegate { get; private set; } | ||
internal NativeMethods.SessionCloseDelegate SessionCloseDelegate { get; private set; } | ||
internal NativeMethods.SessionShutdownDelegate SessionShutdownDelegate { get; private set; } | ||
|
||
internal NativeMethods.ListenerOpenDelegate ListenerOpenDelegate { get; private set; } | ||
internal NativeMethods.ListenerCloseDelegate ListenerCloseDelegate { get; private set; } | ||
internal NativeMethods.ListenerStartDelegate ListenerStartDelegate { get; private set; } | ||
internal NativeMethods.ListenerStopDelegate ListenerStopDelegate { get; private set; } | ||
|
||
internal NativeMethods.ConnectionOpenDelegate ConnectionOpenDelegate { get; private set; } | ||
internal NativeMethods.ConnectionCloseDelegate ConnectionCloseDelegate { get; private set; } | ||
internal NativeMethods.ConnectionShutdownDelegate ConnectionShutdownDelegate { get; private set; } | ||
internal NativeMethods.ConnectionStartDelegate ConnectionStartDelegate { get; private set; } | ||
|
||
internal NativeMethods.StreamOpenDelegate StreamOpenDelegate { get; private set; } | ||
internal NativeMethods.StreamCloseDelegate StreamCloseDelegate { get; private set; } | ||
internal NativeMethods.StreamStartDelegate StreamStartDelegate { get; private set; } | ||
internal NativeMethods.StreamShutdownDelegate StreamShutdownDelegate { get; private set; } | ||
internal NativeMethods.StreamSendDelegate StreamSendDelegate { get; private set; } | ||
internal NativeMethods.StreamReceiveCompleteDelegate StreamReceiveComplete { get; private set; } | ||
|
||
internal NativeMethods.SetContextDelegate SetContextDelegate { get; private set; } | ||
internal NativeMethods.GetContextDelegate GetContextDelegate { get; private set; } | ||
internal NativeMethods.SetCallbackHandlerDelegate SetCallbackHandlerDelegate { get; private set; } | ||
|
||
internal NativeMethods.SetParamDelegate SetParamDelegate { get; private set; } | ||
internal NativeMethods.GetParamDelegate GetParamDelegate { get; private set; } | ||
|
||
internal void RegistrationOpen(byte[] name) | ||
{ | ||
QuicStatusException.ThrowIfFailed(RegistrationOpenDelegate(name, out var ctx)); | ||
_registrationContext = ctx; | ||
} | ||
|
||
internal unsafe uint UnsafeSetParam( | ||
IntPtr Handle, | ||
uint Level, | ||
uint Param, | ||
NativeMethods.QuicBuffer Buffer) | ||
{ | ||
return SetParamDelegate( | ||
Handle, | ||
Level, | ||
Param, | ||
Buffer.Length, | ||
Buffer.Buffer); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
Dispose(disposing: true); | ||
GC.SuppressFinalize(this); | ||
} | ||
|
||
~MsQuicApi() | ||
{ | ||
Dispose(disposing: false); | ||
} | ||
|
||
private void Dispose(bool disposing) | ||
{ | ||
if (_disposed) | ||
{ | ||
return; | ||
} | ||
|
||
RegistrationCloseDelegate?.Invoke(_registrationContext); | ||
|
||
_disposed = true; | ||
} | ||
} | ||
} |
142 changes: 142 additions & 0 deletions
142
src/Servers/Kestrel/Transport.MsQuic/Internal/MsQuicConstants.cs
This file contains 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,142 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Runtime.InteropServices; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace Microsoft.AspNetCore.Server.Kestrel.Transport.MsQuic.Internal | ||
{ | ||
internal static class MsQuicConstants | ||
{ | ||
private const uint Success = 0; | ||
|
||
private const uint PendingWindows = 0x703E5; | ||
private const uint ContinueWindows = 0x704DE; | ||
private const uint OutOfMemoryWindows = 0x8007000E; | ||
private const uint InvalidParameterWindows = 0x80070057; | ||
private const uint InvalidStateWindows = 0x8007139F; | ||
private const uint NotSupportedWindows = 0x80004002; | ||
private const uint NotFoundWindows = 0x80070490; | ||
private const uint BufferTooSmallWindows = 0x8007007A; | ||
private const uint HandshakeFailureWindows = 0x80410000; | ||
private const uint AbortedWindows = 0x80004004; | ||
private const uint AddressInUseWindows = 0x80072740; | ||
private const uint ConnectionTimeoutWindows = 0x800704CF; | ||
private const uint ConnectionIdleWindows = 0x800704D4; | ||
private const uint InternalErrorWindows = 0x80004005; | ||
private const uint ServerBusyWindows = 0x800704C9; | ||
private const uint ProtocolErrorWindows = 0x800704CD; | ||
private const uint VerNegErrorWindows = 0x80410001; | ||
|
||
private const uint PendingLinux = unchecked((uint)-2); | ||
private const uint ContinueLinux = unchecked((uint)-1); | ||
private const uint OutOfMemoryLinux = 12; | ||
private const uint InvalidParameterLinux = 22; | ||
private const uint InvalidStateLinux = 200000002; | ||
private const uint NotSupportedLinux = 95; | ||
private const uint NotFoundLinux = 2; | ||
private const uint BufferTooSmallLinux = 75; | ||
private const uint HandshakeFailureLinux = 200000009; | ||
private const uint AbortedLinux = 200000008; | ||
private const uint AddressInUseLinux = 98; | ||
private const uint ConnectionTimeoutLinux = 110; | ||
private const uint ConnectionIdleLinux = 200000011; | ||
private const uint InternalErrorLinux = 200000012; | ||
private const uint ServerBusyLinux = 200000007; | ||
private const uint ProtocolErrorLinux = 200000013; | ||
private const uint VerNegErrorLinux = 200000014; | ||
|
||
internal static Func<uint, string> ErrorTypeFromErrorCode = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? GetErrorFromWindows : (Func<uint, string>)GetErrorFromLinux; | ||
|
||
public static string GetErrorFromWindows(uint status) | ||
{ | ||
switch (status) | ||
{ | ||
case Success: | ||
return "SUCCESS"; | ||
case PendingWindows: | ||
return "PENDING"; | ||
case ContinueWindows: | ||
return "CONTINUE"; | ||
case OutOfMemoryWindows: | ||
return "OUT_OF_MEMORY"; | ||
case InvalidParameterWindows: | ||
return "INVALID_PARAMETER"; | ||
jkotalik marked this conversation as resolved.
Show resolved
Hide resolved
|
||
case InvalidStateWindows: | ||
return "INVALID_STATE"; | ||
case NotSupportedWindows: | ||
return "NOT_SUPPORTED"; | ||
case NotFoundWindows: | ||
return "NOT_FOUND"; | ||
case BufferTooSmallWindows: | ||
return "BUFFER_TOO_SMALL"; | ||
case HandshakeFailureWindows: | ||
return "HANDSHAKE_FAILURE"; | ||
case AbortedWindows: | ||
return "ABORTED"; | ||
case AddressInUseWindows: | ||
return "ADDRESS_IN_USE"; | ||
case ConnectionTimeoutWindows: | ||
return "CONNECTION_TIMEOUT"; | ||
case ConnectionIdleWindows: | ||
return "CONNECTION_IDLE"; | ||
case InternalErrorWindows: | ||
return "INTERNAL_ERROR"; | ||
case ServerBusyWindows: | ||
return "SERVER_BUSY"; | ||
case ProtocolErrorWindows: | ||
return "PROTOCOL_ERROR"; | ||
case VerNegErrorWindows: | ||
return "VER_NEG_ERROR"; | ||
} | ||
|
||
return status.ToString(); | ||
} | ||
|
||
public static string GetErrorFromLinux(uint status) | ||
{ | ||
switch (status) | ||
{ | ||
case Success: | ||
return "SUCCESS"; | ||
case PendingLinux: | ||
return "PENDING"; | ||
case ContinueLinux: | ||
return "CONTINUE"; | ||
case OutOfMemoryLinux: | ||
return "OUT_OF_MEMORY"; | ||
case InvalidParameterLinux: | ||
return "INVALID_PARAMETER"; | ||
case InvalidStateLinux: | ||
return "INVALID_STATE"; | ||
case NotSupportedLinux: | ||
return "NOT_SUPPORTED"; | ||
case NotFoundLinux: | ||
return "NOT_FOUND"; | ||
case BufferTooSmallLinux: | ||
return "BUFFER_TOO_SMALL"; | ||
case HandshakeFailureLinux: | ||
return "HANDSHAKE_FAILURE"; | ||
case AbortedLinux: | ||
return "ABORTED"; | ||
case AddressInUseLinux: | ||
return "ADDRESS_IN_USE"; | ||
case ConnectionTimeoutLinux: | ||
return "CONNECTION_TIMEOUT"; | ||
case ConnectionIdleLinux: | ||
return "CONNECTION_IDLE"; | ||
case InternalErrorLinux: | ||
return "INTERNAL_ERROR"; | ||
case ServerBusyLinux: | ||
return "SERVER_BUSY"; | ||
case ProtocolErrorLinux: | ||
return "PROTOCOL_ERROR"; | ||
case VerNegErrorLinux: | ||
return "VER_NEG_ERROR"; | ||
} | ||
|
||
return status.ToString(); | ||
} | ||
} | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a little bit clever s.t. we don't need to continuously do runtime checks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, I think that makes sense. We just need to do the platform check once rather than each time we formulate an error code.