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 240863 : InetVpnRoute::InetVpnRoute(const InetVpnPrefix &prefix) 16 240863 : : prefix_(prefix) { 17 240814 : } 18 : 19 2027767 : int InetVpnRoute::CompareTo(const Route &rhs) const { 20 2027767 : const InetVpnRoute &other = static_cast<const InetVpnRoute &>(rhs); 21 2027767 : int res = prefix_.route_distinguisher().CompareTo( 22 : other.prefix_.route_distinguisher()); 23 2027913 : if (res != 0) { 24 793867 : return res; 25 : } 26 1234046 : Ip4Address laddr = prefix_.addr(); 27 1234042 : Ip4Address raddr = other.prefix_.addr(); 28 1234009 : if (laddr < raddr) { 29 509140 : return -1; 30 : } 31 724956 : if (laddr > raddr) { 32 446026 : return 1; 33 : } 34 278901 : if (prefix_.prefixlen() < other.prefix_.prefixlen()) { 35 20 : return -1; 36 : } 37 278888 : if (prefix_.prefixlen() > other.prefix_.prefixlen()) { 38 11 : return 1; 39 : } 40 278868 : return 0; 41 : } 42 : 43 441974 : string InetVpnRoute::ToString() const { 44 441974 : string repr = prefix_.route_distinguisher().ToString() + ":"; 45 441991 : repr += prefix_.addr().to_string(); 46 : char strplen[4]; 47 441986 : snprintf(strplen, sizeof(strplen), "/%d", prefix_.prefixlen()); 48 441986 : repr.append(strplen); 49 : 50 883996 : 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 73333 : void InetVpnRoute::BuildProtoPrefix(BgpProtoPrefix *prefix, 60 : const BgpAttr *attr, 61 : uint32_t label, 62 : uint32_t l3_label) const { 63 73333 : prefix_.BuildProtoPrefix(label, prefix, attr); 64 73318 : } 65 : 66 43165 : void InetVpnRoute::BuildBgpProtoNextHop(vector<uint8_t> &nh, 67 : IpAddress nexthop) const { 68 43165 : nh.resize(4+RouteDistinguisher::kSize); 69 43163 : const Ip4Address::bytes_type &addr_bytes = nexthop.to_v4().to_bytes(); 70 43159 : copy(addr_bytes.begin(), addr_bytes.end(), 71 43161 : nh.begin() + RouteDistinguisher::kSize); 72 43155 : } 73 : 74 58798 : DBEntryBase::KeyPtr InetVpnRoute::GetDBRequestKey() const { 75 : InetVpnTable::RequestKey *key = 76 58798 : new InetVpnTable::RequestKey(GetPrefix(), NULL); 77 58794 : 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 : }