HTTP/2 support
HTTP/2 gives a number of optimizations, while keeping support for the
protocol. HTTP/2 support was recently added to the Redfish
specification. The largest performance increase in bmc usage is likely
header compression. Almost all requests reuse the same header values,
so the hpack based compression scheme in HTTP/2 allows OpenBMC to be
more efficient as a transport, and has the potential to significantly
reduce the number of bytes we're sending on the wire.
This commit adds HTTP2 support to bmcweb through nghttp2 library. When
static linked into bmcweb, this support adds 53.4KB to the bmcweb binary
size. nghttp2 is available in meta-oe already.
Given the experimental nature of this option, it is added under the
meson option "experimental-http2" and disabled by default. The hope is
to enable it at some point in the future.
To accomplish the above, there a new class, HTTP2Connection is created.
This is intended to isolate HTTP/2 connections code from HttpConnection
such that it is far less likely to cause bugs, although it does
duplicate about 20 lines of code (async_read_some, async_write_some,
buffers, etc). This seems worth it for the moment.
In a similar way to Websockets, when an HTTP/2 connection is detected
through ALPN, the HTTP2Connection class will be instantiated, and the
socket object passed to it, thus allowing the Connection class to be
destroyed, and the HTTP2Connection to take over for the user.
Tested: Redfish service validator passes with option enabled
With option disabled
GET /redfish/v1 in curl shows ALPN non negotiation, and fallback to
http1.1
With the option enable
GET /redfish/v1 in curl shows ALPN negotiates to HTTP2
Change-Id: I7839e457e0ba918b0695e04babddd0925ed3383c
Signed-off-by: Ed Tanous <edtanous@google.com>
diff --git a/http/nghttp2_adapters.hpp b/http/nghttp2_adapters.hpp
new file mode 100644
index 0000000..3c1f549
--- /dev/null
+++ b/http/nghttp2_adapters.hpp
@@ -0,0 +1,152 @@
+#pragma once
+
+extern "C"
+{
+#include <nghttp2/nghttp2.h>
+}
+
+#include "logging.hpp"
+
+#include <span>
+
+/* This file contains RAII compatible adapters for nghttp2 structures. They
+ * attempt to be as close to a direct call as possible, while keeping the RAII
+ * lifetime safety for the various classes.*/
+
+struct nghttp2_session;
+
+struct nghttp2_session_callbacks
+{
+ friend nghttp2_session;
+ nghttp2_session_callbacks()
+ {
+ nghttp2_session_callbacks_new(&ptr);
+ }
+
+ ~nghttp2_session_callbacks()
+ {
+ nghttp2_session_callbacks_del(ptr);
+ }
+
+ nghttp2_session_callbacks(const nghttp2_session_callbacks&) = delete;
+ nghttp2_session_callbacks&
+ operator=(const nghttp2_session_callbacks&) = delete;
+ nghttp2_session_callbacks(nghttp2_session_callbacks&&) = delete;
+ nghttp2_session_callbacks& operator=(nghttp2_session_callbacks&&) = delete;
+
+ void setSendCallback(nghttp2_send_callback sendCallback)
+ {
+ nghttp2_session_callbacks_set_send_callback(ptr, sendCallback);
+ }
+
+ void setOnFrameRecvCallback(nghttp2_on_frame_recv_callback recvCallback)
+ {
+ nghttp2_session_callbacks_set_on_frame_recv_callback(ptr, recvCallback);
+ }
+
+ void setOnStreamCloseCallback(nghttp2_on_stream_close_callback onClose)
+ {
+ nghttp2_session_callbacks_set_on_stream_close_callback(ptr, onClose);
+ }
+
+ void setOnHeaderCallback(nghttp2_on_header_callback onHeader)
+ {
+ nghttp2_session_callbacks_set_on_header_callback(ptr, onHeader);
+ }
+
+ void setOnBeginHeadersCallback(
+ nghttp2_on_begin_headers_callback onBeginHeaders)
+ {
+ nghttp2_session_callbacks_set_on_begin_headers_callback(ptr,
+ onBeginHeaders);
+ }
+
+ void setSendDataCallback(nghttp2_send_data_callback onSendData)
+ {
+ nghttp2_session_callbacks_set_send_data_callback(ptr, onSendData);
+ }
+ void setBeforeFrameSendCallback(
+ nghttp2_before_frame_send_callback beforeSendFrame)
+ {
+ nghttp2_session_callbacks_set_before_frame_send_callback(
+ ptr, beforeSendFrame);
+ }
+ void
+ setAfterFrameSendCallback(nghttp2_on_frame_send_callback afterSendFrame)
+ {
+ nghttp2_session_callbacks_set_on_frame_send_callback(ptr,
+ afterSendFrame);
+ }
+ void setAfterFrameNoSendCallback(
+ nghttp2_on_frame_not_send_callback afterSendFrame)
+ {
+ nghttp2_session_callbacks_set_on_frame_not_send_callback(
+ ptr, afterSendFrame);
+ }
+
+ private:
+ nghttp2_session_callbacks* get()
+ {
+ return ptr;
+ }
+
+ nghttp2_session_callbacks* ptr = nullptr;
+};
+
+struct nghttp2_session
+{
+ explicit nghttp2_session(nghttp2_session_callbacks& callbacks)
+ {
+ if (nghttp2_session_server_new(&ptr, callbacks.get(), nullptr) != 0)
+ {
+ BMCWEB_LOG_ERROR << "nghttp2_session_server_new failed";
+ return;
+ }
+ }
+
+ ~nghttp2_session()
+ {
+ nghttp2_session_del(ptr);
+ }
+
+ // explicitly uncopyable
+ nghttp2_session(const nghttp2_session&) = delete;
+ nghttp2_session& operator=(const nghttp2_session&) = delete;
+
+ nghttp2_session(nghttp2_session&& other) noexcept : ptr(other.ptr)
+ {
+ other.ptr = nullptr;
+ }
+
+ nghttp2_session& operator=(nghttp2_session&& other) noexcept = delete;
+
+ int submitSettings(std::span<nghttp2_settings_entry> iv)
+ {
+ return nghttp2_submit_settings(ptr, NGHTTP2_FLAG_NONE, iv.data(),
+ iv.size());
+ }
+ void setUserData(void* object)
+ {
+ nghttp2_session_set_user_data(ptr, object);
+ }
+
+ ssize_t memRecv(std::span<const uint8_t> buffer)
+ {
+ return nghttp2_session_mem_recv(ptr, buffer.data(), buffer.size());
+ }
+
+ ssize_t send()
+ {
+ return nghttp2_session_send(ptr);
+ }
+
+ int submitResponse(int32_t streamId, std::span<const nghttp2_nv> headers,
+ const nghttp2_data_provider* dataPrd)
+ {
+ return nghttp2_submit_response(ptr, streamId, headers.data(),
+ headers.size(), dataPrd);
+ }
+
+ private:
+ nghttp2_session* ptr = nullptr;
+};