blob: c2599a7a04a3754c524432934b49e87f81ffa521 [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):
Chetan Gaonkercd86bdd2016-03-17 00:08:12 -070058 if not ssm_list:
59 ssm_list = self.ssm_list
Thangavelu K S8e413082017-07-13 20:02:14 +000060 self.ssm_table_load(ssm_list, src_list = src_list)
Chetan Gaonkerb424ff82016-03-08 12:11:12 -080061
Thangavelu K S8e413082017-07-13 20:02:14 +000062 def igmp_join(self, groups, src_list = None, record_type = None):
63 if record_type is None:
64 record_type = IGMP_V3_GR_TYPE_INCLUDE
Chetan Gaonkerb424ff82016-03-08 12:11:12 -080065 igmp = IGMPv3(type = IGMP_TYPE_V3_MEMBERSHIP_REPORT, max_resp_code=30,
66 gaddr='224.0.1.1')
67 for g in groups:
Thangavelu K S8e413082017-07-13 20:02:14 +000068 gr = IGMPv3gr(rtype=record_type, mcaddr=g)
69 gr.sources = src_list
Chetan Gaonkerb424ff82016-03-08 12:11:12 -080070 igmp.grps.append(gr)
71
72 pkt = self.igmp_eth/self.igmp_ip/igmp
73 IGMPv3.fixup(pkt)
74 sendp(pkt, iface=self.iface)
75 if self.delay != 0:
76 time.sleep(self.delay)
77
Thangavelu K S8e413082017-07-13 20:02:14 +000078 def igmp_leave(self, groups, src_list = None):
Chetan Gaonkerb424ff82016-03-08 12:11:12 -080079 igmp = IGMPv3(type = IGMP_TYPE_V3_MEMBERSHIP_REPORT, max_resp_code=30,
80 gaddr='224.0.1.1')
81 for g in groups:
Chetan Gaonker38737f82016-05-11 17:44:17 -070082 gr = IGMPv3gr(rtype=IGMP_V3_GR_TYPE_EXCLUDE, mcaddr=g)
Thangavelu K S8e413082017-07-13 20:02:14 +000083 gr.sources = src_list
Chetan Gaonkerb424ff82016-03-08 12:11:12 -080084 igmp.grps.append(gr)
85
86 pkt = self.igmp_eth/self.igmp_ip/igmp
87 IGMPv3.fixup(pkt)
88 sendp(pkt, iface = self.iface)
89 if self.delay != 0:
90 time.sleep(self.delay)
91
92 def onos_load_config(self, config):
ChetanGaonker689b3862016-10-17 16:25:01 -070093 status, code = OnosCtrl.config(config,controller=self.controller)
Chetan Gaonkerb424ff82016-03-08 12:11:12 -080094 if status is False:
A R Karthick76a497a2017-04-12 10:59:39 -070095 log_test.info('JSON config request returned status %d' %code)
Chetan Gaonkerb424ff82016-03-08 12:11:12 -080096 time.sleep(2)
97
Thangavelu K S8e413082017-07-13 20:02:14 +000098 def ssm_table_load(self, groups, src_list = None):
A.R Karthick401a1ed2017-05-18 11:08:27 -070099 ssm_dict = {'apps' : { 'org.opencord.igmp' : { 'ssmTranslate' : [] } } }
100 ssm_xlate_list = ssm_dict['apps']['org.opencord.igmp']['ssmTranslate']
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800101 for g in groups:
Thangavelu K S8e413082017-07-13 20:02:14 +0000102 for s in src_list:
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800103 d = {}
104 d['source'] = s
105 d['group'] = g
106 ssm_xlate_list.append(d)
107 self.onos_load_config(ssm_dict)
108
Chetan Gaonkera58ab6e2016-03-23 15:04:20 -0700109 def cord_port_table_load(self, cord_port_map):
110 cord_group_dict = {'apps' : { 'org.ciena.cordigmp' : { 'cordIgmpTranslate' : [] } } }
111 cord_group_xlate_list = cord_group_dict['apps']['org.ciena.cordigmp']['cordIgmpTranslate']
112 for group, ports in cord_port_map.items():
113 d = {}
114 d['group'] = group
115 d['inputPort'] = ports[0]
116 d['outputPort'] = ports[1]
117 cord_group_xlate_list.append(d)
118 self.onos_load_config(cord_group_dict)
119
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800120class Channels(IgmpChannel):
121 Stopped = 0
122 Started = 1
123 Idle = 0
124 Joined = 1
Thangavelu K S8e413082017-07-13 20:02:14 +0000125 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 -0800126 self.num = num
Chetan Gaonkercd86bdd2016-03-17 00:08:12 -0700127 self.channel_start = channel_start
128 self.channels = self.generate(self.num, self.channel_start)
Chetan Gaonkercbe79642016-03-09 17:45:58 -0800129 self.group_channel_map = {}
Chetan Gaonkercd86bdd2016-03-17 00:08:12 -0700130 #assert_equal(len(self.channels), self.num)
Chetan Gaonkercbe79642016-03-09 17:45:58 -0800131 for i in range(self.num):
132 self.group_channel_map[self.channels[i]] = i
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800133 self.state = self.Stopped
134 self.streams = None
135 self.channel_states = {}
136 self.last_chan = None
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800137 self.iface_mcast = iface_mcast
138 self.mcast_cb = mcast_cb
Thangavelu K S8e413082017-07-13 20:02:14 +0000139 self.src_list = src_list
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800140 for c in range(self.num):
141 self.channel_states[c] = [self.Idle]
Thangavelu K S8e413082017-07-13 20:02:14 +0000142 IgmpChannel.__init__(self, ssm_list = self.channels, iface=iface, src_list = src_list)
A R Karthick338268f2016-06-21 17:12:13 -0700143
Chetan Gaonkercd86bdd2016-03-17 00:08:12 -0700144 def generate(self, num, channel_start = 0):
Chetan Gaonker38737f82016-05-11 17:44:17 -0700145 start = (225 << 24) | ( ( (channel_start >> 16) & 0xff) << 16 ) | \
Chetan Gaonkercd86bdd2016-03-17 00:08:12 -0700146 ( ( (channel_start >> 8) & 0xff ) << 8 ) | (channel_start) & 0xff
147 start += channel_start/256 + 1
148 end = start + num
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800149 group_addrs = []
Chetan Gaonkercd86bdd2016-03-17 00:08:12 -0700150 count = 0
151 while count != num:
152 for i in range(start, end):
153 if i&255:
154 g = '%s.%s.%s.%s' %((i>>24) &0xff, (i>>16)&0xff, (i>>8)&0xff, i&0xff)
A R Karthick76a497a2017-04-12 10:59:39 -0700155 log_test.debug('Adding group %s' %g)
Chetan Gaonkercd86bdd2016-03-17 00:08:12 -0700156 group_addrs.append(g)
157 count += 1
158 start = end
159 end = start + 1
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800160 return group_addrs
161
162 def start(self):
163 if self.state == self.Stopped:
164 if self.streams:
165 self.streams.stop()
166 self.streams = McastTraffic(self.channels, iface=self.iface_mcast, cb = self.mcast_cb)
167 self.streams.start()
168 self.state = self.Started
169
Thangavelu K S8e413082017-07-13 20:02:14 +0000170 def join(self, chan = None, src_list = None, record_type = None):
171 #def join(self, chan = None):
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800172 if chan is None:
173 chan = random.randint(0, self.num)
174 else:
175 if chan >= self.num:
176 chan = 0
177
178 if self.get_state(chan) == self.Joined:
Chetan Gaonkercbe79642016-03-09 17:45:58 -0800179 return chan, 0
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800180 groups = [self.channels[chan]]
Chetan Gaonkercbe79642016-03-09 17:45:58 -0800181 join_start = monotonic.monotonic()
Thangavelu K S8e413082017-07-13 20:02:14 +0000182 self.igmp_join(groups, src_list = src_list, record_type = record_type)
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800183 self.set_state(chan, self.Joined)
184 self.last_chan = chan
Chetan Gaonkercbe79642016-03-09 17:45:58 -0800185 return chan, join_start
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800186
Thangavelu K S8e413082017-07-13 20:02:14 +0000187 def leave(self, chan, force = False, src_list = None):
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800188 if chan is None:
189 chan = self.last_chan
190 if chan is None or chan >= self.num:
191 return False
A R Karthick78d1f492017-05-19 14:24:17 -0700192 if force is False and self.get_state(chan) != self.Joined:
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800193 return False
194 groups = [self.channels[chan]]
Thangavelu K S8e413082017-07-13 20:02:14 +0000195 self.igmp_leave(groups, src_list = src_list)
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800196 self.set_state(chan, self.Idle)
197 if chan == self.last_chan:
198 self.last_chan = None
199 return True
A R Karthick338268f2016-06-21 17:12:13 -0700200
A.R Karthick9d914552017-05-18 11:22:57 -0700201 def join_next(self, chan = None, leave_flag = True):
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800202 if chan is None:
203 chan = self.last_chan
204 if chan is None:
205 return None
206 leave = chan
Thangavelu K S8e413082017-07-13 20:02:14 +0000207 join = chan+1
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800208 else:
209 leave = chan - 1
210 join = chan
A R Karthick338268f2016-06-21 17:12:13 -0700211
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800212 if join >= self.num:
213 join = 0
214
215 if leave >= 0 and leave != join:
A.R Karthick9d914552017-05-18 11:22:57 -0700216 if leave_flag is True:
217 self.leave(leave)
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800218
219 return self.join(join)
220
221 def jump(self):
222 chan = self.last_chan
223 if chan is not None:
224 self.leave(chan)
225 s_next = chan
226 else:
227 s_next = 0
Chetan Gaonkercbe79642016-03-09 17:45:58 -0800228 if self.num - s_next < 2:
229 s_next = 0
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800230 chan = random.randint(s_next, self.num)
231 return self.join(chan)
232
233 def gaddr(self, chan):
Chetan Gaonkercbe79642016-03-09 17:45:58 -0800234 '''Return the group address for a channel'''
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800235 if chan >= self.num:
236 return None
237 return self.channels[chan]
238
Chetan Gaonkercbe79642016-03-09 17:45:58 -0800239 def caddr(self, group):
240 '''Return a channel given a group addr'''
241 if self.group_channel_map.has_key(group):
242 return self.group_channel_map[group]
243 return None
244
Thangavelu K S8e413082017-07-13 20:02:14 +0000245 def recv_cb(self, pkt, src_list = None):
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800246 '''Default channel receive callback'''
A R Karthick76a497a2017-04-12 10:59:39 -0700247 log_test.debug('Received packet from source %s, destination %s' %(pkt[IP].src, pkt[IP].dst))
Thangavelu K S8e413082017-07-13 20:02:14 +0000248 if src_list is None:
249 send_time = float(pkt[IP].payload.load)
250 recv_time = monotonic.monotonic()
251 log_test.debug('Packet received in %.3f usecs' %(recv_time - send_time))
252 elif(pkt[IP].src == src_list[0]):
253 log_test.debug('Received packet from specified source %s, destination %s' %(pkt[IP].src, pkt[IP].dst))
254 elif(pkt[IP].src != src_list[0]):
255 log_test.debug('Received packet not from specified source %s, destination %s' %(pkt[IP].src, pkt[IP].dst))
256 time.sleep(60)
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800257
Thangavelu K S8e413082017-07-13 20:02:14 +0000258 def recv(self, chan, cb = None, count = 1, timeout = 5, src_list = None):
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800259 if chan is None:
260 return None
261 if type(chan) == type([]) or type(chan) == type(()):
262 channel_list=filter(lambda c: c < self.num, chan)
263 groups = map(lambda c: self.gaddr(c), channel_list)
264 else:
265 groups = (self.gaddr(chan),)
266 if cb is None:
Thangavelu K S8e413082017-07-13 20:02:14 +0000267 cb = self.recv_cb(src_list = src_list)
A R Karthick338268f2016-06-21 17:12:13 -0700268 return sniff(prn = cb, count=count, timeout = timeout,
A.R Karthick3f260212017-05-17 14:37:46 -0700269 lfilter = lambda p: IP in p and p[IP].dst in groups, iface = bytes(self.iface[:15]))
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800270
271 def stop(self):
272 if self.streams:
273 self.streams.stop()
274 self.state = self.Stopped
275
276 def get_state(self, chan):
277 return self.channel_states[chan][0]
278
279 def set_state(self, chan, state):
280 self.channel_states[chan][0] = state
281
282if __name__ == '__main__':
Chetan Gaonkercd86bdd2016-03-17 00:08:12 -0700283 num = 5
284 start = 0
285 ssm_list = []
286 for i in xrange(2):
Thangavelu K S8e413082017-07-13 20:02:14 +0000287 channels = Channels(num, start, src_list = src_list)
Chetan Gaonkercd86bdd2016-03-17 00:08:12 -0700288 ssm_list += channels.channels
289 start += num
Thangavelu K S8e413082017-07-13 20:02:14 +0000290 igmpChannel = IgmpChannel(src_list = src_list)
291 igmpChannel.igmp_load_ssm_config(ssm_list, src_list)
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800292 channels.start()
293 for i in range(num):
294 channels.join(i)
295 for i in range(num):
296 channels.recv(i)
297 for i in range(num):
298 channels.leave(i)
299 channels.stop()