Skip to content

Commit

Permalink
Fix deadlock in RibOutUpdates::TailDequeue/PeerDequeue
Browse files Browse the repository at this point in the history
RibOutUpdates::TailDequeue/PeerDequeue can deadlock if 2 RouteUpdates
have the same timestamp.  Calling clock_gettime with CLOCK_MONOTONIC
does not guarantee monotonically increasing timestamps.

Use atomic uint64_t to implement relative timestamp for RouteUpdate.

Change-Id: I328fc96405c51dace5b5e8a79b80026631a0bb4b
Partial-Bug: 1411855
  • Loading branch information
Nischal Sheth committed Jan 16, 2015
1 parent 31a300b commit 6bb5d55
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 15 deletions.
18 changes: 4 additions & 14 deletions src/bgp/bgp_update.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@

#include "bgp/bgp_update.h"

#include <time.h>
#ifdef __APPLE__
#include <mach/mach_time.h>
#endif
#include "base/logging.h"
#include "bgp/bgp_route.h"
#include "bgp/bgp_table.h"

// Automatically initialized to 0 in C++03 mode.
tbb::atomic<uint64_t> RouteUpdate::global_tstamp_;

//
// Find the UpdateInfo element with matching RibOutAttr.
//
Expand Down Expand Up @@ -421,18 +420,9 @@ UpdateList *RouteUpdate::GetUpdateList(RibOut *ribout) {

//
// Update the timestamp in this RouteUpdate.

// The code expects that the APIs use the underlying CPU performance counters
// in order to provide high resolution timestamps.
//
void RouteUpdate::set_tstamp_now() {
#ifdef __APPLE__
tstamp_ = mach_absolute_time();
#else
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
tstamp_ = ts.tv_sec * 1000000000 + ts.tv_nsec;
#endif
tstamp_ = global_tstamp_.fetch_and_increment();
}

//
Expand Down
3 changes: 2 additions & 1 deletion src/bgp/bgp_update.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include <boost/intrusive/list.hpp>
#include <boost/intrusive/slist.hpp>
#include <boost/intrusive/set.hpp>

#include <tbb/atomic.h>
#include <tbb/mutex.h>

#include "bgp/bgp_ribout.h"
Expand Down Expand Up @@ -243,6 +243,7 @@ class RouteUpdate : public DBState, public UpdateEntry {
void FlagSet(Flag flag) { flags_ |= (1 << flag); }
void FlagReset(Flag flag) { flags_ &= ~(1 << flag); }

static tbb::atomic<uint64_t> global_tstamp_;
tbb::mutex mutex_;
BgpRoute *route_;
int8_t queue_id_;
Expand Down

0 comments on commit 6bb5d55

Please sign in to comment.