Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add_flow and del_flow commands added #94

Open
wants to merge 3 commits into
base: carp
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions pox/openflow/of_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,3 +327,34 @@ def list_switches (ofnexus = None):

r.sort(key=lambda item:item['dpid'])
return r

def list_switch (dpidToList, ofnexus = None):
if ofnexus is None:
from pox.core import core
ofnexus = core.openflow

r = []
for dpid,con in ofnexus._connections.iteritems():
if dpid == dpidToList:
ports = []
for p in con.ports.values():
pdict = {
'port_no':p.port_no,
'hw_addr':str(p.hw_addr),
'name':p.name}
for bit,name in of.ofp_port_config_map.items():
if p.config & bit:
pdict[name.split('OFPPC_', 1)[-1].lower()] = True
for bit,name in of.ofp_port_state_map.items():
if p.state & bit:
pdict[name.split('OFPPS_', 1)[-1].lower()] = True
ports.append(pdict)
ports.sort(key=lambda item:item['port_no'])

rr = {
'dpid':dpidToStr(dpid),
'n_tables':con.features.n_tables,
'ports':ports}
r.append(rr)

return r
99 changes: 99 additions & 0 deletions pox/openflow/webservice.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@
Sets the flow table on a switch.
dpid - a string dpid
flows - a list of flow entries
add_flow
Adds a flow entry on the switch.
dpid - a string dpid
flow - a flow entry to be added
del_flow
Deletes a flow entry on the switch.
dpid - a string dpid
flow - a flow entry to be added
get_switch_desc
Gets switch details.
dpid - a string dpid
Expand Down Expand Up @@ -180,6 +188,73 @@ def _handle_ErrorIn (self, event):
self._finish(make_error("OpenFlow Error", data=event.asString()))


class OFAddFlowRequest (OFConRequest):

def _init (self, flows = []):
self.done = False

xid = of.generate_xid()
self.xid = xid

self.count = len(flows)

for flow in flows:
fm = dict_to_flow_mod(flow)
fm.xid = xid

self._con.send(fm)
self._con.send(of.ofp_barrier_request(xid=xid))

def _handle_BarrierIn (self, event):
if event.ofp.xid != self.xid: return
if self.done: return
self.count -= 1
if self.count <= 0:
self._result('flowmod', True)
self.done = True

def _handle_ErrorIn (self, event):
if event.ofp.xid != self.xid: return
if self.done: return
self.clear_table()
self.done = True
self._finish(make_error("OpenFlow Error", data=event.asString()))


class OFDelFlowRequest (OFConRequest):

def _init (self, flows = []):
self.done = False

xid = of.generate_xid()
self.xid = xid

self.count = len(flows)

for flow in flows:
fm = dict_to_flow_mod(flow)
fm.command = 4 # OFPFC_DELETE_STRICT
fm.xid = xid

self._con.send(fm)
self._con.send(of.ofp_barrier_request(xid=xid))

def _handle_BarrierIn (self, event):
if event.ofp.xid != self.xid: return
if self.done: return
self.count -= 1
if self.count <= 0:
self._result('flowmod', True)
self.done = True

def _handle_ErrorIn (self, event):
if event.ofp.xid != self.xid: return
if self.done: return
self.clear_table()
self.done = True
self._finish(make_error("OpenFlow Error", data=event.asString()))


class OFRequestHandler (JSONRPCHandler):

def _exec_set_table (self, dpid, flows):
Expand All @@ -190,6 +265,22 @@ def _exec_set_table (self, dpid, flows):

return OFSetTableRequest(con, flows).get_response()

def _exec_add_flow (self, dpid, flows):
dpid = strToDPID(dpid)
con = core.openflow.getConnection(dpid)
if con is None:
return make_error("No such switch")

return OFAddFlowRequest(con, flows).get_response()

def _exec_del_flow (self, dpid, flows):
dpid = strToDPID(dpid)
con = core.openflow.getConnection(dpid)
if con is None:
return make_error("No such switch")

return OFDelFlowRequest(con, flows).get_response()

def _exec_get_switch_desc (self, dpid):
dpid = strToDPID(dpid)
con = core.openflow.getConnection(dpid)
Expand All @@ -209,6 +300,14 @@ def _exec_get_flow_stats (self, dpid, *args, **kw):
def _exec_get_switches (self):
return {'result':list_switches()}

def _exec_get_switch (self, dpid):
dpid = strToDPID(dpid)
con = core.openflow.getConnection(dpid)
if con is None:
return make_error("No such switch")

return {'result':list_switch(dpid)}



def launch (username='', password=''):
Expand Down