0% found this document useful (0 votes)
946 views3 pages

RYU SDN Controller Beginner's Guide

RYU SDN

Uploaded by

mamadoudianyahoo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
946 views3 pages

RYU SDN Controller Beginner's Guide

RYU SDN

Uploaded by

mamadoudianyahoo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

04/10/2017 RYU Controller Tutorial | SDN Hub

RYU Controller Tutorial

This tutorial is intended for beginners to SDN application development for the RYU platform from NTT. RYU has support for several
versions of OpenFlow, including OpenFlow versions 1.0 and 1.3 that have seen wide spread support from vendors.

For this tutorial, some Python knowledge will be useful, though it isn’t absolutely necessary. This tutorial will help understand RYU’s
internals, steps to build a new application on top of RYU and get introduced to controller programming.

1. Quickstart
Run Mininet on a terminal window using the following command. This starts a network emulation environment to emulate 1 switch with
3 hosts.
$ sudo mn --topo single,3 --mac --controller remote --switch ovsk

The above command will spawn 1 switch that has support for both OpenFlow ver 1.0 and 1.3. Based on your needs, however, you can
force a switch to support OpenFlow 1.3 by executing this command:
$ sudo ovs-vsctl set bridge s1 protocols=OpenFlow13

The wireshark that is part of the VM can parse OpenFlow 1.3 messages. To start wireshark and view OpenFlow messages:

sudo wireshark &

Next, start the RYU Controller. Assume that the main folder where ryu is installed is in /home/ubuntu/ryu, The below command starts
the controller by initiating the OpenFlow Protocol Handler and Simple Switch 1.3 application.
Since the switch supports OpenFlow 1.0 and 1.3, while the application only supports 1.3, the system will auto-negotiate and
choose to proceed will OpenFlow 1.3.

$ cd /home/ubuntu/ryu && ./bin/ryu-manager --verbose ryu/app/simple_switch_13.py


loading app ryu/app/simple_switch_13.py
loading app [Link].ofp_handler
instantiating app ryu/app/simple_switch_13.py of SimpleSwitch13
instantiating app [Link].ofp_handler of OFPHandler
BRICK SimpleSwitch13
CONSUMES EventOFPPacketIn
CONSUMES EventOFPSwitchFeatures
BRICK ofp_event
PROVIDES EventOFPPacketIn TO {'SimpleSwitch13': set(['main'])}
PROVIDES EventOFPSwitchFeatures TO {'SimpleSwitch13': set(['config'])}
CONSUMES EventOFPErrorMsg
CONSUMES EventOFPSwitchFeatures
CONSUMES EventOFPPortDescStatsReply
CONSUMES EventOFPEchoRequest
CONSUMES EventOFPHello

Ensure that we start the correct ryu-manager if multiple versions of the controller is installed in the same system.
Next, check if the hosts in the mininet topology can reach each other

mininet> h1 ping h3
PING [Link] ([Link]) 56(84) bytes of data.
64 bytes from [Link]: icmp_req=1 ttl=64 time=2.76 ms
64 bytes from [Link]: icmp_req=2 ttl=64 time=0.052 ms
64 bytes from [Link]: icmp_req=3 ttl=64 time=0.051 ms

2. RYU Code Structure


The main controller code is organized under the /ryu/ folder (In our VM – /home/ubuntu/ryu/ryu/). Here we discuss the functionalities of the
key components. It is important to become familiar with them.

app/ – Contains set of applications that run on-top of the controller.


base/ – Contains the base class for RYU applications. The RyuApp class in the app_manager.py file is inherited when creating a new
application.

[Link] 1/3
04/10/2017 RYU Controller Tutorial | SDN Hub
controller/ – Contains the required set of files to handle OpenFlow functions (e.g., packets from switches, generating flows, handling
network events, gathering statistics etc).
lib/ – Contains set of packet libraries to parse different protocol headers and a library for OFConfig. In addition, it includes parsers for
Netflow and sFlow too.
ofproto/ – Contains the OpenFlow protocol specific information and related parsers to support different versions of OF protocol (1.0,
1.2, 1.3, 1.4)
topology/: Contains code that performs topology discovery related to OpenFlow switches and handles associated information (e.g.,
ports, links etc). Internally uses LLDP protocol.

3. RYU Controller Code Essentials


Most controller platforms expose some native features to allow these key features:

Ability to listen to asynchronous events (e.g., PACKET_IN, FLOW_REMOVED) and to observe events using
[Link].set_ev_cls decorator.
Ability to parse incoming packets (e.g., ARP, ICMP, TCP) and fabricate packets to send out into the network
Ability to create and send an OpenFlow/SDN message (e.g., PACKET_OUT, FLOW_MOD, STATS_REQUEST) to the programmable
dataplane.

With RYU you can achieve all of those by invoking set of applications to handle network events, parse any switch request and react to
network changes by installing new flows, if required. For instance, creating a new a application involves creating a subclass of RyuApp and
building the required logic to listen for network events.

from [Link] import app_manager

class L2Forwarding(app_manager.RyuApp):
def __init__(self, *args, **kwargs):
super(L2Forwarding, self).__init__(*args, **kwargs)

While the above code represents a valid RYU application, it doesn’t have the logic to handle network events from OpenFlow switches.
Next, to allow an application to receive packets sent by the switch to the controller, the class needs to implement a method which is
decorated by EventOFPPacketIn.

@set_ev_cls(ofp_event.EventOFPPacketIn, MAIN_DISPATCHER)
def packet_in_handler(self, ev):

The first argument of the decorator calls this function everytime a packet_in message is received. The second argument indicates the
switch state. Any information about the switch and the protocol version supported by the switch can be deciphered using the following:

msg = [Link] # Object representing a packet_in data structure.


datapath = [Link] # Switch Datapath ID
ofproto = [Link] # OpenFlow Protocol version the entities negotiated. In our case OF1.3

Once the packet is received, you can decode the packet by importing the packet library under /ryu/lib:

from [Link] import packet


from [Link] import ethernet

We can inspect the packet headers for several packet types: ARP, Ethernet, ICMP, IPv4, IPv6, MPLS, OSPF, LLDP, TCP, UDP.

pkt = [Link]([Link])
eth = pkt.get_protocol([Link])

I use the following two useful commands to extract Ether header details:

dst = [Link]
src = [Link]

Similarly, the OFPPacketOut class can be used to build a packet_out message with the required information (e.g., Datapath ID, associated
actions etc)

out = ofp_parser.OFPPacketOut(datapath=dp,in_port=msg.in_port,actions=actions)#Generate the message


dp.send_msg(out) #Send the message to the switch

Besides a PACKET_OUT, we can also perform a FLOW_MOD insertion into a switch. For this, we build the Match, Action, Instructions and
generate the required Flow. Here is an example of how to create a match header where the in_port and eth_dst matches are extracted
from the PACKET_IN:

msg = [Link]
in_port = [Link]['in_port']

[Link] 2/3
04/10/2017 RYU Controller Tutorial | SDN Hub
# Get the destination ethernet address
pkt = [Link]([Link])
eth = pkt.get_protocol([Link])
dst = [Link]
match = [Link](in_port=in_port, eth_dst=dst)

There are several other fields you can match. Here is an example of creating an action list for the flow.

actions = [ofp_parser.OFPActionOutput(ofp.OFPP_FLOOD)] # Build the required action

OpenFlow 1.3 associates set of instructions with each flow entry such as handling actions to modify/forward packets, support pipeline
processing instructions in the case of multi-table, metering instructions to rate-limit traffic etc. The previous set of actions defined in
OpenFlow 1.0 are one type of instructions defined in OpenFlow 1.3.

Once the match rule and action list is formed, instructions are created as follows:

inst = [[Link](ofproto.OFPIT_APPLY_ACTIONS, actions)]

Given the above code, a Flow can be generated and added to a particular switch.

mod = [Link](datapath=datapath, priority=0, match=match, instructions=inst)


datapath.send_msg(mod)

4. Sample application: MAC Hub or Learning Switch


For the purposes of this tutorial, you should attempt to build a hub and/or a MAC learning switch using the above code snippets. For the
main logic for hub and learning switch refer to [Link].

The implementation of the simple_switch_13.py application can be found under /usr/local/lib/python2.7/dist-packages/ryu/app. Once you
run this with the controller (as explained in the QuickStart section), you will see that mininet ping succeeds:

mininet> h1 ping h2
PING [Link] ([Link]) 56(84) bytes of data.
From [Link] icmp_seq=1 Destination Host Unreachable
From [Link] icmp_seq=2 Destination Host Unreachable
From [Link] icmp_seq=3 Destination Host Unreachable
From [Link] icmp_seq=4 Destination Host Unreachable
64 bytes from [Link]: icmp_req=5 ttl=64 time=0.529 ms
64 bytes from [Link]: icmp_req=6 ttl=64 time=0.133 ms
64 bytes from [Link]: icmp_req=7 ttl=64 time=0.047 ms

Contributor:
Sriram Natarajan, RYU Code Contributor
Any questions, email: natarajan(dot)sriram(at)gmail(dot)com

Copyright 2014 SDN Hub Terrifico Theme powered by WordPress

[Link] 3/3

Common questions

Powered by AI

The `topology` directory in RYU provides essential functionality for discovering network topology related to OpenFlow switches. It manages information about ports, links, and other infrastructure components, typically using the LLDP (Link Layer Discovery Protocol) for detecting link structures. This capability allows RYU applications to maintain an updated view of the network layout and enables efficient routing, traffic engineering, and resource allocation by understanding connectivity and switch interactions .

To start a network emulation environment using Mininet and connect it to the RYU controller supporting OpenFlow 1.3, you need to execute the following command to start Mininet: `sudo mn --topo single,3 --mac --controller remote --switch ovsk`. This sets up a network with one switch and three hosts. To ensure the switch operates with OpenFlow 1.3, issue the command `sudo ovs-vsctl set bridge s1 protocols=OpenFlow13`. To start the RYU Controller, navigate to its main folder and run: `cd /home/ubuntu/ryu && ./bin/ryu-manager --verbose ryu/app/simple_switch_13.py` .

Creating a new SDN application with RYU begins by sub-classing the `RyuApp` base class from `ryu.base.app_manager`. In the subclass, you initialize the application state and utilize decorators such as `@set_ev_cls` to subscribe to specific network events. Implement handler methods to process these events, incorporating logic to parse packets, send responses, or modify flows. Deployment involves placing the application in the RYU app directory and starting the RYU manager with a command pointing to the app, allowing it to interact with flow events and manage OpenFlow switches accordingly .

Flow modification in RYU is handled by constructing FLOW_MOD messages, allowing dynamic alteration of switch flows. This process involves creating match structures using `parser.OFPMatch` to identify packets by headers like in_port and eth_dst, determining packet handling. An action list is compiled, such as outputting to specific ports. Instructions like `parser.OFPInstructionActions` are configured to apply these actions. The flow entry, comprising match, priority, and instruction components, is prepared with `parser.OFPFlowMod` and sent to the switch using `datapath.send_msg(mod)`, altering network data handling efficiently .

OpenFlow 1.3 offers several advantages, especially for SDN applications developed with RYU, including comprehensive support for defining multiple table pipelines, enhancements in group tables, and meter tables enabling traffic monitoring and policy enforcement. OpenFlow 1.3 enables detailed flow matching and output actions which allow complex packet processing and better traffic control. The support for asynchronous messages improves how controllers handle real-time changes in network states, thus enhancing responsiveness and scalability .

In a RYU application, packet headers can be inspected by importing relevant packet libraries. You can use `from ryu.lib.packet import packet, ethernet` to access packet functionality. Once a packet is received, decode it using `pkt = packet.Packet(msg.data)` and inspect various protocols by retrieving headers like `eth = pkt.get_protocol(ethernet.ethernet)`. Then you can extract details such as source and destination using `dst = eth.dst, src = eth.src` .

Configuring switch protocols via the OpenFlow protocol version in an SDN environment defines how switches communicate with controllers, impacting compatibility, feature availability, and interoperability. OpenFlow versions like 1.3 include advanced protocol features such as multi-table pipelines and enhanced group table functionalities, offering more control and efficiency in managing network resources. Correctly setting the protocol ensures the negotiation between switches and controllers supports desired functionalities, allowing SDN applications to accurately implement their logic .

The RYU Controller listens for asynchronous network events by utilizing decorators like `@set_ev_cls`. This tells the application which events to handle, such as PACKET_IN or FLOW_REMOVED. For example, `@set_ev_cls(ofp_event.EventOFPPacketIn, MAIN_DISPATCHER)` decorates a method to handle incoming packets. The method retrieves the event message using `ev.msg` and processes it according to the application logic. The notion of dispatcher states allows managing how different message types are handled depending on switch status .

The `ofproto` directory within RYU is essential for managing different versions of the OpenFlow protocol by containing specific information and parsers for each version. This accommodation enables programmers to handle packet structures and events according to each protocol version's requirements, facilitating backward compatibility and support for newer features. This structural organization allows RYU applications to support multiple OpenFlow versions and ensures seamless interaction with various switch models .

Sending a PACKET_OUT OpenFlow message in a RYU-based application involves constructing a message with relevant packet information and actions. First, retrieve the packet and associated data using the event's `msg` attribute. Then, define actions using `ofp_parser.OFPActionOutput` to specify how the packet should be forwarded. Construct the PACKET_OUT message with `ofp_parser.OFPPacketOut` using parameters like `datapath`, input port, and actions. Finally, send the message to the switch using the `datapath.send_msg(out)` method, ensuring it follows the determined path .

You might also like