Skip to content

Commit

Permalink
Extended community for etree-leaf
Browse files Browse the repository at this point in the history
Add support for etree extended community
Add sticky bit to MacMobility community
Add tests for etree
Add/Modify tests for MacMobility

Change-Id: I4c167ab12a79e89f3e4bcc4b7998cf20814e2a91
Related-bug: #1645092
  • Loading branch information
bailkeri committed Dec 9, 2016
1 parent 72402bf commit 6743e06
Show file tree
Hide file tree
Showing 9 changed files with 215 additions and 9 deletions.
1 change: 1 addition & 0 deletions src/bgp/extended-community/SConscript
Expand Up @@ -16,6 +16,7 @@ libextended_community = env.Library('extended_community',
['default_gateway.cc',
'es_import.cc',
'esi_label.cc',
'etree.cc',
'load_balance.cc',
'mac_mobility.cc',
'router_mac.cc',
Expand Down
55 changes: 55 additions & 0 deletions src/bgp/extended-community/etree.cc
@@ -0,0 +1,55 @@
/*
* Copyright (c) 2016 Juniper Networks, Inc. All rights reserved.
*/

#include "bgp/extended-community/etree.h"

#include <stdio.h>

#include <algorithm>
#include <string>


using std::copy;
using std::string;

ETree::ETree(bool leaf, int label) {
data_[0] = BgpExtendedCommunityType::Evpn;
data_[1] = BgpExtendedCommunityEvpnSubType::ETree;
data_[2] = leaf ? 0x01 : 0x0; // Leaf Indication
data_[3] = 0x00; // Reserved
data_[4] = 0x00; // Reserved
put_value(&data_[5], 3, (label<<4)); // leaf label
}

ETree::ETree(const bytes_type &data) {
copy(data.begin(), data.end(), data_.begin());
}

bool ETree::leaf() const {
uint8_t data[ETree::kSize];
copy(data_.begin(), data_.end(), &data[0]);
if (data[0] == BgpExtendedCommunityType::Evpn &&
data[1] == BgpExtendedCommunityEvpnSubType::ETree) {
return (data[2] & 0x1);
}
return false;
}

int ETree::label() const {
uint8_t data[ETree::kSize];
copy(data_.begin(), data_.end(), &data[0]);
if (data[0] == BgpExtendedCommunityType::Evpn &&
data[1] == BgpExtendedCommunityEvpnSubType::ETree) {
uint32_t value = get_value(data + 5, 3);
return value >> 4;
}
return false;
}

std::string ETree::ToString() {
char temp[50];
snprintf(temp, sizeof(temp), "etree:%s:%d",
(leaf() ? "leaf":"root"), label());
return string(temp);
}
41 changes: 41 additions & 0 deletions src/bgp/extended-community/etree.h
@@ -0,0 +1,41 @@
/*
* Copyright (c) 2016 Juniper Networks, Inc. All rights reserved.
*/

#ifndef SRC_BGP_EXTENDED_COMMUNITY_ETREE_H_
#define SRC_BGP_EXTENDED_COMMUNITY_ETREE_H_

#include <boost/array.hpp>
#include <boost/system/error_code.hpp>

#include <string>

#include "base/parse_object.h"
#include "bgp/extended-community/types.h"

class ETree {
public:
static const int kSize = 8;
typedef boost::array<uint8_t, kSize> bytes_type;

explicit ETree(bool leaf, int label=0);
explicit ETree(const bytes_type &data);

bool leaf() const;
int label() const;

const bytes_type &GetExtCommunity() const {
return data_;
}

const uint64_t GetExtCommunityValue() const {
return get_value(data_.begin(), 8);
}

std::string ToString();

private:
bytes_type data_;
};

#endif // SRC_BGP_EXTENDED_COMMUNITY_ETREE_H_
19 changes: 15 additions & 4 deletions src/bgp/extended-community/mac_mobility.cc
Expand Up @@ -13,18 +13,28 @@
using std::copy;
using std::string;

MacMobility::MacMobility(uint32_t seq) {
MacMobility::MacMobility(uint32_t seq, bool sticky) {
data_[0] = BgpExtendedCommunityType::Evpn;
data_[1] = BgpExtendedCommunityEvpnSubType::MacMobility;
data_[2] = 0x01; // Flags
data_[3] = 0x00; // Reserved
data_[2] = (sticky ? 0x01 : 0x0); // sticky
data_[3] = 0x00; // Reserved
put_value(&data_[4], 4, seq);
}

MacMobility::MacMobility(const bytes_type &data) {
copy(data.begin(), data.end(), data_.begin());
}

bool MacMobility::sticky() const {
uint8_t data[MacMobility::kSize];
copy(data_.begin(), data_.end(), &data[0]);
if (data[0] == BgpExtendedCommunityType::Evpn &&
data[1] == BgpExtendedCommunityEvpnSubType::MacMobility) {
return (data[2] & 0x1);
}
return false;
}

uint32_t MacMobility::sequence_number() const {
uint8_t data[MacMobility::kSize];
copy(data_.begin(), data_.end(), &data[0]);
Expand All @@ -38,6 +48,7 @@ uint32_t MacMobility::sequence_number() const {

std::string MacMobility::ToString() {
char temp[50];
snprintf(temp, sizeof(temp), "mobility:%d", sequence_number());
snprintf(temp, sizeof(temp), "mobility:%s:%d",
(sticky() ? "sticky" : "non-sticky"), sequence_number());
return string(temp);
}
5 changes: 4 additions & 1 deletion src/bgp/extended-community/mac_mobility.h
Expand Up @@ -18,10 +18,11 @@ class MacMobility {
static const int kSize = 8;
typedef boost::array<uint8_t, kSize> bytes_type;

explicit MacMobility(uint32_t seq);
explicit MacMobility(uint32_t seq, bool sticky=false);
explicit MacMobility(const bytes_type &data);

uint32_t sequence_number() const;
bool sticky() const;

const bytes_type &GetExtCommunity() const {
return data_;
Expand All @@ -30,7 +31,9 @@ class MacMobility {
const uint64_t GetExtCommunityValue() const {
return get_value(data_.begin(), 8);
}

std::string ToString();

private:
bytes_type data_;
};
Expand Down
5 changes: 5 additions & 0 deletions src/bgp/extended-community/test/SConscript
Expand Up @@ -47,10 +47,15 @@ load_balance_test = env.UnitTest('load_balance_test',
['load_balance_test.cc'])
env.Alias('src/bgp/extended-community:load_balance_test', load_balance_test)

etree_test = env.UnitTest('etree_test',
['etree_test.cc'])
env.Alias('src/bgp/extended-community:etree_test', etree_test)

test_suite = [
default_gateway_test,
es_import_test,
esi_label_test,
etree_test,
load_balance_test,
mac_mobility_test,
router_mac_test,
Expand Down
67 changes: 67 additions & 0 deletions src/bgp/extended-community/test/etree_test.cc
@@ -0,0 +1,67 @@
/*
* Copyright (c) 2016 Juniper Networks, Inc. All rights reserved.
*/

#include "bgp/extended-community/etree.h"

#include "testing/gunit.h"

using namespace std;

class ETreeTest : public ::testing::Test {
};

TEST_F(ETreeTest, ByteArray_1) {
ETree::bytes_type data = { {
BgpExtendedCommunityType::Evpn,
BgpExtendedCommunityEvpnSubType::ETree,
0x01, 0x00, 0x0, 0x0, 0x0, 0x0
} };
ETree etree(data);
EXPECT_EQ("etree:leaf:0", etree.ToString());
}

TEST_F(ETreeTest, ByteArray_2) {
ETree::bytes_type data = { {
BgpExtendedCommunityType::Evpn,
BgpExtendedCommunityEvpnSubType::ETree,
0x00, 0x00, 0x0, 0x1, 0x0, 0x0
} };
ETree etree(data);
EXPECT_EQ("etree:root:4096", etree.ToString());
}

TEST_F(ETreeTest, ByteArray_3) {
ETree::bytes_type data = { {
BgpExtendedCommunityType::Evpn,
BgpExtendedCommunityEvpnSubType::ETree,
0x00, 0x00, 0x0, 0x0, 0x1, 0x0
} };
ETree etree(data);
EXPECT_EQ("etree:root:16", etree.ToString());
}


TEST_F(ETreeTest, Init) {
boost::system::error_code ec;
ETree etree(true);
EXPECT_EQ(etree.ToString(), "etree:leaf:0");
}

TEST_F(ETreeTest, Init_2) {
boost::system::error_code ec;
ETree etree(false);
EXPECT_EQ(etree.ToString(), "etree:root:0");
}

TEST_F(ETreeTest, Init_3) {
boost::system::error_code ec;
ETree etree(false, 100);
EXPECT_EQ(etree.ToString(), "etree:root:100");
}

int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
int result = RUN_ALL_TESTS();
return result;
}
30 changes: 26 additions & 4 deletions src/bgp/extended-community/test/mac_mobility_test.cc
Expand Up @@ -18,7 +18,7 @@ TEST_F(MacMobilityTest, ByteArray_1) {
0x01, 0x00, 0x0, 0x0, 0x0, 0x1
} };
MacMobility mac_mobility(data);
EXPECT_EQ("mobility:1", mac_mobility.ToString());
EXPECT_EQ("mobility:sticky:1", mac_mobility.ToString());
}

TEST_F(MacMobilityTest, ByteArray_2) {
Expand All @@ -28,21 +28,43 @@ TEST_F(MacMobilityTest, ByteArray_2) {
0x01, 0x00, 0x0, 0x1, 0x0, 0x0
} };
MacMobility mac_mobility(data);
EXPECT_EQ("mobility:65536", mac_mobility.ToString());
EXPECT_EQ("mobility:sticky:65536", mac_mobility.ToString());
}

TEST_F(MacMobilityTest, ByteArray_3) {
MacMobility::bytes_type data = { {
BgpExtendedCommunityType::Evpn,
BgpExtendedCommunityEvpnSubType::MacMobility,
0x00, 0x00, 0x0, 0x0, 0x0, 0x1
} };
MacMobility mac_mobility(data);
EXPECT_EQ("mobility:non-sticky:1", mac_mobility.ToString());
}


TEST_F(MacMobilityTest, Init) {
boost::system::error_code ec;
MacMobility mac_mobility(0x1);
EXPECT_EQ(mac_mobility.ToString(), "mobility:1");
EXPECT_EQ(mac_mobility.ToString(), "mobility:non-sticky:1");
}

TEST_F(MacMobilityTest, Init_2) {
boost::system::error_code ec;
MacMobility mac_mobility(0x10000);
EXPECT_EQ(mac_mobility.ToString(), "mobility:65536");
EXPECT_EQ(mac_mobility.ToString(), "mobility:non-sticky:65536");
}

TEST_F(MacMobilityTest, Init_3) {
boost::system::error_code ec;
MacMobility mac_mobility(0x10000, true);
EXPECT_EQ(mac_mobility.ToString(), "mobility:sticky:65536");
}

TEST_F(MacMobilityTest, Init_4) {
boost::system::error_code ec;
MacMobility mac_mobility(0x10000, false);
EXPECT_EQ(mac_mobility.ToString(), "mobility:non-sticky:65536");
}

int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
Expand Down
1 change: 1 addition & 0 deletions src/bgp/extended-community/types.h
Expand Up @@ -37,6 +37,7 @@ struct BgpExtendedCommunityEvpnSubType {
EsiMplsLabel = 0x01,
EsImport = 0x02,
RouterMac = 0x03,
ETree = 0x05,
};
};

Expand Down

0 comments on commit 6743e06

Please sign in to comment.