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

Initial port of MsQuic transport #15375

Merged
merged 24 commits into from
Nov 1, 2019
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions eng/ProjectReferences.props
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
<ProjectReferenceProvider Include="Microsoft.AspNetCore.Server.Kestrel.Core" ProjectPath="$(RepoRoot)src\Servers\Kestrel\Core\src\Microsoft.AspNetCore.Server.Kestrel.Core.csproj" RefProjectPath="$(RepoRoot)src\Servers\Kestrel\Core\ref\Microsoft.AspNetCore.Server.Kestrel.Core.csproj" />
<ProjectReferenceProvider Include="Microsoft.AspNetCore.Server.Kestrel" ProjectPath="$(RepoRoot)src\Servers\Kestrel\Kestrel\src\Microsoft.AspNetCore.Server.Kestrel.csproj" RefProjectPath="$(RepoRoot)src\Servers\Kestrel\Kestrel\ref\Microsoft.AspNetCore.Server.Kestrel.csproj" />
<ProjectReferenceProvider Include="Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv" ProjectPath="$(RepoRoot)src\Servers\Kestrel\Transport.Libuv\src\Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv.csproj" RefProjectPath="$(RepoRoot)src\Servers\Kestrel\Transport.Libuv\ref\Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv.csproj" />
<ProjectReferenceProvider Include="Microsoft.AspNetCore.Server.Kestrel.Transport.MsQuic" ProjectPath="$(RepoRoot)src\Servers\Kestrel\Transport.MsQuic\src\Microsoft.AspNetCore.Server.Kestrel.Transport.MsQuic.csproj" RefProjectPath="$(RepoRoot)src\Servers\Kestrel\Transport.MsQuic\ref\Microsoft.AspNetCore.Server.Kestrel.Transport.MsQuic.csproj" />
<ProjectReferenceProvider Include="Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets" ProjectPath="$(RepoRoot)src\Servers\Kestrel\Transport.Sockets\src\Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.csproj" RefProjectPath="$(RepoRoot)src\Servers\Kestrel\Transport.Sockets\ref\Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.csproj" />
<ProjectReferenceProvider Include="Microsoft.AspNetCore.Authentication.Certificate" ProjectPath="$(RepoRoot)src\Security\Authentication\Certificate\src\Microsoft.AspNetCore.Authentication.Certificate.csproj" RefProjectPath="$(RepoRoot)src\Security\Authentication\Certificate\ref\Microsoft.AspNetCore.Authentication.Certificate.csproj" />
<ProjectReferenceProvider Include="Microsoft.AspNetCore.Authentication.Cookies" ProjectPath="$(RepoRoot)src\Security\Authentication\Cookies\src\Microsoft.AspNetCore.Authentication.Cookies.csproj" RefProjectPath="$(RepoRoot)src\Security\Authentication\Cookies\ref\Microsoft.AspNetCore.Authentication.Cookies.csproj" />
Expand Down
15 changes: 15 additions & 0 deletions src/Servers/Kestrel/Kestrel.sln
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AspNetCore.WebUti
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "http2cat", "samples\http2cat\http2cat.csproj", "{3D6821F5-F242-4828-8DDE-89488E85512D}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AspNetCore.Server.Kestrel.Transport.MsQuic", "Transport.MsQuic\Microsoft.AspNetCore.Server.Kestrel.Transport.MsQuic.csproj", "{61B6BB7A-6FDA-4A97-BEB7-7E2DEE8E943D}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -466,6 +468,18 @@ Global
{3D6821F5-F242-4828-8DDE-89488E85512D}.Release|x64.Build.0 = Release|Any CPU
{3D6821F5-F242-4828-8DDE-89488E85512D}.Release|x86.ActiveCfg = Release|Any CPU
{3D6821F5-F242-4828-8DDE-89488E85512D}.Release|x86.Build.0 = Release|Any CPU
{61B6BB7A-6FDA-4A97-BEB7-7E2DEE8E943D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{61B6BB7A-6FDA-4A97-BEB7-7E2DEE8E943D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{61B6BB7A-6FDA-4A97-BEB7-7E2DEE8E943D}.Debug|x64.ActiveCfg = Debug|Any CPU
{61B6BB7A-6FDA-4A97-BEB7-7E2DEE8E943D}.Debug|x64.Build.0 = Debug|Any CPU
{61B6BB7A-6FDA-4A97-BEB7-7E2DEE8E943D}.Debug|x86.ActiveCfg = Debug|Any CPU
{61B6BB7A-6FDA-4A97-BEB7-7E2DEE8E943D}.Debug|x86.Build.0 = Debug|Any CPU
{61B6BB7A-6FDA-4A97-BEB7-7E2DEE8E943D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{61B6BB7A-6FDA-4A97-BEB7-7E2DEE8E943D}.Release|Any CPU.Build.0 = Release|Any CPU
{61B6BB7A-6FDA-4A97-BEB7-7E2DEE8E943D}.Release|x64.ActiveCfg = Release|Any CPU
{61B6BB7A-6FDA-4A97-BEB7-7E2DEE8E943D}.Release|x64.Build.0 = Release|Any CPU
{61B6BB7A-6FDA-4A97-BEB7-7E2DEE8E943D}.Release|x86.ActiveCfg = Release|Any CPU
{61B6BB7A-6FDA-4A97-BEB7-7E2DEE8E943D}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -503,6 +517,7 @@ Global
{E0AD50A3-2518-4060-8BB9-5649B04B3A6D} = {F0A1281A-B512-49D2-8362-21EE32B3674F}
{EE45763C-753D-4228-8E5D-A71F8BDB3D89} = {F0A1281A-B512-49D2-8362-21EE32B3674F}
{3D6821F5-F242-4828-8DDE-89488E85512D} = {F826BA61-60A9-45B6-AF29-FD1A6E313EF0}
{61B6BB7A-6FDA-4A97-BEB7-7E2DEE8E943D} = {2B456D08-F72B-4EB8-B663-B6D78FC04BF8}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {48207B50-7D05-4B10-B585-890FE0F4FCE1}
Expand Down
186 changes: 186 additions & 0 deletions src/Servers/Kestrel/Transport.MsQuic/Internal/MsQuicApi.cs
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 src/Servers/Kestrel/Transport.MsQuic/Internal/MsQuicConstants.cs
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;
Copy link
Contributor Author

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.

Copy link
Contributor

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.


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();
}
}
}
Loading