Revised OpenFlow Library  v0.6.0dev
 All Classes Files Functions Variables Friends Groups Pages
cfibtable.h
1 /*
2  * cfibtable.h
3  *
4  * Created on: 15.07.2013
5  * Author: andreas
6  */
7 
8 #ifndef CFIB_H_
9 #define CFIB_H_ 1
10 
11 #include <map>
12 #include <ostream>
13 #include <exception>
14 #include <inttypes.h>
15 
16 #include <rofl/common/crofbase.h>
17 #include <rofl/common/crofdpt.h>
18 #include <rofl/common/caddress.h>
19 
20 #include "cfibentry.h"
21 
22 namespace rofl {
23 namespace examples {
24 namespace ethswctld {
25 
43 class cfibtable : public cfibentry_env {
44 public:
45 
60  static
61  cfibtable&
63  const rofl::cdptid& dptid) {
64  if (cfibtable::fibtables.find(dptid) != cfibtable::fibtables.end()) {
65  delete cfibtable::fibtables[dptid];
66  cfibtable::fibtables.erase(dptid);
67  }
68  new cfibtable(dptid);
69  return *(cfibtable::fibtables[dptid]);
70  };
71 
81  static
82  cfibtable&
84  const rofl::cdptid& dptid) {
85  if (cfibtable::fibtables.find(dptid) == cfibtable::fibtables.end()) {
86  new cfibtable(dptid);
87  }
88  return *(cfibtable::fibtables[dptid]);
89  };
90 
101  static
102  const cfibtable&
104  const rofl::cdptid& dptid) {
105  if (cfibtable::fibtables.find(dptid) == cfibtable::fibtables.end()) {
106  throw exceptions::eFibNotFound("cfibtable::get_fib() dptid not found");
107  }
108  return *(cfibtable::fibtables.at(dptid));
109  };
110 
116  static
117  void
119  const rofl::cdptid& dptid) {
120  if (cfibtable::fibtables.find(dptid) == cfibtable::fibtables.end()) {
121  return;
122  }
123  delete cfibtable::fibtables[dptid];
124  cfibtable::fibtables.erase(dptid);
125  };
126 
132  static
133  bool
135  const rofl::cdptid& dptid) {
136  return (not (cfibtable::fibtables.find(dptid) == cfibtable::fibtables.end()));
137  };
138 
141 public:
142 
152  void
153  clear() {
154  for (std::map<rofl::caddress_ll, cfibentry*>::iterator
155  it = ftable.begin(); it != ftable.end(); ++it) {
156  delete it->second;
157  }
158  ftable.clear();
159  };
160 
171  cfibentry&
173  const rofl::caddress_ll& hwaddr,
174  uint32_t portno) {
175  if (hwaddr.is_multicast() || hwaddr.is_null()) {
176  throw exceptions::eFibInval("cfibtable::add_fib_entry() hwaddr validation failed");
177  }
178  if (ftable.find(hwaddr) != ftable.end()) {
179  drop_fib_entry(hwaddr);
180  }
181  ftable[hwaddr] = new cfibentry(this, dptid, hwaddr, portno);
182  return *(ftable[hwaddr]);
183  };
184 
195  cfibentry&
197  const rofl::caddress_ll& hwaddr,
198  uint32_t portno) {
199  if (hwaddr.is_multicast() || hwaddr.is_null()) {
200  throw exceptions::eFibInval("cfibtable::set_fib_entry() hwaddr validation failed");
201  }
202  if (ftable.find(hwaddr) == ftable.end()) {
203  ftable[hwaddr] = new cfibentry(this, dptid, hwaddr, portno);
204  }
205  return *(ftable[hwaddr]);
206  };
207 
218  cfibentry&
220  const rofl::caddress_ll& hwaddr) {
221  if (hwaddr.is_multicast() || hwaddr.is_null()) {
222  throw exceptions::eFibInval("cfibtable::set_fib_entry() hwaddr validation failed");
223  }
224  if (ftable.find(hwaddr) == ftable.end()) {
225  throw exceptions::eFibNotFound("cfibtable::set_fib_entry() hwaddr not found");
226  }
227  return *(ftable[hwaddr]);
228  };
229 
240  const cfibentry&
242  const rofl::caddress_ll& hwaddr) const {
243  if (hwaddr.is_multicast() || hwaddr.is_null()) {
244  throw exceptions::eFibInval("cfibtable::get_fib_entry() hwaddr validation failed");
245  }
246  if (ftable.find(hwaddr) == ftable.end()) {
247  throw exceptions::eFibNotFound("cfibtable::set_fib_entry() hwaddr not found");
248  }
249  return *(ftable.at(hwaddr));
250  };
251 
257  void
259  const rofl::caddress_ll& hwaddr) {
260  if (ftable.find(hwaddr) == ftable.end()) {
261  return;
262  }
263  cfibentry* fibentry = ftable[hwaddr];
264  ftable.erase(hwaddr);
265  delete fibentry;
266  };
267 
273  bool
275  const rofl::caddress_ll& hwaddr) const {
276  return (not (ftable.find(hwaddr) == ftable.end()));
277  };
278 
281 private:
282 
286  cfibtable(
287  const rofl::cdptid& dptid) :
288  dptid(dptid) {
289  cfibtable::fibtables[dptid] = this;
290  };
291 
295  virtual
296  ~cfibtable() {
297  clear();
298  cfibtable::fibtables.erase(dptid);
299  };
300 
301 private:
302 
303  /*
304  * methods overwritten from cfibentry_env
305  */
306 
310  virtual void
311  fib_timer_expired(
312  const rofl::caddress_ll& hwaddr)
313  { drop_fib_entry(hwaddr); };
314 
318  virtual void
319  fib_port_update(
320  const cfibentry& entry)
321  {};
322 
323 public:
324 
328  friend std::ostream&
329  operator<< (std::ostream& os, cfibtable const& fib) {
330  try {
331  os << rofl::indent(0) << "<cfibtable dpid:" << "dpid: "
332  << rofl::crofdpt::get_dpt(fib.dptid).get_dpid().str() << " >" << std::endl;
333  } catch (rofl::eRofDptNotFound& e) {
334  os << rofl::indent(0) << "<cfibtable dptid:" << "dpid: " << fib.dptid << " >" << std::endl;
335  }
336  rofl::indent i(2);
337  for (std::map<rofl::caddress_ll, cfibentry*>::const_iterator
338  it = fib.ftable.begin(); it != fib.ftable.end(); ++it) {
339  os << *(it->second);
340  }
341  return os;
342  };
343 
344 private:
345 
346  static std::map<rofl::cdptid, cfibtable*> fibtables;
347  rofl::cdptid dptid;
348  std::map<rofl::caddress_ll, cfibentry*> ftable;
349 };
350 
351 }; // namespace ethswctld
352 }; // namespace examples
353 }; // namespace rofl
354 
355 #endif /* CFIB_H_ */
Definition: caddress.h:152
void clear()
Deletes all entries stored in this cfibtable instance.
Definition: cfibtable.h:153
bool is_multicast() const
Check for multicast bit in hardware address.
Definition: caddress.cc:74
Definition: crofdpt.h:51
static void drop_fib(const rofl::cdptid &dptid)
Removes an existing cfibtable instance.
Definition: cfibtable.h:118
rofl-common's internal remote datapath handle.
Definition: cdptid.h:24
bool has_fib_entry(const rofl::caddress_ll &hwaddr) const
Checks whether a cfibentry exists for given hardware address.
Definition: cfibtable.h:274
const rofl::cdpid & get_dpid() const
Returns OpenFlow datapath identifier for this instance.
Definition: crofdpt.h:1259
cfibentry & add_fib_entry(const rofl::caddress_ll &hwaddr, uint32_t portno)
Returns reference to empty cfibentry instance for given hardware address.
Definition: cfibtable.h:172
bool is_null() const
Check for null hardware address.
Definition: caddress.cc:94
static cfibtable & add_fib(const rofl::cdptid &dptid)
Returns reference to new or existing and resetted cfibtable instance.
Definition: cfibtable.h:62
Defines the environment expected by an instance of class cfibentry.
Definition: cfibentry.h:69
void drop_fib_entry(const rofl::caddress_ll &hwaddr)
Removes an existing cfibentry for given hardware address.
Definition: cfibtable.h:258
Stores a FIB entry mapping host hwaddr to switch port number.
Definition: cfibentry.h:113
Invalid parameter specified.
Definition: cfibentry.h:42
cfibentry & set_fib_entry(const rofl::caddress_ll &hwaddr, uint32_t portno)
Returns reference to existing cfibentry instance for given hardware address.
Definition: cfibtable.h:196
static const cfibtable & get_fib(const rofl::cdptid &dptid)
Returns const reference to existing cfibtable instance or throws exception.
Definition: cfibtable.h:103
static cfibtable & set_fib(const rofl::cdptid &dptid)
Returns reference to existing cfibtable instance or creates a new empty one.
Definition: cfibtable.h:83
Definition: logging.h:76
Element not found.
Definition: cfibentry.h:52
static bool has_fib(const rofl::cdptid &dptid)
Checks existence of cfibtable for given identifier.
Definition: cfibtable.h:134
Forwarding Information Base.
Definition: cfibtable.h:43
const cfibentry & get_fib_entry(const rofl::caddress_ll &hwaddr) const
Returns const reference to existing cfibentry instance for given hardware address.
Definition: cfibtable.h:241
static rofl::crofdpt & get_dpt(const rofl::cdptid &dptid)
Returns reference to rofl::crofdpt instance identified by rofl-common's internal identifier.
Definition: crofdpt.cc:17
cfibentry & set_fib_entry(const rofl::caddress_ll &hwaddr)
Returns reference to existing cfibentry instance for given hardware address.
Definition: cfibtable.h:219