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