本文整理汇总了Python中ryu.topology.api.get_switch函数的典型用法代码示例。如果您正苦于以下问题:Python get_switch函数的具体用法?Python get_switch怎么用?Python get_switch使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了get_switch函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _alert_to_block
def _alert_to_block(self, ev):
msg = ev.msg
# Get all dpid from switches
sw_dpid = [s.dp.id for s in get_switch(self.topology_api_app, None)]
# Get datapath by dpid
datapath = ryu.app.ofctl.api.get_datapath(self.topology_api_app,
sw_dpid[0])
# Use datapath to do OFPFlowMod DROP action here
ofproto = datapath.ofproto
parser = datapath.ofproto_parser
pkt = packet.Packet(array.array('B', msg.pkt))
_ipv4 = pkt.get_protocol(ipv4.ipv4)
_tcp = pkt.get_protocol(tcp.tcp)
if _ipv4:
c_and_c_IP = _ipv4.src
c_and_c_port = _tcp.src_port
print ("C&C Server: ", c_and_c_IP, c_and_c_port)
print ("Victim: ", _ipv4.dst, _tcp.dst_port)
match_malicious = parser.OFPMatch(eth_type=ether.ETH_TYPE_IP,
ip_proto=inet.IPPROTO_TCP,
ipv4_src=c_and_c_IP,
tcp_src=c_and_c_port)
actions_drop = []
self.add_flow(datapath, 0, 100, match_malicious, actions_drop)
print "Block C&C Server at %s:%d" % (_ipv4.src, _tcp.src_port)
示例2: get_topology_data
def get_topology_data(self, ev):
switch_list = get_switch(self.topology_api_app, None)
switches=[switch.dp.id for switch in switch_list]
links_list = get_link(self.topology_api_app, None)
links=[(link.src.dpid,link.dst.dpid,{'port':link.src.port_no}) for link in links_list]
print links
print switches
示例3: get_topology_data
def get_topology_data(self, ev):
switch_list = get_switch(self.topology_api_app, None)
switches=[switch.dp.id for switch in switch_list]
self.net.add_nodes_from(switches)
#print "**********List of switches"
#for switch in switch_list:
#self.ls(switch)
#print switch
# self.nodes[self.no_of_nodes] = switch
# self.no_of_nodes += 1
links_list = get_link(self.topology_api_app, None)
#print links_list
#links=[(link.src.dpid,link.dst.dpid,port=link.src.port_no) for link in links_list]
#print links
#self.net.add_edges_from(links)
#links=[(link.dst.dpid,link.src.dpid,port=link.dst.port_no) for link in links_list]
#print links
#self.net.add_edges_from(links)
for link in links_list:
if (link.src.dpid,link.dst.dpid,link.src.port_no) not in list(self.net.edges_iter(data='port')):
self.net.add_edge(link.src.dpid,link.dst.dpid,port=link.src.port_no)
if (link.dst.dpid,link.src.dpid,link.dst.port_no) not in list(self.net.edges_iter(data='port')):
self.net.add_edge(link.dst.dpid,link.src.dpid,port=link.dst.port_no)
print "List of links"
print self.net.edges(data=True, keys=True)
示例4: get_datapath
def get_datapath(self, dpid):
if dpid not in self.dps:
switch = topo_api.get_switch(self, dpid)[0]
self.dps[dpid] = switch.dp
return switch.dp
return self.dps[dpid]
示例5: get_topology_data
def get_topology_data(self,ev):
switch_list = get_switch(self.topology_api_app, None)
switches = [switch.dp.id for switch in switch_list]
links_list = get_link(self.topology_api_app, None)
links = [(link.src.dpid, link.dst.dpid, {'port':(link.src.hw_addr,link.dst.hw_addr)}) for link in links_list]
print len(switches)
print "total links",len(links)
示例6: get_topology_data
def get_topology_data(self, ev):
switch_list = get_switch(self.topology_data_app, None)
switches = [switch.dp.id for switch in switch_list]
self.net.add_nodes_from(switches)
with open(OFP_LINK_PORT, 'w') as outp:
# src_dpid dst_dpid src_dpid_output_port dst_dpid_input_port
links_list = get_link(self.topology_data_app, None)
# print links_list
# add link from one direction
links = [(self._hostname_Check(link.src.dpid), self._hostname_Check(link.dst.dpid),
{'out_port': link.src.port_no})
for link in links_list]
# print links
self.net.add_edges_from(links)
for link in links:
outp.write("%s %s %s\n" % (self._hostname_Check(link[0]),
self._hostname_Check(link[1]), link[2]['out_port']))
# add links from oppsite direction
links = [(link.dst.dpid, link.src.dpid,
{'out_port': link.dst.port_no}) for link in links_list]
# print links
self.net.add_edges_from(links)
for link in links:
outp.write("%s %s %s\n" % (self._hostname_Check(link[0]),
self._hostname_Check(link[1]), link[2]['out_port']))
示例7: addflowsql
def addflowsql(self, dst, dpid, in_port, out_port, flag):
print 'add flow sql !!!!!!!!!!!!!!!!!!!!!!!!!!!'
print dpid,in_port,out_port
data_path=get_switch(self,dpid)#TODO test
print type(data_path)
print '!!!!!!!!!!!!!!!!!!!'
print data_path
datapath=data_path[0].dp#TODO test
print 'datapath = '
print datapath
ofproto = datapath.ofproto
parser = datapath.ofproto_parser
print 'dst = '+str(dst)
print 'dpid = '+str(dpid)
print 'in_port = '+str(in_port)
print 'out port = '+str(out_port)
actions=[parser.OFPActionOutput(out_port)]
match=parser.OFPMatch(in_port=in_port,eth_dst=dst)
f='ff:ff:ff:ff:ff:ff'
if 1==flag:
self.delete_flow(datapath, match)#TODO test delete flow!!
#TODO delete the ff:ff:ff:ff:ff:ff flow
matchf=parser.OFPMatch(in_port=in_port,eth_dst=f)
self.delete_flow(datapath, matchf)
print '!!!!!!!!!!!!!!!!!!add flow!!!!!!!!!!!!!!!!!!!!in_port='+str(in_port)+' dst='+str(dst)+' out_port='+str(out_port)+' dpid='+str(dpid)
self.add_flow(datapath, 1, match, actions)
示例8: get_topology_data
def get_topology_data(self):
if not self.topo_stable:
return
self.topo_stable = False
print 'get_topoloty_data'
self.switch_list = get_switch(self.topology_api_app, None)
self.mSwitches = [switch.dp.id for switch in self.switch_list] # switch.dp.id
self.mDataPaths = [switch.dp for switch in self.switch_list]
print type(self.mDataPaths[0])
self.links_list = get_link(self.topology_api_app, None)
self.links = [(1, link.src.dpid, link.dst.dpid, link.src.port_no, link.dst.port_no) for link in self.links_list]
self.links.sort()
# print 'links : ', self.links
print '\n\nlinks:'
for lk in self.links:
print 'switch ', lk[1], ', port ', lk[3], '--> switch ', lk[2], ', port', lk[4]
print 'switches : ', self.mSwitches
self.constructing_stp_krustal()
# Delete all flows in all datapaths
for dpid in self.mSwitches:
self.delete_flow(dpid)
# Install new flows
for block in self.ports_to_block:
if block in self.ports_to_enable:
continue
dpid = block[0]
port = block[1]
self.block_port(dpid, port)
# for enable in self.ports_to_enable:
# pass
self.start_learning = True
示例9: _switch_enter_handler
def _switch_enter_handler(self, ev):
# get_switch(self, None) outputs the list of switches object.
self.topo_switches = get_switch(self, None)
# get_link(self, None) outputs the list of links object.
self.topo_links = get_link(self, None)
"""
Now you have saved the links and switches of the topo. But they are object, we need to use to_dict() to trans them
"""
# print '*'*40,"Switch_set",'*'*40
for switch in self.topo_switches:
dp = switch.dp
dp_no = dpid_to_str(dp.id)
if (Switch_set.has_key(dp_no) == False):
ports = switch.ports
Switch_set[dp_no] = [port.to_dict() for port in ports]
# pprint.pprint(Switch_set)
Switch_set_json = json.dumps(Switch_set, indent=4)
Ssj_file = open('./Info/Static/Switch_json.json','w+')
Ssj_file.write(Switch_set_json)
Ssj_file.close()
# print '*'*40,"Link_set",'*'*40
Link_set = [ link.to_dict() for link in self.topo_links ]
# pprint.pprint(Link_set)
Link_set_json = json.dumps(Link_set, indent=4)
Lsj_file = open('./Info/Static/Link_json.json','w+')
Lsj_file.write(Link_set_json)
Lsj_file.close()
self.logger.info("******_switch_enter_handler, Switch_set & Link_set******")
示例10: add_block_rule
def add_block_rule(self, rule_action, src_ip, dst_ip, trans_proto, port):
switch_list = get_switch(self.topology_api_app, None)
for switch in switch_list:
datapath = switch.dp
parser = datapath.ofproto_parser
actions = [] # drop
# initial match field
match_dict = {'eth_type': ether.ETH_TYPE_IP}
# fill into the layer3 and layer 4 protocol
# if port == 0, means block all protocol
if port >= 0:
if trans_proto == inet.IPPROTO_TCP:
match_dict.update({'ip_proto': trans_proto,
'tcp_dst': port})
else: # udp
match_dict.update({'ip_proto': trans_proto,
'udp_dst': port})
if len(src_ip) > 0: # not ''
match_dict.update({'ipv4_src': src_ip})
if len(dst_ip) > 0: # not ''
match_dict.update({'ipv4_dst': dst_ip})
match = parser.OFPMatch(**match_dict)
fw_priority = settings.firewall_priority
if rule_action == 'add':
self.add_flow(datapath, fw_priority, match, actions)
elif rule_action == 'delete': # 'off'
self.del_flow(datapath, match)
self._request_stats(datapath) # update flow list in data.py
示例11: get_topology_data
def get_topology_data(self, ev):
self.logger.info("get_topology_data()")
self._update_switch_dpid_list()
switch_list = get_switch(self.topology_data_app, None)
switches = [switch.dp.id for switch in switch_list]
# print "switches: ", switches
self.net.add_nodes_from(switches)
# print "~~~~~ FRONT ~~~~~~~"
# print "switches:", [self._hostname_Check(s) for s in switches]
# print "self.link_port: ", self.link_port
# print "self.net.nodes():", self.net.nodes()
# print "self.net.edges():", self.net.edges()
# print "net nodes: ", self.net.nodes()
for node in self.net.nodes():
self.link_port.setdefault(node, {})
# with open(OFP_LINK_PORT, 'w') as outp:
# src_dpid dst_dpid src_dpid_output_port dst_dpid_input_port
links_list = get_link(self.topology_data_app, None)
# print "links_list: ", links_list
# add link from one direction
links = [(link.src.dpid, link.dst.dpid,
{'out_port': link.src.port_no}) for link in links_list]
# print "links:", links
self.net.add_edges_from(links)
for link in links:
# self.logger.info("%s %s %s\n" % (self._hostname_Check(link[0]),
# self._hostname_Check(link[1]), link[2]['out_port']))
# self.logger.info("%s %s %s\n" % (link[0], link[1], link[2]['out_port']))
# outp.write("%s %s %s\n" % (link[0], link[1], link[2]['out_port']))
self.link_port[link[0]][link[1]] = link[2]['out_port']
示例12: _handler_port_add
def _handler_port_add(self, ev):
"""
Overwrite the legacy Ryu Event.
"""
try:
switch = self.switches[ev.port.dpid]
if self._check_if_ap_port(ev.port):
ap_conf = self._get_ap_conf(switch, ev.port)
switch.is_ap = True
switch.ap_conf = ap_conf
self.accesspoints[ev.port.dpid] = switch
elif self._check_if_gw_port(ev.port):
gw_conf = self._get_gw_conf(switch, ev.port)
switch.is_gw = True
switch.gw_conf = gw_conf
self.gateways[ev.port.dpid] = switch
switch.switch = get_switch(self, ev.port.dpid)[0]
ev_tu = EventSwitchUpdate(switch)
self.send_event_to_observers(ev_tu)
self.logger.info("Port add: " + str(ev.port))
except KeyError:
pass
示例13: get_topology_for_swadd
def get_topology_for_swadd(self, ev):
"""Switch add."""
print "EventSwitchEnter"
self.get_topology_data()
if constant.Capacity > 0:
switch_list = get_switch(self.topology_api_app, None)
rate_setup.init_meter_setup(constant.Capacity, switch_list)
示例14: add_mirror_rule
def add_mirror_rule(self, rule_action, mirror_port, host_port):
switch_list = get_switch(self.topology_api_app, None)
for switch in switch_list:
datapath = switch.dp
parser = datapath.ofproto_parser
if rule_action == 'add':
#add mirror flow by host port
for i in range(len(mirror_data.mirror_table)):
if host_port == mirror_data.mirror_table[i]['out_port']:
match_del = parser.OFPMatch(eth_dst=mirror_data.mirror_table[i]['dst'])
self.del_flow(datapath, match_del, mirror_data.mirror_table[i]['priority'])
mirror_data.mirror_table[i]['mirror_port'] = mirror_port
mirror_data.mirror_table[i]['priority'] = 100
match_add = parser.OFPMatch(eth_dst=mirror_data.mirror_table[i]['dst'])
actions = [parser.OFPActionOutput(mirror_data.mirror_table[i]['out_port']), parser.OFPActionOutput(mirror_data.mirror_table[i]['mirror_port'])]
self.add_flow(datapath, mirror_data.mirror_table[i]['priority'], match_add, actions) #refresh mirror get higher priority
print mirror_data.mirror_table
elif rule_action == 'delete':
for i in range(len(mirror_data.mirror_table)):
if host_port == mirror_data.mirror_table[i]['out_port']:
match_del = parser.OFPMatch(eth_dst=mirror_data.mirror_table[i]['dst'])
self.del_flow(datapath, match_del, mirror_data.mirror_table[i]['priority'])
mirror_data.mirror_table[i]['mirror_port'] = mirror_data.default_mirror_port
mirror_data.mirror_table[i]['priority'] = 1
match_add = parser.OFPMatch(eth_dst=mirror_data.mirror_table[i]['dst'])
actions = [parser.OFPActionOutput(mirror_data.mirror_table[i]['out_port']), parser.OFPActionOutput(mirror_data.mirror_table[i]['mirror_port'])]
self.add_flow(datapath, mirror_data.mirror_table[i]['priority'], match_add, actions) #refresh mirror get higher priority
self._request_stats(datapath) # update flow list in data.py
示例15: _switches
def _switches(self, req, **kwargs):
dpid = None
if 'dpid' in kwargs:
dpid = dpid_lib.str_to_dpid(kwargs['dpid'])
switches = get_switch(self.topology_api_app, dpid)
body = json.dumps([switch.to_dict() for switch in switches])
return Response(content_type='application/json', body=body)