Line data Source code
1 : /* 2 : * Copyright (c) 2018 Juniper Networks, Inc. All rights reserved. 3 : */ 4 : 5 : #ifndef SRC_VNSW_AGENT_OPER_CRYPT_TUNNEL_H_ 6 : #define SRC_VNSW_AGENT_OPER_CRYPT_TUNNEL_H_ 7 : 8 : #include <cmn/agent_cmn.h> 9 : #include <cmn/agent.h> 10 : #include <agent_types.h> 11 : #include <oper/oper_db.h> 12 : 13 : extern SandeshTraceBufferPtr CryptTunnelTraceBuf; 14 : #define CRYPT_TUNNEL_TASK_TRACE(obj, ...)\ 15 : do {\ 16 : CryptTunnelTask##obj::TraceMsg(CryptTunnelTraceBuf, __FILE__, __LINE__,\ 17 : __VA_ARGS__); \ 18 : } while (false) 19 : 20 : class Interface; 21 : class CryptTunnelTable; 22 : class CryptTunnelEntry; 23 : class InstanceTask; 24 : class InstanceTaskExecvp; 25 : class CryptTunnelTask; 26 : class CryptTunnelTaskBase; 27 : struct CryptTunnelEvent; 28 : 29 : 30 : struct CryptTunnelKey : public AgentKey { 31 0 : CryptTunnelKey(IpAddress remote_ip) : AgentKey(), remote_ip_(remote_ip) {} ; 32 0 : virtual ~CryptTunnelKey() { }; 33 : IpAddress remote_ip_; 34 : }; 35 : 36 : struct CryptTunnelConfigData : public AgentData { 37 0 : CryptTunnelConfigData(bool vr_crypt) : AgentData(), 38 0 : vr_to_vr_crypt_(vr_crypt) { }; 39 0 : virtual ~CryptTunnelConfigData() { } 40 : bool vr_to_vr_crypt_; 41 : }; 42 : 43 : struct CryptTunnelAvailableData : public AgentData { 44 0 : CryptTunnelAvailableData(bool available) : AgentData(), 45 0 : tunnel_available_(available) { }; 46 0 : virtual ~CryptTunnelAvailableData() { } 47 : bool tunnel_available_; 48 : }; 49 : 50 : class CryptTunnelEntry : AgentRefCount<CryptTunnelEntry>, public AgentDBEntry { 51 : public: 52 0 : CryptTunnelEntry(IpAddress remote_ip) : AgentDBEntry(), 53 0 : remote_ip_(remote_ip), tunnel_available_(false), 54 0 : vr_to_vr_crypt_(false), tunnel_task_(NULL) { }; 55 0 : virtual ~CryptTunnelEntry() { }; 56 : 57 : virtual bool IsLess(const DBEntry &rhs) const; 58 : virtual KeyPtr GetDBRequestKey() const; 59 : virtual void SetKey(const DBRequestKey *key); 60 : virtual string ToString() const; 61 0 : uint32_t GetRefCount() const { 62 0 : return AgentRefCount<CryptTunnelEntry>::GetRefCount(); 63 : } 64 : bool DBEntrySandesh(Sandesh *sresp, std::string &name) const; 65 : void SendObjectLog(SandeshTraceBufferPtr ptr, 66 : AgentLogEvent::type event) const; 67 0 : bool GetTunnelAvailable() const { return tunnel_available_;} 68 0 : bool GetVRToVRCrypt() const { return vr_to_vr_crypt_;} 69 0 : const IpAddress *GetRemoteIp() const { return &remote_ip_;} 70 0 : const IpAddress *GetSourceIp() const { return &source_ip_;} 71 : void SetTunnelAvailable(bool available) { tunnel_available_ = available;} 72 : void SetVRToVRCrypt(bool crypt) { vr_to_vr_crypt_ = crypt;} 73 : void UpdateTunnelReference(); 74 : CryptTunnelTaskBase *StartCryptTunnel(); 75 : void StopCryptTunnel(); 76 : void ResyncNH(); 77 : void PostAdd(); 78 : private: 79 : friend class CryptTunnelTable; 80 : IpAddress remote_ip_; 81 : IpAddress source_ip_; 82 : bool tunnel_available_; 83 : bool vr_to_vr_crypt_; 84 : CryptTunnelTask *tunnel_task_; 85 : DISALLOW_COPY_AND_ASSIGN(CryptTunnelEntry); 86 : }; 87 : 88 : class CryptTunnelTable : public AgentDBTable { 89 : public: 90 : typedef std::vector<std::string> TunnelEndpointList; 91 : 92 : CryptTunnelTable(Agent *agent, DB *db, const std::string &name); 93 : virtual ~CryptTunnelTable(); 94 : 95 : void set_vr_crypt(bool vr_crypt) { vr_to_vr_crypt_ = vr_crypt;} 96 : void set_crypt_interface(const Interface *interface) { crypt_interface_ = interface;} 97 : bool VRToVRCrypt() const { return vr_to_vr_crypt_;}; 98 : void CryptAvailability(const std::string &remote_ip, bool &crypt_traffic, bool &crypt_path_available); 99 : 100 : bool IsCryptPathAvailable(const std::string &remote_ip); 101 : bool IsCryptTraffic(const std::string &remote_ip); 102 : void Create(const std::string &remote_ip, bool crypt); 103 : void Delete(const std::string &remote_ip); 104 : void Process(DBRequest &req); 105 : 106 : virtual std::unique_ptr<DBEntry> AllocEntry(const DBRequestKey *k) const; 107 0 : virtual size_t Hash(const DBEntry *entry) const {return 0;}; 108 0 : virtual size_t Hash(const DBRequestKey *key) const {return 0;}; 109 : 110 : CryptTunnelEntry *Find(const std::string &remote_ip); 111 : virtual DBEntry *Add(const DBRequest *req); 112 : virtual bool Resync(DBEntry *entry, const DBRequest *req); 113 : virtual bool OnChange(DBEntry *entry, const DBRequest *req); 114 : virtual bool Delete(DBEntry *entry, const DBRequest *req); 115 : 116 : virtual AgentSandeshPtr GetAgentSandesh(const AgentSandeshArguments *args, 117 : const std::string &context); 118 : 119 : static DBTableBase *CreateTable(Agent *agent, DB *db, const std::string &name); 120 : static CryptTunnelTable *GetInstance() {return crypt_tunnel_table_;}; 121 : 122 : bool TunnelEventProcess(CryptTunnelEvent *event); 123 : void TunnelEventEnqueue(CryptTunnelEvent *event); 124 : 125 : private: 126 : static CryptTunnelTable* crypt_tunnel_table_; 127 : bool ChangeHandler(CryptTunnelEntry *entry, const DBRequest *req); 128 : bool vr_to_vr_crypt_; 129 : InterfaceConstRef crypt_interface_; 130 : WorkQueue<CryptTunnelEvent *> tunnel_event_queue_; 131 : DISALLOW_COPY_AND_ASSIGN(CryptTunnelTable); 132 : }; 133 : 134 : 135 : struct CryptTunnelEvent { 136 : public: 137 : enum EventType { 138 : MESSAGE_READ = 0, 139 : TASK_EXIT, 140 : SET_TUNNEL_ENTRY, 141 : STOP_TASK, 142 : EVENT_MAXIMUM 143 : }; 144 : 145 : CryptTunnelEvent(CryptTunnelTaskBase *inst, 146 : CryptTunnelEntry *entry, EventType type, 147 : const std::string &message); 148 : virtual ~CryptTunnelEvent(); 149 : 150 : CryptTunnelTaskBase *tunnel_task_; 151 : CryptTunnelEntry *entry_; 152 : EventType type_; 153 : std::string message_; 154 : DISALLOW_COPY_AND_ASSIGN(CryptTunnelEvent); 155 : }; 156 : 157 : 158 : class CryptTunnelTaskBase { 159 : public: 160 : enum CommandType { 161 : CREATE_TUNNEL = 0, 162 : UPDATE_TUNNEL, 163 : MONITOR_TUNNEL, 164 : DELETE_TUNNEL 165 : }; 166 : CryptTunnelTaskBase(CryptTunnelEntry *entry); 167 : virtual ~CryptTunnelTaskBase(); 168 : 169 : virtual bool CreateTunnelTask() = 0; 170 : 171 : // return true it instance is scheduled to destroy 172 : // when API returns false caller need to assure delete of 173 : // Crypt Tunnel Instance 174 : virtual bool DestroyTunnelTask() = 0; 175 : virtual bool RunTunnelTask(CommandType cmd_type) = 0; 176 : virtual bool StopTunnelTask() = 0; 177 0 : virtual bool UpdateTunnelTask() { return true; } 178 : 179 : // OnRead Callback for Task 180 : void OnRead(const std::string &data); 181 : // OnExit Callback for Task 182 : void OnExit(const boost::system::error_code &ec); 183 : // Callback to enqueue set tunnel entry 184 : void SetTunnelEntry(CryptTunnelEntry *entry); 185 : // Callback to enqueue stop task 186 : void StopTask(CryptTunnelEntry *service); 187 : 188 : void UpdateTunnel(const CryptTunnelEntry *entry, bool available) const; 189 : void set_tunnel_entry(CryptTunnelEntry *entry); 190 : std::string to_string(); 191 : bool active() {return active_;} 192 0 : virtual bool IsRunning() const { return true; } 193 : CryptTunnelEntry *entry() const { return entry_.get(); } 194 : const std::string &last_update_time() const { return last_update_time_; } 195 : 196 : protected: 197 : friend class CryptTunnelTable; 198 : // reference to crypt tunnel entry under 199 : // which this instance is running 200 : CryptTunnelEntryRef entry_; 201 : 202 : // current status of Crypt tunnel 203 : bool active_; 204 : // last update time 205 : std::string last_update_time_; 206 : // instance is delete marked 207 : bool deleted_; 208 : 209 : private: 210 : DISALLOW_COPY_AND_ASSIGN(CryptTunnelTaskBase); 211 : }; 212 : 213 : 214 : // using the instance task infrastructure 215 : class CryptTunnelTask : public CryptTunnelTaskBase { 216 : public: 217 : typedef InstanceTaskExecvp CryptTunnelProcessTunnel; 218 : static const std::string kCryptTunnelCmd; 219 : 220 : CryptTunnelTask(CryptTunnelEntry *entry); 221 : virtual ~CryptTunnelTask(); 222 : 223 : virtual bool CreateTunnelTask(); 224 : virtual bool DestroyTunnelTask(); 225 : virtual bool RunTunnelTask(CommandType cmd_type); 226 : virtual bool StopTunnelTask(); 227 : virtual bool IsRunning() const; 228 : 229 : private: 230 : friend class CryptTunnelTable; 231 : void UpdateTunnelTaskCommand(CommandType cmd_type); 232 : 233 : // task managing external running script for status 234 : boost::scoped_ptr<CryptTunnelProcessTunnel> task_; 235 : 236 : DISALLOW_COPY_AND_ASSIGN(CryptTunnelTask); 237 : }; 238 : 239 : #endif