Skip to content

Commit

Permalink
Finish removing exceptions from bgp.
Browse files Browse the repository at this point in the history
At least mostly.
  • Loading branch information
greearb committed Sep 15, 2017
1 parent 99a2134 commit 4e727b7
Show file tree
Hide file tree
Showing 9 changed files with 109 additions and 71 deletions.
69 changes: 49 additions & 20 deletions xorp/bgp/harness/peer.cc
Expand Up @@ -477,6 +477,8 @@ Peer::send_packet(const string& line, const vector<string>& words)
size_t size = words.size();
uint32_t word = 3;
list<Corrupt> _corrupt;
CorruptMessage m;

if ("corrupt" == words[3]) {
word += 1;
// Pairs of offset byte should be in the stream until we reach
Expand Down Expand Up @@ -530,7 +532,8 @@ Peer::send_packet(const string& line, const vector<string>& words)
TimeVal tv;
_eventloop->current_time(tv);

_trie_sent.process_update_packet(tv, buf, len, _peerdata);
if (!_trie_sent.process_update_packet(tv, buf, len, _peerdata, m))
return -1;
}

_busy++;
Expand Down Expand Up @@ -769,6 +772,7 @@ Peer::send_dump_callback(const XrlError& error, FILE *fp,
const size_t packets_to_send,
const char *comment)
{
CorruptMessage m;
debug_msg("callback %s %s\n", comment, error.str().c_str());
if(XrlError::OKAY() != error) {
XLOG_WARNING("callback: %s %s", comment, error.str().c_str());
Expand All @@ -792,7 +796,7 @@ Peer::send_dump_callback(const XrlError& error, FILE *fp,
*/
TimeVal tv;
_eventloop->current_time(tv);
_trie_sent.process_update_packet(tv, buf, len, _peerdata);
_trie_sent.process_update_packet(tv, buf, len, _peerdata, m);

_smcb = callback(this, &Peer::send_dump_callback,
fp,
Expand Down Expand Up @@ -1620,7 +1624,8 @@ Peer::datain(const bool& status, const TimeVal& tv,
/*
** Save the update message in the receive trie.
*/
_trie_recv.process_update_packet(tv, buf, length, _peerdata);
if (!_trie_recv.process_update_packet(tv, buf, length, _peerdata, m))
goto err;
check_expect(&pac);
break;
}
Expand Down Expand Up @@ -1841,8 +1846,9 @@ community_interpret(const string& community)
const BGPPacket *
Peer::packet(const string& line, const vector<string>& words, int index) const
{
BGPPacket *pac = 0;
BGPPacket *pac = NULL;
CorruptMessage m;
UpdatePacket *bgpupdate = NULL;

if("notify" == words[index]) {
switch(words.size() - (index + 1)) {
Expand Down Expand Up @@ -1886,7 +1892,7 @@ Peer::packet(const string& line, const vector<string>& words, int index) const
return NULL;
}
#endif
UpdatePacket *bgpupdate = new UpdatePacket();
bgpupdate = new UpdatePacket();
MPReachNLRIAttribute<IPv6> mpipv6_nlri(SAFI_UNICAST);
MPUNReachNLRIAttribute<IPv6> mpipv6_withdraw(SAFI_UNICAST);
ClusterListAttribute cl;
Expand All @@ -1904,7 +1910,8 @@ Peer::packet(const string& line, const vector<string>& words, int index) const
string aspath = words[i+1];
if ("empty" == aspath)
aspath = "";
ASPathAttribute aspa(ASPath(aspath.c_str(), m));
ASPath ap(aspath.c_str());
ASPathAttribute aspa(&ap);
if (aspa.invalid())
goto err;
bgpupdate->add_pathatt(aspa);
Expand All @@ -1914,31 +1921,42 @@ Peer::packet(const string& line, const vector<string>& words, int index) const
string as4path = words[i+1];
if ("empty" == as4path)
as4path = "";
AS4PathAttribute aspa(AS4Path(as4path.c_str()));
AS4Path ap(as4path.c_str());
AS4PathAttribute aspa(&ap);
if (aspa.invalid())
goto err;
bgpupdate->add_pathatt(aspa);
debug_msg("as4path: %s\n",
AS4Path(as4path.c_str()).str().c_str());
} else if("nexthop" == words[i]) {
IPv4NextHopAttribute nha(IPv4((const char*)
(words[i+1].c_str())));
IPv4 i4((const char*)(words[i+1].c_str()));
IPv4NextHopAttribute nha(i4, m);
if (nha.invalid())
goto err;
bgpupdate->add_pathatt(nha);
} else if("nexthop6" == words[i]) {
mpipv6_nlri.set_nexthop(IPv6((const char*)
(words[i+1].c_str())));
IPv6 i6((const char*)(words[i+1].c_str()));
if (i6.invalid())
goto err;
mpipv6_nlri.set_nexthop(i6);
} else if("localpref" == words[i]) {
LocalPrefAttribute lpa(atoi(words[i+1].c_str()));
if (lpa.invalid())
goto err;
bgpupdate->add_pathatt(lpa);
} else if("nlri" == words[i]) {
BGPUpdateAttrib upa(IPv4Net((const char*)
(words[i+1].c_str())));
if (upa.invalid())
goto err;
bgpupdate->add_nlri(upa);
} else if("nlri6" == words[i]) {
mpipv6_nlri.add_nlri(words[i+1].c_str());
} else if("withdraw" == words[i]) {
BGPUpdateAttrib upa(IPv4Net((const char*)
(words[i+1].c_str())));
if (upa.invalid())
goto err;
bgpupdate->add_withdrawn(upa);
} else if("withdraw6" == words[i]) {
mpipv6_withdraw.add_withdrawn(IPv6Net(words[i+1].c_str()));
Expand All @@ -1948,22 +1966,30 @@ Peer::packet(const string& line, const vector<string>& words, int index) const
} else if("originatorid" == words[i]) {
OriginatorIDAttribute oid(IPv4((const char *)
(words[i+1].c_str())));
if (oid.invalid())
goto err;
bgpupdate->add_pathatt(oid);
} else if("clusterlist" == words[i]) {
cl.prepend_cluster_id(IPv4((const char *)
(words[i+1].c_str())));
IPv4 i4((const char *)(words[i+1].c_str()));
if (i4.invalid())
goto err;
cl.prepend_cluster_id(i4);
} else if("community" == words[i]) {
community.add_community(community_interpret(words[i+1]));
} else if("as4aggregator" == words[i]) {
IPv4 as4aggid(words[i+1].c_str());
AsNum as4agg(words[i+2]);
AS4AggregatorAttribute asaggatt(as4aggid, as4agg);
if (asaggatt.invalid())
goto err;
bgpupdate->add_pathatt(asaggatt);
debug_msg("as4aggregator: %s %s\n",
as4aggid.str().c_str(), as4agg.str().c_str());
i++;
} else if("pathattr" == words[i]) {
AnyAttribute aa(words[i+1].c_str());
if (aa.invalid())
goto err;
bgpupdate->add_pathatt(aa);
} else {
XLOG_ERROR("Illegal argument to update: <%s>\n[%s]",
Expand All @@ -1988,6 +2014,7 @@ Peer::packet(const string& line, const vector<string>& words, int index) const
}

pac = bgpupdate;
bgpupdate = NULL;
} else if("open" == words[index]) {
size_t size = words.size();
if(0 != ((size - (index + 1)) % 2)) {
Expand Down Expand Up @@ -2068,15 +2095,17 @@ Peer::packet(const string& line, const vector<string>& words, int index) const
return NULL;
}

if (pac->invalid()) {
XLOG_ERROR("Unable to construct packet "
"%s\n[%s])", c.why().c_str(),
line.c_str());
delete pac;
return NULL;
}
if (pac->invalid())
goto err;

debug_msg("%s\n", pac->str().c_str());

return pac;

err:
XLOG_ERROR("Unable to construct packet [%s]\n", m.get_msg().c_str());
delete pac;
if (bgpupdate && (bgpupdate != pac))
delete bgpupdate;
return NULL;
}
36 changes: 12 additions & 24 deletions xorp/bgp/harness/test_peer.cc
Expand Up @@ -365,10 +365,7 @@ TestPeer::connect(const string& host, const uint32_t& port,

struct sockaddr_storage peer;
size_t len = sizeof(peer);
try {
Socket::init_sockaddr(host, port, peer, len);
} catch(UnresolvableHost e) {
error_string = e.why();
if (!Socket::init_sockaddr(host, port, peer, len, error_string)) {
return false;
}

Expand Down Expand Up @@ -434,10 +431,7 @@ TestPeer::listen(const string& host, const uint32_t& port,

struct sockaddr_storage local;
size_t len = sizeof(local);
try {
Socket::init_sockaddr(host, port, local, len);
} catch(UnresolvableHost e) {
error_string = e.why();
if (!Socket::init_sockaddr(host, port, local, len, error_string)) {
return false;
}

Expand Down Expand Up @@ -492,10 +486,7 @@ TestPeer::bind(const string& host, const uint32_t& port,

struct sockaddr_storage local;
size_t len = sizeof(local);
try {
Socket::init_sockaddr(host, port, local, len);
} catch(UnresolvableHost e) {
error_string = e.why();
if (!Socket::init_sockaddr(host, port, local, len, error_string)) {
return false;
}

Expand Down Expand Up @@ -570,8 +561,10 @@ TestPeer::send_complete(AsyncFileWriter::Event ev, const uint8_t *buf,
debug_msg("event: error\n");
/* Don't free the message here we'll get it in the flush */
XLOG_ERROR("Writing buffer failed: %s", strerror(errno));
break;
case AsyncFileOperator::END_OF_FILE:
XLOG_ERROR("End of File: %s", strerror(errno));
break;
case AsyncFileOperator::WOULDBLOCK:
// do nothing
;
Expand Down Expand Up @@ -947,20 +940,15 @@ main(int argc, char **argv)
}
}

try {
EventLoop eventloop;
XrlStdRouter router(eventloop, server, finder_host.c_str());
TestPeer test_peer(eventloop, router, server, verbose);
XrlTestPeerTarget xrl_target(&router, test_peer, trace);
EventLoop eventloop;
XrlStdRouter router(eventloop, server, finder_host.c_str());
TestPeer test_peer(eventloop, router, server, verbose);
XrlTestPeerTarget xrl_target(&router, test_peer, trace);

wait_until_xrl_router_is_ready(eventloop, router);

while(!test_peer.done()) {
eventloop.run();
}
wait_until_xrl_router_is_ready(eventloop, router);

} catch(...) {
xorp_catch_standard_exceptions();
while(!test_peer.done()) {
eventloop.run();
}

//
Expand Down
32 changes: 21 additions & 11 deletions xorp/bgp/harness/trie.cc
Expand Up @@ -84,6 +84,7 @@ Trie::lookup(const IPv6Net& n) const
{
TriePayload payload = _head_ipv6.find(n);
const UpdatePacket *update = payload.get();
CorruptMessage m;

if(0 == update)
return 0;
Expand All @@ -95,7 +96,7 @@ Trie::lookup(const IPv6Net& n) const
/*
** Look for a multiprotocol path attribute.
*/
const MPReachNLRIAttribute<IPv6> *mpreach = 0;
const MPReachNLRIAttribute<IPv6> *mpreach = NULL;
#if 0
list <PathAttribute*>::const_iterator pai;
for (pai = update->pa_list().begin(); pai != update->pa_list().end();
Expand All @@ -109,9 +110,9 @@ Trie::lookup(const IPv6Net& n) const
}
}
#endif
mpreach = update->mpreach<IPv6>(SAFI_UNICAST);
mpreach = update->mpreach<IPv6>(SAFI_UNICAST, m);
if (mpreach == 0)
mpreach = update->mpreach<IPv6>(SAFI_MULTICAST);
mpreach = update->mpreach<IPv6>(SAFI_MULTICAST, m);

if(0 == mpreach)
XLOG_FATAL("If we found the packet in the trie"
Expand All @@ -135,13 +136,15 @@ Trie::lookup(const IPv6Net& n) const
return 0;
}

void
bool
Trie::process_update_packet(const TimeVal& tv, const uint8_t *buf, size_t len,
const BGPPeerData *peerdata)
const BGPPeerData *peerdata, CorruptMessage& m)
{
_update_cnt++;

TriePayload payload(tv, buf, len, peerdata, _first, _last);
TriePayload payload(tv, buf, len, peerdata, _first, _last, m);
if (payload.invalid())
return false;
const UpdatePacket *p = payload.get();

debug_msg("process update packet:\n%s\n", p->str().c_str());
Expand All @@ -166,12 +169,17 @@ Trie::process_update_packet(const TimeVal& tv, const uint8_t *buf, size_t len,
}
}
#endif
mpreach = p->mpreach<IPv6>(SAFI_UNICAST);
mpreach = p->mpreach<IPv6>(SAFI_UNICAST, m);
if (!mpreach)
mpreach = p->mpreach<IPv6>(SAFI_MULTICAST);
mpunreach = p->mpunreach<IPv6>(SAFI_UNICAST);
mpreach = p->mpreach<IPv6>(SAFI_MULTICAST, m);
mpunreach = p->mpunreach<IPv6>(SAFI_UNICAST, m);
if (!mpunreach)
mpunreach = p->mpunreach<IPv6>(SAFI_MULTICAST);
mpunreach = p->mpunreach<IPv6>(SAFI_MULTICAST, m);

if ((mpreach && mpreach->invalid()) ||
(mpunreach && mpunreach->invalid())) {
return false;
}

/*
** IPv4 Withdraws
Expand Down Expand Up @@ -211,6 +219,7 @@ Trie::process_update_packet(const TimeVal& tv, const uint8_t *buf, size_t len,
ni6 != mpreach->nlri_list().end(); ni6++)
add(*ni6, payload);
}
return true;
}

void
Expand Down Expand Up @@ -238,6 +247,7 @@ Trie::replay_walk(const ReplayWalker uw, const BGPPeerData *peerdata) const
Trie trie;
trie.set_warning(false);
uint32_t changes = 0;
CorruptMessage m;

for(const TrieData *p = _first; p; p = p->next()) {
changes = trie.changes();
Expand All @@ -246,7 +256,7 @@ Trie::replay_walk(const ReplayWalker uw, const BGPPeerData *peerdata) const
size_t len = BGPPacket::MAXPACKETSIZE;
debug_msg("Trie::replay_walk\n");
packet->encode(data, len, peerdata);
trie.process_update_packet(p->tv(), data, len, peerdata);
trie.process_update_packet(p->tv(), data, len, peerdata, m);
if(trie.changes() != changes)
uw->dispatch(p->data(), p->tv());
}
Expand Down
6 changes: 3 additions & 3 deletions xorp/bgp/harness/trie.hh
Expand Up @@ -17,7 +17,6 @@
// XORP Inc, 2953 Bunker Hill Lane, Suite 204, Santa Clara, CA 95054, USA;
// http://xorp.net

// $XORP: xorp/bgp/harness/trie.hh,v 1.19 2008/10/02 21:56:27 bms Exp $

#ifndef __BGP_HARNESS_TRIE_HH__
#define __BGP_HARNESS_TRIE_HH__
Expand All @@ -28,6 +27,7 @@
#include "bgp/packet.hh"
#include "trie_payload.hh"
#include "real_trie.hh"

class BGPPeerData;

/**
Expand All @@ -52,8 +52,8 @@ public:
const UpdatePacket *lookup(const string& net) const;
const UpdatePacket *lookup(const IPv4Net& net) const;
const UpdatePacket *lookup(const IPv6Net& net) const;
void process_update_packet(const TimeVal& tv, const uint8_t *buf,
size_t len, const BGPPeerData *peerdata);
bool process_update_packet(const TimeVal& tv, const uint8_t *buf,
size_t len, const BGPPeerData *peerdata, CorruptMessage& m);

typedef RealTrie<IPv4>::TreeWalker TreeWalker_ipv4;
typedef RealTrie<IPv6>::TreeWalker TreeWalker_ipv6;
Expand Down

0 comments on commit 4e727b7

Please sign in to comment.