Hi
Thanks Fujimoto for your response.
I corrected that and it is running now.
I am stuck with another problem.
Here is the code I am running. Everything is going good but I don't
understand why there is a flow entry added in ovs switch. As, when a
packet arrives it matches the if condition if proto == 150 and total_len <=
2000: and generates a packet_in to the controller every time. When I look
into the switch flow table I see entries for both sender and the receiver.
If both sender and receiver enteries are in flow table then how come
packet_in is being generated everytime?Can you please explain why? I am
confused with this. Please help me.
# Copyright (C) 2011 Nippon Telegraph and Telephone Corporation.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from ryu.base import app_manager
from ryu.controller import ofp_event
from ryu.controller.handler import CONFIG_DISPATCHER, MAIN_DISPATCHER
from ryu.controller.handler import set_ev_cls
from ryu.ofproto import ofproto_v1_3
from ryu.lib.packet import packet
from ryu.lib.packet import ethernet,ipv4,udp
from ryu.lib.packet import ether_types
import time
from struct import *
class SimpleSwitch13(app_manager.RyuApp):
OFP_VERSIONS = [ofproto_v1_3.OFP_VERSION]
def __init__(self, *args, **kwargs):
super(SimpleSwitch13, self).__init__(*args, **kwargs)
self.mac_to_port = {}
self.start_time = time.time()
self.iot_data = []
self.iot_data_size = 0
self.ip_ihl=5
self.ip_ver=4
self.ip_tos = 0
self.ip_tot_len = 0 # kernel will fill the correct total length
self.ip_id = 54321 #Id of this packet
self.ip_frag_off = 0
self.ip_ttl = 255
self.ip_proto = 0
self.ip_check = 0 # kernel will fill the correct checksum
self.ip_saddr = 0 #Spoof the source ip address if you want to
self.ip_daddr = 0
self.ip_ihl_ver = (self.ip_ver << 4) + self.ip_ihl
@set_ev_cls(ofp_event.EventOFPSwitchFeatures, CONFIG_DISPATCHER)
def switch_features_handler(self, ev):
datapath = ev.msg.datapath
ofproto = datapath.ofproto
parser = datapath.ofproto_parser
# install table-miss flow entry
#
# We specify NO BUFFER to max_len of the output action due to
# OVS bug. At this moment, if we specify a lesser number, e.g.,
# 128, OVS will send Packet-In with invalid buffer_id and
# truncated packet data. In that case, we cannot output packets
# correctly. The bug has been fixed in OVS v2.1.0.
match = parser.OFPMatch()
actions = [parser.OFPActionOutput(ofproto.OFPP_CONTROLLER,
ofproto.OFPCML_NO_BUFFER)]
self.add_flow(datapath, 0, match, actions)
def add_flow(self, datapath, priority, match, actions, buffer_id=None):
ofproto = datapath.ofproto
parser = datapath.ofproto_parser
inst = [parser.OFPInstructionActions(ofproto.OFPIT_APPLY_ACTIONS,
actions)]
if buffer_id:
mod = parser.OFPFlowMod(datapath=datapath, buffer_id=buffer_id,
priority=priority, match=match,
instructions=inst)
else:
mod = parser.OFPFlowMod(datapath=datapath, priority=priority,
match=match, instructions=inst)
datapath.send_msg(mod)
@set_ev_cls(ofp_event.EventOFPPacketIn, MAIN_DISPATCHER)
def _packet_in_handler(self, ev):
# 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
parser = datapath.ofproto_parser
in_port = msg.match['in_port']
pkt = packet.Packet(msg.data)
eth = pkt.get_protocols(ethernet.ethernet)[0]
if eth.ethertype == ether_types.ETH_TYPE_LLDP:
# ignore lldp packet
return
dst = eth.dst
src = eth.src
# get ipv4 packet
ipv4_packet = pkt.get_protocol(ipv4.ipv4)
if ipv4_packet:
proto = ipv4_packet.proto
total_len = ipv4_packet.total_length
source=ipv4_packet.src
destination= ipv4_packet.dst
print "INFORMATION:", proto
print total_len
else:
proto=total_len=0
#ip_pkt = pkt.get_protocols(ipv4.ipv4)[3]
#ip_src = ip_pkt.src
dpid = datapath.id
self.mac_to_port.setdefault(dpid, {})
self.logger.info("packet in %s %s %s %s", dpid, src, dst, in_port)
if proto == 150 and total_len <= 2000:
data = msg.data
#print " # stores the packet data"
self.iot_data.append(data)
#print "# increment size counter"
self.iot_data_size += total_len
elapsed_time = time.time() - self.start_time
print "ELAPSED: ", elapsed_time
print "BUFFER: ", self.iot_data
if elapsed_time > 5 or self.iot_data_size > 3500:
#if self.iot_server_mac in self.mac_to_port[dpid]:
#out_port = self.mac_to_port[dpid][dst]
#print "1st condition"
self.no_of_data=len(self.iot_data)
self.ip_proto=proto
self.ip_saddr=source
self.ip_daddr=destination
ip_head= pack('!BBHHHBBH16s16s' , self.ip_ihl_ver,
self.ip_tos, self.ip_tot_len, self.ip_id, self.ip_frag_off,
self.ip_ttl,self.ip_check,self.ip_proto, self.ip_saddr, self.ip_daddr)
total_pkts= pack('!I', self.no_of_data)
print "TOTALLLL,,,,",self.no_of_data
ip_head="{" + ip_head + "}"
total_pkts="{" + total_pkts + "}"
s='|startofdata|'
data = s.join(self.iot_data)
data="|startofdata|" + data
pckt= ip_head + total_pkts + data
#print repr(ip_head)
self.iot_data = []
print "BUFFER: ", self.iot_data
self.iot_data_size = 0
self.start_time = time.time()
out_port = 2 #ofproto.OFPP_FLOOD
actions = [parser.OFPActionOutput(out_port)]
#data = ''.join(self.iot_data)
out = parser.OFPPacketOut(datapath=datapath,
buffer_id=ofproto.OFP_NO_BUFFER,
in_port=in_port,
actions=actions,
data=pckt)
print "out--->" , out
datapath.send_msg(out)
else:
# 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]:
out_port = self.mac_to_port[dpid][dst]
else:
out_port = ofproto.OFPP_FLOOD
actions = [parser.OFPActionOutput(out_port)]
# install a flow to avoid packet_in next time
if out_port != ofproto.OFPP_FLOOD:
match = parser.OFPMatch(in_port=in_port, eth_dst=dst)
# verify if we have a valid buffer_id, if yes avoid to send both
# flow_mod & packet_out
if msg.buffer_id != ofproto.OFP_NO_BUFFER:
self.add_flow(datapath, 1, match, actions, msg.buffer_id)
return
else:
self.add_flow(datapath, 1, match, actions)
data = None
if msg.buffer_id == ofproto.OFP_NO_BUFFER:
data = msg.data
out = parser.OFPPacketOut(datapath=datapath,
buffer_id=msg.buffer_id,
in_port=in_port, actions=actions,
data=data)
datapath.send_msg(out)
Thank You
Sheenam
On Fri, Aug 18, 2017 at 12:50 PM, Fujimoto Satoshi <
satoshi.fujimo...@gmail.com> wrote:
> Hi,
>
> from ryu.ryu.app import simple_switch_13.py
>
> The above line should be removed.
> It causes a SyntaxError and Ryu fails to load your app.
>
> Furthermore, other errors seem to exist in your code, such like "global
> name 'time' is not defined".
> Please make sure.
>
> Thanks,
> Fujimoto
>
>
>
> On 2017年08月18日 12:16, Attitude Killer wrote:
>
> Hi
>
> I am trying to run a modified application of simple_switch_13.py.
>
> I am getting this error:
>
> loading app simple_switch_13.py
> Traceback (most recent call last):
> File "/usr/local/bin/ryu-manager", line 9, in <module>
> load_entry_point('ryu==4.16', 'console_scripts', 'ryu-manager')()
> File "/home/server/.local/lib/python2.7/site-packages/ryu/cmd/manager.py",
> line 98, in main
> app_mgr.load_apps(app_lists)
> File
> "/home/server/.local/lib/python2.7/site-packages/ryu/base/app_manager.py",
> line 415, in load_apps
> cls = self.load_app(app_cls_name)
> File
> "/home/server/.local/lib/python2.7/site-packages/ryu/base/app_manager.py",
> line 392, in load_app
> mod = utils.import_module(name)
> File "/home/server/.local/lib/python2.7/site-packages/ryu/utils.py",
> line 106, in import_module
> return importlib.import_module(modname)
> File "/usr/lib/python2.7/importlib/__init__.py", line 37, in
> import_module
> __import__(name)
> ImportError: No module named simple_switch_13.py
>
>
> This is my modified code:
>
> # Copyright (C) 2011 Nippon Telegraph and Telephone Corporation.
> #
> # Licensed under the Apache License, Version 2.0 (the "License");
> # you may not use this file except in compliance with the License.
> # You may obtain a copy of the License at
> #
> # http://www.apache.org/licenses/LICENSE-2.0
> #
> # Unless required by applicable law or agreed to in writing, software
> # distributed under the License is distributed on an "AS IS" BASIS,
> # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
> # implied.
> # See the License for the specific language governing permissions and
> # limitations under the License.
>
> from ryu.base import app_manager
> from ryu.controller import ofp_event
> from ryu.controller.handler import CONFIG_DISPATCHER, MAIN_DISPATCHER
> from ryu.controller.handler import set_ev_cls
> from ryu.ofproto import ofproto_v1_3
> from ryu.lib.packet import packet
> from ryu.lib.packet import ethernet
> from ryu.lib.packet import ether_types
> from ryu.ryu.app import simple_switch_13.py
>
>
> class SimpleSwitch13(app_manager.RyuApp):
> OFP_VERSIONS = [ofproto_v1_3.OFP_VERSION]
>
> def __init__(self, *args, **kwargs):
> super(SimpleSwitch13, self).__init__(*args, **kwargs)
> self.mac_to_port = {}
> self.start_time = time.time()
> self.iot_data = []
> self.iot_data_size = 0
> self.ip_ihl=5
> self.ip_ver=4
> self.ip_tos = 0
> self.ip_tot_len = 0 # kernel will fill the correct total length
> self.ip_id = 54321 #Id of this packet
> self.ip_frag_off = 0
> self.ip_ttl = 255
> self.ip_proto = 0
> self.ip_check = 0 # kernel will fill the correct checksum
> self.ip_saddr = 0 #Spoof the source ip address if you want to
> self.ip_daddr = 0
> self.ip_ihl_ver = (self.ip_ver << 4) + self.ip_ihl
>
> @set_ev_cls(ofp_event.EventOFPSwitchFeatures, CONFIG_DISPATCHER)
> def switch_features_handler(self, ev):
> datapath = ev.msg.datapath
> ofproto = datapath.ofproto
> parser = datapath.ofproto_parser
>
> # install table-miss flow entry
> #
> # We specify NO BUFFER to max_len of the output action due to
> # OVS bug. At this moment, if we specify a lesser number, e.g.,
> # 128, OVS will send Packet-In with invalid buffer_id and
> # truncated packet data. In that case, we cannot output packets
> # correctly. The bug has been fixed in OVS v2.1.0.
> match = parser.OFPMatch()
> actions = [parser.OFPActionOutput(ofproto.OFPP_CONTROLLER,
> ofproto.OFPCML_NO_BUFFER)]
> self.add_flow(datapath, 0, match, actions)
>
> def add_flow(self, datapath, priority, match, actions, buffer_id=None):
> ofproto = datapath.ofproto
> parser = datapath.ofproto_parser
>
> inst = [parser.OFPInstructionActions(ofproto.OFPIT_APPLY_ACTIONS,
> actions)]
> if buffer_id:
> mod = parser.OFPFlowMod(datapath=datapath,
> buffer_id=buffer_id,
> priority=priority, match=match,
> instructions=inst)
> else:
> mod = parser.OFPFlowMod(datapath=datapath, priority=priority,
> match=match, instructions=inst)
> datapath.send_msg(mod)
>
> @set_ev_cls(ofp_event.EventOFPPacketIn, MAIN_DISPATCHER)
> def _packet_in_handler(self, ev):
> # 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
> parser = datapath.ofproto_parser
> in_port = msg.match['in_port']
>
> pkt = packet.Packet(msg.data)
> eth = pkt.get_protocols(ethernet.ethernet)[0]
>
> if eth.ethertype == ether_types.ETH_TYPE_LLDP:
> # ignore lldp packet
> return
> dst = eth.dst
> src = eth.src
>
> #udp
> udp_pkt= pkt.get_protocol(udp.udp)
> if udp_pkt:
> total_len=udp_pkt.total_length
> sr_port = udp_pkt.src_port
> ds_port = udp_pkt.dst_port
> print "UDPPP PORT --->" , sr_port
> print "UDP DESTINATION----> ", ds_port
> print "INFORMATION:UDP LENGTH:", total_len
> else:
> total_len=0
>
> # get ipv4 packet
> ipv4_packet = pkt.get_protocol(ipv4.ipv4)
> if ipv4_packet:
> proto = ipv4_packet.proto
> total_len = ipv4_packet.total_length
> source=ipv4_packet.src
> destination= ipv4_packet.dst
> print "INFORMATION:", proto
> print total_len
> else:
> proto=total_len=0
> #ip_pkt = pkt.get_protocols(ipv4.ipv4)[3]
> #ip_src = ip_pkt.src
>
> #ip_dst = ip_pkt.dst
>
> # print "IP --->SOURCE:",ip_src'''
>
> dpid = datapath.id
> self.mac_to_port.setdefault(dpid, {})
>
> self.logger.info("packet in %s %s %s %s", dpid, src, dst, in_port)
> if proto == 150 and total_len <= 2000:
> #if udp_pkt:
> # if udp_pkt and ds_port == 5006 and total_len <=2000:
> data = msg.data
> #print " # stores the packet data"
> self.iot_data.append(data)
> #print "# increment size counter"
> self.iot_data_size += total_len
> elapsed_time = time.time() - self.start_time
> print "ELAPSED: ", elapsed_time
> print "BUFFER: ", self.iot_data
>
> if elapsed_time > 5 or self.iot_data_size > 3500:
> #if self.iot_server_mac in self.mac_to_port[dpid]:
>
> # out_port = self.mac_to_port[dpid][dst]
> #print "1st condition"
> self.no_of_data=len(self.iot_data)
> self.ip_proto=proto
> self.ip_saddr='10.0.11.6'
> self.ip_daddr='10.0.11.5'
>
>
> ip_head= pack('!BBHHHBBH16s16s' ,
> self.ip_ihl_ver, self.ip_tos, self.ip_tot_len, self.ip_id,
> self.ip_frag_off, self.ip_ttl,self.ip_check,self.ip_proto, self.ip_saddr,
> self.ip_daddr)
> total_pkts= pack('!I', self.no_of_data)
> print "TOTALLLL,,,,",self.no_of_data
> ip_head="{" + ip_head + "}"
> total_pkts="{" + total_pkts + "}"
> s='|startofdata|'
> data = s.join(self.iot_data)
> data="|startofdata|" + data
> pckt= ip_head + total_pkts + data
> #print repr(ip_head)
>
> self.iot_data = []
> print "BUFFER: ", self.iot_data
> self.iot_data_size = 0
> self.start_time = time.time()
>
> out_port = 2 #ofproto.OFPP_FLOOD
> actions = [parser.OFPActionOutput(out_po
> rt)]
>
>
> #data = ''.join(self.iot_data)
>
>
> out = parser.OFPPacketOut(datapath=d
> atapath,
>
> buffer_id=ofproto.OFP_NO_BUFFER,
> in_port=in_port,
> actions=actions,
> data=data)
> print "out--->" , out
> datapath.send_msg(out)
>
>
> else:
> # 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]:
> out_port = self.mac_to_port[dpid][dst]
> else:
> out_port = ofproto.OFPP_FLOOD
>
> actions = [parser.OFPActionOutput(out_port)]
>
> # install a flow to avoid packet_in next time
> if out_port != ofproto.OFPP_FLOOD:
> match = parser.OFPMatch(in_port=in_port, eth_dst=dst)
> # verify if we have a valid buffer_id, if yes avoid to
> send both
> # flow_mod & packet_out
> if msg.buffer_id != ofproto.OFP_NO_BUFFER:
> self.add_flow(datapath, 1, match, actions,
> msg.buffer_id)
> return
> else:
> self.add_flow(datapath, 1, match, actions)
> data = None
> if msg.buffer_id == ofproto.OFP_NO_BUFFER:
> data = msg.data
>
> out = parser.OFPPacketOut(datapath=datapath,
> buffer_id=msg.buffer_id,
> in_port=in_port, actions=actions,
> data=data)
> datapath.send_msg(out)
>
> Please help me in knowing what I am doing wrong. I will appreciate your
> help. Thank You
>
> Thanks
> Sheenam
>
>
> ------------------------------------------------------------------------------
> Check out the vibrant tech community on one of the world's most
> engaging tech sites, Slashdot.org! http://sdm.link/slashdot
>
>
>
> _______________________________________________
> Ryu-devel mailing
> listRyu-devel@lists.sourceforge.nethttps://lists.sourceforge.net/lists/listinfo/ryu-devel
>
>
>
------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Ryu-devel mailing list
Ryu-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ryu-devel