Dear Iwase,

Thank you for the above information. I attached the file to my email and
can you point out where I am doing wrong? I am doing wrong?

1. I am facing an error:
2. I would like to call the function "Configurerules" after a certain time
period? How can I implement?

L2learningswitch: Exception occurred during handler processing. Backtrace
from offending handler [switch_features_handler] servicing event
[EventOFPSwitchFeatures] follows.

Traceback (most recent call last):

  File "/usr/local/lib/python2.7/dist-packages/ryu/base/app_manager.py",
line 290, in _event_loop

    handler(ev)

  File "/usr/local/lib/python2.7/dist-packages/ryu/app/Simple_l2Ryu.py",
line 42, in switch_features_handler

    self.configurerules(ev, lruleset=self.CONF.ruleset)

  File "/usr/local/lib/python2.7/dist-packages/ryu/app/Simple_l2Ryu.py",
line 127, in configurerules

    match = parser.OFPMatch(eth_type=eth_type, eth_src=eth_src1,
eth_dst=eth_dst1, ipv4_src=ipv4_src1, ipv4_dst=ipv4_dst1, in_port=in_port1,
vlan_vid=(0x1000))

NameError: global name 'eth_src1' is not defined.















On 27 January 2017 at 03:21, Iwase Yusuke <[email protected]> wrote:

> FYI, for the syntax of JSON is similar to the input of ofctl_rest.
>   http://ryu.readthedocs.io/en/latest/app/ofctl_rest.html#
> modify-all-matching-flow-entries
>
>
> On 2017年01月27日 11:20, Iwase Yusuke wrote:
> > Hi,
> >
> > Currently, Ryu does not provides such feature, you need to implement it.
> >
> > But just an idea,
> > JSON representation is might be convenient for such purpose, I guess.
> > "ryu.lib.ofctl_v1_3" can accept JSON like dictionary as input,
> > so you can describe flows as JSON.
> >
> > The following example installs flows at switch connection from JSON file.
> >
> > e.g.)
> > $ cat sample_flows.json
> > [
> >   {
> >     "dpid": 1,
> >     "match": {
> >       "in_port": 1
> >     },
> >     "actions": [
> >       {
> >         "type": "OUTPUT",
> >         "port": 2
> >       }
> >     ]
> >   },
> >   {
> >     "dpid": 1,
> >     "match": {
> >       "in_port": 2
> >     },
> >     "actions": [
> >       {
> >         "type": "OUTPUT",
> >         "port": 1
> >       }
> >     ]
> >   }
> > ]
> >
> >
> > $ git diff
> > diff --git a/ryu/app/simple_switch_13.py b/ryu/app/simple_switch_13.py
> > index 3e7c598..dba09e5 100644
> > --- a/ryu/app/simple_switch_13.py
> > +++ b/ryu/app/simple_switch_13.py
> > @@ -13,6 +13,8 @@
> >  # See the License for the specific language governing permissions and
> >  # limitations under the License.
> >
> > +import json
> > +
> >  from ryu.base import app_manager
> >  from ryu.controller import ofp_event
> >  from ryu.controller.handler import CONFIG_DISPATCHER, MAIN_DISPATCHER
> > @@ -21,6 +23,7 @@ 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.lib import ofctl_v1_3
> >
> >
> >  class SimpleSwitch13(app_manager.RyuApp):
> > @@ -48,6 +51,10 @@ class SimpleSwitch13(app_manager.RyuApp):
> >                                            ofproto.OFPCML_NO_BUFFER)]
> >          self.add_flow(datapath, 0, match, actions)
> >
> > +        for flow in json.load(open('sample_flows.json')):
> > +            if datapath.id == flow.get('dpid', 0):
> > +                ofctl_v1_3.mod_flow_entry(datapath, flow,
> ofproto.OFPFC_ADD)
> > +
> >      def add_flow(self, datapath, priority, match, actions,
> buffer_id=None):
> >          ofproto = datapath.ofproto
> >          parser = datapath.ofproto_parser
> >
> >
> > Thanks,
> > Iwase
> >
> >
> > On 2017年01月24日 20:58, raghu rathode wrote:
> >> Dear Sir/Madam,
> >>
> >> I would like to know, how to pass text file as an argument to Ryu
> application. Which contains a set of rules to install on OpenFlow switches.
> >>
> >> For Example:
> >>
> >> Switch 00-00-00-00-00-01:
> >> dl_type=0x0800, nw_src=10.1.0.1/16 <http://10.1.0.1/16>, nw_dst=
> 10.2.0.1/16 <http://10.2.0.1/16>, in_port=4, 
> actions=mod_dl_src:00:00:12:00:01:03,
> mod_dl_dst:00:00:00:00:00:02, output:3
> >> dl_type=0x0800, dl_src=00:00:00:00:00:02, dl_dst=00:00:12:00:01:03,
> in_port=3, nw_src=10.1.0.1/16 <http://10.1.0.1/16>, nw_dst=10.2.0.1/16 <
> http://10.2.0.1/16>, actions=mod_dl_src:00:00:12:00:01:02,
> mod_dl_dst:00:00:12:00:04:01, output:2
> >> dl_type=0x0800, dl_src=00:00:12:00:02:01, dl_dst=00:00:12:00:01:01,
> in_port=1, nw_src=10.3.0.1/16 <http://10.3.0.1/16>, nw_dst=10.4.0.1/16 <
> http://10.4.0.1/16>, actions=mod_dl_src:00:00:12:
> >>
> >>
> >> --
> >> Thanks and Regards
> >> Hari Raghavendar Rao Bandari
> >> App 10,
> >> Karolinenweg 20,
> >> 37075 Goettingen,
> >> Germany.
> >>
> >>
> >> ------------------------------------------------------------
> ------------------
> >> 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
> >> [email protected]
> >> https://lists.sourceforge.net/lists/listinfo/ryu-devel
> >>
>



-- 
Thanks and Regards
Hari Raghavendar Rao Bandari
App 10,
Karolinenweg 20,
37075 Goettingen,
Germany.
from ryu.base import app_manager
from ryu.controller import ofp_event, dpset
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.ofproto import ofproto_v1_3_parser
from ryu.lib.packet import packet, ethernet, arp, ipv4, ipv6, lldp, tcp, udp, ether_types, in_proto, vlan
from ryu.lib.mac import haddr_to_int
from of_parser import *
from ryu.topology import event
from ryu.lib.dpid import dpid_to_str
from ryu.cmd import manager
from collections import deque # standard python queue structure
import sys # for getting command line arguments and passing to Ryu
import time


if __name__ == "__main__":   # Stuff to set additional command line options
    from ryu import cfg
    CONF = cfg.CONF
    CONF.register_cli_opts([
        cfg.StrOpt('ruleset', default=None, help='rule text file'),
    ])


class L2learningswitch(app_manager.RyuApp):
    OFP_VERSIONS = [ofproto_v1_3.OFP_VERSION]
    
    def __init__(self, *args, **kwargs):
        super(L2learningswitch, self).__init__(*args, **kwargs)
        self.mac_to_port = {}
	# Get the param
	self.ruleset = self.CONF.ruleset
        self.logger.info("ruleset: {}".format(self.ruleset))

    @set_ev_cls(ofp_event.EventOFPSwitchFeatures, CONFIG_DISPATCHER)
    def switch_features_handler(self, ev):
        msg = ev.msg
	datapath = msg.datapath
	dpid = datapath.id
	self.logger.info("switch {} came up".format(dpid))
	self.configurerules(ev, lruleset=self.CONF.ruleset)
        
    def configurerules (self, ev, lruleset):
	global vlan_vid1, eth_src1, eth_dst1, ipv4_src1, ipv4_dst1, in_port1
	msg = ev.msg
	datapath = msg.datapath
	dpid = datapath.id
        ofproto = datapath.ofproto
        parser = datapath.ofproto_parser
	
        self.logger.info("Ready for configure init rules in Switch: " + str(dpid))
        cookie = cookie_mask = 0
	table_id = 0
        idle_timeout = 60
        hard_timeout = 120
	buffer_id = ofproto.OFP_NO_BUFFER
        
        of_fib = parse_of_file(lruleset)
        #print of_fib
        of_list = []
        try:
                of_list = of_fib[str(dpid)]
        except KeyError:
                self.logger.info( "Oops! There is no information about the Switch " + str(dpid) + " in the configuration file")
        
	for of_entry in of_list:
                                                                                           
                if "eth_type" in of_entry.keys():
                    eth_type = int(of_entry["eth_type"], 16)

                #if "ip_proto" in of_entry.keys():
                    #ip_proto = int(of_entry["ip_proto"], 10)

                if "priority" in of_entry.keys():
                    req.priority = int(of_entry["priority"], 10)

                # ETH
                if "eth_src" in of_entry.keys():
                    eth_src1 = haddr_to_int(of_entry["eth_src"])

                if "eth_dst" in of_entry.keys():
                    eth_dst1 = haddr_to_int(of_entry["eth_dst"])
                # IP
                if "ipv4_src" in of_entry.keys():
                    ipv4_src1 = of_entry["ipv4_src"]

                if "ipv4_dst" in of_entry.keys():
                    ipv4_dst1 = of_entry["ipv4_dst"]


                 # UDP
                #if "udp_dst" in of_entry.keys():
                    #udp_dst = int(of_entry["udp_dst"], 10)

                #if "udp_src" in of_entry.keys():
                    #udp_src = int(of_entry["udp_src"], 10)

                 # OTHERS
                if "in_port" in of_entry.keys():
                    in_port1 = int(of_entry["in_port"], 10)

                if "vlan_vid" in of_entry.keys():
                    vlan_vid1 = int(of_entry["vlan_vid"], 16)


                    # msg.match.tp_dst = 80
                    # iterate list of actions
                    # output
                    # vlan
		actions =[]

                if "vlan_vid" in of_entry["actions"].keys():
                    actions.append(parser.OFPActionSetField(vlan_vid=int(of_entry["actions"]["vlan_vid"], 16)))

                if "eth_src" in of_entry["actions"].keys():
                    actions.append(parser.OFPActionSetField(eth_src=(of_entry["actions"]["eth_src"])))

                if "eth_dst" in of_entry["actions"].keys():
                    #print "eth_dst: %s " % of_entry["actions"]["eth_dst"]
                    actions.append(parser.OFPActionSetField(eth_dst=(of_entry["actions"]["eth_dst"])))

                if "output" in of_entry["actions"].keys():
                    actions.append(parser.OFPActionOutput(port=int(of_entry["actions"]["output"], 10)))
                # print "set the output port"

                match = parser.OFPMatch(eth_type=eth_type, eth_src=eth_src1, eth_dst=eth_dst1, ipv4_src=ipv4_src1, ipv4_dst=ipv4_dst1, in_port=in_port1, vlan_vid=(0x1000))
		inst = [parser.OFPInstructionActions(ofproto.OFPIT_APPLY_ACTIONS, actions)]
                req = parser.OFPFlowMod(datapath=datapath, cookie=cookie, cookie_mask=cookie_mask, table_id=table_id, command=ofproto.OFPFC_ADD, idle_timeout=idle_timeout, hard_timeout=hard_timeout, buffer_id=buffer_id, out_port=ofproto.OFPP_ANY, match=match,instructions=inst)

                datapath.send_msg(req)

                # f.write(" ending " + str(time.time()) + "\n")
                # f.close()
                self.logger.info("Init rules configured, allow traffic for " + str(dpid))


    @set_ev_cls(ofp_event.EventOFPPacketIn, MAIN_DISPATCHER)
    def packet_in(self, ev):
        
        msg = ev.msg
	datapath = msg.datapath
	ofproto = datapath.ofproto
        parser = datapath.ofproto_parser
	
	pkt = packet.Packet(msg.data)
	eth = pkt.get_protocols(ethernet.ethernet)[0]
	ipv4_pkt = pkt.get_protocol(ipv4.ipv4)
	udp_pkt = pkt.get_protocol(udp.udp)
	dst = eth.dst
	src = eth.src
        if pkt.get_protocol(lldp.lldp):
            return
	if ipv4_pkt:
	   self.logger.info("Intercepted a packet from: {} to {}".format(ipv4_pkt.src,ipv4_pkt.dst))
	#if udp:
	 #  self.logger.info(" Intercepted UDP packet with source port {} and dest port {}".format(udp_pkt.src_port, udp_pkt.dst_port))

    	
    if __name__ == "__main__":
        manager.main(args=sys.argv)
------------------------------------------------------------------------------
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
[email protected]
https://lists.sourceforge.net/lists/listinfo/ryu-devel

Reply via email to