blob: b9363f9a269cce9b940b8a9416d675270f0aae27 [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
ChetanGaonker689b3862016-10-17 16:25:01 -070044 def __init__(self, iface = 'veth0', ssm_list = [], src_list = ['1.2.3.4'], delay = 2,controller=None):
45 self.controller=controller
Chetan Gaonkerb424ff82016-03-08 12:11:12 -080046 self.iface = iface
Chetan Gaonkercd86bdd2016-03-17 00:08:12 -070047 self.ssm_list += ssm_list
Chetan Gaonkerb424ff82016-03-08 12:11:12 -080048 self.src_list = src_list
49 self.delay = delay
A.R Karthick401a1ed2017-05-18 11:08:27 -070050 self.onos_ctrl = OnosCtrl('org.opencord.igmp',controller=self.controller)
Chetan Gaonkerb424ff82016-03-08 12:11:12 -080051 self.onos_ctrl.activate()
A R Karthick338268f2016-06-21 17:12:13 -070052
Chetan Gaonkercd86bdd2016-03-17 00:08:12 -070053 def igmp_load_ssm_config(self, ssm_list = []):
54 if not ssm_list:
55 ssm_list = self.ssm_list
56 self.ssm_table_load(ssm_list)
Chetan Gaonkerb424ff82016-03-08 12:11:12 -080057
Chetan Gaonkercd86bdd2016-03-17 00:08:12 -070058 def igmp_join(self, groups):
Chetan Gaonkerb424ff82016-03-08 12:11:12 -080059 igmp = IGMPv3(type = IGMP_TYPE_V3_MEMBERSHIP_REPORT, max_resp_code=30,
60 gaddr='224.0.1.1')
61 for g in groups:
Chetan Gaonker38737f82016-05-11 17:44:17 -070062 gr = IGMPv3gr(rtype=IGMP_V3_GR_TYPE_INCLUDE, mcaddr=g)
Chetan Gaonkerb424ff82016-03-08 12:11:12 -080063 gr.sources = self.src_list
64 igmp.grps.append(gr)
65
66 pkt = self.igmp_eth/self.igmp_ip/igmp
67 IGMPv3.fixup(pkt)
68 sendp(pkt, iface=self.iface)
69 if self.delay != 0:
70 time.sleep(self.delay)
71
72 def igmp_leave(self, groups):
73 igmp = IGMPv3(type = IGMP_TYPE_V3_MEMBERSHIP_REPORT, max_resp_code=30,
74 gaddr='224.0.1.1')
75 for g in groups:
Chetan Gaonker38737f82016-05-11 17:44:17 -070076 gr = IGMPv3gr(rtype=IGMP_V3_GR_TYPE_EXCLUDE, mcaddr=g)
Chetan Gaonkerb424ff82016-03-08 12:11:12 -080077 gr.sources = self.src_list
78 igmp.grps.append(gr)
79
80 pkt = self.igmp_eth/self.igmp_ip/igmp
81 IGMPv3.fixup(pkt)
82 sendp(pkt, iface = self.iface)
83 if self.delay != 0:
84 time.sleep(self.delay)
85
86 def onos_load_config(self, config):
ChetanGaonker689b3862016-10-17 16:25:01 -070087 status, code = OnosCtrl.config(config,controller=self.controller)
Chetan Gaonkerb424ff82016-03-08 12:11:12 -080088 if status is False:
A R Karthick76a497a2017-04-12 10:59:39 -070089 log_test.info('JSON config request returned status %d' %code)
Chetan Gaonkerb424ff82016-03-08 12:11:12 -080090 time.sleep(2)
91
92 def ssm_table_load(self, groups):
A R Karthick38d5df42017-07-10 13:33:26 -070093 return
A.R Karthick401a1ed2017-05-18 11:08:27 -070094 ssm_dict = {'apps' : { 'org.opencord.igmp' : { 'ssmTranslate' : [] } } }
95 ssm_xlate_list = ssm_dict['apps']['org.opencord.igmp']['ssmTranslate']
Chetan Gaonkerb424ff82016-03-08 12:11:12 -080096 for g in groups:
97 for s in self.src_list:
98 d = {}
99 d['source'] = s
100 d['group'] = g
101 ssm_xlate_list.append(d)
102 self.onos_load_config(ssm_dict)
103
Chetan Gaonkera58ab6e2016-03-23 15:04:20 -0700104 def cord_port_table_load(self, cord_port_map):
A R Karthick38d5df42017-07-10 13:33:26 -0700105 return
Chetan Gaonkera58ab6e2016-03-23 15:04:20 -0700106 cord_group_dict = {'apps' : { 'org.ciena.cordigmp' : { 'cordIgmpTranslate' : [] } } }
107 cord_group_xlate_list = cord_group_dict['apps']['org.ciena.cordigmp']['cordIgmpTranslate']
108 for group, ports in cord_port_map.items():
109 d = {}
110 d['group'] = group
111 d['inputPort'] = ports[0]
112 d['outputPort'] = ports[1]
113 cord_group_xlate_list.append(d)
114 self.onos_load_config(cord_group_dict)
115
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800116class Channels(IgmpChannel):
117 Stopped = 0
118 Started = 1
119 Idle = 0
120 Joined = 1
Chetan Gaonkercd86bdd2016-03-17 00:08:12 -0700121 def __init__(self, num, channel_start = 0, iface = 'veth0', iface_mcast = 'veth2', mcast_cb = None):
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800122 self.num = num
Chetan Gaonkercd86bdd2016-03-17 00:08:12 -0700123 self.channel_start = channel_start
124 self.channels = self.generate(self.num, self.channel_start)
Chetan Gaonkercbe79642016-03-09 17:45:58 -0800125 self.group_channel_map = {}
Chetan Gaonkercd86bdd2016-03-17 00:08:12 -0700126 #assert_equal(len(self.channels), self.num)
Chetan Gaonkercbe79642016-03-09 17:45:58 -0800127 for i in range(self.num):
128 self.group_channel_map[self.channels[i]] = i
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800129 self.state = self.Stopped
130 self.streams = None
131 self.channel_states = {}
132 self.last_chan = None
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800133 self.iface_mcast = iface_mcast
134 self.mcast_cb = mcast_cb
135 for c in range(self.num):
136 self.channel_states[c] = [self.Idle]
Chetan Gaonkercd86bdd2016-03-17 00:08:12 -0700137 IgmpChannel.__init__(self, ssm_list = self.channels, iface=iface)
A R Karthick338268f2016-06-21 17:12:13 -0700138
Chetan Gaonkercd86bdd2016-03-17 00:08:12 -0700139 def generate(self, num, channel_start = 0):
Chetan Gaonker38737f82016-05-11 17:44:17 -0700140 start = (225 << 24) | ( ( (channel_start >> 16) & 0xff) << 16 ) | \
Chetan Gaonkercd86bdd2016-03-17 00:08:12 -0700141 ( ( (channel_start >> 8) & 0xff ) << 8 ) | (channel_start) & 0xff
142 start += channel_start/256 + 1
143 end = start + num
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800144 group_addrs = []
Chetan Gaonkercd86bdd2016-03-17 00:08:12 -0700145 count = 0
146 while count != num:
147 for i in range(start, end):
148 if i&255:
149 g = '%s.%s.%s.%s' %((i>>24) &0xff, (i>>16)&0xff, (i>>8)&0xff, i&0xff)
A R Karthick76a497a2017-04-12 10:59:39 -0700150 log_test.debug('Adding group %s' %g)
Chetan Gaonkercd86bdd2016-03-17 00:08:12 -0700151 group_addrs.append(g)
152 count += 1
153 start = end
154 end = start + 1
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800155 return group_addrs
156
157 def start(self):
158 if self.state == self.Stopped:
159 if self.streams:
160 self.streams.stop()
161 self.streams = McastTraffic(self.channels, iface=self.iface_mcast, cb = self.mcast_cb)
162 self.streams.start()
163 self.state = self.Started
164
165 def join(self, chan = None):
166 if chan is None:
167 chan = random.randint(0, self.num)
168 else:
169 if chan >= self.num:
170 chan = 0
171
172 if self.get_state(chan) == self.Joined:
Chetan Gaonkercbe79642016-03-09 17:45:58 -0800173 return chan, 0
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800174
175 groups = [self.channels[chan]]
Chetan Gaonkercbe79642016-03-09 17:45:58 -0800176 join_start = monotonic.monotonic()
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800177 self.igmp_join(groups)
178 self.set_state(chan, self.Joined)
179 self.last_chan = chan
Chetan Gaonkercbe79642016-03-09 17:45:58 -0800180 return chan, join_start
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800181
A R Karthick78d1f492017-05-19 14:24:17 -0700182 def leave(self, chan, force = False):
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800183 if chan is None:
184 chan = self.last_chan
185 if chan is None or chan >= self.num:
186 return False
A R Karthick78d1f492017-05-19 14:24:17 -0700187 if force is False and self.get_state(chan) != self.Joined:
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800188 return False
189 groups = [self.channels[chan]]
190 self.igmp_leave(groups)
191 self.set_state(chan, self.Idle)
192 if chan == self.last_chan:
193 self.last_chan = None
194 return True
A R Karthick338268f2016-06-21 17:12:13 -0700195
A.R Karthick9d914552017-05-18 11:22:57 -0700196 def join_next(self, chan = None, leave_flag = True):
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800197 if chan is None:
198 chan = self.last_chan
199 if chan is None:
200 return None
201 leave = chan
202 join = chan+1
203 else:
204 leave = chan - 1
205 join = chan
A R Karthick338268f2016-06-21 17:12:13 -0700206
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800207 if join >= self.num:
208 join = 0
209
210 if leave >= 0 and leave != join:
A.R Karthick9d914552017-05-18 11:22:57 -0700211 if leave_flag is True:
212 self.leave(leave)
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800213
214 return self.join(join)
215
216 def jump(self):
217 chan = self.last_chan
218 if chan is not None:
219 self.leave(chan)
220 s_next = chan
221 else:
222 s_next = 0
Chetan Gaonkercbe79642016-03-09 17:45:58 -0800223 if self.num - s_next < 2:
224 s_next = 0
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800225 chan = random.randint(s_next, self.num)
226 return self.join(chan)
227
228 def gaddr(self, chan):
Chetan Gaonkercbe79642016-03-09 17:45:58 -0800229 '''Return the group address for a channel'''
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800230 if chan >= self.num:
231 return None
232 return self.channels[chan]
233
Chetan Gaonkercbe79642016-03-09 17:45:58 -0800234 def caddr(self, group):
235 '''Return a channel given a group addr'''
236 if self.group_channel_map.has_key(group):
237 return self.group_channel_map[group]
238 return None
239
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800240 def recv_cb(self, pkt):
241 '''Default channel receive callback'''
A R Karthick76a497a2017-04-12 10:59:39 -0700242 log_test.debug('Received packet from source %s, destination %s' %(pkt[IP].src, pkt[IP].dst))
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800243 send_time = float(pkt[IP].payload.load)
244 recv_time = monotonic.monotonic()
A R Karthick76a497a2017-04-12 10:59:39 -0700245 log_test.debug('Packet received in %.3f usecs' %(recv_time - send_time))
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800246
A R Karthick338268f2016-06-21 17:12:13 -0700247 def recv(self, chan, cb = None, count = 1, timeout = 5):
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800248 if chan is None:
249 return None
250 if type(chan) == type([]) or type(chan) == type(()):
251 channel_list=filter(lambda c: c < self.num, chan)
252 groups = map(lambda c: self.gaddr(c), channel_list)
253 else:
254 groups = (self.gaddr(chan),)
255 if cb is None:
256 cb = self.recv_cb
A R Karthick338268f2016-06-21 17:12:13 -0700257 return sniff(prn = cb, count=count, timeout = timeout,
A.R Karthick3f260212017-05-17 14:37:46 -0700258 lfilter = lambda p: IP in p and p[IP].dst in groups, iface = bytes(self.iface[:15]))
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800259
260 def stop(self):
261 if self.streams:
262 self.streams.stop()
263 self.state = self.Stopped
264
265 def get_state(self, chan):
266 return self.channel_states[chan][0]
267
268 def set_state(self, chan, state):
269 self.channel_states[chan][0] = state
270
271if __name__ == '__main__':
Chetan Gaonkercd86bdd2016-03-17 00:08:12 -0700272 num = 5
273 start = 0
274 ssm_list = []
275 for i in xrange(2):
276 channels = Channels(num, start)
277 ssm_list += channels.channels
278 start += num
279 igmpChannel = IgmpChannel()
280 igmpChannel.igmp_load_ssm_config(ssm_list)
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800281 channels.start()
282 for i in range(num):
283 channels.join(i)
284 for i in range(num):
285 channels.recv(i)
286 for i in range(num):
287 channels.leave(i)
288 channels.stop()