proxygen
QPACKHeaderTable.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015-present, Facebook, Inc.
3  * All rights reserved.
4  *
5  * This source code is licensed under the BSD-style license found in the
6  * LICENSE file in the root directory of this source tree. An additional grant
7  * of patent rights can be found in the PATENTS file in the same directory.
8  *
9  */
10 #pragma once
11 
14 
15 namespace proxygen {
16 
22 class QPACKHeaderTable : public HeaderTable {
23  public:
24  enum {
26  };
27 
28  QPACKHeaderTable(uint32_t capacityVal, bool trackReferences);
29 
31  QPACKHeaderTable(const QPACKHeaderTable&) = delete;
33 
39  return baseIndex_;
40  }
41 
45  bool isVulnerable(uint32_t absIndex) const {
46  return (absIndex > maxAcked_);
47  }
48 
53  bool canIndex(const HPACKHeader& header) {
54  auto totalBytes = bytes_ + header.bytes();
55  // Don't index headers that would immediately be drained
56  return ((header.bytes() <= (capacity_ - minFree_)) &&
57  (totalBytes <= capacity_ || canEvict(totalBytes - capacity_)));
58  }
59 
63  bool isDraining(uint32_t relativeIndex) {
64  return relativeToAbsolute(relativeIndex) < minUsable_;
65  }
66 
73  std::pair<bool, uint32_t> maybeDuplicate(
74  uint32_t relativeIndex, bool allowVulnerable);
75 
81  bool add(HPACKHeader header) override;
82 
83  bool setCapacity(uint32_t capacity) override;
84 
92  uint32_t getIndex(const HPACKHeader& header,
93  bool allowVulnerable = true) const;
94 
102  const HPACKHeader& getHeader(uint32_t index, uint32_t base = 0) const;
103 
109  bool isValid(uint32_t index, uint32_t base = 0) const;
110 
118  uint32_t nameIndex(const HPACKHeaderName& headerName,
119  bool allowVulnerable = true) const;
120 
121  bool onTableStateSync(uint32_t inserts) {
122  // compare this way to avoid overflow
123  if (inserts > baseIndex_ ||
124  maxAcked_ > baseIndex_ - inserts) {
125  LOG(ERROR) << "Decoder ack'd too much maxAcked_="
126  << maxAcked_ << " baseIndex_=" << baseIndex_
127  << " inserts=" << inserts;
128  return false;
129  }
130  maxAcked_ += inserts;
131  CHECK_LE(maxAcked_, baseIndex_);
132  return true;
133  }
134 
135  void setMaxAcked(uint32_t maxAcked) {
136  if (maxAcked < maxAcked_) {
137  return;
138  }
139  CHECK_LE(maxAcked, baseIndex_);
140  maxAcked_ = maxAcked;
141  }
142 
146  uint32_t relativeToAbsolute(uint32_t relativeIndex) const {
147  DCHECK(isValid(relativeIndex, 0));
148  return baseIndex_ - relativeIndex + 1;
149  }
150 
155  CHECK_LE(absIndex, baseIndex_);
156  return baseIndex_ - absIndex + 1;
157  }
158 
163  void addRef(uint32_t absIndex);
164 
168  void subRef(uint32_t absIndex);
169 
170  private:
171  /*
172  * Shared implementation for getIndex and nameIndex
173  */
174  uint32_t getIndexImpl(const HPACKHeaderName& header,
175  const folly::fbstring& value,
176  bool nameOnly,
177  bool allowVulnerable=true) const;
178 
179  /*
180  * Increase table length to newLength
181  */
182  void increaseTableLengthTo(uint32_t newLength) override;
183 
184  void resizeTable(uint32_t newLength) override;
185 
186  void updateResizedTable(uint32_t oldTail, uint32_t oldLength,
187  uint32_t newLength) override;
191  uint32_t removeLast() override;
192 
196  bool canEvict(uint32_t needed);
197 
201  uint32_t evict(uint32_t needed, uint32_t desiredCapacity) override;
202 
206  uint32_t toInternal(uint32_t externalIndex, uint32_t base) const;
207 
208 
209  uint32_t internalToAbsolute(uint32_t internalIndex) const;
210  uint32_t absoluteToInternal(uint32_t absoluteIndex) const;
211 
216  std::unique_ptr<std::vector<uint16_t>> refCount_;
218 };
219 
220 }
void updateResizedTable(uint32_t oldTail, uint32_t oldLength, uint32_t newLength) override
void resizeTable(uint32_t newLength) override
uint32_t capacity() const
Definition: HeaderTable.h:86
QPACKHeaderTable(uint32_t capacityVal, bool trackReferences)
LogLevel max
Definition: LogLevel.cpp:31
uint32_t internalToAbsolute(uint32_t internalIndex) const
uint32_t toInternal(uint32_t externalIndex, uint32_t base) const
uint32_t absoluteToInternal(uint32_t absoluteIndex) const
uint32_t getBaseIndex() const
uint32_t removeLast() override
bool isVulnerable(uint32_t absIndex) const
bool isDraining(uint32_t relativeIndex)
void setMaxAcked(uint32_t maxAcked)
std::unique_ptr< std::vector< uint16_t > > refCount_
bool onTableStateSync(uint32_t inserts)
uint32_t bytes() const
Definition: HPACKHeader.h:49
bool add(HPACKHeader header) override
uint32_t absoluteToRelative(uint32_t absIndex) const
bool canIndex(const HPACKHeader &header)
std::pair< bool, uint32_t > maybeDuplicate(uint32_t relativeIndex, bool allowVulnerable)
static const char *const value
Definition: Conv.cpp:50
uint32_t getIndexImpl(const HPACKHeaderName &header, const folly::fbstring &value, bool nameOnly, bool allowVulnerable=true) const
bool isValid(uint32_t index, uint32_t base=0) const
uint32_t evict(uint32_t needed, uint32_t desiredCapacity) override
void addRef(uint32_t absIndex)
const HPACKHeader & getHeader(uint32_t index, uint32_t base=0) const
uint32_t relativeToAbsolute(uint32_t relativeIndex) const
uint32_t getIndex(const HPACKHeader &header, bool allowVulnerable=true) const
void increaseTableLengthTo(uint32_t newLength) override
void subRef(uint32_t absIndex)
QPACKHeaderTable & operator=(const QPACKHeaderTable &)=delete
bool setCapacity(uint32_t capacity) override
bool canEvict(uint32_t needed)
uint32_t nameIndex(const HPACKHeaderName &headerName, bool allowVulnerable=true) const