blob: 0d5044202ea1ab4f42af37f38b77f64fd16367f7 [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
Thangavelu K Sb006b8a2017-07-28 19:29:39 +0000150 self.streams_list = []
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800151 for c in range(self.num):
152 self.channel_states[c] = [self.Idle]
Thangavelu K S8e413082017-07-13 20:02:14 +0000153 IgmpChannel.__init__(self, ssm_list = self.channels, iface=iface, src_list = src_list)
A R Karthick338268f2016-06-21 17:12:13 -0700154
Chetan Gaonkercd86bdd2016-03-17 00:08:12 -0700155 def generate(self, num, channel_start = 0):
Chetan Gaonker38737f82016-05-11 17:44:17 -0700156 start = (225 << 24) | ( ( (channel_start >> 16) & 0xff) << 16 ) | \
Chetan Gaonkercd86bdd2016-03-17 00:08:12 -0700157 ( ( (channel_start >> 8) & 0xff ) << 8 ) | (channel_start) & 0xff
158 start += channel_start/256 + 1
159 end = start + num
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800160 group_addrs = []
Chetan Gaonkercd86bdd2016-03-17 00:08:12 -0700161 count = 0
162 while count != num:
163 for i in range(start, end):
164 if i&255:
165 g = '%s.%s.%s.%s' %((i>>24) &0xff, (i>>16)&0xff, (i>>8)&0xff, i&0xff)
A R Karthick76a497a2017-04-12 10:59:39 -0700166 log_test.debug('Adding group %s' %g)
Chetan Gaonkercd86bdd2016-03-17 00:08:12 -0700167 group_addrs.append(g)
168 count += 1
169 start = end
170 end = start + 1
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800171 return group_addrs
172
173 def start(self):
174 if self.state == self.Stopped:
175 if self.streams:
176 self.streams.stop()
Thangavelu K Sb006b8a2017-07-28 19:29:39 +0000177 if self.streams_list:
178 for i in range(len(self.streams_list)):
179 self.streams_list[i].stop()
180 if self.src_list:
181 for i in range(len(self.src_list)):
182 self.streams_list.append(McastTraffic(self.channels, iface=self.iface_mcast, cb = self.mcast_cb, src_ip = self.src_list[i]))
183 self.streams_list[i].start()
184# self.streams = McastTraffic(self.channels, iface=self.iface_mcast, cb = self.mcast_cb)
185# self.streams.start()
186
187 else:
188 self.streams = McastTraffic(self.channels, iface=self.iface_mcast, cb = self.mcast_cb)
189 self.streams.start()
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800190 self.state = self.Started
191
Thangavelu K S8e413082017-07-13 20:02:14 +0000192 def join(self, chan = None, src_list = None, record_type = None):
193 #def join(self, chan = None):
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800194 if chan is None:
195 chan = random.randint(0, self.num)
196 else:
197 if chan >= self.num:
198 chan = 0
199
200 if self.get_state(chan) == self.Joined:
Chetan Gaonkercbe79642016-03-09 17:45:58 -0800201 return chan, 0
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800202 groups = [self.channels[chan]]
Chetan Gaonkercbe79642016-03-09 17:45:58 -0800203 join_start = monotonic.monotonic()
Thangavelu K S8e413082017-07-13 20:02:14 +0000204 self.igmp_join(groups, src_list = src_list, record_type = record_type)
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800205 self.set_state(chan, self.Joined)
206 self.last_chan = chan
Chetan Gaonkercbe79642016-03-09 17:45:58 -0800207 return chan, join_start
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800208
Thangavelu K S8e413082017-07-13 20:02:14 +0000209 def leave(self, chan, force = False, src_list = None):
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800210 if chan is None:
211 chan = self.last_chan
212 if chan is None or chan >= self.num:
213 return False
A R Karthick78d1f492017-05-19 14:24:17 -0700214 if force is False and self.get_state(chan) != self.Joined:
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800215 return False
216 groups = [self.channels[chan]]
Thangavelu K S8e413082017-07-13 20:02:14 +0000217 self.igmp_leave(groups, src_list = src_list)
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800218 self.set_state(chan, self.Idle)
219 if chan == self.last_chan:
220 self.last_chan = None
221 return True
A R Karthick338268f2016-06-21 17:12:13 -0700222
Thangavelu K Sb006b8a2017-07-28 19:29:39 +0000223 def join_next(self, chan = None, src_list = None, leave_flag = True):
224 if chan is None and self.last_chan is not None:
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800225 chan = self.last_chan
226 if chan is None:
227 return None
228 leave = chan
Thangavelu K S8e413082017-07-13 20:02:14 +0000229 join = chan+1
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800230 else:
231 leave = chan - 1
232 join = chan
A R Karthick338268f2016-06-21 17:12:13 -0700233
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800234 if join >= self.num:
235 join = 0
236
237 if leave >= 0 and leave != join:
A.R Karthick9d914552017-05-18 11:22:57 -0700238 if leave_flag is True:
Thangavelu K Sb006b8a2017-07-28 19:29:39 +0000239 self.leave(leave, src_list = src_list)
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800240
Thangavelu K Sb006b8a2017-07-28 19:29:39 +0000241 return self.join(join, src_list = src_list)
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800242
243 def jump(self):
244 chan = self.last_chan
245 if chan is not None:
246 self.leave(chan)
247 s_next = chan
248 else:
249 s_next = 0
Chetan Gaonkercbe79642016-03-09 17:45:58 -0800250 if self.num - s_next < 2:
251 s_next = 0
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800252 chan = random.randint(s_next, self.num)
253 return self.join(chan)
254
255 def gaddr(self, chan):
Chetan Gaonkercbe79642016-03-09 17:45:58 -0800256 '''Return the group address for a channel'''
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800257 if chan >= self.num:
258 return None
259 return self.channels[chan]
260
Chetan Gaonkercbe79642016-03-09 17:45:58 -0800261 def caddr(self, group):
262 '''Return a channel given a group addr'''
263 if self.group_channel_map.has_key(group):
264 return self.group_channel_map[group]
265 return None
266
Thangavelu K S8e413082017-07-13 20:02:14 +0000267 def recv_cb(self, pkt, src_list = None):
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800268 '''Default channel receive callback'''
A R Karthick76a497a2017-04-12 10:59:39 -0700269 log_test.debug('Received packet from source %s, destination %s' %(pkt[IP].src, pkt[IP].dst))
Thangavelu K S8e413082017-07-13 20:02:14 +0000270 if src_list is None:
271 send_time = float(pkt[IP].payload.load)
272 recv_time = monotonic.monotonic()
273 log_test.debug('Packet received in %.3f usecs' %(recv_time - send_time))
274 elif(pkt[IP].src == src_list[0]):
275 log_test.debug('Received packet from specified source %s, destination %s' %(pkt[IP].src, pkt[IP].dst))
276 elif(pkt[IP].src != src_list[0]):
277 log_test.debug('Received packet not from specified source %s, destination %s' %(pkt[IP].src, pkt[IP].dst))
278 time.sleep(60)
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800279
Thangavelu K S8e413082017-07-13 20:02:14 +0000280 def recv(self, chan, cb = None, count = 1, timeout = 5, src_list = None):
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800281 if chan is None:
282 return None
283 if type(chan) == type([]) or type(chan) == type(()):
284 channel_list=filter(lambda c: c < self.num, chan)
285 groups = map(lambda c: self.gaddr(c), channel_list)
286 else:
287 groups = (self.gaddr(chan),)
288 if cb is None:
Thangavelu K S8e413082017-07-13 20:02:14 +0000289 cb = self.recv_cb(src_list = src_list)
A R Karthick338268f2016-06-21 17:12:13 -0700290 return sniff(prn = cb, count=count, timeout = timeout,
A.R Karthick3f260212017-05-17 14:37:46 -0700291 lfilter = lambda p: IP in p and p[IP].dst in groups, iface = bytes(self.iface[:15]))
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800292
293 def stop(self):
294 if self.streams:
295 self.streams.stop()
Thangavelu K Sb006b8a2017-07-28 19:29:39 +0000296 if self.streams_list:
297 for i in range(len(self.streams_list)):
298 self.streams_list[i].stop()
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800299 self.state = self.Stopped
300
301 def get_state(self, chan):
302 return self.channel_states[chan][0]
303
304 def set_state(self, chan, state):
305 self.channel_states[chan][0] = state
306
307if __name__ == '__main__':
Chetan Gaonkercd86bdd2016-03-17 00:08:12 -0700308 num = 5
309 start = 0
310 ssm_list = []
A R Karthick2f4e0ba2017-07-17 10:55:36 -0700311 src_list = [ '1.2.3.4' ]
Chetan Gaonkercd86bdd2016-03-17 00:08:12 -0700312 for i in xrange(2):
Thangavelu K S8e413082017-07-13 20:02:14 +0000313 channels = Channels(num, start, src_list = src_list)
Chetan Gaonkercd86bdd2016-03-17 00:08:12 -0700314 ssm_list += channels.channels
315 start += num
Thangavelu K S8e413082017-07-13 20:02:14 +0000316 igmpChannel = IgmpChannel(src_list = src_list)
317 igmpChannel.igmp_load_ssm_config(ssm_list, src_list)
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800318 channels.start()
319 for i in range(num):
320 channels.join(i)
321 for i in range(num):
322 channels.recv(i)
323 for i in range(num):
324 channels.leave(i)
325 channels.stop()