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