def mod_group_entry(self, req, cmd, **_kwargs): try: group = eval(req.body) except SyntaxError: LOG.debug('invalid syntax %s', req.body) return Response(status=400) dpid = group.get('dpid') dp = self.dpset.get(int(dpid)) if dp is None: return Response(status=404) if dp.ofproto.OFP_VERSION == ofproto_v1_0.OFP_VERSION: LOG.debug('Unsupported OF protocol') return Response(status=501) if cmd == 'add': cmd = dp.ofproto.OFPGC_ADD elif cmd == 'modify': cmd = dp.ofproto.OFPGC_MODIFY elif cmd == 'delete': cmd = dp.ofproto.OFPGC_DELETE else: return Response(status=404) if dp.ofproto.OFP_VERSION == ofproto_v1_2.OFP_VERSION: ofctl_v1_2.mod_group_entry(dp, group, cmd) elif dp.ofproto.OFP_VERSION == ofproto_v1_3.OFP_VERSION: ofctl_v1_3.mod_group_entry(dp, group, cmd) else: LOG.debug('Unsupported OF protocol') return Response(status=501) return Response(status=200)
def mod_group_entry(self, req, cmd, **_kwargs): try: group = ast.literal_eval(req.body) except SyntaxError: LOG.debug('invalid syntax %s', req.body) return Response(status=400) dpid = group.get('dpid') dp = self.dpset.get(int(dpid)) if dp is None: return Response(status=404) if dp.ofproto.OFP_VERSION == ofproto_v1_0.OFP_VERSION: LOG.debug('Request not supported in this OF protocol version') return Response(status=501) if cmd == 'add': cmd = dp.ofproto.OFPGC_ADD elif cmd == 'modify': cmd = dp.ofproto.OFPGC_MODIFY elif cmd == 'delete': cmd = dp.ofproto.OFPGC_DELETE else: return Response(status=404) if dp.ofproto.OFP_VERSION == ofproto_v1_2.OFP_VERSION: ofctl_v1_2.mod_group_entry(dp, group, cmd) elif dp.ofproto.OFP_VERSION == ofproto_v1_3.OFP_VERSION: ofctl_v1_3.mod_group_entry(dp, group, cmd) else: LOG.debug('Unsupported OF protocol') return Response(status=501) return Response(status=200)
def _restore_rules(self, dp, rules): LOG.info("Restauration des règles de "+str(dp.id)+" en cours...") flows = filter(lambda rule: "buckets" not in rule, rules) groups = filter(lambda rule: "buckets" in rule, rules) for rule in groups: ofctl_v1_3.mod_group_entry(dp, rule, dp.ofproto.OFPGC_ADD) for rule in flows: ofctl_v1_3.mod_flow_entry(dp, rule, dp.ofproto.OFPFC_ADD) LOG.info("Restauration des règles de "+str(dp.id)+" terminé")
def initialize_switch(self): flow_stat = ofctl.get_flow_stats(self.dp, self.waiters) for s in flow_stat[str(self.dp.id)]: self.logger.debug("deleting flow [cookie: %d]" % s['cookie']) cmd = self.dp.ofproto.OFPFC_DELETE ofctl.mod_flow_entry(self.dp, {'table_id':self.dp.ofproto.OFPTT_ALL}, cmd) group_stat = ofctl.get_group_desc(self.dp, self.waiters) for s in group_stat[str(self.dp.id)]: self.logger.debug("deleting group[id: %d] %s" % (s['group_id'], s)) cmd = self.dp.ofproto.OFPGC_DELETE ofctl.mod_group_entry(self.dp, {'type':s['type'], 'group_id':s['group_id']}, cmd) ofctl.mod_meter_entry(self.dp, {'meter_id':self.dp.ofproto.OFPM_ALL}, self.dp.ofproto.OFPMC_DELETE)
def initialize_switch(self): flow_stat = ofctl.get_flow_stats(self.dp, self.waiters) for s in flow_stat[str(self.dp.id)]: self.logger.debug("deleting flow [cookie: %d]" % s['cookie']) cmd = self.dp.ofproto.OFPFC_DELETE ofctl.mod_flow_entry(self.dp, {'table_id': self.dp.ofproto.OFPTT_ALL}, cmd) group_stat = ofctl.get_group_desc(self.dp, self.waiters) for s in group_stat[str(self.dp.id)]: self.logger.debug("deleting group[id: %d] %s" % (s['group_id'], s)) cmd = self.dp.ofproto.OFPGC_DELETE ofctl.mod_group_entry(self.dp, { 'type': s['type'], 'group_id': s['group_id'] }, cmd) ofctl.mod_meter_entry(self.dp, {'meter_id': self.dp.ofproto.OFPM_ALL}, self.dp.ofproto.OFPMC_DELETE)
def mod_group_entry(self, req, cmd, **_kwargs): try: group = eval(req.body) except SyntaxError: LOG.debug('invalid syntax %s', req.body) return Response(status=400) dpid = group.get('dpid') dp = self.dpset.get(int(dpid)) if dp is None: return Response(status=404) if dp.ofproto.OFP_VERSION == ofproto_v1_0.OFP_VERSION: LOG.debug('Unsupported OF protocol') return Response(status=501) if cmd == 'add': cmd = dp.ofproto.OFPGC_ADD elif cmd == 'modify': cmd = dp.ofproto.OFPGC_MODIFY elif cmd == 'delete': cmd = dp.ofproto.OFPGC_DELETE else: return Response(status=404) if dp.ofproto.OFP_VERSION == ofproto_v1_2.OFP_VERSION: ofctl_v1_2.mod_group_entry(dp, group, cmd) elif dp.ofproto.OFP_VERSION == ofproto_v1_3.OFP_VERSION: ofctl_v1_3.mod_group_entry(dp, group, cmd) else: LOG.debug('Unsupported OF protocol') return Response(status=501) res = Response(status=200) res.headers.add('Access-Control-Allow-Origin', '*') return res
def _setup_dp(): self.dp = ev.dp # delete all flows and groups at first self.logger.debug("initializing..") self.initialize_switch() # create flood to down group entry buckets = [] for vid in (i + 101 for i in range(3)): actions = [{ 'type': 'PUSH_VLAN', 'ethertype': ether.ETH_TYPE_8021Q }, { 'type': 'SET_FIELD', 'field': 'vlan_vid', 'value': vid | 0x1000 }, { 'type': 'OUTPUT', 'port': self.inter_port }] buckets.append({'actions': actions}) buckets.append( {'actions': [{ 'type': 'OUTPUT', 'port': self.access_port }]}) buckets.append( {'actions': [{ 'type': 'OUTPUT', 'port': self.wlc_port }]}) self.flood_group_ids = [] for i in range(len(buckets)): group_id = self._next_cookie() self.flood_group_ids.append(group_id) b = [] for j, e in enumerate(buckets): if j != i: b.append(e) ofctl.mod_group_entry(self.dp, { 'type': 'ALL', 'group_id': group_id, 'buckets': b }, self.dp.ofproto.OFPGC_ADD) buckets = [] buckets.append( {'actions': [{ 'type': 'OUTPUT', 'port': self.access_port }]}) buckets.append( {'actions': [{ 'type': 'OUTPUT', 'port': self.wlc_port }]}) group_id = self._next_cookie() self.flood_group_ids.append(group_id) ofctl.mod_group_entry(self.dp, { 'type': 'ALL', 'group_id': group_id, 'buckets': buckets }, self.dp.ofproto.OFPGC_ADD) # create 3 meter entry meter_id = self._next_cookie() self.low_meter_id = meter_id meter = { 'meter_id': meter_id, 'flags': 'KBPS', 'bands': [{ 'type': 'DROP', 'rate': 50000 }] } ofctl.mod_meter_entry(self.dp, meter, self.dp.ofproto.OFPMC_ADD) meter_id = self._next_cookie() self.mid_meter_id = meter_id meter = { 'meter_id': meter_id, 'flags': 'KBPS', 'bands': [{ 'type': 'DROP', 'rate': 30000 }] } ofctl.mod_meter_entry(self.dp, meter, self.dp.ofproto.OFPMC_ADD) meter_id = self._next_cookie() self.high_meter_id = meter_id meter = { 'meter_id': meter_id, 'flags': 'KBPS', 'bands': [{ 'type': 'DROP', 'rate': 20000 }] } ofctl.mod_meter_entry(self.dp, meter, self.dp.ofproto.OFPMC_ADD) # default drop at table 0 cmd = self.dp.ofproto.OFPFC_ADD flow = { 'priority': 0, 'table_id': 0, 'cookie': self._next_cookie() } ofctl.mod_flow_entry(self.dp, flow, cmd) # default flood up at table 1 cmd = self.dp.ofproto.OFPFC_ADD # match = {'in_port' : self.inter_port, 'metadata' : '101'} match = {'in_port': self.inter_port} actions = [{'type': 'GROUP', 'group_id': self.flood_group_ids[5]}] flow = { 'match': match, 'priority': 0, 'table_id': 1, 'actions': actions, 'cookie': self._next_cookie() } ofctl.mod_flow_entry(self.dp, flow, cmd) ## match = {'in_port' : self.inter_port, 'metadata' : '102'} # match = {'in_port' : self.inter_port} # actions = [{'type':'GROUP', 'group_id':self.flood_group_ids[1]}] # flow = {'match':match, 'priority':0, 'table_id':1, 'actions':actions, 'cookie':self._next_cookie()} # ofctl.mod_flow_entry(self.dp, flow, cmd) # ## match = {'in_port' : self.inter_port, 'metadata' : '103'} # match = {'in_port' : self.inter_port} # actions = [{'type':'GROUP', 'group_id':self.flood_group_ids[2]}] # flow = {'match':match, 'priority':0, 'table_id':1, 'actions':actions, 'cookie':self._next_cookie()} # ofctl.mod_flow_entry(self.dp, flow, cmd) match = {'in_port': self.access_port} actions = [{'type': 'GROUP', 'group_id': self.flood_group_ids[3]}] flow = { 'match': match, 'priority': 0, 'table_id': 1, 'actions': actions, 'cookie': self._next_cookie() } ofctl.mod_flow_entry(self.dp, flow, cmd) match = {'in_port': self.wlc_port} actions = [{'type': 'GROUP', 'group_id': self.flood_group_ids[4]}] flow = { 'match': match, 'priority': 0, 'table_id': 1, 'actions': actions, 'cookie': self._next_cookie() } ofctl.mod_flow_entry(self.dp, flow, cmd) # packet-in when node is not familier at table 0 actions = [{ 'type': 'OUTPUT', 'port': self.dp.ofproto.OFPP_CONTROLLER }] # should be more tight? may occur many packet in match = {'in_port': self.inter_port} flow = { 'match': match, 'actions': actions, 'table_id': 0, 'priority': 1, 'cookie': self._next_cookie() } ofctl.mod_flow_entry(self.dp, flow, cmd) match = {'in_port': self.access_port} flow = { 'match': match, 'actions': actions, 'table_id': 0, 'priority': 1, 'cookie': self._next_cookie() } ofctl.mod_flow_entry(self.dp, flow, cmd) match = {'in_port': self.wlc_port} flow = { 'match': match, 'actions': actions, 'table_id': 0, 'priority': 1, 'cookie': self._next_cookie() } ofctl.mod_flow_entry(self.dp, flow, cmd)
def _setup_dp(): self.dp = ev.dp # delete all flows and groups at first self.logger.debug("initializing..") self.initialize_switch() # create flood to down group entry buckets = [] for vid in (i+101 for i in range(3)): actions = [{'type':'PUSH_VLAN', 'ethertype':ether.ETH_TYPE_8021Q}, {'type':'SET_FIELD', 'field':'vlan_vid', 'value':vid | 0x1000}, {'type':'OUTPUT', 'port':self.inter_port}] buckets.append({'actions': actions}) buckets.append({'actions':[{'type':'OUTPUT', 'port':self.access_port}]}) buckets.append({'actions':[{'type':'OUTPUT', 'port':self.wlc_port}]}) self.flood_group_ids = [] for i in range(len(buckets)): group_id = self._next_cookie() self.flood_group_ids.append(group_id) b = [] for j, e in enumerate(buckets): if j != i: b.append(e) ofctl.mod_group_entry(self.dp, {'type':'ALL', 'group_id':group_id, 'buckets':b}, self.dp.ofproto.OFPGC_ADD) buckets = [] buckets.append({'actions':[{'type':'OUTPUT', 'port':self.access_port}]}) buckets.append({'actions':[{'type':'OUTPUT', 'port':self.wlc_port}]}) group_id = self._next_cookie() self.flood_group_ids.append(group_id) ofctl.mod_group_entry(self.dp, {'type':'ALL', 'group_id':group_id, 'buckets':buckets}, self.dp.ofproto.OFPGC_ADD) # create 3 meter entry meter_id = self._next_cookie() self.low_meter_id = meter_id meter = {'meter_id': meter_id, 'flags': 'KBPS', 'bands': [{'type': 'DROP', 'rate': 50000}]} ofctl.mod_meter_entry(self.dp, meter, self.dp.ofproto.OFPMC_ADD) meter_id = self._next_cookie() self.mid_meter_id = meter_id meter = {'meter_id': meter_id, 'flags': 'KBPS', 'bands': [{'type': 'DROP', 'rate': 30000}]} ofctl.mod_meter_entry(self.dp, meter, self.dp.ofproto.OFPMC_ADD) meter_id = self._next_cookie() self.high_meter_id = meter_id meter = {'meter_id': meter_id, 'flags': 'KBPS', 'bands': [{'type': 'DROP', 'rate': 20000}]} ofctl.mod_meter_entry(self.dp, meter, self.dp.ofproto.OFPMC_ADD) # default drop at table 0 cmd = self.dp.ofproto.OFPFC_ADD flow = {'priority':0, 'table_id':0, 'cookie':self._next_cookie()} ofctl.mod_flow_entry(self.dp, flow, cmd) # default flood up at table 1 cmd = self.dp.ofproto.OFPFC_ADD # match = {'in_port' : self.inter_port, 'metadata' : '101'} match = {'in_port' : self.inter_port} actions = [{'type':'GROUP', 'group_id':self.flood_group_ids[5]}] flow = {'match':match, 'priority':0, 'table_id':1, 'actions':actions, 'cookie':self._next_cookie()} ofctl.mod_flow_entry(self.dp, flow, cmd) ## match = {'in_port' : self.inter_port, 'metadata' : '102'} # match = {'in_port' : self.inter_port} # actions = [{'type':'GROUP', 'group_id':self.flood_group_ids[1]}] # flow = {'match':match, 'priority':0, 'table_id':1, 'actions':actions, 'cookie':self._next_cookie()} # ofctl.mod_flow_entry(self.dp, flow, cmd) # ## match = {'in_port' : self.inter_port, 'metadata' : '103'} # match = {'in_port' : self.inter_port} # actions = [{'type':'GROUP', 'group_id':self.flood_group_ids[2]}] # flow = {'match':match, 'priority':0, 'table_id':1, 'actions':actions, 'cookie':self._next_cookie()} # ofctl.mod_flow_entry(self.dp, flow, cmd) match = {'in_port' : self.access_port} actions = [{'type':'GROUP', 'group_id':self.flood_group_ids[3]}] flow = {'match':match, 'priority':0, 'table_id':1, 'actions':actions, 'cookie':self._next_cookie()} ofctl.mod_flow_entry(self.dp, flow, cmd) match = {'in_port' : self.wlc_port} actions = [{'type':'GROUP', 'group_id':self.flood_group_ids[4]}] flow = {'match':match, 'priority':0, 'table_id':1, 'actions':actions, 'cookie':self._next_cookie()} ofctl.mod_flow_entry(self.dp, flow, cmd) # packet-in when node is not familier at table 0 actions = [{'type':'OUTPUT', 'port':self.dp.ofproto.OFPP_CONTROLLER}] # should be more tight? may occur many packet in match = {'in_port' : self.inter_port} flow = {'match':match, 'actions':actions, 'table_id':0, 'priority':1, 'cookie':self._next_cookie()} ofctl.mod_flow_entry(self.dp, flow, cmd) match = {'in_port' : self.access_port} flow = {'match':match, 'actions':actions, 'table_id':0, 'priority':1, 'cookie':self._next_cookie()} ofctl.mod_flow_entry(self.dp, flow, cmd) match = {'in_port' : self.wlc_port} flow = {'match':match, 'actions':actions, 'table_id':0, 'priority':1, 'cookie':self._next_cookie()} ofctl.mod_flow_entry(self.dp, flow, cmd)