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

udp_listener: refactor ActiveUdpListener creation #7884

Merged
merged 28 commits into from
Aug 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
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
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;
mattklein123 marked this conversation as resolved.
Show resolved Hide resolved
}
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;
mattklein123 marked this conversation as resolved.
Show resolved Hide resolved

// 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;
mattklein123 marked this conversation as resolved.
Show resolved Hide resolved
/**
* @return the actual Listener object.
*/
virtual Listener* listener() PURE;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry to keep asking for changes here, but why do we need this accessor if we have enable/disable below? Can we have one or the other? Can't we just enable/disable through this?

/wait-any

Copy link
Contributor Author

@danzh2010 danzh2010 Aug 26, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ConnectionHandlerImpl::findListenerByAddress() returns a reference to it. We discussed this before and agree'ed to have both interfaces: https://github.com/envoyproxy/envoy/pull/7884/files/75898021f385722f5f465adf7b1f403a20819ea1#r317200472

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right that's fine. What I'm saying is there is no reason to expose the full listener as well as disable/enable, etc. I would remove disable and enable. The reason to have an explicit destroy method is to avoid exposing the unique_ptr by reference, which could be filled by the caller with a different listener. Does that make sense?

/wait

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. Removed.

/**
* 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 {
mattklein123 marked this conversation as resolved.
Show resolved Hide resolved
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;
mattklein123 marked this conversation as resolved.
Show resolved Hide resolved
};

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 @@ -255,3 +255,9 @@ envoy_cc_library(
"@envoy_api//envoy/config/trace/v2:trace_cc",
],
)

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 @@ -64,6 +64,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 @@ -260,6 +261,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 @@ -440,3 +443,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.
mattklein123 marked this conversation as resolved.
Show resolved Hide resolved
class ActiveRawUdpListenerConfigFactory : public ActiveUdpListenerConfigFactory {
public:
Network::ActiveUdpListenerFactoryPtr
createActiveUdpListenerFactory(const Protobuf::Message&) override;

std::string name() override;
};

DECLARE_FACTORY(ActiveRawUdpListenerConfigFactory);

} // namespace Server
} // namespace Envoy
Loading