---
# System prepended metadata

title: Mininet OVS 4 (Group Select 的用法
tags: [SDN, data structure]

---

# Mininet OVS 4

Action Bucket


# Topology 

![](https://hackmd.io/_uploads/BJSJPtbY3.png)



## Node/Switch/Controller Configuration



Adding Node and switch 

```
 # Add hosts and switches
h1= net.addHost( 'h1', mac="00:00:00:00:00:01" )
h2 = net.addHost( 'h2', mac="00:00:00:00:00:02" )
 
s1 = net.addSwitch( 's1', protocols=["OpenFlow10,OpenFlow13"], listenPort=6634 )
s2 = net.addSwitch( 's2', protocols=["OpenFlow10,OpenFlow13"], listenPort=6635 )
s3 = net.addSwitch( 's3', protocols=["OpenFlow10,OpenFlow13"], listenPort=6636 )
s4 = net.addSwitch( 's4', protocols=["OpenFlow10,OpenFlow13"], listenPort=6637 )
 
c0 = net.addController( 'c0', controller=RemoteController, ip='127.0.0.1', port=6633 )

```

switch 後面可設定 使用的openflow 版本 (1.0,1.3)



### Establish Connections 

```
net.addLink( h1, s1)
net.addLink( h2, s4)
net.addLink( s1, s2)
net.addLink( s1, s3)
net.addLink( s2, s4)
net.addLink( s3, s4)
```

# Flows Configuration



```
## S2,S3,S4 routing setting 
ovs-ofctl -O OpenFlow13 add-flow s2 in_port=1,actions=output:2
ovs-ofctl -O OpenFlow13 add-flow s2 in_port=2,actions=output:1
ovs-ofctl -O OpenFlow13 add-flow s3 in_port=1,actions=output:2
ovs-ofctl -O OpenFlow13 add-flow s3 in_port=2,actions=output:1
ovs-ofctl -O OpenFlow13 add-flow s4 in_port=2,actions=output:1
ovs-ofctl -O OpenFlow13 add-flow s4 in_port=3,actions=output:1
ovs-ofctl -O OpenFlow13 add-flow s4 in_port=1,actions=output:3
ovs-ofctl -O OpenFlow13 add-flow s1 in_port=2,actions=output:1
ovs-ofctl -O OpenFlow13 add-flow s1 in_port=3,actions=output:1


ovs-ofctl -O OpenFlow13 add-group s1 group_id=5,type=select,bucket=output:2,bucket=output:3
ovs-ofctl -O OpenFlow13 add-flow s1 in_port=1,actions=group:5

```

select -> Using round robin Algorithm 
依序倫巡
bucket1
bucket2
bucket1
bucket2

會輪替那兩個bucket 

## Show S1 Flows 


```
root@ubuntu:/home/user# ovs-ofctl -O OpenFlow13 dump-flows s1
OFPST_FLOW reply (OF1.3) (xid=0x2):
 cookie=0x0, duration=143.488s, table=0, n_packets=2, n_bytes=214, in_port=2 actions=output:1
 cookie=0x0, duration=143.476s, table=0, n_packets=2, n_bytes=214, in_port=3 actions=output:1
 cookie=0x0, duration=141.776s, table=0, n_packets=0, n_bytes=0, in_port=1 actions=group:0

```

## Show S1 group 

```
root@ubuntu:/home/user# ovs-ofctl -O OpenFlow13 dump-groups s1
OFPST_GROUP_DESC reply (OF1.3) (xid=0x2):
 group_id=0,type=select,bucket=actions=output:2,bucket=actions=output:3
```

## Show S1 port (Interface)

```
root@ubuntu:/home/user# ovs-ofctl -O OpenFlow13 dump-ports s1
OFPST_PORT reply (OF1.3) (xid=0x2): 4 ports
  port LOCAL: rx pkts=0, bytes=0, drop=0, errs=0, frame=0, over=0, crc=0
           tx pkts=0, bytes=0, drop=0, errs=0, coll=0
           duration=1266.012s
  port  1: rx pkts=10, bytes=828, drop=0, errs=0, frame=0, over=0, crc=0
           tx pkts=31, bytes=3701, drop=0, errs=0, coll=0
           duration=1266.023s
  port  2: rx pkts=28, bytes=3368, drop=0, errs=0, frame=0, over=0, crc=0
           tx pkts=27, bytes=3261, drop=0, errs=0, coll=0
           duration=1266.025s
  port  3: rx pkts=28, bytes=3368, drop=0, errs=0, frame=0, over=0, crc=0
           tx pkts=27, bytes=3261, drop=0, errs=0, coll=0
           duration=1266.021s

```

我們可以觀察 port 2/3 的TX 來看select Group 有沒有生效

## Testing 


### First TCP Connection 

![](https://hackmd.io/_uploads/r1HaCtbKn.png)


```
root@ubuntu:/home/user# ovs-ofctl -O OpenFlow13 dump-ports s1
OFPST_PORT reply (OF1.3) (xid=0x2): 4 ports
  port LOCAL: rx pkts=0, bytes=0, drop=0, errs=0, frame=0, over=0, crc=0
           tx pkts=0, bytes=0, drop=0, errs=0, coll=0
           duration=1473.441s
  port  1: rx pkts=608409, bytes=27878698642, drop=0, errs=0, frame=0, over=0, crc=0
           tx pkts=339387, bytes=22499857, drop=0, errs=0, coll=0
           duration=1473.452s
  port  2: rx pkts=28, bytes=3368, drop=0, errs=0, frame=0, over=0, crc=0
           tx pkts=608424, bytes=27878700991, drop=0, errs=0, coll=0
           duration=1473.454s
  port  3: rx pkts=339384, bytes=22499524, drop=0, errs=0, frame=0, over=0, crc=0
           tx pkts=29, bytes=3345, drop=0, errs=0, coll=0
           duration=1473.450s
```

port 2 tx=606424 (Select!!)
port 3 tx=29

### Second TCP Connection


![](https://hackmd.io/_uploads/H1QWJ5WY3.png)
```
root@ubuntu:/home/user# ovs-ofctl -O OpenFlow13 dump-ports s1
OFPST_PORT reply (OF1.3) (xid=0x2): 4 ports
  port LOCAL: rx pkts=0, bytes=0, drop=0, errs=0, frame=0, over=0, crc=0
           tx pkts=0, bytes=0, drop=0, errs=0, coll=0
           duration=2089.767s
  port  1: rx pkts=2388371, bytes=108396565758, drop=0, errs=0, frame=0, over=0, crc=0
           tx pkts=1367002, bytes=90486448, drop=0, errs=0, coll=0
           duration=2089.778s
  port  2: rx pkts=30, bytes=3582, drop=0, errs=0, frame=0, over=0, crc=0
           tx pkts=1818416, bytes=82458437824, drop=0, errs=0, coll=0
           duration=2089.780s
  port  3: rx pkts=1366996, bytes=90485794, drop=0, errs=0, frame=0, over=0, crc=0
           tx pkts=570001, bytes=25938133842, drop=0, errs=0, coll=0
           duration=2089.776s
```

group_id 設成0 會有問題

## script 


```
from mininet.net import Mininet
from mininet.node import Controller, RemoteController, OVSKernelSwitch, UserSwitch, OVSSwitch
from mininet.cli import CLI
from mininet.log import setLogLevel
from mininet.link import Link, TCLink
 
def topology():
        net = Mininet( controller=RemoteController, link=TCLink, switch=OVSKernelSwitch)
 
        # Add hosts and switches
        h1= net.addHost( 'h1', mac="00:00:00:00:00:01" )
        h2 = net.addHost( 'h2', mac="00:00:00:00:00:02" )
 
        s1 = net.addSwitch( 's1', protocols=["OpenFlow10,OpenFlow13"], listenPort=6634 )
        s2 = net.addSwitch( 's2', protocols=["OpenFlow10,OpenFlow13"], listenPort=6635 )
        s3 = net.addSwitch( 's3', protocols=["OpenFlow10,OpenFlow13"], listenPort=6636 )
        s4 = net.addSwitch( 's4', protocols=["OpenFlow10,OpenFlow13"], listenPort=6637 )
 
        c0 = net.addController( 'c0', controller=RemoteController, ip='127.0.0.1', port=6633 )
 
        net.addLink( h1, s1)
        net.addLink( h2, s4)
        net.addLink( s1, s2)
        net.addLink( s1, s3)
        net.addLink( s2, s4)
        net.addLink( s3, s4)
        net.build()
        c0.start()
        s1.start( [c0] )
        s2.start( [c0] )
        s3.start( [c0] )
        s4.start( [c0] )
 
        print "*** Running CLI"
        CLI( net )
 
        print "*** Stopping network"
        net.stop()
 
if __name__ == '__main__':
    setLogLevel( 'info' )
    topology()

```

## Rules 

```
ovs-ofctl -O OpenFlow13 add-flow s2 in_port=1,actions=output:2
ovs-ofctl -O OpenFlow13 add-flow s2 in_port=2,actions=output:1
ovs-ofctl -O OpenFlow13 add-flow s3 in_port=1,actions=output:2
ovs-ofctl -O OpenFlow13 add-flow s3 in_port=2,actions=output:1
ovs-ofctl -O OpenFlow13 add-flow s4 in_port=2,actions=output:1
ovs-ofctl -O OpenFlow13 add-flow s4 in_port=3,actions=output:1
ovs-ofctl -O OpenFlow13 add-flow s4 in_port=1,actions=output:3
ovs-ofctl -O OpenFlow13 add-flow s1 in_port=2,actions=output:1
ovs-ofctl -O OpenFlow13 add-flow s1 in_port=3,actions=output:1
ovs-ofctl -O OpenFlow13 add-group s1 group_id=5,type=select,bucket=output:2,bucket=output:3
ovs-ofctl -O OpenFlow13 add-flow s1 in_port=1,actions=group:5

```

UDP 沒辦法使用 select grupd function 來做 load blance !!