xref: /trafficserver/iocore/net/quic/QUICStream.h (revision 88909bcf)
1 /** @file
2  *
3  *  A brief file description
4  *
5  *  @section license License
6  *
7  *  Licensed to the Apache Software Foundation (ASF) under one
8  *  or more contributor license agreements.  See the NOTICE file
9  *  distributed with this work for additional information
10  *  regarding copyright ownership.  The ASF licenses this file
11  *  to you under the Apache License, Version 2.0 (the
12  *  "License"); you may not use this file except in compliance
13  *  with the License.  You may obtain a copy of the License at
14  *
15  *      http://www.apache.org/licenses/LICENSE-2.0
16  *
17  *  Unless required by applicable law or agreed to in writing, software
18  *  distributed under the License is distributed on an "AS IS" BASIS,
19  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  *  See the License for the specific language governing permissions and
21  *  limitations under the License.
22  */
23 
24 #pragma once
25 
26 #include "tscore/List.h"
27 
28 #include "P_VConnection.h"
29 #include "I_Event.h"
30 
31 #include "QUICFrame.h"
32 #include "QUICStreamState.h"
33 #include "QUICFlowController.h"
34 #include "QUICIncomingFrameBuffer.h"
35 #include "QUICFrameGenerator.h"
36 #include "QUICConnection.h"
37 #include "QUICFrameRetransmitter.h"
38 #include "QUICDebugNames.h"
39 
40 /**
41  * @brief QUIC Stream
42  * TODO: This is similar to Http2Stream. Need to think some integration.
43  */
44 class QUICStream : public QUICFrameGenerator, public QUICFrameRetransmitter
45 {
46 public:
QUICStream()47   QUICStream() {}
48   QUICStream(QUICConnectionInfoProvider *cinfo, QUICStreamId sid);
49   virtual ~QUICStream();
50 
51   QUICStreamId id() const;
52   QUICStreamDirection direction() const;
53   const QUICConnectionInfoProvider *connection_info() const;
54   bool is_bidirectional() const;
55   QUICOffset final_offset() const;
56 
57   /*
58    * QUICApplication need to call one of these functions when it process VC_EVENT_*
59    */
60   virtual void on_read();
61   virtual void on_eos();
62 
63   virtual QUICConnectionErrorUPtr recv(const QUICStreamFrame &frame);
64   virtual QUICConnectionErrorUPtr recv(const QUICMaxStreamDataFrame &frame);
65   virtual QUICConnectionErrorUPtr recv(const QUICStreamDataBlockedFrame &frame);
66   virtual QUICConnectionErrorUPtr recv(const QUICStopSendingFrame &frame);
67   virtual QUICConnectionErrorUPtr recv(const QUICRstStreamFrame &frame);
68   virtual QUICConnectionErrorUPtr recv(const QUICCryptoFrame &frame);
69 
70   QUICOffset reordered_bytes() const;
71   virtual QUICOffset largest_offset_received() const;
72   virtual QUICOffset largest_offset_sent() const;
73 
74   virtual void stop_sending(QUICStreamErrorUPtr error);
75   virtual void reset(QUICStreamErrorUPtr error);
76 
77   LINK(QUICStream, link);
78 
79 protected:
80   QUICConnectionInfoProvider *_connection_info = nullptr;
81   QUICStreamId _id                             = 0;
82   QUICOffset _send_offset                      = 0;
83   QUICOffset _reordered_bytes                  = 0;
84 
85   void _records_rst_stream_frame(QUICEncryptionLevel level, const QUICRstStreamFrame &frame);
86   void _records_stream_frame(QUICEncryptionLevel level, const QUICStreamFrame &frame);
87   void _records_stop_sending_frame(QUICEncryptionLevel level, const QUICStopSendingFrame &frame);
88   void _records_crypto_frame(QUICEncryptionLevel level, const QUICCryptoFrame &frame);
89 };
90 
91 // This is VConnection class for VIO operation.
92 class QUICStreamVConnection : public VConnection, public QUICStream
93 {
94 public:
QUICStreamVConnection(QUICConnectionInfoProvider * cinfo,QUICStreamId sid)95   QUICStreamVConnection(QUICConnectionInfoProvider *cinfo, QUICStreamId sid) : VConnection(nullptr), QUICStream(cinfo, sid)
96   {
97     mutex = new_ProxyMutex();
98   }
99 
QUICStreamVConnection()100   QUICStreamVConnection() : VConnection(nullptr) {}
101   virtual ~QUICStreamVConnection();
102 
103   LINK(QUICStreamVConnection, link);
104 
105 protected:
106   virtual int64_t _process_read_vio();
107   virtual int64_t _process_write_vio();
108   void _signal_read_event();
109   void _signal_write_event();
110   void _signal_read_eos_event();
111   Event *_send_tracked_event(Event *, int, VIO *);
112 
113   void _write_to_read_vio(QUICOffset offset, const uint8_t *data, uint64_t data_length, bool fin);
114 
115   VIO _read_vio;
116   VIO _write_vio;
117 
118   Event *_read_event  = nullptr;
119   Event *_write_event = nullptr;
120 };
121 
122 #define QUICStreamDebug(fmt, ...)                                                                        \
123   Debug("quic_stream", "[%s] [%" PRIu64 "] [%s] " fmt, this->_connection_info->cids().data(), this->_id, \
124         QUICDebugNames::stream_state(this->_state.get()), ##__VA_ARGS__)
125 
126 #define QUICVStreamDebug(fmt, ...)                                                                         \
127   Debug("v_quic_stream", "[%s] [%" PRIu64 "] [%s] " fmt, this->_connection_info->cids().data(), this->_id, \
128         QUICDebugNames::stream_state(this->_state.get()), ##__VA_ARGS__)
129 
130 #define QUICStreamFCDebug(fmt, ...)                                                                         \
131   Debug("quic_flow_ctrl", "[%s] [%" PRIu64 "] [%s] " fmt, this->_connection_info->cids().data(), this->_id, \
132         QUICDebugNames::stream_state(this->_state.get()), ##__VA_ARGS__)
133 
134 extern const uint32_t MAX_STREAM_FRAME_OVERHEAD;
135