Home | History | Annotate | Download | only in congestion_control
      1 // Copyright (c) 2013 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 #include "net/quic/congestion_control/pacing_sender.h"
      6 
      7 namespace net {
      8 
      9 PacingSender::PacingSender(SendAlgorithmInterface* sender,
     10                            QuicTime::Delta alarm_granularity)
     11     : sender_(sender),
     12       alarm_granularity_(alarm_granularity),
     13       next_packet_send_time_(QuicTime::Zero()),
     14       was_last_send_delayed_(false),
     15       max_segment_size_(kDefaultMaxPacketSize) {
     16 }
     17 
     18 PacingSender::~PacingSender() {}
     19 
     20 void PacingSender::SetMaxPacketSize(QuicByteCount max_packet_size) {
     21   max_segment_size_ = max_packet_size;
     22   sender_->SetMaxPacketSize(max_packet_size);
     23 }
     24 
     25 void PacingSender::SetFromConfig(const QuicConfig& config, bool is_server) {
     26   sender_->SetFromConfig(config, is_server);
     27 }
     28 
     29 void PacingSender::OnIncomingQuicCongestionFeedbackFrame(
     30       const QuicCongestionFeedbackFrame& feedback,
     31       QuicTime feedback_receive_time,
     32       const SendAlgorithmInterface::SentPacketsMap& sent_packets) {
     33   sender_->OnIncomingQuicCongestionFeedbackFrame(
     34       feedback, feedback_receive_time, sent_packets);
     35 }
     36 
     37 void PacingSender::OnPacketAcked(
     38     QuicPacketSequenceNumber acked_sequence_number,
     39     QuicByteCount acked_bytes,
     40     QuicTime::Delta rtt) {
     41   sender_->OnPacketAcked(acked_sequence_number, acked_bytes, rtt);
     42 }
     43 
     44 void PacingSender::OnPacketLost(QuicPacketSequenceNumber sequence_number,
     45                                 QuicTime ack_receive_time) {
     46   sender_->OnPacketLost(sequence_number, ack_receive_time);
     47 }
     48 
     49 bool PacingSender::OnPacketSent(
     50     QuicTime sent_time,
     51     QuicPacketSequenceNumber sequence_number,
     52     QuicByteCount bytes,
     53     TransmissionType transmission_type,
     54     HasRetransmittableData has_retransmittable_data) {
     55   // Only pace data packets.
     56   if (has_retransmittable_data == HAS_RETRANSMITTABLE_DATA) {
     57     // The next packet should be sent as soon as the current packets has
     58     // been transferred.  We pace at twice the rate of the underlying
     59     // sender's bandwidth estimate to help ensure that pacing doesn't become
     60     // a bottleneck.
     61     const float kPacingAggression = 2;
     62     QuicTime::Delta delay =
     63         BandwidthEstimate().Scale(kPacingAggression).TransferTime(bytes);
     64     next_packet_send_time_ = next_packet_send_time_.Add(delay);
     65   }
     66   return sender_->OnPacketSent(sent_time, sequence_number, bytes,
     67                                transmission_type, has_retransmittable_data);
     68 }
     69 
     70 void PacingSender::OnRetransmissionTimeout() {
     71   sender_->OnRetransmissionTimeout();
     72 }
     73 
     74 void PacingSender::OnPacketAbandoned(QuicPacketSequenceNumber sequence_number,
     75                                      QuicByteCount abandoned_bytes) {
     76   sender_->OnPacketAbandoned(sequence_number, abandoned_bytes);
     77 }
     78 
     79 QuicTime::Delta PacingSender::TimeUntilSend(
     80       QuicTime now,
     81       TransmissionType transmission_type,
     82       HasRetransmittableData has_retransmittable_data,
     83       IsHandshake handshake) {
     84   QuicTime::Delta time_until_send =
     85       sender_->TimeUntilSend(now, transmission_type,
     86                              has_retransmittable_data, handshake);
     87   if (!time_until_send.IsZero()) {
     88     DCHECK(time_until_send.IsInfinite());
     89     // The underlying sender prevents sending.
     90     return time_until_send;
     91   }
     92 
     93   if (has_retransmittable_data == NO_RETRANSMITTABLE_DATA) {
     94     // Don't pace ACK packets, since they do not count against CWND and do not
     95     // cause CWND to grow.
     96     return QuicTime::Delta::Zero();
     97   }
     98 
     99   if (!was_last_send_delayed_ &&
    100       (!next_packet_send_time_.IsInitialized() ||
    101        now > next_packet_send_time_.Add(alarm_granularity_))) {
    102     // An alarm did not go off late, instead the application is "slow"
    103     // delivering data.  In this case, we restrict the amount of lost time
    104     // that we can make up for.
    105     next_packet_send_time_ = now.Subtract(alarm_granularity_);
    106   }
    107 
    108   // If the end of the epoch is far enough in the future, delay the send.
    109   if (next_packet_send_time_ > now.Add(alarm_granularity_)) {
    110     was_last_send_delayed_ = true;
    111     DVLOG(1) << "Delaying packet: "
    112              << next_packet_send_time_.Subtract(now).ToMicroseconds();
    113     return next_packet_send_time_.Subtract(now);
    114   }
    115 
    116   // Sent it immediately.  The epoch end will be adjusted in OnPacketSent.
    117   was_last_send_delayed_ = false;
    118   DVLOG(1) << "Sending packet now";
    119   return QuicTime::Delta::Zero();
    120 }
    121 
    122 QuicBandwidth PacingSender::BandwidthEstimate() const {
    123   return sender_->BandwidthEstimate();
    124 }
    125 
    126 QuicTime::Delta PacingSender::SmoothedRtt() const {
    127   return sender_->SmoothedRtt();
    128 }
    129 
    130 QuicTime::Delta PacingSender::RetransmissionDelay() const {
    131   return sender_->RetransmissionDelay();
    132 }
    133 
    134 QuicByteCount PacingSender::GetCongestionWindow() const {
    135   return sender_->GetCongestionWindow();
    136 }
    137 
    138 }  // namespace net
    139