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