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 241017 : InetVpnRoute::InetVpnRoute(const InetVpnPrefix &prefix) 16 241017 : : prefix_(prefix) { 17 240982 : } 18 : 19 2031195 : int InetVpnRoute::CompareTo(const Route &rhs) const { 20 2031195 : const InetVpnRoute &other = static_cast<const InetVpnRoute &>(rhs); 21 2031195 : int res = prefix_.route_distinguisher().CompareTo( 22 : other.prefix_.route_distinguisher()); 23 2031319 : if (res != 0) { 24 796211 : return res; 25 : } 26 1235108 : Ip4Address laddr = prefix_.addr(); 27 1235091 : Ip4Address raddr = other.prefix_.addr(); 28 1235058 : if (laddr < raddr) { 29 510811 : return -1; 30 : } 31 724371 : if (laddr > raddr) { 32 445180 : return 1; 33 : } 34 279174 : if (prefix_.prefixlen() < other.prefix_.prefixlen()) { 35 20 : return -1; 36 : } 37 279191 : if (prefix_.prefixlen() > other.prefix_.prefixlen()) { 38 11 : return 1; 39 : } 40 279174 : return 0; 41 : } 42 : 43 443947 : string InetVpnRoute::ToString() const { 44 443947 : string repr = prefix_.route_distinguisher().ToString() + ":"; 45 443962 : repr += prefix_.addr().to_string(); 46 : char strplen[4]; 47 443959 : snprintf(strplen, sizeof(strplen), "/%d", prefix_.prefixlen()); 48 443951 : repr.append(strplen); 49 : 50 887940 : 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 73886 : void InetVpnRoute::BuildProtoPrefix(BgpProtoPrefix *prefix, 60 : const BgpAttr *attr, 61 : uint32_t label, 62 : uint32_t l3_label) const { 63 73886 : prefix_.BuildProtoPrefix(label, prefix, attr); 64 73877 : } 65 : 66 42953 : void InetVpnRoute::BuildBgpProtoNextHop(vector<uint8_t> &nh, 67 : IpAddress nexthop) const { 68 42953 : nh.resize(4+RouteDistinguisher::kSize); 69 42946 : const Ip4Address::bytes_type &addr_bytes = nexthop.to_v4().to_bytes(); 70 42948 : copy(addr_bytes.begin(), addr_bytes.end(), 71 42948 : nh.begin() + RouteDistinguisher::kSize); 72 42948 : } 73 : 74 58765 : DBEntryBase::KeyPtr InetVpnRoute::GetDBRequestKey() const { 75 : InetVpnTable::RequestKey *key = 76 58765 : new InetVpnTable::RequestKey(GetPrefix(), NULL); 77 58755 : 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 : }