blob: 71cda6a6ee8f459b306db528c5d29bad69ca4885 [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
22from scapy.all import *
23from McastTraffic import *
24from IGMP import *
25from OnosCtrl import OnosCtrl
26from nose.tools import *
27log.setLevel('DEBUG')
28
29conf.verb = 0
30
31class IgmpChannel:
32
33 IGMP_DST_MAC = "01:00:5e:00:01:01"
34 IGMP_SRC_MAC = "5a:e1:ac:ec:4d:a1"
35 IP_SRC = '1.2.3.4'
36 IP_DST = '224.0.1.1'
37 igmp_eth = Ether(dst = IGMP_DST_MAC, src = IGMP_SRC_MAC, type = ETH_P_IP)
38 igmp_ip = IP(dst = IP_DST, src = IP_SRC)
A R Karthick338268f2016-06-21 17:12:13 -070039 ssm_list = []
Chetan Gaonkerb424ff82016-03-08 12:11:12 -080040
ChetanGaonker689b3862016-10-17 16:25:01 -070041 def __init__(self, iface = 'veth0', ssm_list = [], src_list = ['1.2.3.4'], delay = 2,controller=None):
42 self.controller=controller
Chetan Gaonkerb424ff82016-03-08 12:11:12 -080043 self.iface = iface
Chetan Gaonkercd86bdd2016-03-17 00:08:12 -070044 self.ssm_list += ssm_list
Chetan Gaonkerb424ff82016-03-08 12:11:12 -080045 self.src_list = src_list
46 self.delay = delay
ChetanGaonker689b3862016-10-17 16:25:01 -070047 self.onos_ctrl = OnosCtrl('org.onosproject.igmp',controller=self.controller)
Chetan Gaonkerb424ff82016-03-08 12:11:12 -080048 self.onos_ctrl.activate()
A R Karthick338268f2016-06-21 17:12:13 -070049
Chetan Gaonkercd86bdd2016-03-17 00:08:12 -070050 def igmp_load_ssm_config(self, ssm_list = []):
51 if not ssm_list:
52 ssm_list = self.ssm_list
53 self.ssm_table_load(ssm_list)
Chetan Gaonkerb424ff82016-03-08 12:11:12 -080054
Chetan Gaonkercd86bdd2016-03-17 00:08:12 -070055 def igmp_join(self, groups):
Chetan Gaonkerb424ff82016-03-08 12:11:12 -080056 igmp = IGMPv3(type = IGMP_TYPE_V3_MEMBERSHIP_REPORT, max_resp_code=30,
57 gaddr='224.0.1.1')
58 for g in groups:
Chetan Gaonker38737f82016-05-11 17:44:17 -070059 gr = IGMPv3gr(rtype=IGMP_V3_GR_TYPE_INCLUDE, mcaddr=g)
Chetan Gaonkerb424ff82016-03-08 12:11:12 -080060 gr.sources = self.src_list
61 igmp.grps.append(gr)
62
63 pkt = self.igmp_eth/self.igmp_ip/igmp
64 IGMPv3.fixup(pkt)
65 sendp(pkt, iface=self.iface)
66 if self.delay != 0:
67 time.sleep(self.delay)
68
69 def igmp_leave(self, groups):
70 igmp = IGMPv3(type = IGMP_TYPE_V3_MEMBERSHIP_REPORT, max_resp_code=30,
71 gaddr='224.0.1.1')
72 for g in groups:
Chetan Gaonker38737f82016-05-11 17:44:17 -070073 gr = IGMPv3gr(rtype=IGMP_V3_GR_TYPE_EXCLUDE, mcaddr=g)
Chetan Gaonkerb424ff82016-03-08 12:11:12 -080074 gr.sources = self.src_list
75 igmp.grps.append(gr)
76
77 pkt = self.igmp_eth/self.igmp_ip/igmp
78 IGMPv3.fixup(pkt)
79 sendp(pkt, iface = self.iface)
80 if self.delay != 0:
81 time.sleep(self.delay)
82
83 def onos_load_config(self, config):
ChetanGaonker689b3862016-10-17 16:25:01 -070084 status, code = OnosCtrl.config(config,controller=self.controller)
Chetan Gaonkerb424ff82016-03-08 12:11:12 -080085 if status is False:
Chetan Gaonkera58ab6e2016-03-23 15:04:20 -070086 log.info('JSON config request returned status %d' %code)
Chetan Gaonkerb424ff82016-03-08 12:11:12 -080087 time.sleep(2)
88
89 def ssm_table_load(self, groups):
90 ssm_dict = {'apps' : { 'org.onosproject.igmp' : { 'ssmTranslate' : [] } } }
91 ssm_xlate_list = ssm_dict['apps']['org.onosproject.igmp']['ssmTranslate']
92 for g in groups:
93 for s in self.src_list:
94 d = {}
95 d['source'] = s
96 d['group'] = g
97 ssm_xlate_list.append(d)
98 self.onos_load_config(ssm_dict)
99
Chetan Gaonkera58ab6e2016-03-23 15:04:20 -0700100 def cord_port_table_load(self, cord_port_map):
101 cord_group_dict = {'apps' : { 'org.ciena.cordigmp' : { 'cordIgmpTranslate' : [] } } }
102 cord_group_xlate_list = cord_group_dict['apps']['org.ciena.cordigmp']['cordIgmpTranslate']
103 for group, ports in cord_port_map.items():
104 d = {}
105 d['group'] = group
106 d['inputPort'] = ports[0]
107 d['outputPort'] = ports[1]
108 cord_group_xlate_list.append(d)
109 self.onos_load_config(cord_group_dict)
110
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800111class Channels(IgmpChannel):
112 Stopped = 0
113 Started = 1
114 Idle = 0
115 Joined = 1
Chetan Gaonkercd86bdd2016-03-17 00:08:12 -0700116 def __init__(self, num, channel_start = 0, iface = 'veth0', iface_mcast = 'veth2', mcast_cb = None):
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800117 self.num = num
Chetan Gaonkercd86bdd2016-03-17 00:08:12 -0700118 self.channel_start = channel_start
119 self.channels = self.generate(self.num, self.channel_start)
Chetan Gaonkercbe79642016-03-09 17:45:58 -0800120 self.group_channel_map = {}
Chetan Gaonkercd86bdd2016-03-17 00:08:12 -0700121 #assert_equal(len(self.channels), self.num)
Chetan Gaonkercbe79642016-03-09 17:45:58 -0800122 for i in range(self.num):
123 self.group_channel_map[self.channels[i]] = i
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800124 self.state = self.Stopped
125 self.streams = None
126 self.channel_states = {}
127 self.last_chan = None
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800128 self.iface_mcast = iface_mcast
129 self.mcast_cb = mcast_cb
130 for c in range(self.num):
131 self.channel_states[c] = [self.Idle]
Chetan Gaonkercd86bdd2016-03-17 00:08:12 -0700132 IgmpChannel.__init__(self, ssm_list = self.channels, iface=iface)
A R Karthick338268f2016-06-21 17:12:13 -0700133
Chetan Gaonkercd86bdd2016-03-17 00:08:12 -0700134 def generate(self, num, channel_start = 0):
Chetan Gaonker38737f82016-05-11 17:44:17 -0700135 start = (225 << 24) | ( ( (channel_start >> 16) & 0xff) << 16 ) | \
Chetan Gaonkercd86bdd2016-03-17 00:08:12 -0700136 ( ( (channel_start >> 8) & 0xff ) << 8 ) | (channel_start) & 0xff
137 start += channel_start/256 + 1
138 end = start + num
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800139 group_addrs = []
Chetan Gaonkercd86bdd2016-03-17 00:08:12 -0700140 count = 0
141 while count != num:
142 for i in range(start, end):
143 if i&255:
144 g = '%s.%s.%s.%s' %((i>>24) &0xff, (i>>16)&0xff, (i>>8)&0xff, i&0xff)
145 log.debug('Adding group %s' %g)
146 group_addrs.append(g)
147 count += 1
148 start = end
149 end = start + 1
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800150 return group_addrs
151
152 def start(self):
153 if self.state == self.Stopped:
154 if self.streams:
155 self.streams.stop()
156 self.streams = McastTraffic(self.channels, iface=self.iface_mcast, cb = self.mcast_cb)
157 self.streams.start()
158 self.state = self.Started
159
160 def join(self, chan = None):
161 if chan is None:
162 chan = random.randint(0, self.num)
163 else:
164 if chan >= self.num:
165 chan = 0
166
167 if self.get_state(chan) == self.Joined:
Chetan Gaonkercbe79642016-03-09 17:45:58 -0800168 return chan, 0
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800169
170 groups = [self.channels[chan]]
Chetan Gaonkercbe79642016-03-09 17:45:58 -0800171 join_start = monotonic.monotonic()
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800172 self.igmp_join(groups)
173 self.set_state(chan, self.Joined)
174 self.last_chan = chan
Chetan Gaonkercbe79642016-03-09 17:45:58 -0800175 return chan, join_start
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800176
177 def leave(self, chan):
178 if chan is None:
179 chan = self.last_chan
180 if chan is None or chan >= self.num:
181 return False
182 if self.get_state(chan) != self.Joined:
183 return False
184 groups = [self.channels[chan]]
185 self.igmp_leave(groups)
186 self.set_state(chan, self.Idle)
187 if chan == self.last_chan:
188 self.last_chan = None
189 return True
A R Karthick338268f2016-06-21 17:12:13 -0700190
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800191 def join_next(self, chan = None):
192 if chan is None:
193 chan = self.last_chan
194 if chan is None:
195 return None
196 leave = chan
197 join = chan+1
198 else:
199 leave = chan - 1
200 join = chan
A R Karthick338268f2016-06-21 17:12:13 -0700201
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800202 if join >= self.num:
203 join = 0
204
205 if leave >= 0 and leave != join:
206 self.leave(leave)
207
208 return self.join(join)
209
210 def jump(self):
211 chan = self.last_chan
212 if chan is not None:
213 self.leave(chan)
214 s_next = chan
215 else:
216 s_next = 0
Chetan Gaonkercbe79642016-03-09 17:45:58 -0800217 if self.num - s_next < 2:
218 s_next = 0
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800219 chan = random.randint(s_next, self.num)
220 return self.join(chan)
221
222 def gaddr(self, chan):
Chetan Gaonkercbe79642016-03-09 17:45:58 -0800223 '''Return the group address for a channel'''
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800224 if chan >= self.num:
225 return None
226 return self.channels[chan]
227
Chetan Gaonkercbe79642016-03-09 17:45:58 -0800228 def caddr(self, group):
229 '''Return a channel given a group addr'''
230 if self.group_channel_map.has_key(group):
231 return self.group_channel_map[group]
232 return None
233
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800234 def recv_cb(self, pkt):
235 '''Default channel receive callback'''
236 log.debug('Received packet from source %s, destination %s' %(pkt[IP].src, pkt[IP].dst))
237 send_time = float(pkt[IP].payload.load)
238 recv_time = monotonic.monotonic()
239 log.debug('Packet received in %.3f usecs' %(recv_time - send_time))
240
A R Karthick338268f2016-06-21 17:12:13 -0700241 def recv(self, chan, cb = None, count = 1, timeout = 5):
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800242 if chan is None:
243 return None
244 if type(chan) == type([]) or type(chan) == type(()):
245 channel_list=filter(lambda c: c < self.num, chan)
246 groups = map(lambda c: self.gaddr(c), channel_list)
247 else:
248 groups = (self.gaddr(chan),)
249 if cb is None:
250 cb = self.recv_cb
A R Karthick338268f2016-06-21 17:12:13 -0700251 return sniff(prn = cb, count=count, timeout = timeout,
252 lfilter = lambda p: IP in p and p[IP].dst in groups, iface = self.iface)
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800253
254 def stop(self):
255 if self.streams:
256 self.streams.stop()
257 self.state = self.Stopped
258
259 def get_state(self, chan):
260 return self.channel_states[chan][0]
261
262 def set_state(self, chan, state):
263 self.channel_states[chan][0] = state
264
265if __name__ == '__main__':
Chetan Gaonkercd86bdd2016-03-17 00:08:12 -0700266 num = 5
267 start = 0
268 ssm_list = []
269 for i in xrange(2):
270 channels = Channels(num, start)
271 ssm_list += channels.channels
272 start += num
273 igmpChannel = IgmpChannel()
274 igmpChannel.igmp_load_ssm_config(ssm_list)
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800275 channels.start()
276 for i in range(num):
277 channels.join(i)
278 for i in range(num):
279 channels.recv(i)
280 for i in range(num):
281 channels.leave(i)
282 channels.stop()