1
#pragma once
2

            
3
#include "envoy/extensions/transport_sockets/tap/v3/tap.pb.h"
4
#include "envoy/network/connection.h"
5
#include "envoy/stats/scope.h"
6
#include "envoy/stats/stats_macros.h"
7

            
8
#include "source/extensions/common/tap/tap.h"
9

            
10
namespace Envoy {
11
namespace Extensions {
12
namespace TransportSockets {
13
namespace Tap {
14

            
15
/**
16
 * All stats for the tap filter. @see stats_macros.h
17
 */
18
#define ALL_TRANSPORT_TAP_STATS(COUNTER)                                                           \
19
17
  COUNTER(streamed_submit)                                                                         \
20
17
  COUNTER(buffered_submit)
21

            
22
/**
23
 * Wrapper struct for tap filter stats. @see stats_macros.h
24
 */
25
struct TransportTapStats {
26
  ALL_TRANSPORT_TAP_STATS(GENERATE_COUNTER_STRUCT)
27
};
28

            
29
/**
30
 * Per-socket tap implementation. Abstractly handles all socket lifecycle events in order to tap
31
 * if the configuration matches.
32
 */
33
class PerSocketTapper {
34
public:
35
17
  virtual ~PerSocketTapper() = default;
36

            
37
  /**
38
   * Called when the socket is closed.
39
   * @param event supplies the close type.
40
   */
41
  virtual void closeSocket(Network::ConnectionEvent event) PURE;
42

            
43
  /**
44
   * Called when data is read from the underlying transport.
45
   * @param data supplies the read data.
46
   * @param bytes_read supplies the number of bytes read (data might already have bytes in it).
47
   */
48
  virtual void onRead(const Buffer::Instance& data, uint32_t bytes_read) PURE;
49

            
50
  /**
51
   * Called when data is written to the underlying transport.
52
   * @param data supplies the written data.
53
   * @param bytes_written supplies the number of bytes written (data might not have been fully
54
   *                      written).
55
   * @param end_stream supplies whether this is the end of socket writes.
56
   */
57
  virtual void onWrite(const Buffer::Instance& data, uint32_t bytes_written, bool end_stream) PURE;
58
};
59

            
60
using PerSocketTapperPtr = std::unique_ptr<PerSocketTapper>;
61

            
62
/**
63
 * Abstract socket tap configuration.
64
 */
65
class SocketTapConfig : public virtual Extensions::Common::Tap::TapConfig {
66
public:
67
  /**
68
   * @return a new per-socket tapper which is used to handle tapping of a discrete socket.
69
   * @param connection supplies the underlying network connection.
70
   */
71
  virtual PerSocketTapperPtr createPerSocketTapper(
72
      const envoy::extensions::transport_sockets::tap::v3::SocketTapConfig& tap_config,
73
      const TransportTapStats& stats, const Network::Connection& connection) PURE;
74

            
75
  /**
76
   * @return time source to use for stamping events.
77
   */
78
  virtual TimeSource& timeSource() const PURE;
79
};
80

            
81
using SocketTapConfigSharedPtr = std::shared_ptr<SocketTapConfig>;
82

            
83
} // namespace Tap
84
} // namespace TransportSockets
85
} // namespace Extensions
86
} // namespace Envoy