Line data Source code
1 : /* 2 : * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved. 3 : * Copyright Contributors to the OpenSDN project 4 : */ 5 : 6 : #include "bgp/l3vpn/inetvpn_route.h" 7 : 8 : #include <algorithm> 9 : 10 : #include "bgp/l3vpn/inetvpn_table.h" 11 : 12 : using std::copy; 13 : using std::string; 14 : using std::vector; 15 : 16 13935 : InetVpnRoute::InetVpnRoute(const InetVpnPrefix &prefix) 17 13935 : : prefix_(prefix) { 18 13931 : } 19 : 20 91682 : int InetVpnRoute::CompareTo(const Route &rhs) const { 21 91682 : const InetVpnRoute &other = static_cast<const InetVpnRoute &>(rhs); 22 91682 : int res = prefix_.route_distinguisher().CompareTo( 23 : other.prefix_.route_distinguisher()); 24 91715 : if (res != 0) { 25 64393 : return res; 26 : } 27 27322 : Ip4Address laddr = prefix_.addr(); 28 27319 : Ip4Address raddr = other.prefix_.addr(); 29 27315 : if (laddr < raddr) { 30 7416 : return -1; 31 : } 32 19911 : if (laddr > raddr) { 33 4644 : return 1; 34 : } 35 15263 : if (prefix_.prefixlen() < other.prefix_.prefixlen()) { 36 0 : return -1; 37 : } 38 15264 : if (prefix_.prefixlen() > other.prefix_.prefixlen()) { 39 0 : return 1; 40 : } 41 15264 : return 0; 42 : } 43 : 44 33710 : string InetVpnRoute::ToString() const { 45 33710 : string repr = prefix_.route_distinguisher().ToString() + ":"; 46 33719 : repr += prefix_.addr().to_string(); 47 : char strplen[4]; 48 33718 : snprintf(strplen, sizeof(strplen), "/%d", prefix_.prefixlen()); 49 33717 : repr.append(strplen); 50 : 51 67438 : return repr; 52 0 : } 53 : 54 0 : void InetVpnRoute::SetKey(const DBRequestKey *reqkey) { 55 0 : const InetVpnTable::RequestKey *key = 56 : static_cast<const InetVpnTable::RequestKey *>(reqkey); 57 0 : prefix_ = key->prefix; 58 0 : } 59 : 60 4724 : void InetVpnRoute::BuildProtoPrefix(BgpProtoPrefix *prefix, 61 : const BgpAttr *attr, 62 : uint32_t label, 63 : uint32_t l3_label) const { 64 4724 : prefix_.BuildProtoPrefix(label, prefix, attr); 65 4721 : } 66 : 67 2879 : void InetVpnRoute::BuildBgpProtoNextHop(vector<uint8_t> &nh, 68 : IpAddress nexthop) const { 69 2879 : nh.resize(4+RouteDistinguisher::kSize); 70 2880 : const Ip4Address::bytes_type &addr_bytes = nexthop.to_v4().to_bytes(); 71 2880 : copy(addr_bytes.begin(), addr_bytes.end(), 72 2880 : nh.begin() + RouteDistinguisher::kSize); 73 2877 : } 74 : 75 5551 : DBEntryBase::KeyPtr InetVpnRoute::GetDBRequestKey() const { 76 : InetVpnTable::RequestKey *key = 77 5551 : new InetVpnTable::RequestKey(GetPrefix(), NULL); 78 5544 : return KeyPtr(key); 79 : } 80 : 81 : // Check whether 'this' is more specific than rhs. 82 0 : bool InetVpnRoute::IsMoreSpecific(const string &match) const { 83 0 : boost::system::error_code ec; 84 : 85 0 : InetVpnPrefix prefix = InetVpnPrefix::FromString(match, &ec); 86 0 : if (!ec) { 87 0 : return GetPrefix().IsMoreSpecific(prefix); 88 : } 89 : 90 0 : return false; 91 : } 92 : 93 : // Check whether 'this' is less specific than rhs. 94 0 : bool InetVpnRoute::IsLessSpecific(const string &match) const { 95 0 : boost::system::error_code ec; 96 : 97 0 : InetVpnPrefix prefix = InetVpnPrefix::FromString(match, &ec); 98 0 : if (!ec) { 99 0 : return prefix.IsMoreSpecific(GetPrefix()); 100 : } 101 : 102 0 : return false; 103 : }