Line data Source code
1 : /* 2 : * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved. 3 : */ 4 : 5 : #ifndef vnsw_agent_tunnel_nh_hpp 6 : #define vnsw_agent_tunnel_nh_hpp 7 : 8 : #include <base/dependency.h> 9 : #include <cmn/agent_cmn.h> 10 : #include <cmn/agent.h> 11 : #include <oper/nexthop.h> 12 : #include <oper/agent_route.h> 13 : 14 : class TunnelNH : public NextHop { 15 : public: 16 : TunnelNH(VrfEntry *vrf, const Ip4Address &sip, const Ip4Address &dip, 17 : bool policy, TunnelType type, const MacAddress &rewrite_dmac); 18 : virtual ~TunnelNH(); 19 : 20 0 : virtual std::string ToString() const { 21 0 : return "Tunnel to " + dip_.to_string() + 22 0 : " rewrite mac " + rewrite_dmac_.ToString(); 23 : } 24 : virtual bool NextHopIsLess(const DBEntry &rhs) const; 25 0 : virtual bool TunnelNextHopIsLess(const DBEntry &rhs) const { 26 0 : return false; 27 : } 28 : virtual void SetKey(const DBRequestKey *key); 29 : virtual bool ChangeEntry(const DBRequest *req); 30 : virtual void Delete(const DBRequest *req); 31 : virtual KeyPtr GetDBRequestKey() const; 32 : virtual bool CanAdd() const; 33 : 34 : const uint32_t vrf_id() const; 35 0 : const VrfEntry *GetVrf() const {return vrf_.get();}; 36 0 : const Ip4Address *GetSip() const {return &sip_;}; 37 0 : const Ip4Address *GetDip() const {return &dip_;}; 38 0 : const AgentRoute *GetRt() const {return tunnel_dst_rt_;}; 39 0 : const MacAddress *GetDmac() const {return &(encap_list_[0]->dmac_);} 40 0 : const bool &IsEncapValid(const uint32_t &i) const {return encap_list_[i]->valid_;} 41 0 : const MacAddress &rewrite_dmac() const {return rewrite_dmac_;} 42 0 : const TunnelType &GetTunnelType() const {return tunnel_type_;}; 43 0 : const Interface *GetCryptInterface() const {return crypt_interface_.get();}; 44 0 : bool GetCrypt() const {return crypt_;}; 45 0 : bool GetCryptTunnelAvailable() const {return crypt_tunnel_available_;}; 46 : virtual void SendObjectLog(const NextHopTable *table, 47 : AgentLogEvent::type event) const; 48 0 : virtual bool DeleteOnZeroRefCount() const { 49 0 : return true; 50 : } 51 : 52 0 : virtual bool MatchEgressData(const NextHop *nh) const { 53 0 : const TunnelNH *tun_nh = dynamic_cast<const TunnelNH *>(nh); 54 0 : if (tun_nh && vrf_ == tun_nh->vrf_ && dip_ == tun_nh->dip_) { 55 0 : return true; 56 : } 57 0 : return false; 58 : } 59 0 : virtual bool NeedMplsLabel() { return false; } 60 : 61 : struct EncapData { 62 12 : EncapData(NextHop *nh, InterfaceConstRef intf) : arp_rt_(nh), interface_(intf), valid_(false) {} 63 : DependencyRef<NextHop, AgentRoute> arp_rt_; 64 : InterfaceConstRef interface_; 65 : MacAddress dmac_; 66 : bool valid_; 67 : }; 68 : typedef boost::shared_ptr<EncapData> EncapDataPtr; 69 : typedef std::vector<EncapDataPtr> EncapDataList; 70 0 : const EncapDataList GetEncapDataList() const { return encap_list_; }; 71 : protected: 72 : VrfEntryRef vrf_; 73 : Ip4Address sip_; 74 : Ip4Address dip_; 75 : TunnelType tunnel_type_; 76 : AgentRoute* tunnel_dst_rt_; 77 : EncapDataList encap_list_; 78 : bool crypt_; 79 : bool crypt_tunnel_available_; 80 : InterfaceConstRef crypt_interface_; 81 : MacAddress rewrite_dmac_; 82 : private: 83 : DISALLOW_COPY_AND_ASSIGN(TunnelNH); 84 : }; 85 : 86 : class LabelledTunnelNH : public TunnelNH { 87 : public: 88 : LabelledTunnelNH(VrfEntry *vrf, const Ip4Address &sip, 89 : const Ip4Address &dip, bool policy, TunnelType type, 90 : const MacAddress &rewrite_dmac, uint32_t label); 91 : virtual ~LabelledTunnelNH(); 92 : 93 0 : virtual std::string ToString() const { 94 0 : return "Tunnel to " + GetDip()->to_string() + 95 0 : " rewrite mac " + rewrite_dmac().ToString(); 96 : } 97 : virtual bool TunnelNextHopIsLess(const DBEntry &rhs) const; 98 : virtual bool ChangeEntry(const DBRequest *req); 99 : virtual KeyPtr GetDBRequestKey() const; 100 0 : uint32_t const GetTransportLabel() const { return transport_mpls_label_;}; 101 : 102 : virtual void SendObjectLog(const NextHopTable *table, 103 : AgentLogEvent::type event) const; 104 0 : virtual bool DeleteOnZeroRefCount() const { 105 0 : return true; 106 : } 107 : 108 0 : virtual bool NeedMplsLabel() { return false; } 109 0 : TunnelType::Type const GetTransportTunnelType() const { 110 0 : return transport_tunnel_type_;} 111 : void SetTransportTunnelType() { 112 : transport_tunnel_type_ = TunnelType::ComputeType( 113 : TunnelType::MplsType()); 114 : } 115 : private: 116 : uint32_t transport_mpls_label_; 117 : TunnelType::Type transport_tunnel_type_; 118 : DISALLOW_COPY_AND_ASSIGN(LabelledTunnelNH); 119 : }; 120 : ///////////////////////////////////////////////////////////////////////////// 121 : // Mirror NH definition 122 : ///////////////////////////////////////////////////////////////////////////// 123 : class MirrorNHKey : public NextHopKey { 124 : public: 125 0 : MirrorNHKey(const string &vrf_name, const IpAddress &sip, 126 : const uint16_t sport, const IpAddress &dip, 127 0 : const uint16_t dport) : 128 0 : NextHopKey(NextHop::MIRROR, false), vrf_key_(vrf_name), sip_(sip), 129 0 : dip_(dip), sport_(sport), dport_(dport){ 130 0 : }; 131 0 : virtual ~MirrorNHKey() { }; 132 : 133 : virtual NextHop *AllocEntry() const; 134 0 : virtual NextHopKey *Clone() const { 135 0 : return new MirrorNHKey(vrf_key_.name_, sip_, sport_, dip_, dport_); 136 : } 137 : private: 138 : friend class MirrorNH; 139 : VrfKey vrf_key_; 140 : IpAddress sip_; 141 : IpAddress dip_; 142 : uint16_t sport_; 143 : uint16_t dport_; 144 : DISALLOW_COPY_AND_ASSIGN(MirrorNHKey); 145 : }; 146 : 147 : class MirrorNHData : public NextHopData { 148 : public: 149 : MirrorNHData() : NextHopData() {}; 150 : virtual ~MirrorNHData() { }; 151 : private: 152 : friend class MirrorNH; 153 : DISALLOW_COPY_AND_ASSIGN(MirrorNHData); 154 : }; 155 : 156 : class MirrorNH : public NextHop { 157 : public: 158 : MirrorNH(const VrfKey &vkey, const IpAddress &sip, uint16_t sport, 159 : const IpAddress &dip, uint16_t dport); 160 0 : virtual ~MirrorNH() { } 161 : 162 : virtual bool NextHopIsLess(const DBEntry &rhs) const; 163 : virtual void SetKey(const DBRequestKey *key); 164 : virtual bool ChangeEntry(const DBRequest *req); 165 : virtual void Delete(const DBRequest *req); 166 : virtual KeyPtr GetDBRequestKey() const; 167 : virtual bool CanAdd() const; 168 : 169 : const uint32_t vrf_id() const; 170 0 : const VrfEntry *GetVrf() const {return vrf_.get();} 171 0 : const IpAddress *GetSip() const {return &sip_;} 172 0 : const uint16_t GetSPort() const {return sport_;} 173 0 : const IpAddress *GetDip() const {return &dip_;} 174 0 : const uint16_t GetDPort() const {return dport_;} 175 0 : const AgentRoute *GetRt() const {return arp_rt_.get();} 176 0 : virtual std::string ToString() const { 177 0 : return "Mirror to " + dip_.to_string(); 178 : } 179 : virtual void SendObjectLog(const NextHopTable *table, 180 : AgentLogEvent::type event) const; 181 0 : virtual bool DeleteOnZeroRefCount() const { 182 0 : return true; 183 : } 184 0 : virtual bool MatchEgressData(const NextHop *nh) const { 185 0 : const MirrorNH *mir_nh = dynamic_cast<const MirrorNH *>(nh); 186 0 : if (mir_nh && vrf_ == mir_nh->vrf_ && dip_ == mir_nh->dip_) { 187 0 : return true; 188 : } 189 0 : return false; 190 : } 191 0 : virtual bool NeedMplsLabel() { return false; } 192 : private: 193 : InetUnicastAgentRouteTable *GetRouteTable(); 194 : std::string vrf_name_; 195 : VrfEntryRef vrf_; 196 : IpAddress sip_; 197 : uint16_t sport_; 198 : IpAddress dip_; 199 : uint16_t dport_; 200 : DependencyRef<NextHop, AgentRoute> arp_rt_; 201 : InterfaceConstRef interface_; 202 : MacAddress dmac_; 203 : DISALLOW_COPY_AND_ASSIGN(MirrorNH); 204 : }; 205 : #endif