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 : #ifndef __CONTROLLER_PEER_H__
7 : #define __CONTROLLER_PEER_H__
8 :
9 : #include <map>
10 : #include <string>
11 :
12 : #include <boost/function.hpp>
13 : #include <boost/system/error_code.hpp>
14 : #include <boost/scoped_ptr.hpp>
15 : #include <boost/shared_ptr.hpp>
16 : #include <cmn/agent.h>
17 : #include <oper/peer.h>
18 : #include <pugixml/pugixml.hpp>
19 : #include <xmpp_enet_types.h>
20 : #include <xmpp_unicast_types.h>
21 : #include <xmpp/xmpp_channel.h>
22 :
23 : class AgentRoute;
24 : class Peer;
25 : class BgpPeer;
26 : class VrfEntry;
27 : class XmlPugi;
28 : class PathPreference;
29 : class AgentPath;
30 : class EcmpLoadBalance;
31 : struct ControllerTimer;
32 : struct EndOfRibTxTimer;
33 : struct EndOfRibRxTimer;
34 : struct LlgrStaleTimer;
35 : class ControllerEcmpRoute;
36 :
37 : class XmlWriter : public pugi::xml_writer {
38 : public:
39 296 : explicit XmlWriter(std::string *repr) : repr_(repr) { }
40 541 : virtual void write(const void *data, size_t size) {
41 541 : repr_->append(static_cast<const char*>(data), size);
42 541 : }
43 : private:
44 : std::string *repr_;
45 : };
46 :
47 : class AgentXmppChannel {
48 : public:
49 : AgentXmppChannel(Agent *agent,
50 : const std::string &xmpp_server,
51 : const std::string &label_range, uint8_t xs_idx);
52 : virtual ~AgentXmppChannel();
53 :
54 : virtual std::string ToString() const;
55 : virtual bool SendUpdate(const uint8_t *msg, size_t msgsize);
56 : virtual void ReceiveUpdate(const XmppStanza::XmppMessage *msg);
57 : virtual void ReceiveEvpnUpdate(XmlPugi *pugi);
58 : virtual void ReceiveMulticastUpdate(XmlPugi *pugi);
59 : virtual void ReceiveMvpnUpdate(XmlPugi *pugi);
60 : virtual void ReceiveV4V6Update(XmlPugi *pugi);
61 : virtual void ReceiveInet4MplsUpdate(XmlPugi *pugi);
62 4223 : XmppChannel *GetXmppChannel() { return channel_; }
63 : void ReceiveBgpMessage(std::unique_ptr<XmlBase> impl);
64 :
65 : //Helper to identify if specified peer has active BGP peer attached
66 : static bool IsXmppChannelActive(const Agent *agent, AgentXmppChannel *peer);
67 : static bool IsBgpPeerActive(const Agent *agent, AgentXmppChannel *peer);
68 : static bool SetConfigPeer(AgentXmppChannel *peer);
69 : static void SetMulticastPeer(AgentXmppChannel *old_peer,
70 : AgentXmppChannel *new_peer);
71 : static void CleanConfigStale(AgentXmppChannel *agent_xmpp_channel);
72 : static void XmppClientChannelEvent(AgentXmppChannel *peer,
73 : xmps::PeerState state);
74 : static void HandleAgentXmppClientChannelEvent(AgentXmppChannel *peer,
75 : xmps::PeerState state);
76 : static bool ControllerSendCfgSubscribe(AgentXmppChannel *peer);
77 : static bool ControllerSendVmCfgSubscribe(AgentXmppChannel *peer,
78 : const boost::uuids::uuid &vm_id, bool subscribe);
79 : static bool ControllerSendSubscribe(AgentXmppChannel *peer,
80 : VrfEntry *vrf,
81 : bool subscribe);
82 : static bool ControllerSendRouteAdd(AgentXmppChannel *peer,
83 : AgentRoute *route,
84 : const Ip4Address *nexthop_ip,
85 : const VnListType &vn_list,
86 : uint32_t label,
87 : uint32_t tunnel_bmap,
88 : const SecurityGroupList *sg_list,
89 : const TagList *tag_list,
90 : const CommunityList *communities,
91 : Agent::RouteTableType type,
92 : const PathPreference &path_preference,
93 : const EcmpLoadBalance &ecmp_load_balance,
94 : uint32_t native_vrf_id);
95 : static bool ControllerSendEvpnRouteAdd(AgentXmppChannel *peer,
96 : AgentRoute *route,
97 : const Ip4Address *nexthop_ip,
98 : std::string vn,
99 : uint32_t mpls_label,
100 : uint32_t tunnel_bmap,
101 : const SecurityGroupList *sg_list,
102 : const TagList *tag_list,
103 : const CommunityList *communities,
104 : const std::string &destination,
105 : const std::string &source,
106 : const PathPreference &path_preference);
107 : static bool ControllerSendMcastRouteAdd(AgentXmppChannel *peer,
108 : AgentRoute *route);
109 : //Deletes to control node
110 : static bool ControllerSendRouteDelete(AgentXmppChannel *peer,
111 : AgentRoute *route,
112 : const VnListType &vn_list,
113 : uint32_t label,
114 : uint32_t tunnel_bmap,
115 : const SecurityGroupList *sg_list,
116 : const TagList *tag_list,
117 : const CommunityList *communities,
118 : Agent::RouteTableType type,
119 : const PathPreference &path_preference);
120 : static bool ControllerSendEvpnRouteDelete(AgentXmppChannel *peer,
121 : AgentRoute *route,
122 : std::string vn,
123 : uint32_t mpls_label,
124 : const std::string &destination,
125 : const std::string &source,
126 : uint32_t tunnel_bmap);
127 : static bool ControllerSendMcastRouteDelete(AgentXmppChannel *peer,
128 : AgentRoute *route);
129 : //Channel event handlers
130 : void Ready();
131 : void NotReady();
132 : void TimedOut();
133 :
134 : //End of rib rx/tx
135 : void StartEndOfRibTxWalker();
136 : void StopEndOfRibTxWalker();
137 : void EndOfRibTx();
138 : void EndOfRibRx();
139 :
140 : // Routines for BGP peer manipulations, lifecycle of bgp peer in xmpp
141 : // channel is as follows:
142 : // 1) Created whenever channel is xmps::READY
143 : // 2) When channel moves out of READY state, bgp peer moves to decommisioned
144 : // list. Once moved there it can never go back to active peer and can only
145 : // get deleted later.
146 : // 3) On arrival of some other active peer(i.e. channel is READY) cleanup
147 : // timers are started, expiration of which triggers removal of
148 : // decommissioned peer and eventually gets destroyed.
149 : void CreateBgpPeer();
150 : void RegisterXmppChannel(XmppChannel *channel);
151 :
152 217 : std::string GetXmppServer() { return xmpp_server_; }
153 226 : uint8_t GetXmppServerIdx() { return xs_idx_; }
154 32 : std::string GetMcastLabelRange() { return label_range_; }
155 :
156 709 : Agent *agent() const {return agent_;}
157 4264 : BgpPeer *bgp_peer_id() {
158 4264 : return static_cast<BgpPeer *>(bgp_peer_id_.get());
159 : }
160 : PeerPtr bgp_peer_id_ref() {return bgp_peer_id_;}
161 : std::string GetBgpPeerName() const;
162 : void UpdateConnectionInfo(xmps::PeerState state);
163 : bool ControllerSendEvpnRouteCommon(AgentRoute *route,
164 : const Ip4Address *nexthop_ip,
165 : std::string vn,
166 : const SecurityGroupList *sg_list,
167 : const TagList *tag_list,
168 : const CommunityList *communities,
169 : uint32_t mpls_label,
170 : uint32_t tunnel_bmap,
171 : const std::string &destination,
172 : const std::string &source,
173 : const PathPreference &path_preference,
174 : bool associate);
175 : bool ControllerSendMcastRouteCommon(AgentRoute *route,
176 : bool associate);
177 : bool ControllerSendIPMcastRouteCommon(AgentRoute *route, bool associate);
178 : bool ControllerSendMvpnRouteCommon(AgentRoute *route, bool associate);
179 : bool BuildEvpnMulticastMessage(autogen::EnetItemType &item,
180 : std::stringstream &node_id,
181 : AgentRoute *route,
182 : const Ip4Address *nh_ip,
183 : const std::string &vn,
184 : const SecurityGroupList *sg_list,
185 : const TagList *tag_list,
186 : const CommunityList *communities,
187 : uint32_t label,
188 : uint32_t tunnel_bmap,
189 : bool associate,
190 : const AgentPath *path,
191 : bool assisted_replication);
192 : void AddMulticastEvpnRoute(const std::string &vrf_name,
193 : const IpAddress &source,
194 : const IpAddress &group,
195 : autogen::EnetItemType *item);
196 : void AddEvpnRoute(const std::string &vrf_name, std::string mac_addr,
197 : const IpAddress &ip, uint32_t plen,
198 : autogen::EnetItemType *item);
199 : void AddEvpnEcmpRoute(std::string vrf_name, const MacAddress &mac,
200 : const IpAddress &ip, uint32_t plen,
201 : autogen::EnetItemType *item,
202 : const VnListType &vn_list);
203 : template <typename TYPE>
204 : void BuildTagList(const TYPE *item, TagList &tag_list);
205 : uint64_t route_published_time() const {return route_published_time_;}
206 : EndOfRibTxTimer *end_of_rib_tx_timer();
207 : EndOfRibRxTimer *end_of_rib_rx_timer();
208 : LlgrStaleTimer *llgr_stale_timer();
209 : //Sequence number for this channel
210 : uint64_t sequence_number() const;
211 : void Unregister();
212 :
213 : protected:
214 : virtual void WriteReadyCb(const boost::system::error_code &ec);
215 :
216 : private:
217 : void AddFabricVrfRoute(const Ip4Address &prefix_addr,
218 : uint32_t prefix_len,
219 : const Ip4Address &addr,
220 : const VnListType &vn_list,
221 : const SecurityGroupList &sg_list,
222 : const TagList &tag_list);
223 : void PeerIsNotConfig();
224 : InetUnicastAgentRouteTable *PrefixToRouteTable(const std::string &vrf_name,
225 : const IpAddress &prefix_addr);
226 : InetUnicastAgentRouteTable *PrefixToRouteMplsTable(const std::string &vrf_name,
227 : const IpAddress &prefix_addr);
228 : void ReceiveInternal(const XmppStanza::XmppMessage *msg);
229 : void AddRoute(std::string vrf_name, IpAddress ip, uint32_t plen,
230 : autogen::ItemType *item);
231 : void AddMplsRoute(std::string vrf_name, IpAddress ip, uint32_t plen,
232 : autogen::ItemType *item);
233 : void AddMulticastEvpnRoute(const std::string &vrf_name,
234 : const MacAddress &mac,
235 : autogen::EnetItemType *item);
236 : void AddRemoteMplsRoute(std::string vrf_name, IpAddress ip, uint32_t plen,
237 : autogen::ItemType *item,
238 : const VnListType &vn_list);
239 : void AddRemoteRoute(std::string vrf_name, IpAddress prefix_addr,
240 : uint32_t prefix_len, autogen::ItemType *item,
241 : const VnListType &vn_list);
242 : void AddInetEcmpRoute(std::string vrf_name, IpAddress ip, uint32_t plen,
243 : autogen::ItemType *item,
244 : const VnListType &vn_list);
245 : void AddInetMplsEcmpRoute(std::string vrf_name, IpAddress ip, uint32_t plen,
246 : autogen::ItemType *item,
247 : const VnListType &vn_list);
248 : template <typename TYPE>
249 : ControllerEcmpRoute *BuildEcmpData(TYPE *item,
250 : const VnListType &vn_list,
251 : const EcmpLoadBalance &ecmp_load_balance,
252 : const AgentRouteTable *rt_table,
253 : const std::string &prefix_str);
254 : //Common helpers
255 : bool ControllerSendV4V6UnicastRouteCommon(AgentRoute *route,
256 : const VnListType &vn_list,
257 : const SecurityGroupList *sg_list,
258 : const TagList *tag_list,
259 : const CommunityList *communities,
260 : uint32_t mpls_label,
261 : uint32_t tunnel_bmap,
262 : const PathPreference &path_preference,
263 : bool associate,
264 : Agent::RouteTableType type,
265 : const EcmpLoadBalance &ecmp_load_balance,
266 : uint32_t native_vrf_id);
267 : bool BuildTorMulticastMessage(autogen::EnetItemType &item,
268 : std::stringstream &node_id,
269 : AgentRoute *route,
270 : const Ip4Address *nh_ip,
271 : const std::string &vn,
272 : const SecurityGroupList *sg_list,
273 : const TagList *tag_list,
274 : const CommunityList *communities,
275 : uint32_t label,
276 : uint32_t tunnel_bmap,
277 : const std::string &destination,
278 : const std::string &source,
279 : bool associate);
280 : bool BuildEvpnUnicastMessage(autogen::EnetItemType &item,
281 : std::stringstream &node_id,
282 : AgentRoute *route,
283 : const Ip4Address *nh_ip,
284 : const std::string &vn,
285 : const SecurityGroupList *sg_list,
286 : const TagList *tag_list,
287 : const CommunityList *communities,
288 : uint32_t label,
289 : uint32_t tunnel_bmap,
290 : const PathPreference &path_prefernce,
291 : bool associate);
292 : bool BuildAndSendEvpnDom(autogen::EnetItemType &item,
293 : std::stringstream &ss_node,
294 : const AgentRoute *route,
295 : bool associate);
296 : template <typename TYPE> bool IsEcmp(const TYPE &nexthops);
297 : template <typename TYPE> void GetVnList(const TYPE &nexthops,
298 : VnListType *vn_list);
299 :
300 : XmppChannel *channel_;
301 : std::string channel_str_;
302 : std::string xmpp_server_;
303 : std::string label_range_;
304 : uint8_t xs_idx_;
305 : PeerPtr bgp_peer_id_;
306 : uint64_t route_published_time_;
307 : boost::scoped_ptr<EndOfRibTxTimer> end_of_rib_tx_timer_;
308 : boost::scoped_ptr<EndOfRibRxTimer> end_of_rib_rx_timer_;
309 : boost::scoped_ptr<LlgrStaleTimer> llgr_stale_timer_;
310 : Agent *agent_;
311 : };
312 :
313 : #endif // __CONTROLLER_PEER_H__
|