blob: 8266c7e421a21fe2b0fd7a9d83c146b1dd6fb6c3 [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
28from nose.tools import *
29log.setLevel('DEBUG')
30
31conf.verb = 0
32
33class IgmpChannel:
34
35 IGMP_DST_MAC = "01:00:5e:00:01:01"
36 IGMP_SRC_MAC = "5a:e1:ac:ec:4d:a1"
37 IP_SRC = '1.2.3.4'
38 IP_DST = '224.0.1.1'
39 igmp_eth = Ether(dst = IGMP_DST_MAC, src = IGMP_SRC_MAC, type = ETH_P_IP)
40 igmp_ip = IP(dst = IP_DST, src = IP_SRC)
A R Karthick338268f2016-06-21 17:12:13 -070041 ssm_list = []
Chetan Gaonkerb424ff82016-03-08 12:11:12 -080042
ChetanGaonker689b3862016-10-17 16:25:01 -070043 def __init__(self, iface = 'veth0', ssm_list = [], src_list = ['1.2.3.4'], delay = 2,controller=None):
44 self.controller=controller
Chetan Gaonkerb424ff82016-03-08 12:11:12 -080045 self.iface = iface
Chetan Gaonkercd86bdd2016-03-17 00:08:12 -070046 self.ssm_list += ssm_list
Chetan Gaonkerb424ff82016-03-08 12:11:12 -080047 self.src_list = src_list
48 self.delay = delay
ChetanGaonker689b3862016-10-17 16:25:01 -070049 self.onos_ctrl = OnosCtrl('org.onosproject.igmp',controller=self.controller)
Chetan Gaonkerb424ff82016-03-08 12:11:12 -080050 self.onos_ctrl.activate()
A R Karthick338268f2016-06-21 17:12:13 -070051
Chetan Gaonkercd86bdd2016-03-17 00:08:12 -070052 def igmp_load_ssm_config(self, ssm_list = []):
53 if not ssm_list:
54 ssm_list = self.ssm_list
55 self.ssm_table_load(ssm_list)
Chetan Gaonkerb424ff82016-03-08 12:11:12 -080056
Chetan Gaonkercd86bdd2016-03-17 00:08:12 -070057 def igmp_join(self, groups):
Chetan Gaonkerb424ff82016-03-08 12:11:12 -080058 igmp = IGMPv3(type = IGMP_TYPE_V3_MEMBERSHIP_REPORT, max_resp_code=30,
59 gaddr='224.0.1.1')
60 for g in groups:
Chetan Gaonker38737f82016-05-11 17:44:17 -070061 gr = IGMPv3gr(rtype=IGMP_V3_GR_TYPE_INCLUDE, mcaddr=g)
Chetan Gaonkerb424ff82016-03-08 12:11:12 -080062 gr.sources = self.src_list
63 igmp.grps.append(gr)
64
65 pkt = self.igmp_eth/self.igmp_ip/igmp
66 IGMPv3.fixup(pkt)
67 sendp(pkt, iface=self.iface)
68 if self.delay != 0:
69 time.sleep(self.delay)
70
71 def igmp_leave(self, groups):
72 igmp = IGMPv3(type = IGMP_TYPE_V3_MEMBERSHIP_REPORT, max_resp_code=30,
73 gaddr='224.0.1.1')
74 for g in groups:
Chetan Gaonker38737f82016-05-11 17:44:17 -070075 gr = IGMPv3gr(rtype=IGMP_V3_GR_TYPE_EXCLUDE, mcaddr=g)
Chetan Gaonkerb424ff82016-03-08 12:11:12 -080076 gr.sources = self.src_list
77 igmp.grps.append(gr)
78
79 pkt = self.igmp_eth/self.igmp_ip/igmp
80 IGMPv3.fixup(pkt)
81 sendp(pkt, iface = self.iface)
82 if self.delay != 0:
83 time.sleep(self.delay)
84
85 def onos_load_config(self, config):
ChetanGaonker689b3862016-10-17 16:25:01 -070086 status, code = OnosCtrl.config(config,controller=self.controller)
Chetan Gaonkerb424ff82016-03-08 12:11:12 -080087 if status is False:
Chetan Gaonkera58ab6e2016-03-23 15:04:20 -070088 log.info('JSON config request returned status %d' %code)
Chetan Gaonkerb424ff82016-03-08 12:11:12 -080089 time.sleep(2)
90
91 def ssm_table_load(self, groups):
92 ssm_dict = {'apps' : { 'org.onosproject.igmp' : { 'ssmTranslate' : [] } } }
93 ssm_xlate_list = ssm_dict['apps']['org.onosproject.igmp']['ssmTranslate']
94 for g in groups:
95 for s in self.src_list:
96 d = {}
97 d['source'] = s
98 d['group'] = g
99 ssm_xlate_list.append(d)
100 self.onos_load_config(ssm_dict)
101
Chetan Gaonkera58ab6e2016-03-23 15:04:20 -0700102 def cord_port_table_load(self, cord_port_map):
103 cord_group_dict = {'apps' : { 'org.ciena.cordigmp' : { 'cordIgmpTranslate' : [] } } }
104 cord_group_xlate_list = cord_group_dict['apps']['org.ciena.cordigmp']['cordIgmpTranslate']
105 for group, ports in cord_port_map.items():
106 d = {}
107 d['group'] = group
108 d['inputPort'] = ports[0]
109 d['outputPort'] = ports[1]
110 cord_group_xlate_list.append(d)
111 self.onos_load_config(cord_group_dict)
112
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800113class Channels(IgmpChannel):
114 Stopped = 0
115 Started = 1
116 Idle = 0
117 Joined = 1
Chetan Gaonkercd86bdd2016-03-17 00:08:12 -0700118 def __init__(self, num, channel_start = 0, iface = 'veth0', iface_mcast = 'veth2', mcast_cb = None):
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800119 self.num = num
Chetan Gaonkercd86bdd2016-03-17 00:08:12 -0700120 self.channel_start = channel_start
121 self.channels = self.generate(self.num, self.channel_start)
Chetan Gaonkercbe79642016-03-09 17:45:58 -0800122 self.group_channel_map = {}
Chetan Gaonkercd86bdd2016-03-17 00:08:12 -0700123 #assert_equal(len(self.channels), self.num)
Chetan Gaonkercbe79642016-03-09 17:45:58 -0800124 for i in range(self.num):
125 self.group_channel_map[self.channels[i]] = i
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800126 self.state = self.Stopped
127 self.streams = None
128 self.channel_states = {}
129 self.last_chan = None
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800130 self.iface_mcast = iface_mcast
131 self.mcast_cb = mcast_cb
132 for c in range(self.num):
133 self.channel_states[c] = [self.Idle]
Chetan Gaonkercd86bdd2016-03-17 00:08:12 -0700134 IgmpChannel.__init__(self, ssm_list = self.channels, iface=iface)
A R Karthick338268f2016-06-21 17:12:13 -0700135
Chetan Gaonkercd86bdd2016-03-17 00:08:12 -0700136 def generate(self, num, channel_start = 0):
Chetan Gaonker38737f82016-05-11 17:44:17 -0700137 start = (225 << 24) | ( ( (channel_start >> 16) & 0xff) << 16 ) | \
Chetan Gaonkercd86bdd2016-03-17 00:08:12 -0700138 ( ( (channel_start >> 8) & 0xff ) << 8 ) | (channel_start) & 0xff
139 start += channel_start/256 + 1
140 end = start + num
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800141 group_addrs = []
Chetan Gaonkercd86bdd2016-03-17 00:08:12 -0700142 count = 0
143 while count != num:
144 for i in range(start, end):
145 if i&255:
146 g = '%s.%s.%s.%s' %((i>>24) &0xff, (i>>16)&0xff, (i>>8)&0xff, i&0xff)
147 log.debug('Adding group %s' %g)
148 group_addrs.append(g)
149 count += 1
150 start = end
151 end = start + 1
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800152 return group_addrs
153
154 def start(self):
155 if self.state == self.Stopped:
156 if self.streams:
157 self.streams.stop()
158 self.streams = McastTraffic(self.channels, iface=self.iface_mcast, cb = self.mcast_cb)
159 self.streams.start()
160 self.state = self.Started
161
162 def join(self, chan = None):
163 if chan is None:
164 chan = random.randint(0, self.num)
165 else:
166 if chan >= self.num:
167 chan = 0
168
169 if self.get_state(chan) == self.Joined:
Chetan Gaonkercbe79642016-03-09 17:45:58 -0800170 return chan, 0
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800171
172 groups = [self.channels[chan]]
Chetan Gaonkercbe79642016-03-09 17:45:58 -0800173 join_start = monotonic.monotonic()
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800174 self.igmp_join(groups)
175 self.set_state(chan, self.Joined)
176 self.last_chan = chan
Chetan Gaonkercbe79642016-03-09 17:45:58 -0800177 return chan, join_start
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800178
179 def leave(self, chan):
180 if chan is None:
181 chan = self.last_chan
182 if chan is None or chan >= self.num:
183 return False
184 if self.get_state(chan) != self.Joined:
185 return False
186 groups = [self.channels[chan]]
187 self.igmp_leave(groups)
188 self.set_state(chan, self.Idle)
189 if chan == self.last_chan:
190 self.last_chan = None
191 return True
A R Karthick338268f2016-06-21 17:12:13 -0700192
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800193 def join_next(self, chan = None):
194 if chan is None:
195 chan = self.last_chan
196 if chan is None:
197 return None
198 leave = chan
199 join = chan+1
200 else:
201 leave = chan - 1
202 join = chan
A R Karthick338268f2016-06-21 17:12:13 -0700203
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800204 if join >= self.num:
205 join = 0
206
207 if leave >= 0 and leave != join:
208 self.leave(leave)
209
210 return self.join(join)
211
212 def jump(self):
213 chan = self.last_chan
214 if chan is not None:
215 self.leave(chan)
216 s_next = chan
217 else:
218 s_next = 0
Chetan Gaonkercbe79642016-03-09 17:45:58 -0800219 if self.num - s_next < 2:
220 s_next = 0
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800221 chan = random.randint(s_next, self.num)
222 return self.join(chan)
223
224 def gaddr(self, chan):
Chetan Gaonkercbe79642016-03-09 17:45:58 -0800225 '''Return the group address for a channel'''
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800226 if chan >= self.num:
227 return None
228 return self.channels[chan]
229
Chetan Gaonkercbe79642016-03-09 17:45:58 -0800230 def caddr(self, group):
231 '''Return a channel given a group addr'''
232 if self.group_channel_map.has_key(group):
233 return self.group_channel_map[group]
234 return None
235
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800236 def recv_cb(self, pkt):
237 '''Default channel receive callback'''
238 log.debug('Received packet from source %s, destination %s' %(pkt[IP].src, pkt[IP].dst))
239 send_time = float(pkt[IP].payload.load)
240 recv_time = monotonic.monotonic()
241 log.debug('Packet received in %.3f usecs' %(recv_time - send_time))
242
A R Karthick338268f2016-06-21 17:12:13 -0700243 def recv(self, chan, cb = None, count = 1, timeout = 5):
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800244 if chan is None:
245 return None
246 if type(chan) == type([]) or type(chan) == type(()):
247 channel_list=filter(lambda c: c < self.num, chan)
248 groups = map(lambda c: self.gaddr(c), channel_list)
249 else:
250 groups = (self.gaddr(chan),)
251 if cb is None:
252 cb = self.recv_cb
A R Karthick338268f2016-06-21 17:12:13 -0700253 return sniff(prn = cb, count=count, timeout = timeout,
254 lfilter = lambda p: IP in p and p[IP].dst in groups, iface = self.iface)
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800255
256 def stop(self):
257 if self.streams:
258 self.streams.stop()
259 self.state = self.Stopped
260
261 def get_state(self, chan):
262 return self.channel_states[chan][0]
263
264 def set_state(self, chan, state):
265 self.channel_states[chan][0] = state
266
267if __name__ == '__main__':
Chetan Gaonkercd86bdd2016-03-17 00:08:12 -0700268 num = 5
269 start = 0
270 ssm_list = []
271 for i in xrange(2):
272 channels = Channels(num, start)
273 ssm_list += channels.channels
274 start += num
275 igmpChannel = IgmpChannel()
276 igmpChannel.igmp_load_ssm_config(ssm_list)
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800277 channels.start()
278 for i in range(num):
279 channels.join(i)
280 for i in range(num):
281 channels.recv(i)
282 for i in range(num):
283 channels.leave(i)
284 channels.stop()