Revised OpenFlow Library  v0.6.0dev
 All Classes Files Functions Variables Friends Groups Pages
cflowtable.h
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4 
5 /*
6  * cflowtable.h
7  *
8  * Created on: 15.08.2014
9  * Author: andreas
10  */
11 
12 #ifndef CFLOWTABLE_H_
13 #define CFLOWTABLE_H_
14 
15 #include <map>
16 #include <ostream>
17 #include <exception>
18 #include <inttypes.h>
19 
20 #include <rofl/common/crofbase.h>
21 #include <rofl/common/crofdpt.h>
22 #include <rofl/common/caddress.h>
23 
24 #include "cflowentry.h"
25 
26 namespace rofl {
27 namespace examples {
28 namespace ethswctld {
29 
46 class cflowtable : public cflowentry_env {
47 public:
48 
63  static
64  cflowtable&
66  const rofl::cdptid& dptid) {
67  if (cflowtable::flowtables.find(dptid) != cflowtable::flowtables.end()) {
68  delete cflowtable::flowtables[dptid];
69  cflowtable::flowtables.erase(dptid);
70  }
71  new cflowtable(dptid);
72  return *(cflowtable::flowtables[dptid]);
73  };
74 
84  static
85  cflowtable&
87  const rofl::cdptid& dptid) {
88  if (cflowtable::flowtables.find(dptid) == cflowtable::flowtables.end()) {
89  new cflowtable(dptid);
90  }
91  return *(cflowtable::flowtables[dptid]);
92  };
93 
104  static
105  const cflowtable&
107  const rofl::cdptid& dptid) {
108  if (cflowtable::flowtables.find(dptid) == cflowtable::flowtables.end()) {
109  throw exceptions::eFlowNotFound("cflowtable::get_flowtable() dptid not found");
110  }
111  return *(cflowtable::flowtables.at(dptid));
112  };
113 
119  static
120  void
122  const rofl::cdptid& dptid) {
123  if (cflowtable::flowtables.find(dptid) == cflowtable::flowtables.end()) {
124  return;
125  }
126  delete cflowtable::flowtables[dptid];
127  cflowtable::flowtables.erase(dptid);
128  };
129 
135  static
136  bool
138  const rofl::cdptid& dptid) {
139  return (not (cflowtable::flowtables.find(dptid) == cflowtable::flowtables.end()));
140  };
141 
144 public:
145 
155  void
156  clear() {
157  for (std::map<rofl::caddress_ll, std::map<rofl::caddress_ll, cflowentry*> >::iterator
158  it = ftable.begin(); it != ftable.end(); ++it) {
159  for (std::map<rofl::caddress_ll, cflowentry*>::iterator
160  jt = it->second.begin(); jt != it->second.end(); ++jt) {
161  delete jt->second;
162  }
163  }
164  ftable.clear();
165  };
166 
178  cflowentry&
180  const rofl::caddress_ll& src,
181  const rofl::caddress_ll& dst,
182  uint32_t portno) {
183  if (src.is_multicast() || src.is_null() || dst.is_multicast() || dst.is_null()) {
184  throw exceptions::eFlowInval("cflowtable::add_flow_entry() invalid address");
185  }
186  if (ftable[src].find(dst) != ftable[src].end()) {
187  drop_flow_entry(src, dst);
188  }
189  ftable[src][dst] = new cflowentry(this, dptid, src, dst, portno);
190  return *(ftable[src][dst]);
191  };
192 
204  cflowentry&
206  const rofl::caddress_ll& src,
207  const rofl::caddress_ll& dst,
208  uint32_t portno) {
209  if (src.is_multicast() || src.is_null() || dst.is_multicast() || dst.is_null()) {
210  throw exceptions::eFlowInval("cflowtable::set_flow_entry() invalid address");
211  }
212  if (ftable[src].find(dst) == ftable[src].end()) {
213  ftable[src][dst] = new cflowentry(this, dptid, src, dst, portno);
214  }
215  return *(ftable[src][dst]);
216  };
217 
229  cflowentry&
231  const rofl::caddress_ll& src,
232  const rofl::caddress_ll& dst) {
233  if (src.is_multicast() || src.is_null() || dst.is_multicast() || dst.is_null()) {
234  throw exceptions::eFlowInval("cflowtable::set_flow_entry() invalid address");
235  }
236  if (ftable[src].find(dst) == ftable[src].end()) {
237  throw exceptions::eFlowNotFound("cflowtable::set_flow_entry() destination address not found");
238  }
239  return *(ftable[src][dst]);
240  };
241 
253  const cflowentry&
255  const rofl::caddress_ll& src,
256  const rofl::caddress_ll& dst) const {
257  if (src.is_multicast() || src.is_null() || dst.is_multicast() || dst.is_null()) {
258  throw exceptions::eFlowInval("cflowtable::get_flow_entry() invalid address");
259  }
260  if (ftable.at(src).find(dst) == ftable.at(src).end()) {
261  throw exceptions::eFlowNotFound("cflowtable::get_flow_entry() destination address not found");
262  }
263  return *(ftable.at(src).at(dst));
264  };
265 
272  void
274  const rofl::caddress_ll& src,
275  const rofl::caddress_ll& dst) {
276  if (ftable[src].find(dst) == ftable[src].end()) {
277  return;
278  }
279  cflowentry* flowentry = ftable[src][dst];
280  ftable[src].erase(dst);
281  delete flowentry;
282  };
283 
290  bool
291  has_flow_entry(const rofl::caddress_ll& src, const rofl::caddress_ll& dst) const {
292  return (not (ftable.at(src).find(dst) == ftable.at(src).end()));
293  };
294 
297 private:
298 
302  cflowtable(
303  const rofl::cdptid& dptid) :
304  dptid(dptid) {
305  cflowtable::flowtables[dptid] = this;
306  };
307 
311  virtual
312  ~cflowtable() {
313  clear();
314  cflowtable::flowtables.erase(dptid);
315  };
316 
317 private:
318 
322  virtual void
323  flow_timer_expired(
324  const cflowentry& entry) {
325  drop_flow_entry(entry.get_src(), entry.get_dst());
326  };
327 
328 public:
329 
333  friend std::ostream&
334  operator<< (std::ostream& os, cflowtable const& flowtable) {
335  try {
336  os << rofl::indent(0) << "<cflowtable dpid:"
337  << rofl::crofdpt::get_dpt(flowtable.dptid).get_dpid().str() << " >" << std::endl;
338  } catch (rofl::eRofDptNotFound& e) {
339  os << rofl::indent(0) << "<cflowtable dptid:" << flowtable.dptid << " >" << std::endl;
340  }
341  rofl::indent i(2);
342  for (std::map<rofl::caddress_ll, std::map<rofl::caddress_ll, cflowentry*> >::const_iterator
343  it = flowtable.ftable.begin(); it != flowtable.ftable.end(); ++it) {
344  for (std::map<rofl::caddress_ll, cflowentry*>::const_iterator
345  jt = it->second.begin(); jt != it->second.end(); ++jt) {
346  os << *(jt->second);
347  }
348  }
349  return os;
350  };
351 
352 private:
353 
354  rofl::cdptid dptid;
355  std::map<rofl::caddress_ll, std::map<rofl::caddress_ll, cflowentry*> > ftable;
356  static std::map<rofl::cdptid, cflowtable*> flowtables;
357 };
358 
359 }; // namespace ethswctld
360 }; // namespace examples
361 }; // namespace rofl
362 
363 #endif /* CFLOWTABLE_H_ */
static void drop_flowtable(const rofl::cdptid &dptid)
Removes an existing cflowtable instance.
Definition: cflowtable.h:121
Element not found.
Definition: cflowentry.h:54
Definition: caddress.h:152
void drop_flow_entry(const rofl::caddress_ll &src, const rofl::caddress_ll &dst)
Removes an existing cflowentry for given source and destination hardware address. ...
Definition: cflowtable.h:273
static const cflowtable & get_flowtable(const rofl::cdptid &dptid)
Returns const reference to existing cflowtable instance or throws exception.
Definition: cflowtable.h:106
Defines the environment expected by an instance of class cflowentry.
Definition: cflowentry.h:71
bool is_multicast() const
Check for multicast bit in hardware address.
Definition: caddress.cc:74
Definition: crofdpt.h:51
rofl-common's internal remote datapath handle.
Definition: cdptid.h:24
const rofl::cdpid & get_dpid() const
Returns OpenFlow datapath identifier for this instance.
Definition: crofdpt.h:1259
cflowentry & set_flow_entry(const rofl::caddress_ll &src, const rofl::caddress_ll &dst)
Returns reference to existing cflowentry instance for given hardware address.
Definition: cflowtable.h:230
bool is_null() const
Check for null hardware address.
Definition: caddress.cc:94
Invalid parameter specified.
Definition: cflowentry.h:43
Stores an active flow entry.
Definition: cflowentry.h:109
void clear()
Deletes all entries stored in this cflowtable instance.
Definition: cflowtable.h:156
static cflowtable & set_flowtable(const rofl::cdptid &dptid)
Returns reference to existing cflowtable instance or creates a new empty one.
Definition: cflowtable.h:86
static bool has_flowtable(const rofl::cdptid &dptid)
Checks existence of cflowtable for given identifier.
Definition: cflowtable.h:137
cflowentry & add_flow_entry(const rofl::caddress_ll &src, const rofl::caddress_ll &dst, uint32_t portno)
Returns reference to empty cflowentry instance for given hardware address.
Definition: cflowtable.h:179
cflowentry & set_flow_entry(const rofl::caddress_ll &src, const rofl::caddress_ll &dst, uint32_t portno)
Returns reference to existing cflowentry instance for given hardware address.
Definition: cflowtable.h:205
Flow Table of active flows.
Definition: cflowtable.h:46
const cflowentry & get_flow_entry(const rofl::caddress_ll &src, const rofl::caddress_ll &dst) const
Returns const reference to existing cflowentry instance for given hardware address.
Definition: cflowtable.h:254
static cflowtable & add_flowtable(const rofl::cdptid &dptid)
Returns reference to new or existing and resetted cflowtable instance.
Definition: cflowtable.h:65
Definition: logging.h:76
bool has_flow_entry(const rofl::caddress_ll &src, const rofl::caddress_ll &dst) const
Checks whether a cflowentry exists for given source and destination hardware address.
Definition: cflowtable.h:291
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