/
bgp_config_ifmap.h
378 lines (313 loc) · 13.5 KB
/
bgp_config_ifmap.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/*
* Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
*/
#ifndef BGP_BGP_CONFIG_IFMAP_H__
#define BGP_BGP_CONFIG_IFMAP_H__
#include <map>
#include <set>
#include <vector>
#include <boost/function.hpp>
#include <boost/scoped_ptr.hpp>
#include "base/task_trigger.h"
#include "base/util.h"
#include "bgp/bgp_config.h"
#include "ifmap/ifmap_config_listener.h"
#include "ifmap/ifmap_node_proxy.h"
class BgpConfigListener;
typedef struct IFMapConfigListener::ConfigDelta BgpConfigDelta;
class BgpIfmapConfigManager;
class BgpIfmapInstanceConfig;
class BgpServer;
class DB;
class DBGraph;
namespace autogen {
class BgpPeering;
class BgpRouter;
class RoutingInstance;
struct BgpRouterParams;
}
//
// A BgpIfmapPeeringConfig corresponds to a bgp-peering link in the
// schema. Since a bgp-peering is a link with attributes, it's
// represented as a node in the configuration database. There's a
// single BgpIfmapPeeringConfig between a given pair of bgp-routers in
// a particular routing-instance.
//
// A bgp-peering link is represented in IFMap DB using a autogen::BgpPeering
// object. This object has a vector of autogen::BgpSessions, with each entry
// corresponding to a single session. A BgpNeighborConfig is created for each
// session.
//
// The NeighborMap keeps pointers to all the BgpNeighborConfig objects
// for a BgpIfmapPeeringConfig. There's typically just a single entry
// in this map, but we allow for multiple entries if there's more than
// 1 autogen::BgpSesssion in the autogen::BgpPeering.
//
// A BgpIfmapPeeringConfig gets created/updated/deleted when the
// IFMapNode for the bgp-peering is created/updated/deleted.
//
// There's no direct operational object corresponding to
// BgpIfmapPeeringConfig. A BgpIfmapPeeringConfig results in the
// creation of one or more BgpNeighborConfigs each of which is
// associated with a BgpPeer.
//
// The instance_ field is a back pointer to the BgpInstanceConfig object for
// the routing-instance to which this bgp-peering belongs.
// The IFMapNodeProxy maintains a reference to the IFMapNode object for the
// bgp-peering in the ifmap metadata table.
//
class BgpIfmapPeeringConfig {
public:
typedef std::map<std::string, BgpNeighborConfig *> NeighborMap;
explicit BgpIfmapPeeringConfig(BgpIfmapInstanceConfig *instance);
~BgpIfmapPeeringConfig();
void SetNodeProxy(IFMapNodeProxy *proxy);
void Update(BgpIfmapConfigManager *manager,
const autogen::BgpPeering *peering);
void Delete(BgpIfmapConfigManager *manager);
static bool GetRouterPair(DBGraph *db_graph, const std::string &localname,
IFMapNode *node,
std::pair<IFMapNode *, IFMapNode *> *pair);
const IFMapNode *node() const { return node_proxy_.node(); }
BgpIfmapInstanceConfig *instance() { return instance_; }
const BgpIfmapInstanceConfig *instance() const { return instance_; }
std::string name() const { return name_; }
size_t size() const { return neighbors_.size(); }
const autogen::BgpPeering *bgp_peering() const {
return bgp_peering_.get();
}
private:
void BuildNeighbors(BgpIfmapConfigManager *manager,
const autogen::BgpRouter *local_rt_config,
const std::string &peername,
const autogen::BgpRouter *remote_rt_config,
const autogen::BgpPeering *peering, NeighborMap *map);
BgpIfmapInstanceConfig *instance_;
std::string name_;
IFMapNodeProxy node_proxy_;
NeighborMap neighbors_;
boost::intrusive_ptr<const autogen::BgpPeering> bgp_peering_;
DISALLOW_COPY_AND_ASSIGN(BgpIfmapPeeringConfig);
};
//
// Represents a local IFMap bgp-router node in a specific routing-instance.
//
// A BgpProtocolConfig is created/updated/deleted when the IFMapNode for the
// bgp-router is created/updated/deleted.
//
// The instance_ field is a back pointer to the BgpInstanceConfig object for
// the routing-instance to which this bgp-router belongs.
// The IFMapNodeProxy maintains a reference to the IFMapNode object for the
// bgp-router in the ifmap bgp-router table.
//
class BgpIfmapProtocolConfig {
public:
explicit BgpIfmapProtocolConfig(BgpIfmapInstanceConfig *instance);
~BgpIfmapProtocolConfig();
void SetNodeProxy(IFMapNodeProxy *proxy);
void Update(BgpIfmapConfigManager *manager,
const autogen::BgpRouter *router);
void Delete(BgpIfmapConfigManager *manager);
const std::string &InstanceName() const;
const autogen::BgpRouterParams &router_params() const;
const autogen::BgpRouter *bgp_router() const { return bgp_router_.get(); }
IFMapNode *node() { return node_proxy_.node(); }
const BgpIfmapInstanceConfig *instance() { return instance_; }
const BgpProtocolConfig *protocol_config() const { return &data_; }
private:
BgpIfmapInstanceConfig *instance_;
IFMapNodeProxy node_proxy_;
BgpProtocolConfig data_;
boost::intrusive_ptr<const autogen::BgpRouter> bgp_router_;
DISALLOW_COPY_AND_ASSIGN(BgpIfmapProtocolConfig);
};
//
// Internal representation of routing-instance.
//
// A BgpInstanceConfig causes creation of a RoutingInstance. RoutingInstance
// has a pointer to the BgpInstanceConfig. This pointer gets invalidated and
// cleared when the RoutingInstance is undergoing deletion.
//
// The IFMapNodeProxy maintains a reference to the IFMapNode object for the
// routing-instance in the ifmap routing-instance table.
//
// The BgpInstanceConfig for the master instance is manually created when the
// BgpConfigManager is initialized. It gets deleted when the BgpConfigManager
// is terminated.
//
// For other routing-instances, a BgpInstanceConfig is created when we first
// see the IFMapNode for the routing-instance or when we see the IFMapNode for
// the local bgp-router in the routing-instance.
//
// BgpInstanceConfig is deleted only when all references to it are
// gone. The IFMapNode for the routing-instance must be deleted, any
// BgpProtcolConfig for the local bgp-router in the routing-instance
// must be deleted and all the BgpNeighborConfigs and
// BgpIfmapPeeringConfigs in the routing-instance must be deleted.
//
// The protocol_ field is a pointer to the BgpProtocolConfig object
// for the local bgp-router object, if any, in the routing-instance.
// The NeighborMap keeps pointers to all the BgpNeighborConfig objects
// in a BgpInstanceConfig. The PeeringMap keeps pointers to all the
// BgpIfmapPeeringConfig objects in the BgpInstanceConfig.
//
class BgpIfmapInstanceConfig {
public:
typedef std::map<std::string, BgpNeighborConfig *> NeighborMap;
typedef std::map<std::string, BgpIfmapPeeringConfig *> PeeringMap;
typedef BgpInstanceConfig::RouteTargetList RouteTargetList;
explicit BgpIfmapInstanceConfig(const std::string &name);
~BgpIfmapInstanceConfig();
void SetNodeProxy(IFMapNodeProxy *proxy);
void Update(BgpIfmapConfigManager *manager,
const autogen::RoutingInstance *config);
// The corresponding if-map node has been deleted.
void ResetConfig();
bool DeleteIfEmpty(BgpConfigManager *manager);
const BgpIfmapProtocolConfig *protocol_config() const {
return protocol_.get();
}
BgpIfmapProtocolConfig *protocol_config_mutable() {
return protocol_.get();
}
BgpIfmapProtocolConfig *LocateProtocol();
void ResetProtocol();
void AddNeighbor(BgpConfigManager *manager, BgpNeighborConfig *neighbor);
void ChangeNeighbor(BgpConfigManager *manager, BgpNeighborConfig *neighbor);
void DeleteNeighbor(BgpConfigManager *manager, BgpNeighborConfig *neighbor);
const BgpNeighborConfig *FindNeighbor(const std::string &name) const;
BgpConfigManager::NeighborMapRange NeighborMapItems() const;
const NeighborMap &neighbors() const { return neighbors_; }
const PeeringMap &peerings() const { return peerings_; }
void AddPeering(BgpIfmapPeeringConfig *peering);
void DeletePeering(BgpIfmapPeeringConfig *peering);
IFMapNode *node() { return node_proxy_.node(); }
const std::string &name() const { return name_; }
BgpInstanceConfig *instance_config() { return &data_; }
const BgpInstanceConfig *instance_config() const { return &data_; }
const RouteTargetList &import_list() const {
return data_.import_list();
}
const RouteTargetList &export_list() const {
return data_.export_list();
}
const std::string &virtual_network() const {
return data_.virtual_network();
}
int virtual_network_index() const { return data_.virtual_network_index(); }
private:
friend class BgpConfigManagerTest;
std::string name_;
IFMapNodeProxy node_proxy_;
BgpInstanceConfig data_;
boost::scoped_ptr<BgpIfmapProtocolConfig> protocol_;
NeighborMap neighbors_;
PeeringMap peerings_;
DISALLOW_COPY_AND_ASSIGN(BgpIfmapInstanceConfig);
};
//
// BgpConfigData contains all the configuration data that's relevant to a
// node. The BgpConfigManager has a pointer to BgpConfigData.
//
// The IfmapInstanceMap stores pointers to the BgpIfmapInstanceConfigs
// that have been created for all the routing-instances.
// The IfmapPeeringMap stores pointers to the BgpIfmapPeeringConfigs
// that have been created for all the bgp-peerings.
//
class BgpIfmapConfigData {
public:
typedef BgpConfigManager::InstanceMap BgpInstanceMap;
typedef std::map<std::string, BgpIfmapInstanceConfig *> IfmapInstanceMap;
typedef std::map<std::string, BgpIfmapPeeringConfig *> IfmapPeeringMap;
BgpIfmapConfigData();
~BgpIfmapConfigData();
BgpIfmapInstanceConfig *LocateInstance(const std::string &name);
void DeleteInstance(BgpIfmapInstanceConfig *rti);
BgpIfmapInstanceConfig *FindInstance(const std::string &name);
const BgpIfmapInstanceConfig *FindInstance(const std::string &name) const;
BgpIfmapPeeringConfig *CreatePeering(BgpIfmapInstanceConfig *rti,
IFMapNodeProxy *proxy);
void DeletePeering(BgpIfmapPeeringConfig *peer);
BgpIfmapPeeringConfig *FindPeering(const std::string &name);
const BgpIfmapPeeringConfig *FindPeering(const std::string &name) const;
int PeeringCount() const { return peerings_.size(); }
BgpConfigManager::InstanceMapRange InstanceMapItems(
const std::string &start_name = std::string()) const;
const IfmapInstanceMap &instances() const { return instances_; }
const IfmapPeeringMap &peerings() const { return peerings_; }
private:
IfmapInstanceMap instances_;
BgpInstanceMap instance_config_map_;
IfmapPeeringMap peerings_;
DISALLOW_COPY_AND_ASSIGN(BgpIfmapConfigData);
};
//
// This class is responsible for managing all bgp related configuration for
// a BgpServer. A BgpServer allocates an instance of a BgpConfigManager.
//
// BgpConfigManager listens to updates for IFMap objects which are relevant
// to BGP. This is accomplished using an instance of the BgpConfigListener.
// The BgpConfigListener is used to build a ChangeList of BgpConfigDeltas
// that are then processed by the BgpConfigManager.
//
// Internal representations of interesting IFMap objects are generated and
// updated as BgpConfigDeltas are processed. An instance of BgpConfigData
// is used to store the internal representations (the BgpXyzConfig classes).
//
// Notifications of changes to the internal representations are sent to the
// registered Observers. The BgpServer::ConfigUpdater is the only client that
// registers observers.
//
class BgpIfmapConfigManager : public BgpConfigManager,
public IFMapConfigListener::ConfigManager {
public:
BgpIfmapConfigManager(BgpServer *server);
virtual ~BgpIfmapConfigManager();
void Initialize(DB *db, DBGraph *db_graph, const std::string &localname);
/*
* begin: BgpConfigManager Interface
*/
virtual void Terminate();
virtual const std::string &localname() const { return localname_; }
virtual InstanceMapRange InstanceMapItems(
const std::string &start_name = std::string()) const;
virtual NeighborMapRange NeighborMapItems(
const std::string &instance_name) const;
virtual int NeighborCount(const std::string &instance_name) const;
virtual const BgpInstanceConfig *FindInstance(
const std::string &name) const;
virtual const BgpProtocolConfig *GetProtocolConfig(
const std::string &instance_name) const;
virtual const BgpNeighborConfig *FindNeighbor(
const std::string &instance_name, const std::string &name) const;
// end: BgpConfigManager
void DefaultBgpRouterParams(autogen::BgpRouterParams ¶m);
void OnChange();
DB *database() { return db_; }
DBGraph *graph() { return db_graph_; }
const BgpIfmapConfigData *config() const { return config_.get(); }
private:
friend class BgpConfigListenerTest;
typedef std::vector<BgpConfigDelta> ChangeList;
typedef std::map<std::string,
boost::function<void(const BgpConfigDelta &)> >IdentifierMap;
void IdentifierMapInit();
void DefaultConfig();
void ProcessChanges(const ChangeList &change_list);
void ProcessRoutingInstance(const BgpConfigDelta &change);
void ProcessBgpRouter(const BgpConfigDelta &change);
void ProcessBgpProtocol(const BgpConfigDelta &change);
void ProcessBgpPeering(const BgpConfigDelta &change);
bool ConfigHandler();
static int config_task_id_;
static const int kConfigTaskInstanceId;
DB *db_;
DBGraph *db_graph_;
std::string localname_;
IdentifierMap id_map_;
TaskTrigger trigger_;
boost::scoped_ptr<BgpConfigListener> listener_;
boost::scoped_ptr<BgpIfmapConfigData> config_;
DISALLOW_COPY_AND_ASSIGN(BgpIfmapConfigManager);
};
#endif