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