# SDN / Openflow Lab 2.7 – 2020
## Section 1.1
a) The version used for OpenFlow is 1.3.
b) The following capabilities are supported: FLOW_STATS, TABLE_STATS, PORT_STATS, QUEUE_STATS.
The following capabilities are not supported: GROUP_STATS, IP_REASM, PORT_BLOCKED.

## Section 1.4
a) The version used for OpenFlow is 1.3.

b) 9 ports are used.
c) Local port represents the switch's local networking stack and its management stack.

d) Only one flow is programmed.
e) Because it is the basic rule, it sends all the packets to the controller.

f) We found 494 flows.
g) Because flows are designed to stay alive only when in used, so after some time, they are deleted.
## Section 1.5
a) There is no impact on the ping.

b) See screenshots.
Before:

After:

c) The flow is now going through the switch `00:00:00:00:00:00:00:01`.
d) See screenshot.

## Section 2.1
d) `GET /of/datapaths`
e) `GET /net/links` with parameter `dpid: 00:00:00:00:00:00:00:02`
f) `GET /of/datapaths/{dpid}/ports` with parameter `dpid: 00:00:00:00:00:00:00:02`
## Section 2.2
a) Before: 
After: 
Script:
```bash
#!/bin/bash
#-------------------------------------------------------------------------------
# Copyright 2013-2016 Hewlett Packard Co., All Rights Reserved.
#-------------------------------------------------------------------------------
#
# Installs a detour through spine switch #1 for TCP port 80 traffic.
# Detour is installed back-wards, i.e. from last hop to first.
#
# Requires a valid session to be established via web-login command.
#
# written by Thomas Vachuska & Liem Nguyen
# modified by Bruno Hareng to be OF 1.3 compliant and to the controller 2.6
#
#-------------------------------------------------------------------------------
post - v2.0/of/datapaths/00:00:00:00:00:00:00:0e/flows '{
"flow": {
"priority": 30000,
"idle_timeout": 30,
"match": [
{"eth_type": "ipv4"},
{"ipv4_src": "10.0.0.1"},
{"ipv4_dst": "10.0.0.22"}
],
"instructions": [{"apply_actions":[{"output": 6}]}]
}
}'
post - v2.0/of/datapaths/00:00:00:00:00:00:00:02/flows '{
"flow": {
"priority": 30000,
"idle_timeout": 30,
"match": [
{"eth_type": "ipv4"},
{"ipv4_src": "10.0.0.1"},
{"ipv4_dst": "10.0.0.22"}
],
"instructions": [{"apply_actions":[{"output": 6}]}]
}
}'
post - v2.0/of/datapaths/00:00:00:00:00:00:00:01/flows '{
"flow": {
"priority": 30000,
"idle_timeout": 30,
"match": [
{"eth_type": "ipv4"},
{"ipv4_src": "10.0.0.1"},
{"ipv4_dst": "10.0.0.22"}
],
"instructions": [{"apply_actions":[{"output": 2}]}]
}
}'
post - v2.0/of/datapaths/00:00:00:00:00:00:00:0b/flows '{
"flow": {
"priority": 30000,
"idle_timeout": 30,
"match": [
{"eth_type": "ipv4"},
{"ipv4_src": "10.0.0.1"},
{"ipv4_dst": "10.0.0.22"}
],
"instructions": [{"apply_actions":[{"output": 1}]}]
}
}'
```
b) Blocking ping between h11 and h44:
```bash
#!/bin/bash
post - v2.0/of/datapaths/00:00:00:00:00:00:00:0b/flows '{
"flow": {
"priority": 30000,
"idle_timeout": 30,
"match": [
{"eth_type": "ipv4"},
{"ipv4_src": "10.0.0.1"},
{"ipv4_dst": "10.0.0.22"},
{"ip_proto": "icmp"}
],
"instructions": [{"apply_actions":[{"output": 0}]}]
}
}'
```
## Section 3
a) Port of long-detour in python :
```python
#!/usr/bin/env python
#
# Copyright 2013 Hewlett-Packard Development Company, L.P.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import hpsdnclient as hp
import os
""" Short Detour 2.0 """
#initialize the api
controller = os.getenv('SCA')
auth = hp.XAuthToken(user='sdn',password='skyline', server=controller)
api = hp.Api(controller=controller, auth=auth)
#create the match object
match = hp.datatypes.Match(eth_type="ipv4",ipv4_src="10.0.0.1",
ipv4_dst="10.0.0.22")
#create the action objects
output1 = hp.datatypes.Action(output=1)
output2 = hp.datatypes.Action(output=2)
output6 = hp.datatypes.Action(output=6)
#create the flows
flow1 = hp.datatypes.Flow(priority=30000, idle_timeout=30,
match=match, actions=output6)
flow2 = hp.datatypes.Flow(priority=30000, idle_timeout=30,
match=match, actions=output1)
flow3 = hp.datatypes.Flow(priority=30000, idle_timeout=30,
match=match, actions=output2)
#push the flows to the datatpaths
api.add_flows('00:00:00:00:00:00:00:0e', flow1)
api.add_flows('00:00:00:00:00:00:00:02', flow1)
api.add_flows('00:00:00:00:00:00:00:0b', flow2)
api.add_flows('00:00:00:00:00:00:00:01', flow3)
```
b) Port of block ping in python :
```python
#!/usr/bin/env python
#
# Copyright 2013 Hewlett-Packard Development Company, L.P.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import hpsdnclient as hp
import os
""" Short Detour 2.0 """
#initialize the api
controller = os.getenv('SCA')
auth = hp.XAuthToken(user='sdn',password='skyline', server=controller)
api = hp.Api(controller=controller, auth=auth)
#create the match object
match = hp.datatypes.Match(eth_type="ipv4",ipv4_src="10.0.0.1",
ipv4_dst="10.0.0.22", ip_proto="icmp")
#create the action objects
output0 = hp.datatypes.Action(output=0)
#create the flows
flow1 = hp.datatypes.Flow(priority=30000, idle_timeout=30,
match=match, actions=output0)
#push the flows to the datatpaths
api.add_flows('00:00:00:00:00:00:00:0b', flow1)
```
c) Lockout of an ip :
```python
#!/usr/bin/env python
import argparse
import os
import hpsdnclient as hp
from hpsdnclient.datatypes import Flow, Match, Action
def main():
parser = argparse.ArgumentParser()
parser.add_argument('--ip', type=str,
help="The IP Address to kill",
required=True)
args = parser.parse_args()
kill_flow(args.ip)
def kill_flow(ip):
#Authenticate to the controller
controller = os.getenv('SCA')
auth = hp.XAuthToken(user='sdn',password='skyline', server=controller)
api = hp.Api(controller=controller, auth=auth)
#Define your match, action and your flow
match = hp.datatypes.Match(eth_type="ipv4", ipv4_src=ip)
action = hp.datatypes.Action(output=0)
flow = hp.datatypes.Flow(priority=30000, idle_timeout=30,
match=match, actions=action)
datapaths = api.get_datapaths()
for d in datapaths:
api.add_flows(d.dpid, flow)
if __name__ == "__main__":
main()
```
## Section 4
```python
#!/usr/bin/env python
#
# Copyright 2013 Hewlett-Packard Development Company, L.P.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import hpsdnclient as hp
import os
""" Short Detour 2.0 """
#initialize the api
controller = os.getenv('SCA')
auth = hp.XAuthToken(user='sdn',password='skyline', server=controller)
api = hp.Api(controller=controller, auth=auth)
#create the action objects
outputs = [
hp.datatypes.Action(output=0),
hp.datatypes.Action(output=3),
hp.datatypes.Action(output=4),
hp.datatypes.Action(output=5),
hp.datatypes.Action(output=6),
hp.datatypes.Action(output=7),
hp.datatypes.Action(output=8)
]
#push the flows to the datapaths
#drop all traffic to web servers
for i in range(1, 7):
match = hp.datatypes.Match(eth_type="ipv4", ipv4_dst="10.0.0.{}".format(i))
flow = hp.datatypes.Flow(priority=30000, idle_timeout=30,
match=match, actions=outputs[0])
api.add_flows('00:00:00:00:00:00:00:0b', flow)
#authorize web traffic from clients to servers
for i in range(10, 17):
for j in range(1, 7):
match = hp.datatypes.Match(eth_type="ipv4", ipv4_src="10.0.0.{}".format(i),
ipv4_dst="10.0.0.{}".format(j), ip_proto="tcp",
tcp_dst="80")
flow = hp.datatypes.Flow(priority=60000, idle_timeout=30,
match=match, actions=outputs[j])
api.add_flows('00:00:00:00:00:00:00:0b', flow)
```