1 // Copyright (c) 2019 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef QUICHE_QUIC_CORE_BATCH_WRITER_QUIC_BATCH_WRITER_BASE_H_ 6 #define QUICHE_QUIC_CORE_BATCH_WRITER_QUIC_BATCH_WRITER_BASE_H_ 7 8 #include <cstdint> 9 10 #include "quiche/quic/core/batch_writer/quic_batch_writer_buffer.h" 11 #include "quiche/quic/core/quic_packet_writer.h" 12 #include "quiche/quic/core/quic_types.h" 13 #include "quiche/quic/platform/api/quic_ip_address.h" 14 #include "quiche/quic/platform/api/quic_socket_address.h" 15 16 namespace quic { 17 18 // QuicBatchWriterBase implements logic common to all derived batch writers, 19 // including maintaining write blockage state and a skeleton implemention of 20 // WritePacket(). 21 // A derived batch writer must override the FlushImpl() function to send all 22 // buffered writes in a batch. It must also override the CanBatch() function 23 // to control whether/when a WritePacket() call should flush. 24 class QUIC_EXPORT_PRIVATE QuicBatchWriterBase : public QuicPacketWriter { 25 public: 26 explicit QuicBatchWriterBase( 27 std::unique_ptr<QuicBatchWriterBuffer> batch_buffer); 28 29 // ATTENTION: If this write triggered a flush, and the flush failed, all 30 // buffered packets will be dropped to allow the next write to work. The 31 // number of dropped packets can be found in WriteResult.dropped_packets. 32 WriteResult WritePacket(const char* buffer, size_t buf_len, 33 const QuicIpAddress& self_address, 34 const QuicSocketAddress& peer_address, 35 PerPacketOptions* options) override; 36 IsWriteBlocked()37 bool IsWriteBlocked() const final { return write_blocked_; } 38 SetWritable()39 void SetWritable() final { write_blocked_ = false; } 40 MessageTooBigErrorCode()41 absl::optional<int> MessageTooBigErrorCode() const override { 42 return EMSGSIZE; 43 } 44 GetMaxPacketSize(const QuicSocketAddress &)45 QuicByteCount GetMaxPacketSize( 46 const QuicSocketAddress& /*peer_address*/) const final { 47 return kMaxOutgoingPacketSize; 48 } 49 SupportsReleaseTime()50 bool SupportsReleaseTime() const override { return false; } 51 IsBatchMode()52 bool IsBatchMode() const final { return true; } 53 GetNextWriteLocation(const QuicIpAddress &,const QuicSocketAddress &)54 QuicPacketBuffer GetNextWriteLocation( 55 const QuicIpAddress& /*self_address*/, 56 const QuicSocketAddress& /*peer_address*/) final { 57 // No need to explicitly delete QuicBatchWriterBuffer. 58 return {batch_buffer_->GetNextWriteLocation(), nullptr}; 59 } 60 61 WriteResult Flush() override; 62 63 protected: batch_buffer()64 const QuicBatchWriterBuffer& batch_buffer() const { return *batch_buffer_; } batch_buffer()65 QuicBatchWriterBuffer& batch_buffer() { return *batch_buffer_; } 66 buffered_writes()67 const quiche::QuicheCircularDeque<BufferedWrite>& buffered_writes() const { 68 return batch_buffer_->buffered_writes(); 69 } 70 71 // Given the release delay in |options| and the state of |batch_buffer_|, get 72 // the absolute release time. 73 struct QUIC_NO_EXPORT ReleaseTime { 74 // The actual (absolute) release time. 75 uint64_t actual_release_time = 0; 76 // The difference between |actual_release_time| and ideal release time, 77 // which is (now + |options->release_time_delay|). 78 QuicTime::Delta release_time_offset = QuicTime::Delta::Zero(); 79 }; GetReleaseTime(const PerPacketOptions *)80 virtual ReleaseTime GetReleaseTime( 81 const PerPacketOptions* /*options*/) const { 82 QUICHE_DCHECK(false) 83 << "Should not be called since release time is unsupported."; 84 return ReleaseTime{0, QuicTime::Delta::Zero()}; 85 } 86 87 struct QUIC_EXPORT_PRIVATE CanBatchResult { CanBatchResultCanBatchResult88 CanBatchResult(bool can_batch, bool must_flush) 89 : can_batch(can_batch), must_flush(must_flush) {} 90 // Whether this write can be batched with existing buffered writes. 91 bool can_batch; 92 // If |can_batch|, whether the caller must flush after this packet is 93 // buffered. 94 // Always true if not |can_batch|. 95 bool must_flush; 96 }; 97 98 // Given the existing buffered writes(in buffered_writes()), whether a new 99 // write(in the arguments) can be batched. 100 virtual CanBatchResult CanBatch(const char* buffer, size_t buf_len, 101 const QuicIpAddress& self_address, 102 const QuicSocketAddress& peer_address, 103 const PerPacketOptions* options, 104 uint64_t release_time) const = 0; 105 106 struct QUIC_EXPORT_PRIVATE FlushImplResult { 107 // The return value of the Flush() interface, which is: 108 // - WriteResult(WRITE_STATUS_OK, <bytes_flushed>) if all buffered writes 109 // were sent successfully. 110 // - WRITE_STATUS_BLOCKED or WRITE_STATUS_ERROR, if the batch write is 111 // blocked or returned an error while sending. If a portion of buffered 112 // writes were sent successfully, |FlushImplResult.num_packets_sent| and 113 // |FlushImplResult.bytes_written| contain the number of successfully sent 114 // packets and their total bytes. 115 WriteResult write_result; 116 int num_packets_sent; 117 // If write_result.status == WRITE_STATUS_OK, |bytes_written| will be equal 118 // to write_result.bytes_written. Otherwise |bytes_written| will be the 119 // number of bytes written before WRITE_BLOCK or WRITE_ERROR happened. 120 int bytes_written; 121 }; 122 123 // Send all buffered writes(in buffered_writes()) in a batch. 124 // buffered_writes() is guaranteed to be non-empty when this function is 125 // called. 126 virtual FlushImplResult FlushImpl() = 0; 127 128 private: 129 WriteResult InternalWritePacket(const char* buffer, size_t buf_len, 130 const QuicIpAddress& self_address, 131 const QuicSocketAddress& peer_address, 132 PerPacketOptions* options); 133 134 // Calls FlushImpl() and check its post condition. 135 FlushImplResult CheckedFlush(); 136 137 bool write_blocked_; 138 std::unique_ptr<QuicBatchWriterBuffer> batch_buffer_; 139 }; 140 141 // QuicUdpBatchWriter is a batch writer backed by a UDP socket. 142 class QUIC_EXPORT_PRIVATE QuicUdpBatchWriter : public QuicBatchWriterBase { 143 public: QuicUdpBatchWriter(std::unique_ptr<QuicBatchWriterBuffer> batch_buffer,int fd)144 QuicUdpBatchWriter(std::unique_ptr<QuicBatchWriterBuffer> batch_buffer, 145 int fd) 146 : QuicBatchWriterBase(std::move(batch_buffer)), fd_(fd) {} 147 fd()148 int fd() const { return fd_; } 149 150 private: 151 const int fd_; 152 }; 153 154 } // namespace quic 155 156 #endif // QUICHE_QUIC_CORE_BATCH_WRITER_QUIC_BATCH_WRITER_BASE_H_ 157