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 20 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
41 changes: 41 additions & 0 deletions include/envoy/network/connection_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,50 @@ 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 ListenerPtr& 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.

nit: I would return a raw pointer here vs. a reference to the smart_ptr, or better an optional reference wrapper if it can be null.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually the caller will call reset() on it...

Copy link
Member

Choose a reason for hiding this comment

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

nit: I think it would be better to provide an explicit shutdown(), destroy(), close(), etc. method would be clearer from an interface perspective.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

wrapping all the interfaces in Network::Listener doesn't help with unique_ptr::reset(). This interface allows underlying Listener object to be destroyed anytime.

Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure I follow. Can't you just have an extra method on this interface called close() or destroy() or something? Exposing the unique ptr so that the caller can call reset on it seems pretty strange? I think the intent is to just destroy it?

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'm not sure I if understand the intention of reset(), but I think we want to destroy the listener but keep the entry in the container? We could expose close() and destroy() and hide listner(), but how to handle reset() call then?

Copy link
Member

Choose a reason for hiding this comment

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

I don't follow? Is the only purpose of exposing the listener pointer so that it can be reset/deleted? If so yeah can we just swap it with a close/destroy function?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ah, I see what you mean. Yes, listener().reset() can be wrapped in a new interface destroy(). However, ConnectionHandlerImpl::findListenerByAddress() expects to return a Listener&, which returns ActiveListener::listener() directly.

Copy link
Member

Choose a reason for hiding this comment

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

Right what I'm saying is to have 2 methods, 1 to return the listener which is an optional reference wrapper, and an explicit destroy method. I think that will be much more clear. Thank you!

/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.

Got it. I make ActiveListener to inherit from Listener and added destroy() to it. And I also changed listener() to return a pointer to have less change to conn_handler_impl

};

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 config provides information needed to create ActiveUdpListener and
* UdpListener objects.
* @return the ActiveUdpListener created.
*/
virtual Network::ConnectionHandler::ActiveListenerPtr
createActiveUdpListener(ConnectionHandler& parent, 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 @@ -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
13 changes: 13 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,7 @@ envoy_cc_library(
":filter_chain_manager_lib",
":lds_api_lib",
":transport_socket_config_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 +442,14 @@ envoy_cc_library(
"//include/envoy/server:transport_socket_config_interface",
],
)

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",
"//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"

namespace Envoy {
namespace Server {

Network::ConnectionHandler::ActiveListenerPtr
ActiveRawUdpListenerFactory::createActiveUdpListener(Network::ConnectionHandler& parent,
Network::ListenerConfig& config) const {
return std::make_unique<ConnectionHandlerImpl::ActiveUdpListener>(
dynamic_cast<ConnectionHandlerImpl&>(parent), config);
mattklein123 marked this conversation as resolved.
Show resolved Hide resolved
}

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

std::string ActiveRawUdpListenerConfigFactory::name() { return "raw_udp_listener"; }
mattklein123 marked this conversation as resolved.
Show resolved Hide resolved

REGISTER_FACTORY(ActiveRawUdpListenerConfigFactory, Server::ActiveUdpListenerConfigFactory);

} // namespace Server
} // namespace Envoy
30 changes: 30 additions & 0 deletions source/server/active_raw_udp_listener_config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#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 {
Network::ConnectionHandler::ActiveListenerPtr
createActiveUdpListener(Network::ConnectionHandler& parent,
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 {

mattklein123 marked this conversation as resolved.
Show resolved Hide resolved
Network::ActiveUdpListenerFactoryPtr
createActiveUdpListenerFactory(const Protobuf::Message&) override;

std::string name() override;
};

DECLARE_FACTORY(ActiveRawUdpListenerConfigFactory);

} // namespace Server
} // namespace Envoy
Loading