blob: 594b4f9010e15ca66b4317bc36c3f8445868aad9 [file] [log] [blame]
A R Karthick338268f2016-06-21 17:12:13 -07001#
Chetan Gaonkercfcce782016-05-10 10:10:42 -07002# Copyright 2016-present Ciena Corporation
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
A R Karthick338268f2016-06-21 17:12:13 -07007#
Chetan Gaonkercfcce782016-05-10 10:10:42 -07008# http://www.apache.org/licenses/LICENSE-2.0
A R Karthick338268f2016-06-21 17:12:13 -07009#
Chetan Gaonkercfcce782016-05-10 10:10:42 -070010# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15#
Chetan Gaonkerb424ff82016-03-08 12:11:12 -080016import threading
17import sys
18import os
19import time
20import monotonic
21import random
A.R Karthick2e99c472017-03-22 19:13:51 -070022import logging
23logging.getLogger('scapy.runtime').setLevel(logging.ERROR)
Chetan Gaonkerb424ff82016-03-08 12:11:12 -080024from scapy.all import *
25from McastTraffic import *
26from IGMP import *
27from OnosCtrl import OnosCtrl
A R Karthick76a497a2017-04-12 10:59:39 -070028from CordTestUtils import log_test
Chetan Gaonkerb424ff82016-03-08 12:11:12 -080029from nose.tools import *
A R Karthick76a497a2017-04-12 10:59:39 -070030log_test.setLevel('DEBUG')
Chetan Gaonkerb424ff82016-03-08 12:11:12 -080031
32conf.verb = 0
33
34class IgmpChannel:
35
36 IGMP_DST_MAC = "01:00:5e:00:01:01"
37 IGMP_SRC_MAC = "5a:e1:ac:ec:4d:a1"
38 IP_SRC = '1.2.3.4'
39 IP_DST = '224.0.1.1'
40 igmp_eth = Ether(dst = IGMP_DST_MAC, src = IGMP_SRC_MAC, type = ETH_P_IP)
41 igmp_ip = IP(dst = IP_DST, src = IP_SRC)
A R Karthick338268f2016-06-21 17:12:13 -070042 ssm_list = []
Chetan Gaonkerb424ff82016-03-08 12:11:12 -080043
Thangavelu K S8e413082017-07-13 20:02:14 +000044 def __init__(self, iface = 'veth0', ssm_list = [], src_list = None, delay = 2,controller=None):
45
ChetanGaonker689b3862016-10-17 16:25:01 -070046 self.controller=controller
Chetan Gaonkerb424ff82016-03-08 12:11:12 -080047 self.iface = iface
Chetan Gaonkercd86bdd2016-03-17 00:08:12 -070048 self.ssm_list += ssm_list
Thangavelu K S8e413082017-07-13 20:02:14 +000049 if src_list is None:
50 self.src_list = ['1.2.3.4']
51 else:
52 self.src_list = src_list
Chetan Gaonkerb424ff82016-03-08 12:11:12 -080053 self.delay = delay
A.R Karthick401a1ed2017-05-18 11:08:27 -070054 self.onos_ctrl = OnosCtrl('org.opencord.igmp',controller=self.controller)
Chetan Gaonkerb424ff82016-03-08 12:11:12 -080055 self.onos_ctrl.activate()
A R Karthick338268f2016-06-21 17:12:13 -070056
Thangavelu K S8e413082017-07-13 20:02:14 +000057 def igmp_load_ssm_config(self, ssm_list = [], src_list = None):
A R Karthick2f4e0ba2017-07-17 10:55:36 -070058 if src_list is None:
59 src_list = self.src_list
Chetan Gaonkercd86bdd2016-03-17 00:08:12 -070060 if not ssm_list:
61 ssm_list = self.ssm_list
Thangavelu K S8e413082017-07-13 20:02:14 +000062 self.ssm_table_load(ssm_list, src_list = src_list)
Chetan Gaonkerb424ff82016-03-08 12:11:12 -080063
Thangavelu K S8e413082017-07-13 20:02:14 +000064 def igmp_join(self, groups, src_list = None, record_type = None):
A R Karthick2f4e0ba2017-07-17 10:55:36 -070065 if src_list is None:
66 src_list = self.src_list
Thangavelu K S8e413082017-07-13 20:02:14 +000067 if record_type is None:
68 record_type = IGMP_V3_GR_TYPE_INCLUDE
Chetan Gaonkerb424ff82016-03-08 12:11:12 -080069 igmp = IGMPv3(type = IGMP_TYPE_V3_MEMBERSHIP_REPORT, max_resp_code=30,
70 gaddr='224.0.1.1')
71 for g in groups:
Thangavelu K S8e413082017-07-13 20:02:14 +000072 gr = IGMPv3gr(rtype=record_type, mcaddr=g)
73 gr.sources = src_list
Chetan Gaonkerb424ff82016-03-08 12:11:12 -080074 igmp.grps.append(gr)
75
76 pkt = self.igmp_eth/self.igmp_ip/igmp
77 IGMPv3.fixup(pkt)
78 sendp(pkt, iface=self.iface)
79 if self.delay != 0:
80 time.sleep(self.delay)
81
Thangavelu K S8e413082017-07-13 20:02:14 +000082 def igmp_leave(self, groups, src_list = None):
A R Karthick2f4e0ba2017-07-17 10:55:36 -070083 if src_list is None:
84 src_list = self.src_list
Chetan Gaonkerb424ff82016-03-08 12:11:12 -080085 igmp = IGMPv3(type = IGMP_TYPE_V3_MEMBERSHIP_REPORT, max_resp_code=30,
86 gaddr='224.0.1.1')
87 for g in groups:
Chetan Gaonker38737f82016-05-11 17:44:17 -070088 gr = IGMPv3gr(rtype=IGMP_V3_GR_TYPE_EXCLUDE, mcaddr=g)
Thangavelu K S8e413082017-07-13 20:02:14 +000089 gr.sources = src_list
Chetan Gaonkerb424ff82016-03-08 12:11:12 -080090 igmp.grps.append(gr)
91
92 pkt = self.igmp_eth/self.igmp_ip/igmp
93 IGMPv3.fixup(pkt)
94 sendp(pkt, iface = self.iface)
95 if self.delay != 0:
96 time.sleep(self.delay)
97
98 def onos_load_config(self, config):
ChetanGaonker689b3862016-10-17 16:25:01 -070099 status, code = OnosCtrl.config(config,controller=self.controller)
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800100 if status is False:
A R Karthick76a497a2017-04-12 10:59:39 -0700101 log_test.info('JSON config request returned status %d' %code)
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800102 time.sleep(2)
103
Thangavelu K S8e413082017-07-13 20:02:14 +0000104 def ssm_table_load(self, groups, src_list = None):
A R Karthick2f4e0ba2017-07-17 10:55:36 -0700105 return
106 if src_list is None:
107 src_list = self.src_list
A.R Karthick401a1ed2017-05-18 11:08:27 -0700108 ssm_dict = {'apps' : { 'org.opencord.igmp' : { 'ssmTranslate' : [] } } }
109 ssm_xlate_list = ssm_dict['apps']['org.opencord.igmp']['ssmTranslate']
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800110 for g in groups:
Thangavelu K S8e413082017-07-13 20:02:14 +0000111 for s in src_list:
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800112 d = {}
113 d['source'] = s
114 d['group'] = g
115 ssm_xlate_list.append(d)
116 self.onos_load_config(ssm_dict)
117
Chetan Gaonkera58ab6e2016-03-23 15:04:20 -0700118 def cord_port_table_load(self, cord_port_map):
A R Karthick2f4e0ba2017-07-17 10:55:36 -0700119 return
Chetan Gaonkera58ab6e2016-03-23 15:04:20 -0700120 cord_group_dict = {'apps' : { 'org.ciena.cordigmp' : { 'cordIgmpTranslate' : [] } } }
121 cord_group_xlate_list = cord_group_dict['apps']['org.ciena.cordigmp']['cordIgmpTranslate']
122 for group, ports in cord_port_map.items():
123 d = {}
124 d['group'] = group
125 d['inputPort'] = ports[0]
126 d['outputPort'] = ports[1]
127 cord_group_xlate_list.append(d)
128 self.onos_load_config(cord_group_dict)
129
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800130class Channels(IgmpChannel):
131 Stopped = 0
132 Started = 1
133 Idle = 0
134 Joined = 1
Thangavelu K S8e413082017-07-13 20:02:14 +0000135 def __init__(self, num, channel_start = 0, iface = 'veth0', iface_mcast = 'veth2', mcast_cb = None, src_list = None):
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800136 self.num = num
Chetan Gaonkercd86bdd2016-03-17 00:08:12 -0700137 self.channel_start = channel_start
138 self.channels = self.generate(self.num, self.channel_start)
Chetan Gaonkercbe79642016-03-09 17:45:58 -0800139 self.group_channel_map = {}
Chetan Gaonkercd86bdd2016-03-17 00:08:12 -0700140 #assert_equal(len(self.channels), self.num)
Chetan Gaonkercbe79642016-03-09 17:45:58 -0800141 for i in range(self.num):
142 self.group_channel_map[self.channels[i]] = i
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800143 self.state = self.Stopped
144 self.streams = None
145 self.channel_states = {}
146 self.last_chan = None
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800147 self.iface_mcast = iface_mcast
148 self.mcast_cb = mcast_cb
Thangavelu K S8e413082017-07-13 20:02:14 +0000149 self.src_list = src_list
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800150 for c in range(self.num):
151 self.channel_states[c] = [self.Idle]
Thangavelu K S8e413082017-07-13 20:02:14 +0000152 IgmpChannel.__init__(self, ssm_list = self.channels, iface=iface, src_list = src_list)
A R Karthick338268f2016-06-21 17:12:13 -0700153
Chetan Gaonkercd86bdd2016-03-17 00:08:12 -0700154 def generate(self, num, channel_start = 0):
Chetan Gaonker38737f82016-05-11 17:44:17 -0700155 start = (225 << 24) | ( ( (channel_start >> 16) & 0xff) << 16 ) | \
Chetan Gaonkercd86bdd2016-03-17 00:08:12 -0700156 ( ( (channel_start >> 8) & 0xff ) << 8 ) | (channel_start) & 0xff
157 start += channel_start/256 + 1
158 end = start + num
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800159 group_addrs = []
Chetan Gaonkercd86bdd2016-03-17 00:08:12 -0700160 count = 0
161 while count != num:
162 for i in range(start, end):
163 if i&255:
164 g = '%s.%s.%s.%s' %((i>>24) &0xff, (i>>16)&0xff, (i>>8)&0xff, i&0xff)
A R Karthick76a497a2017-04-12 10:59:39 -0700165 log_test.debug('Adding group %s' %g)
Chetan Gaonkercd86bdd2016-03-17 00:08:12 -0700166 group_addrs.append(g)
167 count += 1
168 start = end
169 end = start + 1
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800170 return group_addrs
171
172 def start(self):
173 if self.state == self.Stopped:
174 if self.streams:
175 self.streams.stop()
176 self.streams = McastTraffic(self.channels, iface=self.iface_mcast, cb = self.mcast_cb)
177 self.streams.start()
178 self.state = self.Started
179
Thangavelu K S8e413082017-07-13 20:02:14 +0000180 def join(self, chan = None, src_list = None, record_type = None):
181 #def join(self, chan = None):
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800182 if chan is None:
183 chan = random.randint(0, self.num)
184 else:
185 if chan >= self.num:
186 chan = 0
187
188 if self.get_state(chan) == self.Joined:
Chetan Gaonkercbe79642016-03-09 17:45:58 -0800189 return chan, 0
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800190 groups = [self.channels[chan]]
Chetan Gaonkercbe79642016-03-09 17:45:58 -0800191 join_start = monotonic.monotonic()
Thangavelu K S8e413082017-07-13 20:02:14 +0000192 self.igmp_join(groups, src_list = src_list, record_type = record_type)
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800193 self.set_state(chan, self.Joined)
194 self.last_chan = chan
Chetan Gaonkercbe79642016-03-09 17:45:58 -0800195 return chan, join_start
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800196
Thangavelu K S8e413082017-07-13 20:02:14 +0000197 def leave(self, chan, force = False, src_list = None):
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800198 if chan is None:
199 chan = self.last_chan
200 if chan is None or chan >= self.num:
201 return False
A R Karthick78d1f492017-05-19 14:24:17 -0700202 if force is False and self.get_state(chan) != self.Joined:
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800203 return False
204 groups = [self.channels[chan]]
Thangavelu K S8e413082017-07-13 20:02:14 +0000205 self.igmp_leave(groups, src_list = src_list)
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800206 self.set_state(chan, self.Idle)
207 if chan == self.last_chan:
208 self.last_chan = None
209 return True
A R Karthick338268f2016-06-21 17:12:13 -0700210
A.R Karthick9d914552017-05-18 11:22:57 -0700211 def join_next(self, chan = None, leave_flag = True):
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800212 if chan is None:
213 chan = self.last_chan
214 if chan is None:
215 return None
216 leave = chan
Thangavelu K S8e413082017-07-13 20:02:14 +0000217 join = chan+1
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800218 else:
219 leave = chan - 1
220 join = chan
A R Karthick338268f2016-06-21 17:12:13 -0700221
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800222 if join >= self.num:
223 join = 0
224
225 if leave >= 0 and leave != join:
A.R Karthick9d914552017-05-18 11:22:57 -0700226 if leave_flag is True:
227 self.leave(leave)
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800228
229 return self.join(join)
230
231 def jump(self):
232 chan = self.last_chan
233 if chan is not None:
234 self.leave(chan)
235 s_next = chan
236 else:
237 s_next = 0
Chetan Gaonkercbe79642016-03-09 17:45:58 -0800238 if self.num - s_next < 2:
239 s_next = 0
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800240 chan = random.randint(s_next, self.num)
241 return self.join(chan)
242
243 def gaddr(self, chan):
Chetan Gaonkercbe79642016-03-09 17:45:58 -0800244 '''Return the group address for a channel'''
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800245 if chan >= self.num:
246 return None
247 return self.channels[chan]
248
Chetan Gaonkercbe79642016-03-09 17:45:58 -0800249 def caddr(self, group):
250 '''Return a channel given a group addr'''
251 if self.group_channel_map.has_key(group):
252 return self.group_channel_map[group]
253 return None
254
Thangavelu K S8e413082017-07-13 20:02:14 +0000255 def recv_cb(self, pkt, src_list = None):
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800256 '''Default channel receive callback'''
A R Karthick76a497a2017-04-12 10:59:39 -0700257 log_test.debug('Received packet from source %s, destination %s' %(pkt[IP].src, pkt[IP].dst))
Thangavelu K S8e413082017-07-13 20:02:14 +0000258 if src_list is None:
259 send_time = float(pkt[IP].payload.load)
260 recv_time = monotonic.monotonic()
261 log_test.debug('Packet received in %.3f usecs' %(recv_time - send_time))
262 elif(pkt[IP].src == src_list[0]):
263 log_test.debug('Received packet from specified source %s, destination %s' %(pkt[IP].src, pkt[IP].dst))
264 elif(pkt[IP].src != src_list[0]):
265 log_test.debug('Received packet not from specified source %s, destination %s' %(pkt[IP].src, pkt[IP].dst))
266 time.sleep(60)
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800267
Thangavelu K S8e413082017-07-13 20:02:14 +0000268 def recv(self, chan, cb = None, count = 1, timeout = 5, src_list = None):
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800269 if chan is None:
270 return None
271 if type(chan) == type([]) or type(chan) == type(()):
272 channel_list=filter(lambda c: c < self.num, chan)
273 groups = map(lambda c: self.gaddr(c), channel_list)
274 else:
275 groups = (self.gaddr(chan),)
276 if cb is None:
Thangavelu K S8e413082017-07-13 20:02:14 +0000277 cb = self.recv_cb(src_list = src_list)
A R Karthick338268f2016-06-21 17:12:13 -0700278 return sniff(prn = cb, count=count, timeout = timeout,
A.R Karthick3f260212017-05-17 14:37:46 -0700279 lfilter = lambda p: IP in p and p[IP].dst in groups, iface = bytes(self.iface[:15]))
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800280
281 def stop(self):
282 if self.streams:
283 self.streams.stop()
284 self.state = self.Stopped
285
286 def get_state(self, chan):
287 return self.channel_states[chan][0]
288
289 def set_state(self, chan, state):
290 self.channel_states[chan][0] = state
291
292if __name__ == '__main__':
Chetan Gaonkercd86bdd2016-03-17 00:08:12 -0700293 num = 5
294 start = 0
295 ssm_list = []
A R Karthick2f4e0ba2017-07-17 10:55:36 -0700296 src_list = [ '1.2.3.4' ]
Chetan Gaonkercd86bdd2016-03-17 00:08:12 -0700297 for i in xrange(2):
Thangavelu K S8e413082017-07-13 20:02:14 +0000298 channels = Channels(num, start, src_list = src_list)
Chetan Gaonkercd86bdd2016-03-17 00:08:12 -0700299 ssm_list += channels.channels
300 start += num
Thangavelu K S8e413082017-07-13 20:02:14 +0000301 igmpChannel = IgmpChannel(src_list = src_list)
302 igmpChannel.igmp_load_ssm_config(ssm_list, src_list)
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800303 channels.start()
304 for i in range(num):
305 channels.join(i)
306 for i in range(num):
307 channels.recv(i)
308 for i in range(num):
309 channels.leave(i)
310 channels.stop()