xref: /trafficserver/iocore/net/quic/QUICPinger.cc (revision a80d7794)
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 #include "QUICPinger.h"
25 
26 void
request()27 QUICPinger::request()
28 {
29   SCOPED_MUTEX_LOCK(lock, this->_mutex, this_ethread());
30   ++this->_need_to_fire;
31 }
32 
33 void
cancel()34 QUICPinger::cancel()
35 {
36   SCOPED_MUTEX_LOCK(lock, this->_mutex, this_ethread());
37   if (this->_need_to_fire > 0) {
38     --this->_need_to_fire;
39   }
40 }
41 
42 bool
_will_generate_frame(QUICEncryptionLevel level,size_t current_packet_size,bool ack_eliciting)43 QUICPinger::_will_generate_frame(QUICEncryptionLevel level, size_t current_packet_size, bool ack_eliciting)
44 {
45   SCOPED_MUTEX_LOCK(lock, this->_mutex, this_ethread());
46 
47   if (level != QUICEncryptionLevel::ONE_RTT) {
48     return false;
49   }
50 
51   // PING Frame is meaningless for ack_eliciting packet. Cancel it.
52   if (ack_eliciting) {
53     this->_ack_eliciting_packet_out = true;
54     this->cancel();
55     return false;
56   }
57 
58   if (this->_ack_eliciting_packet_out == false && !ack_eliciting && current_packet_size > 0 && this->_need_to_fire == 0) {
59     // force to send an PING Frame
60     this->request();
61   }
62 
63   // only update `_ack_eliciting_packet_out` when we has something to send.
64   if (current_packet_size) {
65     this->_ack_eliciting_packet_out = ack_eliciting;
66   }
67   return this->_need_to_fire > 0;
68 }
69 
70 /**
71  * @param connection_credit This is not used. Because PING frame is not flow-controlled
72  */
73 QUICFrame *
_generate_frame(uint8_t * buf,QUICEncryptionLevel level,uint64_t,uint16_t maximum_frame_size,size_t current_packet_size)74 QUICPinger::_generate_frame(uint8_t *buf, QUICEncryptionLevel level, uint64_t /* connection_credit */, uint16_t maximum_frame_size,
75                             size_t current_packet_size)
76 {
77   SCOPED_MUTEX_LOCK(lock, this->_mutex, this_ethread());
78   QUICFrame *frame = nullptr;
79 
80   if (level == QUICEncryptionLevel::ONE_RTT && this->_need_to_fire > 0 && maximum_frame_size > 0) {
81     // don't care ping frame lost or acked
82     frame = QUICFrameFactory::create_ping_frame(buf, 0, nullptr);
83     --this->_need_to_fire;
84     this->_ack_eliciting_packet_out = true;
85   }
86 
87   return frame;
88 }
89 
90 uint64_t
count()91 QUICPinger::count()
92 {
93   SCOPED_MUTEX_LOCK(lock, this->_mutex, this_ethread());
94   return this->_need_to_fire;
95 }
96