Line data Source code
1 : /* 2 : * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved. 3 : * Copyright Contributors to the OpenSDN project 4 : */ 5 : #ifndef vnsw_agent_vgw_cfg_h 6 : #define vnsw_agent_vgw_cfg_h 7 : 8 : #include <boost/property_tree/ptree.hpp> 9 : #include <base/address.h> 10 : 11 : class InetInterface; 12 : 13 : // Simple Virtual Gateway config class 14 : // Supports virtual-gateway for single virtual-network for now. 15 : class VirtualGatewayConfig { 16 : public: 17 : struct Subnet { 18 0 : Subnet() : ip_(0), plen_(0) {} 19 0 : Subnet(const Ip4Address &ip, uint8_t plen) : ip_(ip), plen_(plen) {} 20 0 : ~Subnet() {} 21 0 : bool operator<(const Subnet &rhs) const { 22 0 : if (ip_ != rhs.ip_) 23 0 : return ip_ < rhs.ip_; 24 : 25 0 : return (plen_ < rhs.plen_); 26 : } 27 : 28 : Ip4Address ip_; 29 : uint8_t plen_; 30 : }; 31 : typedef std::vector<Subnet> SubnetList; 32 : 33 68 : VirtualGatewayConfig(const std::string &interface_name) : 34 68 : interface_name_(interface_name), vrf_name_(""), subnets_(), routes_(), 35 68 : interface_(), version_(0) {} 36 0 : VirtualGatewayConfig(const std::string &interface_name, 37 : const std::string &vrf_name, 38 : const SubnetList &subnets, 39 : const SubnetList &routes, 40 0 : uint32_t version) : 41 0 : interface_name_(interface_name), vrf_name_(vrf_name), 42 0 : subnets_(subnets), routes_(routes), version_(version) {} 43 0 : VirtualGatewayConfig(const VirtualGatewayConfig &rhs) : 44 0 : interface_name_(rhs.interface_name_), vrf_name_(rhs.vrf_name_), 45 0 : subnets_(rhs.subnets_), routes_(rhs.routes_), version_(rhs.version_) {} 46 86 : ~VirtualGatewayConfig() {} 47 : 48 351 : const std::string& interface_name() const { return interface_name_; } 49 166 : const std::string& vrf_name() const { return vrf_name_; } 50 87 : const SubnetList& subnets() const { return subnets_; } 51 100 : const SubnetList& routes() const { return routes_; } 52 0 : uint32_t version() const { return version_; } 53 0 : void set_subnets(const SubnetList &subnets) const { subnets_ = subnets; } 54 0 : void set_routes(const SubnetList &routes) const { routes_ = routes; } 55 0 : const InetInterface *get_interface() const { return interface_; } 56 0 : void set_interface(InetInterface *intrface) const { 57 0 : interface_ = intrface; 58 0 : } 59 0 : void set_version(uint32_t version) const { version_ = version; } 60 : 61 : private: 62 : // Interface connecting gateway to host-os 63 : std::string interface_name_; 64 : // Public network name 65 : std::string vrf_name_; 66 : // Vector of subnets 67 : mutable SubnetList subnets_; 68 : // Vector of routes 69 : mutable SubnetList routes_; 70 : // Inet interface pointer 71 : mutable InetInterface *interface_; 72 : // client version number of the entry 73 : mutable uint32_t version_; 74 : }; 75 : 76 : struct VirtualGatewayInfo { 77 : std::string interface_name_; 78 : std::string vrf_name_; 79 : VirtualGatewayConfig::SubnetList subnets_; 80 : VirtualGatewayConfig::SubnetList routes_; 81 : 82 0 : VirtualGatewayInfo(const std::string &interfacestr) 83 0 : : interface_name_(interfacestr) {} 84 : VirtualGatewayInfo(const std::string &interfacestr, const std::string &vrf, 85 : VirtualGatewayConfig::SubnetList &subnets, 86 : VirtualGatewayConfig::SubnetList &routes) 87 : : interface_name_(interfacestr), vrf_name_(vrf) { 88 : subnets_.swap(subnets); 89 : routes_.swap(routes); 90 : } 91 : }; 92 : 93 : struct VirtualGatewayData { 94 : enum MessageType { 95 : Add, 96 : Delete, 97 : Audit 98 : }; 99 : 100 : MessageType message_type_; 101 : std::vector<VirtualGatewayInfo> vgw_list_; 102 : uint32_t version_; 103 : 104 0 : VirtualGatewayData(MessageType type, std::vector<VirtualGatewayInfo> &list, 105 : uint32_t version) 106 0 : : message_type_(type), version_(version) { 107 0 : vgw_list_.swap(list); 108 0 : } 109 : VirtualGatewayData(MessageType type, uint32_t version) 110 : : message_type_(type), version_(version) {} 111 : }; 112 : 113 : class VirtualGatewayConfigTable { 114 : public: 115 : struct VirtualGatewayConfigCompare { 116 150 : bool operator()(const VirtualGatewayConfig &cfg1, 117 : const VirtualGatewayConfig &cfg2) const { 118 150 : return cfg1.interface_name() < cfg2.interface_name(); 119 : } 120 : }; 121 : 122 : typedef std::set<VirtualGatewayConfig, VirtualGatewayConfigCompare> Table; 123 : 124 2 : VirtualGatewayConfigTable() : agent_(NULL), work_queue_(nullptr) { } 125 2 : ~VirtualGatewayConfigTable() { } 126 : 127 : void InitFromConfig(const boost::property_tree::ptree pt); 128 : void InitDone(Agent *agent); 129 : void Shutdown(); 130 96 : const Table &table() const {return table_;} 131 : 132 : void Enqueue(boost::shared_ptr<VirtualGatewayData> request); 133 : bool ProcessRequest(boost::shared_ptr<VirtualGatewayData> request); 134 : 135 : private: 136 : void BuildSubnetList(const std::string &subnets, 137 : VirtualGatewayConfig::SubnetList &results); 138 : bool AddVgw(VirtualGatewayInfo &vgw, uint32_t version); 139 : bool DeleteVgw(const std::string &interface_name); 140 : void DeleteVgw(Table::iterator it); 141 : void DeleteAllOldVersionVgw(uint32_t version); 142 : bool FindChange(const VirtualGatewayConfig::SubnetList &old_subnets, 143 : const VirtualGatewayConfig::SubnetList &new_subnets, 144 : VirtualGatewayConfig::SubnetList &add_list, 145 : VirtualGatewayConfig::SubnetList &del_list); 146 : 147 : Agent *agent_; 148 : Table table_; 149 : std::unique_ptr< WorkQueue<boost::shared_ptr<VirtualGatewayData> > > work_queue_; 150 : DISALLOW_COPY_AND_ASSIGN(VirtualGatewayConfigTable); 151 : }; 152 : 153 : #endif //vnsw_agent_vgw_cfg_h