Chetan Gaonker | 7ab338c | 2016-04-15 17:23:17 -0700 | [diff] [blame] | 1 | import unittest |
| 2 | from nose.tools import * |
| 3 | from nose.twistedtools import reactor, deferred |
| 4 | from twisted.internet import defer |
| 5 | from scapy.all import * |
| 6 | import time |
| 7 | import json |
| 8 | import threading |
Chetan Gaonker | 7ab338c | 2016-04-15 17:23:17 -0700 | [diff] [blame] | 9 | from OnosCtrl import OnosCtrl |
Chetan Gaonker | 3533faa | 2016-04-25 17:50:14 -0700 | [diff] [blame] | 10 | from OnosFlowCtrl import OnosFlowCtrl, get_mac |
Chetan Gaonker | 7ab338c | 2016-04-15 17:23:17 -0700 | [diff] [blame] | 11 | from OltConfig import OltConfig |
| 12 | log.setLevel('INFO') |
| 13 | |
Chetan Gaonker | 7ab338c | 2016-04-15 17:23:17 -0700 | [diff] [blame] | 14 | class flows_exchange(unittest.TestCase): |
| 15 | |
| 16 | #Use the first available device id as our device id to program flows |
| 17 | app = 'org.onosproject.cli' |
| 18 | PORT_TX_DEFAULT = 2 |
| 19 | PORT_RX_DEFAULT = 1 |
| 20 | INTF_TX_DEFAULT = 'veth2' |
| 21 | INTF_RX_DEFAULT = 'veth0' |
| 22 | default_port_map = { |
| 23 | PORT_TX_DEFAULT : INTF_TX_DEFAULT, |
| 24 | PORT_RX_DEFAULT : INTF_RX_DEFAULT, |
| 25 | INTF_TX_DEFAULT : PORT_TX_DEFAULT, |
| 26 | INTF_RX_DEFAULT : PORT_RX_DEFAULT |
| 27 | } |
| 28 | |
| 29 | @classmethod |
| 30 | def setUpClass(cls): |
| 31 | cls.olt = OltConfig() |
| 32 | cls.port_map = cls.olt.olt_port_map() |
| 33 | if not cls.port_map: |
| 34 | cls.port_map = cls.default_port_map |
Chetan Gaonker | a9b6fcb | 2016-04-15 17:35:24 -0700 | [diff] [blame] | 35 | cls.device_id = 'of:' + get_mac() ##match against our device id |
Chetan Gaonker | 7ab338c | 2016-04-15 17:23:17 -0700 | [diff] [blame] | 36 | |
| 37 | def test_flow_mac(self): |
| 38 | '''Add and verify flows with MAC selectors''' |
| 39 | egress = 1 |
| 40 | ingress = 2 |
| 41 | egress_mac = '00:00:00:00:00:01' |
| 42 | ingress_mac = '00:00:00:00:00:02' |
| 43 | flow = OnosFlowCtrl(deviceId = self.device_id, |
| 44 | egressPort = egress, |
| 45 | ingressPort = ingress, |
| 46 | ethSrc = ingress_mac, |
| 47 | ethDst = egress_mac) |
| 48 | result = flow.addFlow() |
| 49 | assert_equal(result, True) |
| 50 | ##wait for flows to be added to ONOS |
| 51 | time.sleep(3) |
| 52 | self.success = False |
| 53 | def mac_recv_task(): |
| 54 | def recv_cb(pkt): |
| 55 | log.info('Pkt seen with ingress mac %s, egress mac %s' %(pkt.src, pkt.dst)) |
| 56 | self.success = True |
| 57 | sniff(count=2, timeout=5, lfilter = lambda p: p.src == ingress_mac, |
| 58 | prn = recv_cb, iface = self.port_map[egress]) |
| 59 | |
| 60 | t = threading.Thread(target = mac_recv_task) |
| 61 | t.start() |
| 62 | pkt = Ether(src = ingress_mac, dst = egress_mac)/IP() |
| 63 | log.info('Sending a packet to verify if flows are correct') |
| 64 | sendp(pkt, count=50, iface = self.port_map[ingress]) |
| 65 | t.join() |
| 66 | assert_equal(self.success, True) |
| 67 | |
| 68 | def test_flow_ip(self): |
| 69 | '''Add and verify flows with IPv4 selectors''' |
| 70 | egress = 1 |
| 71 | ingress = 2 |
| 72 | egress_map = { 'ether': '00:00:00:00:00:03', 'ip': '192.168.30.1' } |
| 73 | ingress_map = { 'ether': '00:00:00:00:00:04', 'ip': '192.168.40.1' } |
| 74 | flow = OnosFlowCtrl(deviceId = self.device_id, |
| 75 | egressPort = egress, |
| 76 | ingressPort = ingress, |
| 77 | ethType = '0x0800', |
| 78 | ipSrc = ('IPV4_SRC', ingress_map['ip']+'/32'), |
| 79 | ipDst = ('IPV4_DST', egress_map['ip']+'/32') |
| 80 | ) |
| 81 | result = flow.addFlow() |
| 82 | assert_equal(result, True) |
| 83 | ##wait for flows to be added to ONOS |
| 84 | time.sleep(3) |
| 85 | self.success = False |
| 86 | def mac_recv_task(): |
| 87 | def recv_cb(pkt): |
| 88 | log.info('Pkt seen with ingress ip %s, egress ip %s' %(pkt[IP].src, pkt[IP].dst)) |
| 89 | self.success = True |
| 90 | sniff(count=2, timeout=5, |
Chetan Gaonker | 3533faa | 2016-04-25 17:50:14 -0700 | [diff] [blame] | 91 | lfilter = lambda p: IP in p and p[IP].dst == egress_map['ip'] and p[IP].src == ingress_map['ip'], |
Chetan Gaonker | 7ab338c | 2016-04-15 17:23:17 -0700 | [diff] [blame] | 92 | prn = recv_cb, iface = self.port_map[egress]) |
| 93 | |
| 94 | t = threading.Thread(target = mac_recv_task) |
| 95 | t.start() |
| 96 | L2 = Ether(src = ingress_map['ether'], dst = egress_map['ether']) |
| 97 | L3 = IP(src = ingress_map['ip'], dst = egress_map['ip']) |
| 98 | pkt = L2/L3 |
| 99 | log.info('Sending a packet to verify if flows are correct') |
| 100 | sendp(pkt, count=50, iface = self.port_map[ingress]) |
| 101 | t.join() |
| 102 | assert_equal(self.success, True) |