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 1046 : InetVpnRoute::InetVpnRoute(const InetVpnPrefix &prefix) 17 1046 : : prefix_(prefix) { 18 1045 : } 19 : 20 3726 : int InetVpnRoute::CompareTo(const Route &rhs) const { 21 3726 : const InetVpnRoute &other = static_cast<const InetVpnRoute &>(rhs); 22 3726 : int res = prefix_.route_distinguisher().CompareTo( 23 : other.prefix_.route_distinguisher()); 24 3725 : if (res != 0) { 25 1841 : return res; 26 : } 27 1884 : Ip4Address laddr = prefix_.addr(); 28 1884 : Ip4Address raddr = other.prefix_.addr(); 29 1884 : if (laddr < raddr) { 30 320 : return -1; 31 : } 32 1564 : if (laddr > raddr) { 33 64 : return 1; 34 : } 35 1500 : if (prefix_.prefixlen() < other.prefix_.prefixlen()) { 36 0 : return -1; 37 : } 38 1500 : if (prefix_.prefixlen() > other.prefix_.prefixlen()) { 39 0 : return 1; 40 : } 41 1500 : return 0; 42 : } 43 : 44 1078 : string InetVpnRoute::ToString() const { 45 1078 : string repr = prefix_.route_distinguisher().ToString() + ":"; 46 1078 : repr += prefix_.addr().to_string(); 47 : char strplen[4]; 48 1078 : snprintf(strplen, sizeof(strplen), "/%d", prefix_.prefixlen()); 49 1078 : repr.append(strplen); 50 : 51 2156 : 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 0 : void InetVpnRoute::BuildProtoPrefix(BgpProtoPrefix *prefix, 61 : const BgpAttr *attr, 62 : uint32_t label, 63 : uint32_t l3_label) const { 64 0 : prefix_.BuildProtoPrefix(label, prefix, attr); 65 0 : } 66 : 67 0 : void InetVpnRoute::BuildBgpProtoNextHop(vector<uint8_t> &nh, 68 : IpAddress nexthop) const { 69 0 : nh.resize(4+RouteDistinguisher::kSize); 70 0 : const Ip4Address::bytes_type &addr_bytes = nexthop.to_v4().to_bytes(); 71 0 : copy(addr_bytes.begin(), addr_bytes.end(), 72 0 : nh.begin() + RouteDistinguisher::kSize); 73 0 : } 74 : 75 0 : DBEntryBase::KeyPtr InetVpnRoute::GetDBRequestKey() const { 76 : InetVpnTable::RequestKey *key = 77 0 : new InetVpnTable::RequestKey(GetPrefix(), NULL); 78 0 : 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 : }