Experiment 2 Open vSwitch Virtual Switch Practice

Open vSwitch Virtual Switch Practice

Experiment purpose

  1. Basic operation of Open vSwitch
  2. You can use the OVS command to operate the Open vSwitch and manage the flow table through the command line terminal
  3. You can run the OVS command through Mininet's Python code to control the Open vSwitch in the network topology

(1) Basic Experiments

Experiment 1 Create OVS switch

1. OVS switch

Complete the screenshot

2. Test connectivity

Successful ping

Experiment 2: Mininet distribution flow table

1. Custom topology

Running Topology

Screenshot of pingall

2. VLAN division

View the screenshot of the command result of the OVS flow table:

wireshark verification:

H1 and h3 are in the same LAN

H2 and h4 are in the same LAN

(2) Advanced Experiment

Requirement: use Python code to realize VlAN partition


Write the topo.py file and run it

from mininet.net import Mininet
from mininet.node import Node
from mininet.link import Link
from mininet.log import  setLogLevel, info
def myNet():
    "Create network from scratch using Open vSwitch."
    info( "*** Creating nodes\n" )
    switch1 = Node( 's1', inNamespace=False )
    switch2 = Node( 's2', inNamespace=False )
    h1 = Node( 'h1' )
    h2 = Node( 'h2' )
    h3 = Node( 'h3' )
    h4 = Node( 'h4' )

    info( "*** Creating links\n" )
    Link( h1, switch1)
    Link( h2, switch1)
    Link( h3, switch2)
    Link( h4, switch2)
    Link( switch1, switch2)
    info( "*** Configuring hosts\n" )
    h1.setIP( '' )
    h2.setIP( '' )
    h3.setIP( '' )
    h4.setIP( '' )
    info( "*** Starting network using Open vSwitch\n" )
    switch1.cmd( 'ovs-vsctl del-br dp0' )
    switch1.cmd( 'ovs-vsctl add-br dp0' )

    switch2.cmd( 'ovs-vsctl del-br dp1' )
    switch2.cmd( 'ovs-vsctl add-br dp1' )

    for intf in switch1.intfs.values():
        print (intf)
        print (switch1.cmd( 'ovs-vsctl add-port dp0 %s' % intf ))

    for intf in switch2.intfs.values():
        print (intf)
        print (switch2.cmd( 'ovs-vsctl add-port dp1 %s' % intf ))
    print (switch1.cmd(r'ovs-vsctl show'))
    print (switch1.cmd(r'ovs-ofctl -O OpenFlow13 add-flow dp0 priority=1,in_port=1,actions=push_vlan:0x8100,set_field:4096-\>vlan_vid,output:3'))
    print (switch1.cmd(r'ovs-ofctl -O OpenFlow13 add-flow dp0 priority=1,in_port=2,actions=push_vlan:0x8100,set_field:4097-\>vlan_vid,output:3'))
    print (switch1.cmd(r'ovs-ofctl -O OpenFlow13 add-flow dp0 priority=1,dl_vlan=0,actions=pop_vlan,output:1'))
    print (switch1.cmd(r'ovs-ofctl -O OpenFlow13 add-flow dp0 priority=1,dl_vlan=1,actions=pop_vlan,output:2'))

    print (switch2.cmd(r'ovs-ofctl -O OpenFlow13 add-flow dp1 priority=1,in_port=1,actions=push_vlan:0x8100,set_field:4096-\>vlan_vid,output:3'))
    print (switch2.cmd(r'ovs-ofctl -O OpenFlow13 add-flow dp1 priority=1,in_port=2,actions=push_vlan:0x8100,set_field:4097-\>vlan_vid,output:3'))
    print (switch2.cmd(r'ovs-ofctl -O OpenFlow13 add-flow dp1 priority=1,dl_vlan=0,actions=pop_vlan,output:1'))
    print (switch2.cmd(r'ovs-ofctl -O OpenFlow13 add-flow dp1 priority=1,dl_vlan=1,actions=pop_vlan,output:2'))

    #switch0.cmd('tcpdump -i s0-eth0 -U -w aaa &')
    #h0.cmd('tcpdump -i h0-eth0 -U -w aaa &')
    info( "*** Running test\n" )
    h1.cmdPrint( 'ping -c 3 ' + h3.IP() )
    h2.cmdPrint( 'ping -c 3 ' + h4.IP() )
    h1.cmdPrint( 'ping -c 3 ' + h4.IP() )
    h2.cmdPrint( 'ping -c 3 ' + h3.IP() )

    #print switch0.cmd( 'ovs-ofctl show dp0' )    
    #print switch0.cmd( 'ovs-ofctl dump-tables  dp0' )
    #print switch0.cmd( 'ovs-ofctl dump-ports   dp0' )
    #print switch0.cmd( 'ovs-ofctl dump-flows  dp0' )
    #print switch0.cmd( 'ovs-ofctl dump-aggregate  dp0' )
    #print switch0.cmd( 'ovs-ofctl queue-stats dp0' )
    info( "*** Stopping network\n" )
    switch1.cmd( 'ovs-vsctl del-br dp0' )
    switch2.cmd( 'ovs-vsctl del-br dp1' )
    info( '\n' )
if __name__ == '__main__':
    setLogLevel( 'info' )
    info( '*** Scratch network demo (kernel datapath)\n' )

2. Run

3. Validation results

1. Code validation:

The results meet the requirements

2. wireshark verification

H1 and h3 are in the same LAN

H2 and h4 are in the same LAN


Experiment summary

1) This experiment relearned the knowledge about network level forwarding, such as

So we can re understand the VLAN format and the significance of various parameter settings during the experiment, (In fact, the value of set_field here is the value of setting TCI, not the value of VLAN ID. In fact, the VLAN ID of 4096 is 0, and the CFI bit must be set to 1. This is because OVS stipulates that the bit must be 1, while the OpenFlow standard does not indicate that it is a reserved field, but many OpenFlow switches specify that the bit must be 1 to take effect. In addition, when the configuration flow table value is 0x8100, it represents the IEEE 802.1Q VLAN data frame. If Devices that do not support 802.1Q will discard such frames after receiving them)

2) Through advanced experiments, I learned how to use python files to distribute stream tables, and can test in python files. I encountered many bug s in the process of writing python files, but I finally solved them through constant attempts, which improved my ability to solve problems and innovate. Also understand the different methods of network writing, and can be well applied to events.

3) During the experiment, we have a deeper understanding of some definitions of virtual LAN, and achieve the role of virtual LAN through the design of some tags

4) Some serious problems have led to a great extension of homework time. They are not confident to view the teaching documents. Instead, they are more likely to solve problems through the Internet, and the search efficiency is extremely slow. For example, in the process of using wireshark, they do not use the command line to capture packets, but use wireshark's own filter. This requires finding the switch port, and then looking at it, rather than directly calling wireshark in the process of running code, Extremely unsmooth in operation.


The experiment should be finished early, without delay.

Tags: SDN

Posted by obay on Tue, 20 Sep 2022 21:48:11 +0300