forest-net
an overlay networks for large-scale virtual worlds
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator
Router.h
Go to the documentation of this file.
1 
9 #ifndef ROUTER_H
10 #define ROUTER_H
11 
12 #include <queue>
13 #include <map>
14 #include <chrono>
15 #include <thread>
16 #include <mutex>
17 #include <atomic>
18 
19 #include "stdinc.h"
20 #include "Forest.h"
21 #include "CtlPkt.h"
22 
23 #include "Quu.h"
24 #include "IfaceTable.h"
25 #include "LinkTable.h"
26 #include "ComtreeTable.h"
27 #include "RouteTable.h"
28 #include "PacketStore.h"
29 #include "StatsModule.h"
30 #include "PacketLog.h"
31 #include "QuManager.h"
32 
33 #include "RouterInProc.h"
34 #include "RouterOutProc.h"
35 
36 using namespace std::chrono;
37 using std::thread;
38 using std::mutex;
39 using std::unique_lock;
40 
41 namespace forest {
42 
43 class RouterInProc;
44 class RouterOutProc;
45 
49 struct RouterInfo {
50  string mode;
51 
53  ipa_t bootIp;
54  ipp_t portNum;
56  ipa_t nmIp;
60 
61  string ifTbl;
62  string lnkTbl;
63  string comtTbl;
64  string rteTbl;
65  string statSpec;
66 
67  seconds runLength;
68 };
69 
70 class Router {
71 public:
72  Router(const RouterInfo&);
73  ~Router();
74 
75  bool readTables(const RouterInfo&);
76  bool setup();
77  bool setupIface(int);
78  bool setupAllIfaces();
79  void run();
80  void dump(ostream& os);
81 
82 private:
83  ipa_t bootIp;
84  bool booting;
85 
87  ipa_t nmIp;
90 
91  seconds runLength;
92  high_resolution_clock::time_point tZero;
93 
94  uint64_t seqNum;
95  mutex snLock;
96 
99  ListPair *leafAdr;
100 
103 
112 
113  mutex iftMtx;
114  mutex ltMtx;
115  mutex cttMtx;
116  mutex rtMtx;
117 
118  int *sock;
120 
121  // sub-components of the router - run as separate threads
122  friend class RouterInProc;
123  friend class RouterOutProc;
124  friend class RouterControl;
125 
126  RouterInProc *rip;
127  RouterOutProc *rop;
128 
129  // setup
130  bool setupIfaces();
131  bool setupLeafAddresses();
132  bool setupQueues();
133  bool checkTables();
134  bool setAvailRates();
135  void addLocalRoutes();
136 
137  // manage leaf addresses
138  fAdr_t allocLeafAdr();
139  bool allocLeafAdr(fAdr_t);
140  void freeLeafAdr(fAdr_t);
141  bool validLeafAdr(fAdr_t) const;
142  bool isFreeLeafAdr(fAdr_t) const;
143 
144  uint64_t nextSeqNum();
145 };
146 
152 inline fAdr_t Router::allocLeafAdr() {
153  int offset = leafAdr->firstOut();
154  if (offset == 0) return 0;
155  leafAdr->swap(offset);
156  return firstLeafAdr + offset - 1;
157 }
158 
165 inline bool Router::allocLeafAdr(fAdr_t adr) {
166  int offset = (adr - firstLeafAdr) + 1;
167  if (!leafAdr->isOut(offset)) return false;
168  leafAdr->swap(offset);
169  return true;
170 }
171 
176 inline void Router::freeLeafAdr(fAdr_t adr) {
177  int offset = (adr - firstLeafAdr) + 1;
178  if (!leafAdr->isIn(offset)) return;
179  leafAdr->swap(offset);
180  return;
181 }
182 
188 inline bool Router::validLeafAdr(fAdr_t adr) const {
189  int offset = (adr - firstLeafAdr) + 1;
190  return leafAdr->isIn(offset);
191 }
192 
198 inline bool Router::isFreeLeafAdr(fAdr_t adr) const {
199  int offset = (adr - firstLeafAdr) + 1;
200  return leafAdr->isOut(offset);
201 }
202 
203 } // ends namespace
204 
205 #endif