本文整理汇总了Python中ryu.lib.packet.packet.Packet.get_protocol方法的典型用法代码示例。如果您正苦于以下问题:Python Packet.get_protocol方法的具体用法?Python Packet.get_protocol怎么用?Python Packet.get_protocol使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ryu.lib.packet.packet.Packet
的用法示例。
在下文中一共展示了Packet.get_protocol方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _packet_in_handler
# 需要导入模块: from ryu.lib.packet.packet import Packet [as 别名]
# 或者: from ryu.lib.packet.packet.Packet import get_protocol [as 别名]
def _packet_in_handler(self, ev):
self.logger.debug("my_arp: _packet_in_handler:")
# If you hit this you might want to increase
# the "miss_send_length" of your switch
if ev.msg.msg_len < ev.msg.total_len:
self.logger.debug("packet truncated: only %s of %s bytes",
ev.msg.msg_len, ev.msg.total_len)
msg = ev.msg
datapath = msg.datapath
# ofproto = datapath.ofproto
inPort = msg.match['in_port']
packets = Packet(msg.data)
dpid = datapath.id
self.mac_to_port.setdefault(dpid, {})
eth = packets.get_protocols(ethernet)[0]
src = eth.src
dst = eth.dst
self.mac_to_port[dpid][src] = inPort
data = msg.data
self.arp_learning.setdefault(dpid, [])
self.packetToport.setdefault(dpid, [])
etherFrame = packets.get_protocol(ethernet)
# if dst == LLDP_MAC_NEAREST_BRIDGE:
# return
# print "packets: ", packets
# print "packets.get_protocols(ethernet): ", packets.get_protocols(ethernet)
# print "etherFrame######", etherFrame
# etherFrame = packets.get_protocol(ethernet)
etherFrame = packets.get_protocol(ethernet)
# print etherFrame
# print ether
# print hex(etherFrame.ethertype)
# print hex(ether.ETH_TYPE_ARP)
if etherFrame.ethertype == ether.ETH_TYPE_ARP:
arpPacket = packets.get_protocol(arp)
arpArriveTime = time.time()
srcMac = etherFrame.src
arp_dstIP = arpPacket.dst_ip
self.packetToport[datapath.id] = [srcMac, arp_dstIP, inPort, arpArriveTime]
# print "arp"
# print "packets: ", packets
# print "packets.get_protocols(ethernet): ", packets.get_protocols(ethernet)
# print "ARP: %s" % arpPacket.opcode
# # self.logger.info("packet in %s %s %s %s", dpid, src, dst, in_port)
# if arpPacket.opcode == 1:
# print "ARP Requst"
# self.logger.info("packet in %s %s %s %s", datapath.id, srcMac, dst, inPort)
# elif arpPacket.opcode == 2:
# print "ARP Reply"
# self.logger.info("packet in %s %s %s %s", datapath.id, srcMac, dst, inPort)
self.receive_arp(datapath, packets, etherFrame, inPort, data)
return 0
else:
self.logger.debug("Drop packet")
return 1
示例2: packet_in_handler
# 需要导入模块: from ryu.lib.packet.packet import Packet [as 别名]
# 或者: from ryu.lib.packet.packet.Packet import get_protocol [as 别名]
def packet_in_handler(self, ev):
msg = ev.msg
pkt = Packet(msg.data)
dp = msg.datapath
#self._request_stats(dp)
in_port = msg.match['in_port']
#self._request_stats(dp)
ofp = dp.ofproto
ofp_parser = dp.ofproto_parser
#pdb.set_trace()
actions = []
#dp.send_msg(out)
ip = pkt.get_protocol(ipv4.ipv4)
pk_tcp = pkt.get_protocol(tcp.tcp)
#self.total = self.total + pk_tcp.window_size
actions.append(ofp_parser.OFPActionOutput(ofp.OFPP_FLOOD))
out = ofp_parser.OFPPacketOut(
datapath=dp, buffer_id=msg.buffer_id, in_port=in_port,
actions=actions)
dp.send_msg(out)
op = randint(0,1)
if ip:
#print ip
ip.ttl = op
#print ip
#actions.append(ofp.OFPActionDecNwTtl())
if ip.ttl == 1:
self.tabelaPacotesGrandes.append(ip)
self.total = self.total + ip.total_length
print "----------------------------------TTL COM 1----------------------------------"
else:
self.tabelaPacotesPequenos.append(ip)
self.total = self.total + ip.total_length
print "----------------------------------TTL COM 0----------------------------------"
self._send_packet(dp, in_port, pkt)
示例3: packet_In_handler
# 需要导入模块: from ryu.lib.packet.packet import Packet [as 别名]
# 或者: from ryu.lib.packet.packet.Packet import get_protocol [as 别名]
def packet_In_handler(self, event):
msg = event.msg
datapath = msg.datapath
if datapath.id not in self.allDatapath:
self.allDatapath[datapath.id] = datapath
ofproto = datapath.ofproto
parser = datapath.ofproto_parser
match = msg.match
data = msg.data
in_port = match["in_port"]
pkt = Packet(data)
eth = pkt.get_protocol(ethernet)
src = eth.src
dst = eth.dst
self.mac_to_port.setdefault(datapath.id, {})
self.mac_to_port[datapath.id][src] = in_port
if dst in self.mac_to_port[datapath.id]:
out_port = self.mac_to_port[datapath.id][dst]
else:
out_port = ofproto.OFPP_FLOOD
actions = [parser.OFPActionOutput(out_port)]
match = parser.OFPMatch(in_port=in_port, eth_src=src, eth_dst=dst)
instructions = [parser.OFPInstructionActions(ofproto.OFPIT_APPLY_ACTIONS, actions=actions)]
self.addFlow(datapath=datapath, priority=1, match=match, instructions=instructions)
out_msg = parser.OFPPacketOut(
datapath=datapath, buffer_id=msg.buffer_id, in_port=in_port, actions=actions, data=None
)
datapath.send_msg(out_msg)
示例4: packetIn_handler
# 需要导入模块: from ryu.lib.packet.packet import Packet [as 别名]
# 或者: from ryu.lib.packet.packet.Packet import get_protocol [as 别名]
def packetIn_handler(self,event):
msg = event.msg
data = msg.data
datapath = msg.datapath
dapid = datapath.id
ofproto = datapath.ofproto
parser = datapath.ofproto_parser
in_port = msg.match['in_port']
pkt = Packet(data)
eth = pkt.get_protocol(ethernet)
eth_src = eth.src
eth_dst = eth.dst
self.mac_to_port.setdefault(datapath.id,{})
self.mac_to_port[datapath.id].setdefault(eth_src,in_port)
#self.mac_to_port[datapath.id][eth_src] = in_port
allPkts = dict((p.protocol_name,p) for p in pkt.protocols)
print("packet in %s %s"%(str(eth_src),str(eth_dst)))
if eth_dst in self.mac_to_port[datapath.id]:
out_port = self.mac_to_port[datapath.id][eth_dst]
else:
out_port = ofproto.OFPP_FLOOD
actions = [parser.OFPActionOutput(out_port)]
pktout_msg = parser.OFPPacketOut(datapath=datapath,buffer_id=ofproto.OFP_NO_BUFFER,
in_port=in_port,actions=actions,data=msg.data)
datapath.send_msg(pktout_msg)
示例5: _packet_in_handler
# 需要导入模块: from ryu.lib.packet.packet import Packet [as 别名]
# 或者: from ryu.lib.packet.packet.Packet import get_protocol [as 别名]
def _packet_in_handler(self, ev):
"""Handle packet_in events."""
if not self.link_discovery:
return
msg = ev.msg
in_port = msg.match['in_port']
packet = Packet(msg.data)
efm = packet.get_protocol(ethernet.ethernet)
if efm.ethertype == ether.ETH_TYPE_ARP:
self.send_event_to_observers(event.EventArpReceived(ev))
try:
src_dpid, src_port_no = LLDPPacket.lldp_parse(msg.data)
except LLDPPacket.LLDPUnknownFormat as e:
# This handler can receive all the packtes which can be
# not-LLDP packet. Ignore it silently
return
else:
dst_dpid = msg.datapath.id
dst_port_no = in_port
src = self._get_port(src_dpid, src_port_no)
if not src or src.dpid == dst_dpid:
return
dst = self._get_port(dst_dpid, dst_port_no)
if not dst:
return
old_peer = self.links.get_peer(src)
#LOG.debug("Packet-In")
#LOG.debug(" src=%s", src)
#LOG.debug(" dst=%s", dst)
#LOG.debug(" old_peer=%s", old_peer)
if old_peer and old_peer != dst:
old_link = Link(src, old_peer)
self.send_event_to_observers(event.EventLinkDelete(old_link))
link = Link(src, dst)
if not link in self.links:
self.send_event_to_observers(event.EventLinkAdd(link))
if not self.links.update_link(src, dst):
# reverse link is not detected yet.
# So schedule the check early because it's very likely it's up
try:
self.ports.lldp_received(dst)
except KeyError as e:
# There are races between EventOFPPacketIn and
# EventDPPortAdd. So packet-in event can happend before
# port add event. In that case key error can happend.
# LOG.debug('lldp_received: KeyError %s', e)
pass
else:
self.ports.move_front(dst)
self.lldp_event.set()
示例6: packet_in_handler
# 需要导入模块: from ryu.lib.packet.packet import Packet [as 别名]
# 或者: from ryu.lib.packet.packet.Packet import get_protocol [as 别名]
def packet_in_handler(self, ev):
msg = ev.msg
datapath = msg.datapath
ofproto = datapath.ofproto
parser = datapath.ofproto_parser
pkt = Packet(msg.data)
pkt_udp = pkt.get_protocol(udp.udp)
pkt_tcp = pkt.get_protocol(tcp.tcp)
header_list = dict((p.protocol_name, p)
for p in pkt.protocols if type(p) != str)
self.logger.info(header_list)
if pkt_udp:
src_port = pkt_udp.src_port
dst_port = pkt_udp.dst_port
elif pkt_tcp:
src_port = pkt_tcp.src_port
dst_port = pkt_tcp.dst_port
for ipproto in [6,17]:
mod_flow_entry(datapath,
{'priority' : 100,
'table_id' : 0,
'match' : {'tp_src': src_port,
'dl_type': 2048,
'ip_proto': ipproto},
'actions' :[{'type': 'WRITE_METADATA',
'metadata': src_port << 16,
'metadata_mask' : 4294901760},
{'type': 'GOTO_TABLE',
'table_id' : 1}]},
ofproto.OFPFC_ADD)
mod_flow_entry(datapath,
{'priority' : 100,
'table_id' : 1,
'match' : {'tp_dst': dst_port,
'dl_type': 2048,
'ip_proto': ipproto},
'actions' :[{'type': 'WRITE_METADATA',
'metadata': dst_port,
'metadata_mask': 65535},
{'type' : 'GOTO_TABLE',
'table_id' : 2}]},
ofproto.OFPFC_ADD)
self.packet_out(datapath, pkt)
示例7: _packet_in_handler
# 需要导入模块: from ryu.lib.packet.packet import Packet [as 别名]
# 或者: from ryu.lib.packet.packet.Packet import get_protocol [as 别名]
def _packet_in_handler(self, ev):
self.logger.debug("my_arp: _packet_in_handler:")
# If you hit this you might want to increase
# the "miss_send_length" of your switch
if ev.msg.msg_len < ev.msg.total_len:
self.logger.debug("packet truncated: only %s of %s bytes",
ev.msg.msg_len, ev.msg.total_len)
msg = ev.msg
datapath = msg.datapath
# ofproto = datapath.ofproto
inPort = msg.match['in_port']
packets = Packet(msg.data)
dpid = datapath.id
eth = packets.get_protocols(ethernet)[0]
src = eth.src
dst = eth.dst
self.mac_to_port.setdefault(hex(dpid), {})
self.arp_learning.setdefault(dpid, [])
self.packetToport.setdefault(dpid, {})
if dst == LLDP_MAC_NEAREST_BRIDGE:
return
if src in self.mac_to_port[hex(dpid)].keys():
pass
else:
self.mac_to_port[hex(dpid)][src] = inPort
data = msg.data
# print mac_to_port for verification
# self.logger.info("MySimpleArp: self.mac_to_port")
# for key, value in self.mac_to_port.items():
# print "\t", self._hostname_Check(int(str(key), 16)), value
etherFrame = packets.get_protocol(ethernet)
if etherFrame.ethertype == ether.ETH_TYPE_ARP:
self.logger.debug("\n:")
dst = eth.dst
self.receive_arp(datapath, packets, etherFrame, inPort, data)
return 0
else:
self.logger.debug("Drop packet")
return 1
示例8: _packet_in_handler
# 需要导入模块: from ryu.lib.packet.packet import Packet [as 别名]
# 或者: from ryu.lib.packet.packet.Packet import get_protocol [as 别名]
def _packet_in_handler(self, ev):
self.logger.debug("my_arp: _packet_in_handler:")
# If you hit this you might want to increase
# the "miss_send_length" of your switch
if ev.msg.msg_len < ev.msg.total_len:
self.logger.debug("packet truncated: only %s of %s bytes", ev.msg.msg_len, ev.msg.total_len)
msg = ev.msg
datapath = msg.datapath
# ofproto = datapath.ofproto
inPort = msg.match["in_port"]
packets = Packet(msg.data)
dpid = datapath.id
eth = packets.get_protocols(ethernet)[0]
src = eth.src
dst = eth.dst
self.mac_to_port.setdefault(hex(dpid), {})
self.arp_learning.setdefault(dpid, [])
self.packetToport.setdefault(dpid, {})
if dst == LLDP_MAC_NEAREST_BRIDGE:
return
if src in self.mac_to_port[hex(dpid)].keys():
pass
else:
self.mac_to_port[hex(dpid)][src] = inPort
data = msg.data
etherFrame = packets.get_protocol(ethernet)
if etherFrame.ethertype == ether.ETH_TYPE_ARP:
self.logger.debug("\n:")
# arpPacket = packets.get_protocol(arp)
# arpArriveTime = time.time()
# srcMac = etherFrame.src
# arp_dstIP = arpPacket.dst_ip
dst = eth.dst
self.receive_arp(datapath, packets, etherFrame, inPort, data)
return 0
else:
self.logger.debug("Drop packet")
return 1
示例9: myPacketIn_handler
# 需要导入模块: from ryu.lib.packet.packet import Packet [as 别名]
# 或者: from ryu.lib.packet.packet.Packet import get_protocol [as 别名]
def myPacketIn_handler(self,event):
msg = event.msg
datapath = msg.datapath
print("datapathId is %d"%datapath.id)
ofproto = datapath.ofproto
parser = datapath.ofproto_parser
data = msg.data
in_port = msg.in_port
self.mac_to_port.setdefault(datapath.id,{})
pkt = Packet(data)
eth = pkt.get_protocol(ethernet)
src =eth.src
dst = eth.dst
self.mac_to_port[datapath.id][src] = in_port
if dst not in self.mac_to_port[datapath.id]:
out_port = ofproto.OFPP_FLOOD
else:
out_port = self.mac_to_port[datapath.id][dst]
actions = [parser.OFPActionOutput(out_port)]
out_msg = parser.OFPPacketOut(datapath,msg.buffer_id,in_port,actions,data)
datapath.send_msg(out_msg)
示例10: arp_received_handler
# 需要导入模块: from ryu.lib.packet.packet import Packet [as 别名]
# 或者: from ryu.lib.packet.packet.Packet import get_protocol [as 别名]
def arp_received_handler(self, ev):
msg = ev.ev.msg
datapath = msg.datapath
in_port = msg.match['in_port']
packet = Packet(msg.data)
arppkt = packet.get_protocol(arp.arp)
if arppkt.opcode == arp.ARP_REQUEST:
self.broadcast_to_end_nodes(msg)
try:
src_port, dst_port = db.handle_arp_packet(arppkt, datapath.id, in_port)
self.process_end_hw_addr_flows(src_port)
self.process_end_hw_addr_flows(dst_port)
if src_port[0] != dst_port[0]:
self.process_route(src_port, dst_port)
self.process_route(dst_port, src_port)
if arppkt.opcode == arp.ARP_REPLY:
target_switch = self.switches[dst_port[0]].switch
self.arp_packet_out(target_switch.dp, dst_port[1], msg.data)
except db.ArpTableNotFoundException:
pass
示例11: packetIn_handler
# 需要导入模块: from ryu.lib.packet.packet import Packet [as 别名]
# 或者: from ryu.lib.packet.packet.Packet import get_protocol [as 别名]
def packetIn_handler(self,event):
msg = event.msg
data = msg.data
datapath = msg.datapath
dapid = datapath.id
ofproto = datapath.ofproto
parser = datapath.ofproto_parser
in_port = msg.in_port
pkt = Packet(data)
eth = pkt.get_protocol(ethernet)
src = eth.src
dst = eth.dst
self.mac_to_port.setdefault(datapath.id,{})
self.mac_to_port[datapath.id].setdefault(src,in_port)
if dst in self.mac_to_port[dapid]:
out_port = self.mac_to_port[dapid][dst]
else:
out_port = ofproto.OFPP_FLOOD
actions = [parser.OFPActionOutput(out_port)]
pktout_msg = parser.OFPPacketOut(datapath=datapath,buffer_id=msg.buffer_id,in_port=in_port,actions=actions,data=data)
datapath.send_msg(pktout_msg)
示例12: packetIn_handler
# 需要导入模块: from ryu.lib.packet.packet import Packet [as 别名]
# 或者: from ryu.lib.packet.packet.Packet import get_protocol [as 别名]
def packetIn_handler(self,event):
msg = event.msg
data = msg.data
datapath = msg.datapath
dapid = datapath.id
ofproto = datapath.ofproto
parser = datapath.ofproto_parser
in_port = msg.in_port
pkt = Packet(data)
eth = pkt.get_protocol(ethernet)
eth_src = eth.src
eth_dst = eth.dst
self.mac_to_port.setdefault(datapath.id,{})
self.mac_to_port[datapath.id][eth_src] = in_port
print("eth_src: %s,eth_dst: %s"%(str(eth_src),str(eth_dst)))
allPkts = dict((p.protcol_name,p) for p in pkt.protocols)
if ARP in allPkts:
arp_opcode = allPkts[ARP].opcode
arp_src_mac = allPkts[ARP].src_mac
arp_dst_mac = allPkts[ARP].dst_mac
arp_src_ip = allPkts[ARP].src_ip
arp_dst_ip = allPkts[ARP].dst_ip
print("opcode: %s,src_mac: %s,dst_mac: %s,src_ip: %s,dst_ip: %s"%(
str(arp_opcode),str(arp_src_mac),str(arp_dst_mac),
str(arp_src_ip),str(arp_dst_ip)))
if eth_dst in self.mac_to_port[datapath.id]:
out_port = self.mac_to_port[datapath.id][eth_dst]
else:
out_port = ofproto.OFPP_FLOOD
actions = [parser.OFPActionOutput(out_port)]
pktout_msg = parser.OFPPacketOut(datapath=datapath,buffer_id=ofproto.OFP_NO_BUFFER,
in_port=in_port,actions=actions,data=msg.data)
datapath.send_msg(pktout_msg)
示例13: packet_in_handler
# 需要导入模块: from ryu.lib.packet.packet import Packet [as 别名]
# 或者: from ryu.lib.packet.packet.Packet import get_protocol [as 别名]
def packet_in_handler(self, ev):
msg = ev.msg
datapath = msg.datapath
ofproto = datapath.ofproto
parser = datapath.ofproto_parser
in_port = msg.match["in_port"]
pkt = packet.Packet(msg.data)
eth = pkt.get_protocols(ethernet)[0]
dst = eth.dst
packets = Packet(msg.data)
data = msg.data
if eth.ethertype == ether.ETH_TYPE_ARP:
arp_pkt = pkt.get_protocols(arp.arp)[0]
srcMac = arp_pkt.src_mac
srcIP = arp_pkt.src_ip
etherFrame = packets.get_protocol(ethernet)
self.receive_arp(datapath, packets, etherFrame, in_port, data)
elif eth.ethertype == ether.ETH_TYPE_IP:
ip = pkt.get_protocols(ipv4.ipv4)[0]
srcMac = eth.src
srcIP = ip.src
else:
return
if self.isRouter(srcMac):
return
if srcIP not in self.hosts:
self.hosts[srcIP] = {}
# Always update MAC and switch-port location, just in case
# DHCP reassigned the IP or the host moved
self.hosts[srcIP]["mac"] = srcMac
self.updateHostTable(srcIP, dpid_lib.dpid_to_str(datapath.id), in_port)
示例14: packet_in_handler
# 需要导入模块: from ryu.lib.packet.packet import Packet [as 别名]
# 或者: from ryu.lib.packet.packet.Packet import get_protocol [as 别名]
def packet_in_handler(self, ev):
msg = ev.msg
pkt = Packet(msg.data)
dp = msg.datapath
buffer_id = msg.buffer_id
in_port = msg.match['in_port']
match_old = msg.match
ofp = dp.ofproto
ofp_parser = dp.ofproto_parser
#pdb.set_trace()
actions = []
instructions = []
#dp.send_msg(out)
ip = pkt.get_protocol(ipv4.ipv4)
tcp1 = pkt.get_protocol(tcp.tcp)
pk_tcp = pkt.get_protocol(tcp.tcp)
#self.total = self.total + pk_tcp.window_size
eth = pkt.get_protocol(ethernet.ethernet)
dst = eth.dst # destination MAC
src = eth.src # source MAC
actions.append(ofp_parser.OFPActionOutput(ofp.OFPP_FLOOD))
out = ofp_parser.OFPPacketOut(
datapath=dp, buffer_id=msg.buffer_id, in_port=in_port,
actions=actions)
dp.send_msg(out)
op = randint(0,1)
actions = []
dpid = dp.id
self.mac_to_port.setdefault(dpid, {})
# learn a mac address to avoid FLOOD next time.
self.mac_to_port[dpid][src] = in_port
if dst in self.mac_to_port[dpid]:
#if destination is know send it to the right port
out_port = self.mac_to_port[dpid][dst]
else:
#otherwise, flood
out_port = ofp.OFPP_FLOOD
if ip and tcp1:
#print ip
ip.ttl = op
#print ip
#actions.append(ofp.OFPActionDecNwTtl())
if ip.ttl == 1:
self.tabelaPacotesGrandes.append(ip)
self.total = self.total + ip.total_length
print "----------------------------------TTL COM 1----------------------------------"
else:
self.tabelaPacotesPequenos.append(ip)
self.total = self.total + ip.total_length
print "----------------------------------TTL COM 0----------------------------------"
self._send_packet(pkt)
if ip and tcp1:
actions.append(dp.ofproto_parser.OFPActionSetNwTtl(10))
match = dp.ofproto_parser.OFPMatch(dl_dst=ip.dst,dl_src=ip.src,src_port = tcp1.src_port, dst_port =tcp1.dst_port)
instructions =[OFPInstructionActions(ofp.OFPIT_APPLY_ACTIONS, actions=actions)]
prio = 100
else:
match = match_old
prio = 1
actions.append(dp.ofproto_parser.OFPActionOutput(out_port))
if out_port != ofp.OFPP_FLOOD:
mod = dp.ofproto_parser.OFPFlowMod(datapath=dp, cookie=0, cookie_mask=0, table_id=0, command=0,buffer_id = buffer_id,
idle_timeout=0, hard_timeout=0, priority=prio, out_port=out_port,
flags=ofp.OFPFF_SEND_FLOW_REM, match=match, instructions=instructions)
dp.send_msg(mod)
out = dp.ofproto_parser.OFPPacketOut(datapath=dp, in_port=in_port,buffer_id = buffer_id, actions=actions)
dp.send_msg(out)