47226ae9b76e26fbbe30dd966aa949a00edb14b5
[public/netxms.git] / src / server / include / nms_topo.h
1 /*
2 ** NetXMS - Network Management System
3 ** Copyright (C) 2003-2013 Victor Kirhenshtein
4 **
5 ** This program is free software; you can redistribute it and/or modify
6 ** it under the terms of the GNU General Public License as published by
7 ** the Free Software Foundation; either version 2 of the License, or
8 ** (at your option) any later version.
9 **
10 ** This program is distributed in the hope that it will be useful,
11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ** GNU General Public License for more details.
14 **
15 ** You should have received a copy of the GNU General Public License
16 ** along with this program; if not, write to the Free Software
17 ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 **
19 ** File: nms_topo.h
20 **
21 **/
22
23 #ifndef _nms_topo_h_
24 #define _nms_topo_h_
25
26 #include <netxms_maps.h>
27
28 class Node;
29 class Interface;
30
31 /**
32 * LLDP local port info
33 */
34 struct LLDP_LOCAL_PORT_INFO
35 {
36 UINT32 portNumber;
37 BYTE localId[256];
38 size_t localIdLen;
39 TCHAR ifDescr[192];
40 };
41
42 /**
43 * Hop information structure
44 */
45 struct HOP_INFO
46 {
47 UINT32 nextHop; // Next hop address
48 NetObj *object; // Current hop object
49 UINT32 ifIndex; // Interface index or VPN connector object ID
50 bool isVpn; // TRUE if next hop is behind VPN tunnel
51 TCHAR name[MAX_OBJECT_NAME];
52 };
53
54 /**
55 * Network path trace
56 */
57 class NetworkPath
58 {
59 private:
60 UINT32 m_sourceAddress;
61 int m_hopCount;
62 int m_allocated;
63 HOP_INFO *m_path;
64 bool m_complete;
65
66 public:
67 NetworkPath(UINT32 srcAddr);
68 ~NetworkPath();
69
70 void addHop(UINT32 nextHop, NetObj *currentObject, UINT32 ifIndex, bool isVpn, const TCHAR *name);
71 void setComplete() { m_complete = true; }
72
73 UINT32 getSourceAddress() { return m_sourceAddress; }
74 bool isComplete() { return m_complete; }
75 int getHopCount() { return m_hopCount; }
76 HOP_INFO *getHopInfo(int index) { return ((index >= 0) && (index < m_hopCount)) ? &m_path[index] : NULL; }
77
78 void fillMessage(CSCPMessage *msg);
79 };
80
81 /**
82 * FDB entry
83 */
84 struct FDB_ENTRY
85 {
86 UINT32 port; // Port number
87 UINT32 ifIndex; // Interface index
88 BYTE macAddr[MAC_ADDR_LENGTH]; // MAC address
89 UINT32 nodeObject; // ID of node object or 0 if not found
90 };
91
92 /**
93 * FDB port mapping entry
94 */
95 struct PORT_MAPPING_ENTRY
96 {
97 UINT32 port;
98 UINT32 ifIndex;
99 };
100
101 /**
102 * Switch forwarding database
103 */
104 class ForwardingDatabase : public RefCountObject
105 {
106 private:
107 int m_fdbSize;
108 int m_fdbAllocated;
109 FDB_ENTRY *m_fdb;
110 int m_pmSize;
111 int m_pmAllocated;
112 PORT_MAPPING_ENTRY *m_portMap;
113 time_t m_timestamp;
114
115 UINT32 ifIndexFromPort(UINT32 port);
116
117 public:
118 ForwardingDatabase();
119 virtual ~ForwardingDatabase();
120
121 void addEntry(FDB_ENTRY *entry);
122 void addPortMapping(PORT_MAPPING_ENTRY *entry);
123 void sort();
124
125 time_t getTimeStamp() { return m_timestamp; }
126 int getAge() { return (int)(time(NULL) - m_timestamp); }
127 int getSize() { return m_fdbSize; }
128 FDB_ENTRY *getEntry(int index) { return ((index >= 0) && (index < m_fdbSize)) ? &m_fdb[index] : NULL; }
129
130 UINT32 findMacAddress(const BYTE *macAddr);
131 bool isSingleMacOnPort(UINT32 ifIndex, BYTE *macAddr = NULL);
132 int getMacCountOnPort(UINT32 ifIndex);
133
134 void print(CONSOLE_CTX ctx, Node *owner);
135 };
136
137 /**
138 * Link layer discovery protocols
139 */
140 enum LinkLayerProtocol
141 {
142 LL_PROTO_UNKNOWN = 0, /* unknown source */
143 LL_PROTO_FDB = 1, /* obtained from switch forwarding database */
144 LL_PROTO_CDP = 2, /* Cisco Discovery Protocol */
145 LL_PROTO_LLDP = 3, /* Link Layer Discovery Protocol */
146 LL_PROTO_NDP = 4, /* Nortel Discovery Protocol */
147 LL_PROTO_EDP = 5, /* Extreme Discovery Protocol */
148 LL_PROTO_STP = 6 /* Spanning Tree Protocol */
149 };
150
151 /**
152 * Link layer neighbor information
153 */
154 struct LL_NEIGHBOR_INFO
155 {
156 UINT32 ifLocal; // Local interface index
157 UINT32 ifRemote; // Remote interface index
158 UINT32 objectId; // ID of connected object
159 bool isPtToPt; // true if this is point-to-point link
160 int protocol; // Protocol used to obtain information
161 };
162
163 /**
164 * link layer neighbors
165 */
166 class LinkLayerNeighbors : public RefCountObject
167 {
168 private:
169 int m_count;
170 int m_allocated;
171 LL_NEIGHBOR_INFO *m_connections;
172 void *m_data[4];
173
174 bool isDuplicate(LL_NEIGHBOR_INFO *info);
175
176 public:
177 LinkLayerNeighbors();
178 virtual ~LinkLayerNeighbors();
179
180 void addConnection(LL_NEIGHBOR_INFO *info);
181 LL_NEIGHBOR_INFO *getConnection(int index) { return ((index >= 0) && (index < m_count)) ? &m_connections[index] : NULL; }
182
183 void setData(int index, void *data) { if ((index >= 0) && (index < 4)) m_data[index] = data; }
184 void *getData(int index) { return ((index >= 0) && (index < 4)) ? m_data[index] : NULL; }
185 void setData(void *data) { setData(0, data); }
186 void *getData() { return getData(0); }
187 int getSize() { return m_count; }
188 };
189
190
191 //
192 // VRRP information
193 //
194
195 #define VRRP_STATE_INITIALIZE 1
196 #define VRRP_STATE_BACKUP 2
197 #define VRRP_STATE_MASTER 3
198
199 #define VRRP_VIP_ACTIVE 1
200 #define VRRP_VIP_DISABLED 2
201 #define VRRP_VIP_NOTREADY 3
202
203 class VrrpRouter
204 {
205 friend UINT32 VRRPHandler(UINT32, SNMP_Variable *, SNMP_Transport *, void *);
206
207 private:
208 UINT32 m_id;
209 UINT32 m_ifIndex;
210 int m_state;
211 BYTE m_virtualMacAddr[MAC_ADDR_LENGTH];
212 int m_ipAddrCount;
213 UINT32 *m_ipAddrList;
214
215 void addVirtualIP(SNMP_Variable *var);
216 static UINT32 walkerCallback(UINT32 snmpVersion, SNMP_Variable *var, SNMP_Transport *transport, void *arg);
217
218 protected:
219 bool readVirtualIP(UINT32 snmpVersion, SNMP_Transport *transport);
220
221 public:
222 VrrpRouter(UINT32 id, UINT32 ifIndex, int state, BYTE *macAddr);
223 ~VrrpRouter();
224
225 UINT32 getId() { return m_id; }
226 UINT32 getIfIndex() { return m_ifIndex; }
227 int getState() { return m_state; }
228 BYTE *getVirtualMacAddr() { return m_virtualMacAddr; }
229 int getVipCount() { return m_ipAddrCount; }
230 UINT32 getVip(int index) { return ((index >= 0) && (index < m_ipAddrCount)) ? m_ipAddrList[index] : 0; }
231 };
232
233 class VrrpInfo
234 {
235 friend UINT32 VRRPHandler(UINT32, SNMP_Variable *, SNMP_Transport *, void *);
236
237 private:
238 int m_version;
239 ObjectArray<VrrpRouter> *m_routers;
240
241 protected:
242 void addRouter(VrrpRouter *router) { m_routers->add(router); }
243
244 public:
245 VrrpInfo(int version);
246 ~VrrpInfo();
247
248 int getVersion() { return m_version; }
249 int getSize() { return m_routers->size(); }
250 VrrpRouter *getRouter(int index) { return m_routers->get(index); }
251 };
252
253
254 //
255 // Topology functions
256 //
257
258 NetworkPath *TraceRoute(Node *pSrc, Node *pDest);
259 void BuildL2Topology(nxmap_ObjList &topology, Node *root, int nDepth, bool includeEndNodes);
260 ForwardingDatabase *GetSwitchForwardingDatabase(Node *node);
261 NetObj *FindInterfaceConnectionPoint(const BYTE *macAddr, int *type);
262
263 ObjectArray<LLDP_LOCAL_PORT_INFO> *GetLLDPLocalPortInfo(SNMP_Transport *snmp);
264
265 LinkLayerNeighbors *BuildLinkLayerNeighborList(Node *node);
266 void AddLLDPNeighbors(Node *node, LinkLayerNeighbors *nbs);
267 void AddNDPNeighbors(Node *node, LinkLayerNeighbors *nbs);
268 void AddCDPNeighbors(Node *node, LinkLayerNeighbors *nbs);
269 void AddSTPNeighbors(Node *node, LinkLayerNeighbors *nbs);
270 void BuildLldpId(int type, const BYTE *data, int length, TCHAR *id, int idLen);
271
272 void BridgeMapPorts(int snmpVersion, SNMP_Transport *transport, InterfaceList *ifList);
273
274 VrrpInfo *GetVRRPInfo(Node *node);
275
276 #endif /* _nms_topo_h_ */