Skip to content

Commit

Permalink
udp_listener: refactor ActiveUdpListener creation (envoyproxy#7884)
Browse files Browse the repository at this point in the history
Signed-off-by: Dan Zhang <[email protected]>
  • Loading branch information
danzh2010 authored and venilnoronha committed Nov 8, 2019
1 parent de61162 commit 680bf1b
Show file tree
Hide file tree
Showing 25 changed files with 386 additions and 102 deletions.
2 changes: 2 additions & 0 deletions api/envoy/api/v2/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ api_proto_library_internal(
"//envoy/api/v2/core:address",
"//envoy/api/v2/core:base",
"//envoy/api/v2/listener",
"//envoy/api/v2/listener:udp_listener_config",
],
)

Expand All @@ -120,6 +121,7 @@ api_go_grpc_library(
"//envoy/api/v2/core:address_go_proto",
"//envoy/api/v2/core:base_go_proto",
"//envoy/api/v2/listener:listener_go_proto",
"//envoy/api/v2/listener:udp_listener_config_go_proto",
],
)

Expand Down
11 changes: 10 additions & 1 deletion api/envoy/api/v2/lds.proto
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import "envoy/api/v2/core/address.proto";
import "envoy/api/v2/core/base.proto";
import "envoy/api/v2/discovery.proto";
import "envoy/api/v2/listener/listener.proto";
import "envoy/api/v2/listener/udp_listener_config.proto";

import "google/api/annotations.proto";
import "google/protobuf/duration.proto";
Expand Down Expand Up @@ -44,7 +45,7 @@ service ListenerDiscoveryService {
}
}

// [#comment:next free field: 18]
// [#comment:next free field: 19]
message Listener {
// The unique name by which this listener is known. If no name is provided,
// Envoy will allocate an internal UUID for the listener. If the listener is to be dynamically
Expand Down Expand Up @@ -194,4 +195,12 @@ message Listener {

// Specifies the intended direction of the traffic relative to the local Envoy.
core.TrafficDirection traffic_direction = 16;

// If the protocol in the listener socket address in :ref:`protocol
// <envoy_api_field_core.SocketAddress.protocol>` is :ref:'UDP
// <envoy_api_field_core.Protocol.UDP>`, this field specifies the actual udp listener to create,
// i.e. :ref:`udp_listener_name
// <envoy_api_field_listener.UdpListenerConfig.udp_listener_name>` = "raw_udp_listener" for
// creating a packet-oriented UDP listener. If not present, treat it as "raw_udp_listener".
listener.UdpListenerConfig udp_listener_config = 18;
}
17 changes: 17 additions & 0 deletions api/envoy/api/v2/listener/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,20 @@ api_go_proto_library(
"//envoy/api/v2/core:base_go_proto",
],
)

api_proto_library_internal(
name = "udp_listener_config",
srcs = ["udp_listener_config.proto"],
visibility = ["//envoy/api/v2:friends"],
deps = [
"//envoy/api/v2/core:base",
],
)

api_go_proto_library(
name = "udp_listener_config",
proto = ":udp_listener_config",
deps = [
"//envoy/api/v2/core:base_go_proto",
],
)
31 changes: 31 additions & 0 deletions api/envoy/api/v2/listener/udp_listener_config.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
syntax = "proto3";

package envoy.api.v2.listener;

option java_outer_classname = "ListenerProto";
option java_multiple_files = true;
option java_package = "io.envoyproxy.envoy.api.v2.listener";
option go_package = "listener";
option csharp_namespace = "Envoy.Api.V2.ListenerNS";
option ruby_package = "Envoy::Api::V2::ListenerNS";

import "google/protobuf/struct.proto";
import "google/protobuf/any.proto";

// [#protodoc-title: Udp Listener Config]
// Listener :ref:`configuration overview <config_listeners>`

message UdpListenerConfig {
// Used to look up UDP listener factory, matches "raw_udp_listener" or
// "quic_listener" to create a specific udp listener.
// If not specified, treat as "raw_udp_listener".
string udp_listener_name = 1;

// Used to create a specific listener factory. To some factory, e.g.
// "raw_udp_listener", config is not needed.
oneof config_type {
google.protobuf.Struct config = 2;

google.protobuf.Any typed_config = 3;
}
}
1 change: 1 addition & 0 deletions docs/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ PROTO_RST="
/envoy/api/v2/srds/envoy/api/v2/srds.proto.rst
/envoy/api/v2/lds/envoy/api/v2/lds.proto.rst
/envoy/api/v2/listener/listener/envoy/api/v2/listener/listener.proto.rst
/envoy/api/v2/listener/udp_listener_config/envoy/api/v2/listener/udp_listener_config.proto.rst
/envoy/api/v2/ratelimit/ratelimit/envoy/api/v2/ratelimit/ratelimit.proto.rst
/envoy/config/accesslog/v2/als/envoy/config/accesslog/v2/als.proto.rst
/envoy/config/accesslog/v2/file/envoy/config/accesslog/v2/file.proto.rst
Expand Down
1 change: 1 addition & 0 deletions docs/root/api-v2/listeners/listeners.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ Listeners

../api/v2/lds.proto
../api/v2/listener/listener.proto
../api/v2/listener/udp_listener_config.proto
50 changes: 50 additions & 0 deletions include/envoy/network/connection_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,59 @@ class ConnectionHandler {
* after they have been temporarily disabled.
*/
virtual void enableListeners() PURE;

/**
* Used by ConnectionHandler to manage listeners.
*/
class ActiveListener {
public:
virtual ~ActiveListener() = default;

/**
* @return the tag value as configured.
*/
virtual uint64_t listenerTag() PURE;
/**
* @return the actual Listener object.
*/
virtual Listener* listener() PURE;
/**
* Destroy the actual Listener it wraps.
*/
virtual void destroy() PURE;
};

using ActiveListenerPtr = std::unique_ptr<ActiveListener>;
};

using ConnectionHandlerPtr = std::unique_ptr<ConnectionHandler>;

/**
* A registered factory interface to create different kinds of
* ActiveUdpListener.
*/
class ActiveUdpListenerFactory {
public:
virtual ~ActiveUdpListenerFactory() = default;

/**
* Creates an ActiveUdpListener object and a corresponding UdpListener
* according to given config.
* @param parent is the owner of the created ActiveListener objects.
* @param dispatcher is used to create actual UDP listener.
* @param logger might not need to be passed in.
* TODO(danzh): investigate if possible to use statically defined logger in ActiveUdpListener
* implementation instead.
* @param config provides information needed to create ActiveUdpListener and
* UdpListener objects.
* @return the ActiveUdpListener created.
*/
virtual ConnectionHandler::ActiveListenerPtr
createActiveUdpListener(ConnectionHandler& parent, Event::Dispatcher& disptacher,
spdlog::logger& logger, Network::ListenerConfig& config) const PURE;
};

using ActiveUdpListenerFactoryPtr = std::unique_ptr<ActiveUdpListenerFactory>;

} // namespace Network
} // namespace Envoy
7 changes: 7 additions & 0 deletions include/envoy/network/listener.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ namespace Envoy {
namespace Network {

class UdpListenerFilterManager;
class ActiveUdpListenerFactory;

/**
* A configuration for an individual listener.
Expand Down Expand Up @@ -90,6 +91,12 @@ class ListenerConfig {
* @return const std::string& the listener's name.
*/
virtual const std::string& name() const PURE;

/**
* @return factory pointer if listening on UDP socket, otherwise return
* nullptr.
*/
virtual const ActiveUdpListenerFactory* udpListenerFactory() PURE;
};

/**
Expand Down
6 changes: 6 additions & 0 deletions include/envoy/server/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -274,3 +274,9 @@ envoy_cc_library(
"//include/envoy/event:dispatcher_interface",
],
)

envoy_cc_library(
name = "active_udp_listener_config_interface",
hdrs = ["active_udp_listener_config.h"],
deps = ["//include/envoy/network:connection_handler_interface"],
)
29 changes: 29 additions & 0 deletions include/envoy/server/active_udp_listener_config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#pragma once

#include "envoy/network/connection_handler.h"

namespace Envoy {
namespace Server {

/**
* Interface to create udp listener according to
* envoy::api::v2::listener::UdpListenerConfig.udp_listener_name.
*/
class ActiveUdpListenerConfigFactory {
public:
virtual ~ActiveUdpListenerConfigFactory() = default;

/**
* Create an ActiveUdpListenerFactory object according to given message.
*/
virtual Network::ActiveUdpListenerFactoryPtr
createActiveUdpListenerFactory(const Protobuf::Message& message) PURE;

/**
* Used to identify which udp listener to create: quic or raw udp.
*/
virtual std::string name() PURE;
};

} // namespace Server
} // namespace Envoy
21 changes: 21 additions & 0 deletions source/server/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ envoy_cc_library(
"//include/envoy/network:filter_interface",
"//include/envoy/network:listen_socket_interface",
"//include/envoy/network:listener_interface",
"//include/envoy/server:active_udp_listener_config_interface",
"//include/envoy/server:listener_manager_interface",
"//include/envoy/stats:timespan",
"//source/common/common:linked_object",
Expand Down Expand Up @@ -261,6 +262,8 @@ envoy_cc_library(
":filter_chain_manager_lib",
":lds_api_lib",
":transport_socket_config_lib",
":well_known_names_lib",
"//include/envoy/server:active_udp_listener_config_interface",
"//include/envoy/server:filter_config_interface",
"//include/envoy/server:listener_manager_interface",
"//include/envoy/server:transport_socket_config_interface",
Expand Down Expand Up @@ -452,3 +455,21 @@ envoy_cc_library(
"//include/envoy/server:transport_socket_config_interface",
],
)

envoy_cc_library(
name = "well_known_names_lib",
hdrs = ["well_known_names.h"],
deps = ["//source/common/singleton:const_singleton"],
)

envoy_cc_library(
name = "active_raw_udp_listener_config",
srcs = ["active_raw_udp_listener_config.cc"],
hdrs = ["active_raw_udp_listener_config.h"],
deps = [
":connection_handler_lib",
":well_known_names_lib",
"//include/envoy/registry",
"//include/envoy/server:active_udp_listener_config_interface",
],
)
26 changes: 26 additions & 0 deletions source/server/active_raw_udp_listener_config.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include "server/active_raw_udp_listener_config.h"

#include "server/connection_handler_impl.h"
#include "server/well_known_names.h"

namespace Envoy {
namespace Server {

Network::ConnectionHandler::ActiveListenerPtr ActiveRawUdpListenerFactory::createActiveUdpListener(
Network::ConnectionHandler& /*parent*/, Event::Dispatcher& dispatcher,
spdlog::logger& /*logger*/, Network::ListenerConfig& config) const {
return std::make_unique<ActiveUdpListener>(dispatcher, config);
}

Network::ActiveUdpListenerFactoryPtr
ActiveRawUdpListenerConfigFactory::createActiveUdpListenerFactory(
const Protobuf::Message& /*message*/) {
return std::make_unique<Server::ActiveRawUdpListenerFactory>();
}

std::string ActiveRawUdpListenerConfigFactory::name() { return UdpListenerNames::get().RawUdp; }

REGISTER_FACTORY(ActiveRawUdpListenerConfigFactory, Server::ActiveUdpListenerConfigFactory);

} // namespace Server
} // namespace Envoy
31 changes: 31 additions & 0 deletions source/server/active_raw_udp_listener_config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#pragma once

#include "envoy/network/connection_handler.h"
#include "envoy/registry/registry.h"
#include "envoy/server/active_udp_listener_config.h"

namespace Envoy {
namespace Server {

class ActiveRawUdpListenerFactory : public Network::ActiveUdpListenerFactory {
public:
Network::ConnectionHandler::ActiveListenerPtr
createActiveUdpListener(Network::ConnectionHandler& parent, Event::Dispatcher& disptacher,
spdlog::logger& logger, Network::ListenerConfig& config) const override;
};

// This class uses a protobuf config to create a UDP listener factory which
// creates a Server::ConnectionHandlerImpl::ActiveUdpListener.
// This is the default UDP listener if not specified in config.
class ActiveRawUdpListenerConfigFactory : public ActiveUdpListenerConfigFactory {
public:
Network::ActiveUdpListenerFactoryPtr
createActiveUdpListenerFactory(const Protobuf::Message&) override;

std::string name() override;
};

DECLARE_FACTORY(ActiveRawUdpListenerConfigFactory);

} // namespace Server
} // namespace Envoy
Loading

0 comments on commit 680bf1b

Please sign in to comment.