OpenSDN source code
bgp_update_queue.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
3  * Copyright Contributors to the OpenSDN project
4  */
5 
6 #include "bgp/bgp_update_queue.h"
7 
8 #include <boost/tuple/tuple.hpp>
9 
10 using boost::tie;
11 
12 //
13 // Initialize the UpdateQueue and add the tail marker to the FIFO.
14 //
15 UpdateQueue::UpdateQueue(const RibOut *ribout, int queue_id)
16  : queue_id_(queue_id),
17  encoding_is_xmpp_(ribout->IsEncodingXmpp()),
18  tstamp_(0),
19  marker_count_(0) {
20  queue_.push_back(tail_marker_);
21 }
22 
23 //
24 // Destroy the UpdateQueue after removing the tail marker from the FIFO.
25 //
27  queue_.erase(queue_.iterator_to(tail_marker_));
28  assert(queue_.empty());
29  assert(attr_set_.empty());
30 }
31 
32 //
33 // Enqueue the specified RouteUpdate to the UpdateQueue. Updates both the
34 // FIFO and the set container.
35 //
36 // Return true if the UpdateQueue had no RouteUpdates after the tail marker.
37 //
39  rt_update->set_tstamp(++tstamp_);
40 
41  // Insert at the end of the FIFO. Remember if the FIFO previously had
42  // no RouteUpdates after the tail marker.
43  UpdateEntry *tail_upentry = &(queue_.back());
44  bool need_tail_dequeue = (tail_upentry == &tail_marker_);
45  queue_.push_back(*rt_update);
46 
47  // Go through the UpdateInfo list and insert each element into the set
48  // container for the UpdateQueue. Also set up the back pointer to the
49  // RouteUpdate.
50  UpdateInfoSList &uinfo_slist = rt_update->Updates();
51  for (UpdateInfoSList::List::iterator iter = uinfo_slist->begin();
52  iter != uinfo_slist->end(); ++iter) {
53  iter->update = rt_update;
54  UpdatesByAttr::iterator set_iter;
55  bool result;
56  tie(set_iter, result) = attr_set_.insert(*iter);
57  assert(result);
58  }
59  return need_tail_dequeue;
60 }
61 
62 //
63 // Dequeue the specified RouteUpdate from the UpdateQueue. All UpdateInfo
64 // elements for the RouteUpdate are removed from the set container.
65 //
67  queue_.erase(queue_.iterator_to(*rt_update));
68  UpdateInfoSList &uinfo_slist = rt_update->Updates();
69  for (UpdateInfoSList::List::iterator iter = uinfo_slist->begin();
70  iter != uinfo_slist->end(); ++iter) {
71  attr_set_.erase(attr_set_.iterator_to(*iter));
72  }
73 }
74 
75 //
76 // Return the next RouteUpdate after the given UpdateEntry on the UpdateQueue.
77 // Traverses the FIFO and skips over MARKERs and other element types to find
78 // the next RouteUpdate.
79 //
80 // Return NULL if there's no such RouteUpdate on the FIFO.
81 //
83  UpdatesByOrder::iterator iter = queue_.iterator_to(*current_upentry);
84  while (++iter != queue_.end()) {
85  UpdateEntry *upentry = iter.operator->();
86  if (upentry->IsUpdate()) {
87  return static_cast<RouteUpdate *>(upentry);
88  }
89  }
90  return NULL;
91 }
92 
93 //
94 // Return the next UpdateEntry on the UpdateQueue after the one specified.
95 // Traverses the FIFO and returns the next UpdateEntry, irrespective of the
96 // type.
97 //
98 // Return NULL if there's no such UpdateEntry on the FIFO.
99 //
101  UpdatesByOrder::iterator iter = queue_.iterator_to(*current_upentry);
102  if (++iter == queue_.end()) {
103  return NULL;
104  }
105  return iter.operator->();
106 }
107 
108 //
109 // Dequeue the specified UpdateInfo from the set container.
110 //
111 void UpdateQueue::AttrDequeue(UpdateInfo *current_uinfo) {
112  attr_set_.erase(attr_set_.iterator_to(*current_uinfo));
113 }
114 
115 //
116 // Return the next UpdateInfo after the one provided. This traverses the set
117 // container and returns the next one if it has the same BgpAttr. Note that
118 // we must not consider the label in order to ensure optimal packing.
119 //
120 // Returns NULL if there are no more updates with the same BgpAttr. This is
121 // relaxed if the RibOut encoding is XMPP since it's possible to pack items
122 // with different BgpAttrs in the same message given that each item is self
123 // contained.
124 //
125 // Also return NULL if the next UpdateInfo is for the same RouteUpdate. This
126 // can happen in corner cases where the label (or the set for ecmp nexthops
127 // in case of an XMPP ribout) for a route changes between Join operations for
128 // 2 different sets of IPeerUpdates. Returning such an UpdateInfo results in
129 // data corruption.
130 //
132  UpdatesByAttr::iterator iter = attr_set_.iterator_to(*current_uinfo);
133  ++iter;
134  if (iter == attr_set_.end()) {
135  return NULL;
136  }
137  UpdateInfo *next_uinfo = iter.operator->();
138  if (next_uinfo->update == current_uinfo->update) {
139  return NULL;
140  }
141  if (encoding_is_xmpp_) {
142  const RibOutAttr &next_roattr = next_uinfo->roattr;
143  const RibOutAttr &current_roattr = current_uinfo->roattr;
144  if (next_roattr.IsReachable() == current_roattr.IsReachable()) {
145  return next_uinfo;
146  } else {
147  return NULL;
148  }
149  }
150  if (next_uinfo->roattr.attr() == current_uinfo->roattr.attr()) {
151  return next_uinfo;
152  }
153  return NULL;
154 }
155 
156 //
157 // Add the provided UpdateMarker after a specific RouteUpdate. Also update
158 // the MarkerList so that all peers in the provided UpdateMarker now point
159 // to it.
160 //
162  assert(!marker->members.empty());
163  marker_count_++;
164  queue_.insert(++queue_.iterator_to(*rt_update), *marker);
165 
166  for (size_t i = marker->members.find_first();
167  i != BitSet::npos; i = marker->members.find_next(i)) {
168  assert(markers_[i]);
169  markers_[i] = marker;
170  }
171 }
172 
173 //
174 // Move the provided UpdateMarker so that it's after the RouteUpdate. Since
175 // the entire UpdateMarker itself is being moved, there's no need to update
176 // the MarkerList.
177 //
178 // If the UpdateMarker is the tail marker, skip over all markers after the
179 // RouteUpdate till we hit the next RouteUpdate or the end of the queue.
180 // This is needed for the case where some peers get blocked after sending
181 // the last RouteUpdate in the queue. In that scenario, the marker for the
182 // blocked peers is inserted after the RouteUpdate and we then try to move
183 // the tail marker after the RouteUpdate.
184 //
186  queue_.erase(queue_.iterator_to(*marker));
187 
188  UpdatesByOrder::iterator iter = queue_.iterator_to(*rt_update);
189  if (marker == &tail_marker_) {
190  for (iter++; iter != queue_.end() && iter->IsMarker(); iter++) {
191  }
192  queue_.insert(iter, *marker);
193  } else {
194  queue_.insert(++iter, *marker);
195  }
196 }
197 
198 //
199 // Split the specified UpdateMarker based on the RibPeerSet. A new marker is
200 // created for the peers in the RibPeerSet and is inserted immediately before
201 // the existing marker. The bits corresponding to the RibPeerSet being split
202 // are reset in the existing marker.
203 //
204 // The MarkerList is updated so that all the peers in in the RibPeerSet point
205 // to the new marker.
206 //
207 void UpdateQueue::MarkerSplit(UpdateMarker *marker, const RibPeerSet &msplit) {
208  assert(!msplit.empty());
209  UpdateMarker *split_marker = new UpdateMarker();
210  split_marker->members = msplit;
211 
212  marker->members.Reset(msplit);
213  assert(!marker->members.empty());
214  UpdatesByOrder::iterator mpos = queue_.iterator_to(*marker);
215  queue_.insert(mpos, *split_marker);
216  marker_count_++;
217 
218  for (size_t i = msplit.find_first();
219  i != BitSet::npos; i = msplit.find_next(i)) {
220  assert(markers_[i]);
221  markers_[i] = split_marker;
222  }
223 }
224 
225 //
226 // Merge the peers corresponding to the RibPeerSet from the src UpdateMarker
227 // to the dst. The bits corresponding to the RibPeerSet being merged into the
228 // dst are reset in the src marker. If the src UpdateMarker has no more peers,
229 // as a result, it is removed from the FIFO and destroyed.
230 //
232  UpdateMarker *src_marker, const RibPeerSet &bitset) {
233  assert(!bitset.empty());
234 
235  // Set the bits in dst and update the MarkerList. Be sure to set the dst
236  // before we reset the src since bitset maybe a reference to src->members.
237  dst_marker->members.Set(bitset);
238  for (size_t i = bitset.find_first();
239  i != BitSet::npos; i = bitset.find_next(i)) {
240  assert(markers_[i]);
241  markers_[i] = dst_marker;
242  }
243 
244  // Reset the bits in the src and get rid of it in case it's now empty.
245  src_marker->members.Reset(bitset);
246  if (src_marker->members.empty()) {
247  queue_.erase(queue_.iterator_to(*src_marker));
248  assert(src_marker != &tail_marker_);
249  delete src_marker;
250  marker_count_--;
251  }
252 }
253 
254 //
255 // Return the UpdateMarker for the peer specified by the bit position.
256 //
258  assert(markers_[bit]);
259  return markers_[bit];
260 }
261 
262 //
263 // Join a new peer, as represented by it's bit index, to the UpdateQueue.
264 // Since it's a new peer, it starts out at the tail marker. Also add the
265 // peer's bit index and the tail marker pair to the MarkerList, growing
266 // the MarkerList if necessary.
267 //
268 // Return true if the tail marker is not the last entry in the queue. The
269 // caller should trigger a tail dequeue for the RibOut if so to take care
270 // of the possibility that all peers in the tail marker are blocked. Note
271 // that a tail dequeue may already be pending in the BgpUpdateSender, but
272 // an extra one is harmless.
273 //
274 bool UpdateQueue::Join(int bit) {
275  UpdateMarker *marker = &tail_marker_;
276  marker->members.set(bit);
277  if (markers_.size() < (size_t)(bit + 1))
278  markers_.resize(bit + 1, NULL);
279  assert(!markers_[bit]);
280  markers_[bit] = marker;
281  return (&tail_marker_ != &queue_.back());
282 }
283 
284 //
285 // Leave a peer, as represented by it's bit index, from the UpdateQueue.
286 // Find the current marker for the peer and clear the peer bit's entry in
287 // the MarkerList. Don't bother shrinking the MarkerList - the entry can
288 // be reused by a subsequent Join.
289 // Reset the peer's bit in the marker and get rid of the marker itself if
290 // it's now empty.
291 //
292 void UpdateQueue::Leave(int bit) {
293  UpdateMarker *marker = markers_[bit];
294  markers_[bit] = NULL;
295  assert(marker);
296  marker->members.reset(bit);
297  if (marker != &tail_marker_ && marker->members.empty()) {
298  queue_.erase(queue_.iterator_to(*marker));
299  delete marker;
300  }
301 }
302 
304  for (size_t idx = 0; idx < markers_.size(); ++idx) {
305  UpdateMarker *marker = markers_[idx];
306  if (!marker)
307  continue;
308  CHECK_INVARIANT(marker->members.test(idx));
309  }
310  return true;
311 }
312 
313 //
314 // Return true if the UpdateQueue is empty. It's considered empty if there
315 // are as no UpdateInfo elements in the set container. We don't look at the
316 // FIFO since that may still have the tail marker on it.
317 //
318 bool UpdateQueue::empty() const {
319  return attr_set_.empty();
320 }
321 
322 size_t UpdateQueue::size() const {
323  return attr_set_.size();
324 }
325 
327  return marker_count_;
328 }
void Set(const BitSet &rhs)
Definition: bitset.cc:463
BitSet & reset(size_t pos)
Definition: bitset.cc:137
static const size_t npos
Definition: bitset.h:20
size_t find_first() const
Definition: bitset.cc:243
void Reset(const BitSet &rhs)
Definition: bitset.cc:471
size_t find_next(size_t pos) const
Definition: bitset.cc:256
bool empty() const
Definition: bitset.cc:166
BitSet & set(size_t pos)
Definition: bitset.cc:126
bool test(size_t pos) const
Definition: bitset.cc:147
const BgpAttr * attr() const
Definition: bgp_ribout.h:99
bool IsReachable() const
Definition: bgp_ribout.h:96
void set_tstamp(uint64_t tstamp)
Definition: bgp_update.h:234
UpdateInfoSList & Updates()
Definition: bgp_update.h:215
bool IsUpdate()
Definition: bgp_update.h:38
bool Enqueue(RouteUpdate *rt_update)
UpdateEntry * NextEntry(UpdateEntry *upentry)
void MarkerSplit(UpdateMarker *marker, const RibPeerSet &msplit)
uint64_t tstamp_
UpdateInfo * AttrNext(UpdateInfo *current_uinfo)
bool CheckInvariants() const
MarkerList markers_
void AddMarker(UpdateMarker *marker, RouteUpdate *rt_update)
bool Join(int bit)
UpdateQueue(const RibOut *ribout, int queue_id)
void MarkerMerge(UpdateMarker *dst_marker, UpdateMarker *src_marker, const RibPeerSet &bitset)
void Dequeue(RouteUpdate *rt_update)
void MoveMarker(UpdateMarker *marker, RouteUpdate *rt_update)
RouteUpdate * NextUpdate(UpdateEntry *upentry)
bool empty() const
UpdatesByAttr attr_set_
UpdateMarker * GetMarker(int bit)
size_t marker_count() const
size_t marker_count_
void Leave(int bit)
size_t size() const
UpdatesByOrder queue_
void AttrDequeue(UpdateInfo *current_uinfo)
UpdateMarker tail_marker_
RibOutAttr roattr
Definition: bgp_update.h:101
RouteUpdate * update
Definition: bgp_update.h:107
RibPeerSet members
Definition: bgp_update.h:56
#define CHECK_INVARIANT(Cond)
Definition: util.h:30