0% found this document useful (0 votes)
3 views10 pages

SDN Lab - Python1

This document outlines the steps to create a network topology using Mininet and the POX controller, including starting the controller, defining a topology with one switch and three hosts, and verifying connectivity. It also provides Python code for a simple firewall application and instructions for building an L2 Learning Switch application using the Ryu SDN Controller. The document includes commands for running the experiments and checking flow table rules, ensuring a comprehensive guide for setting up and testing SDN applications.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views10 pages

SDN Lab - Python1

This document outlines the steps to create a network topology using Mininet and the POX controller, including starting the controller, defining a topology with one switch and three hosts, and verifying connectivity. It also provides Python code for a simple firewall application and instructions for building an L2 Learning Switch application using the Ryu SDN Controller. The document includes commands for running the experiments and checking flow table rules, ensuring a comprehensive guide for setting up and testing SDN applications.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

2. Creating a 1 controller, 3 node topology, POX controller.

Step-by-Step Procedure
To conduct this experiment, follow these steps to integrate Mininet and the POX controller:
1. Start the POX Controller: Open a terminal and navigate to the POX directory. Execute
the controller script with a basic forwarding component (like l2_learning) so the
switch can handle traffic.
 Command: sudo python [Link] forwarding.l2_learning
 You should see INFO:core:POX 0.x.x is up in the Scribd SDN Lab Manual.
2. Create the Python Topology Script: Write a Python script using the Mininet library to
define the 1-switch, 3-host topology.
3. Run the Topology: In a separate terminal, run your Python script using sudo python
<your_script_name>.py. This script will connect to the "Remote Controller" (POX)
running on the default port 6633.
4. Verify Connectivity: Once the Mininet CLI (mininet>) appears, use commands
like pingall to verify that all three nodes can communicate through the POX
controller.
5. Examine Traffic: Use the nodes or net commands to confirm the structure of 1 switch,
3 hosts, and 1 controller

Python Code for Topology (3node_topo.py)


from [Link] import Mininet
from [Link] import RemoteController, OVSSwitch
from [Link] import CLI
from [Link] import setLogLevel

def customTopology():
# Initialize Mininet with a Remote Controller (POX)
net = Mininet(controller=RemoteController, switch=OVSSwitch)

print("*** Adding Controller")


[Link]('c0', port=6633)
print("*** Adding Hosts")
h1 = [Link]('h1', ip='[Link]')
h2 = [Link]('h2', ip='[Link]')
h3 = [Link]('h3', ip='[Link]')

print("*** Adding Switch")


s1 = [Link]('s1')

print("*** Creating Links")


[Link](h1, s1)
[Link](h2, s1)
[Link](h3, s1)

print("*** Starting Network")


[Link]()

print("*** Running CLI")


CLI(net)

print("*** Stopping Network")


[Link]()

if __name__ == '__main__':
setLogLevel('info')
customTopology() EXPECTED OUTPUT
OUTPUT
After running the script and performing a pingall in the CLI, your terminal should display:
 Mininet Initialization:
text
*** Adding Controller
*** Adding Hosts
*** Adding Switch
*** Creating Links
*** Starting Network
*** Starting 1 switches
*** Starting CLI
mininet>

Connectivity Test (pingall):


text
mininet> pingall
*** Ping: testing ping reachability
h1 -> h2 h3
h2 -> h1 h3
h3 -> h1 h2
*** Results: 0% dropped (6/6 received)
Controller Logs:
The POX terminal will show INFO:openflow.of_01:[00-00-00-00-00-01 1]
connected indicating the switch has successfully connected to the controller.
Execution Guide
 Running the Script: Save the code above as topo_3node.py. Ensure your POX
controller is already running in a separate terminal. Then, execute the script
with: sudo python3 topo_3node.py.
 Verification: Use the Mininet Setup and Basic Commands Guide (Scribd) to
troubleshoot any connection issues, such as ensuring the remote controller port
(default 6633) is not blocked.
ABILITY TO VIEW, READ/WRITE FLOW TABLE RULES (FOR DIFFERENT
APPLICATIONS - SAY
FIREWALL, LEARNING SWITCH ETC.), POX, OPEN VSWITCH

1. Step-by-Step Procedure
To conduct this experiment, follow these steps to set up the topology and manage flow
entries:
1. Start the Network Topology:
Launch Mininet with a single switch and three hosts, connecting to a remote
controller.
bash
sudo mn --topo single,3 --mac --switch ovsk --controller remote
2. Start the POX Controller:
In a new terminal window, navigate to your POX directory and run the L2 learning
module. You can find detailed setup instructions in the SDN Lab Manual.
bash
./[Link] forwarding.l2_learning
3. Verify Initial Connectivity:
Back in the Mininet CLI, test the reachability between hosts.
bash
mininet> h1 ping h2
4. View Flow Table Rules:
Open a third terminal to inspect the flow entries installed by the controller on
switch s1 using Open vSwitch commands.
bash
sudo ovs-ofctl dump-flows s1
5. Write Manual Rules (Firewall Example):
You can manually insert a rule to drop all traffic from h1 (MAC 00:00:00:00:00:01)
to simulate a basic firewall.
bash
sudo ovs-ofctl add-flow s1 priority=100,dl_src=00:00:00:00:00:01,actions=dro
PROGRAM
from [Link] import core
import [Link].libopenflow_01 as of

log = [Link]()

class SimpleFirewall (object):


def __init__ (self, connection):
[Link] = connection
[Link](self)

def _handle_PacketIn (self, event):


packet = [Link]()

# Example Rule: Block traffic from h1 (00:00:00:00:00:01)


if [Link] == '00:00:00:00:00:01':
[Link]("Blocking traffic from %s", [Link])
msg = of.ofp_flow_mod()
[Link].dl_src = [Link]
[Link](of.ofp_action_output(port = of.OFPP_NONE)) # Drop action
[Link](msg)
return

# Default: Learning Switch behavior (simplified)


msg = of.ofp_packet_out()
[Link] = [Link]
[Link](of.ofp_action_output(port = of.OFPP_FLOOD))
[Link](msg)

def launch ():


def start_switch (event):
[Link]("Controlling %s" % ([Link],))
SimpleFirewall([Link])
[Link]("ConnectionUp", start_switch)

OUTPUT

 Mininet Terminal: When h1 tries to ping h2, the ping will fail
(Destination Host Unreachable) because of the firewall rule.
 POX Terminal: You will see log messages such as:
INFO:Blocking traffic from 00:00:00:00:00:01
 OVS Flow Dump: Running sudo ovs-ofctl dump-flows s1 will display the
manual or controller-inserted rules, appearing similar to this according
to Scribd's SDN Lab Guide:
text
cookie=0x0, duration=10.123s, table=0, n_packets=5, n_bytes=490,
priority=100,dl_src=00:00:00:00:00:01 actions=drop
BUILDING A SDN BASED APPLICATION
This experiment demonstrates building a simple L2 Learning Switch
application using the Python-based Ryu SDN Controller and testing it with
a Mininet network topology.

. Prerequisites
Ensure you have the following installed (typically on an Ubuntu VM):
 Mininet
 Ryu Controller (pip install ryu)
 Python 3
1. Ryu SDN Application Code
# Save this file as l2_learning_switch.py
from [Link] import app_manager
from [Link] import ofp_event
from [Link] import CONFIG_DISPATCHER,
MAIN_DISPATCHER
from [Link] import set_ev_cls
from [Link] import ofproto_v1_3
from [Link] import packet
from [Link] import ethernet

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 = {}

@set_ev_cls(ofp_event.EventOFPSwitchFeatures,
CONFIG_DISPATCHER)
def switch_features_handler(self, ev):
datapath = [Link]
ofproto = [Link]
parser = datapath.ofproto_parser

# Install table-miss flow entry


match = [Link]()
actions = [[Link](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 = [Link]
parser = datapath.ofproto_parser

inst =
[[Link](ofproto.OFPIT_APPLY_ACTIONS,
actions)]
if buffer_id:
mod = [Link](datapath=datapath,
buffer_id=buffer_id,
priority=priority, match=match,
instructions=inst)
else:
mod = [Link](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):
msg = [Link]
datapath = [Link]
ofproto = [Link]
parser = datapath.ofproto_parser
in_port = [Link]['in_port']

pkt = [Link]([Link])
eth = pkt.get_protocols([Link])[0]

dst = [Link]
src = [Link]

dpid = format([Link], "d").zfill(16)


self.mac_to_port.setdefault(dpid, {})

[Link]("packet in %s %s %s %s", dpid, src, dst, in_port)


# Learn a mac address to avoid FLOOD next time.
self.mac_to_port[dpid][src] = in_port

# If the destination mac address is already learned,


# decide which port to output the packet, otherwise flood.
if dst in self.mac_to_port[dpid]:
out_port = self.mac_to_port[dpid][dst]
else:
out_port = ofproto.OFPP_FLOOD

actions = [[Link](out_port)]

# Install a flow to avoid packet_in next time


if out_port != ofproto.OFPP_FLOOD:
match = [Link](in_port=in_port, eth_dst=dst)
self.add_flow(datapath, 1, match, actions)

data = None
if msg.buffer_id == ofproto.OFP_NO_BUFFER:
data = [Link]

out = [Link](datapath=datapath,
buffer_id=msg.buffer_id,
in_port=in_port, actions=actions, data=data)
datapath.send_msg(out)

3. Running the Experiment


1. Terminal 1 (Run Controller):
bash
ryu-manager l2_learning_switch.py
Terminal 2 (Run Mininet):
Create a topology with one switch and three hosts:
bash
sudo mn --topo single,3 --mac --switch ovsk --controller remote

Terminal 2 (Test Connectivity):


In the mininet> prompt, run:
bash
mininet> pingall
4. Output

Ryu Controller Log (Terminal 1):


You will see packet-in events showing the switch learning MAC
addresses.
loading app l2_learning_switch.py
...
[INFO] packet in 0000000000000001 00:00:00:00:00:01
00:00:00:00:00:02 1
[INFO] packet in 0000000000000001 00:00:00:00:00:02
00:00:00:00:00:01 2

Mininet CLI (Terminal 2):


text
mininet> pingall
*** Ping: testing ping reachability
h1 -> h2 h3
h2 -> h1 h3
h3 -> h1 h2
*** Results: 0% dropped (6/6 received)

You might also like