blob: f0f5960daf9e61d75c67f66b892dce21f30ef3b6 [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
Chetan Gaonkercd86bdd2016-03-17 00:08:12 -070041 def __init__(self, iface = 'veth0', ssm_list = [], src_list = ['1.2.3.4'], delay = 2):
Chetan Gaonkerb424ff82016-03-08 12:11:12 -080042 self.iface = iface
Chetan Gaonkercd86bdd2016-03-17 00:08:12 -070043 self.ssm_list += ssm_list
Chetan Gaonkerb424ff82016-03-08 12:11:12 -080044 self.src_list = src_list
45 self.delay = delay
46 self.onos_ctrl = OnosCtrl('org.onosproject.igmp')
47 self.onos_ctrl.activate()
A R Karthick338268f2016-06-21 17:12:13 -070048
Chetan Gaonkercd86bdd2016-03-17 00:08:12 -070049 def igmp_load_ssm_config(self, ssm_list = []):
50 if not ssm_list:
51 ssm_list = self.ssm_list
52 self.ssm_table_load(ssm_list)
Chetan Gaonkerb424ff82016-03-08 12:11:12 -080053
Chetan Gaonkercd86bdd2016-03-17 00:08:12 -070054 def igmp_join(self, groups):
Chetan Gaonkerb424ff82016-03-08 12:11:12 -080055 igmp = IGMPv3(type = IGMP_TYPE_V3_MEMBERSHIP_REPORT, max_resp_code=30,
56 gaddr='224.0.1.1')
57 for g in groups:
Chetan Gaonker38737f82016-05-11 17:44:17 -070058 gr = IGMPv3gr(rtype=IGMP_V3_GR_TYPE_INCLUDE, mcaddr=g)
Chetan Gaonkerb424ff82016-03-08 12:11:12 -080059 gr.sources = self.src_list
60 igmp.grps.append(gr)
61
62 pkt = self.igmp_eth/self.igmp_ip/igmp
63 IGMPv3.fixup(pkt)
64 sendp(pkt, iface=self.iface)
65 if self.delay != 0:
66 time.sleep(self.delay)
67
68 def igmp_leave(self, groups):
69 igmp = IGMPv3(type = IGMP_TYPE_V3_MEMBERSHIP_REPORT, max_resp_code=30,
70 gaddr='224.0.1.1')
71 for g in groups:
Chetan Gaonker38737f82016-05-11 17:44:17 -070072 gr = IGMPv3gr(rtype=IGMP_V3_GR_TYPE_EXCLUDE, mcaddr=g)
Chetan Gaonkerb424ff82016-03-08 12:11:12 -080073 gr.sources = self.src_list
74 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
82 def onos_load_config(self, config):
Chetan Gaonkera2b87df2016-03-31 15:41:31 -070083 status, code = OnosCtrl.config(config)
Chetan Gaonkerb424ff82016-03-08 12:11:12 -080084 if status is False:
Chetan Gaonkera58ab6e2016-03-23 15:04:20 -070085 log.info('JSON config request returned status %d' %code)
Chetan Gaonkerb424ff82016-03-08 12:11:12 -080086 time.sleep(2)
87
88 def ssm_table_load(self, groups):
89 ssm_dict = {'apps' : { 'org.onosproject.igmp' : { 'ssmTranslate' : [] } } }
90 ssm_xlate_list = ssm_dict['apps']['org.onosproject.igmp']['ssmTranslate']
91 for g in groups:
92 for s in self.src_list:
93 d = {}
94 d['source'] = s
95 d['group'] = g
96 ssm_xlate_list.append(d)
97 self.onos_load_config(ssm_dict)
98
Chetan Gaonkera58ab6e2016-03-23 15:04:20 -070099 def cord_port_table_load(self, cord_port_map):
100 cord_group_dict = {'apps' : { 'org.ciena.cordigmp' : { 'cordIgmpTranslate' : [] } } }
101 cord_group_xlate_list = cord_group_dict['apps']['org.ciena.cordigmp']['cordIgmpTranslate']
102 for group, ports in cord_port_map.items():
103 d = {}
104 d['group'] = group
105 d['inputPort'] = ports[0]
106 d['outputPort'] = ports[1]
107 cord_group_xlate_list.append(d)
108 self.onos_load_config(cord_group_dict)
109
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800110class Channels(IgmpChannel):
111 Stopped = 0
112 Started = 1
113 Idle = 0
114 Joined = 1
Chetan Gaonkercd86bdd2016-03-17 00:08:12 -0700115 def __init__(self, num, channel_start = 0, iface = 'veth0', iface_mcast = 'veth2', mcast_cb = None):
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800116 self.num = num
Chetan Gaonkercd86bdd2016-03-17 00:08:12 -0700117 self.channel_start = channel_start
118 self.channels = self.generate(self.num, self.channel_start)
Chetan Gaonkercbe79642016-03-09 17:45:58 -0800119 self.group_channel_map = {}
Chetan Gaonkercd86bdd2016-03-17 00:08:12 -0700120 #assert_equal(len(self.channels), self.num)
Chetan Gaonkercbe79642016-03-09 17:45:58 -0800121 for i in range(self.num):
122 self.group_channel_map[self.channels[i]] = i
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800123 self.state = self.Stopped
124 self.streams = None
125 self.channel_states = {}
126 self.last_chan = None
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800127 self.iface_mcast = iface_mcast
128 self.mcast_cb = mcast_cb
129 for c in range(self.num):
130 self.channel_states[c] = [self.Idle]
Chetan Gaonkercd86bdd2016-03-17 00:08:12 -0700131 IgmpChannel.__init__(self, ssm_list = self.channels, iface=iface)
A R Karthick338268f2016-06-21 17:12:13 -0700132
Chetan Gaonkercd86bdd2016-03-17 00:08:12 -0700133 def generate(self, num, channel_start = 0):
Chetan Gaonker38737f82016-05-11 17:44:17 -0700134 start = (225 << 24) | ( ( (channel_start >> 16) & 0xff) << 16 ) | \
Chetan Gaonkercd86bdd2016-03-17 00:08:12 -0700135 ( ( (channel_start >> 8) & 0xff ) << 8 ) | (channel_start) & 0xff
136 start += channel_start/256 + 1
137 end = start + num
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800138 group_addrs = []
Chetan Gaonkercd86bdd2016-03-17 00:08:12 -0700139 count = 0
140 while count != num:
141 for i in range(start, end):
142 if i&255:
143 g = '%s.%s.%s.%s' %((i>>24) &0xff, (i>>16)&0xff, (i>>8)&0xff, i&0xff)
144 log.debug('Adding group %s' %g)
145 group_addrs.append(g)
146 count += 1
147 start = end
148 end = start + 1
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800149 return group_addrs
150
151 def start(self):
152 if self.state == self.Stopped:
153 if self.streams:
154 self.streams.stop()
155 self.streams = McastTraffic(self.channels, iface=self.iface_mcast, cb = self.mcast_cb)
156 self.streams.start()
157 self.state = self.Started
158
159 def join(self, chan = None):
160 if chan is None:
161 chan = random.randint(0, self.num)
162 else:
163 if chan >= self.num:
164 chan = 0
165
166 if self.get_state(chan) == self.Joined:
Chetan Gaonkercbe79642016-03-09 17:45:58 -0800167 return chan, 0
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800168
169 groups = [self.channels[chan]]
Chetan Gaonkercbe79642016-03-09 17:45:58 -0800170 join_start = monotonic.monotonic()
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800171 self.igmp_join(groups)
172 self.set_state(chan, self.Joined)
173 self.last_chan = chan
Chetan Gaonkercbe79642016-03-09 17:45:58 -0800174 return chan, join_start
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800175
176 def leave(self, chan):
177 if chan is None:
178 chan = self.last_chan
179 if chan is None or chan >= self.num:
180 return False
181 if self.get_state(chan) != self.Joined:
182 return False
183 groups = [self.channels[chan]]
184 self.igmp_leave(groups)
185 self.set_state(chan, self.Idle)
186 if chan == self.last_chan:
187 self.last_chan = None
188 return True
A R Karthick338268f2016-06-21 17:12:13 -0700189
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800190 def join_next(self, chan = None):
191 if chan is None:
192 chan = self.last_chan
193 if chan is None:
194 return None
195 leave = chan
196 join = chan+1
197 else:
198 leave = chan - 1
199 join = chan
A R Karthick338268f2016-06-21 17:12:13 -0700200
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800201 if join >= self.num:
202 join = 0
203
204 if leave >= 0 and leave != join:
205 self.leave(leave)
206
207 return self.join(join)
208
209 def jump(self):
210 chan = self.last_chan
211 if chan is not None:
212 self.leave(chan)
213 s_next = chan
214 else:
215 s_next = 0
Chetan Gaonkercbe79642016-03-09 17:45:58 -0800216 if self.num - s_next < 2:
217 s_next = 0
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800218 chan = random.randint(s_next, self.num)
219 return self.join(chan)
220
221 def gaddr(self, chan):
Chetan Gaonkercbe79642016-03-09 17:45:58 -0800222 '''Return the group address for a channel'''
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800223 if chan >= self.num:
224 return None
225 return self.channels[chan]
226
Chetan Gaonkercbe79642016-03-09 17:45:58 -0800227 def caddr(self, group):
228 '''Return a channel given a group addr'''
229 if self.group_channel_map.has_key(group):
230 return self.group_channel_map[group]
231 return None
232
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800233 def recv_cb(self, pkt):
234 '''Default channel receive callback'''
235 log.debug('Received packet from source %s, destination %s' %(pkt[IP].src, pkt[IP].dst))
236 send_time = float(pkt[IP].payload.load)
237 recv_time = monotonic.monotonic()
238 log.debug('Packet received in %.3f usecs' %(recv_time - send_time))
239
A R Karthick338268f2016-06-21 17:12:13 -0700240 def recv(self, chan, cb = None, count = 1, timeout = 5):
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800241 if chan is None:
242 return None
243 if type(chan) == type([]) or type(chan) == type(()):
244 channel_list=filter(lambda c: c < self.num, chan)
245 groups = map(lambda c: self.gaddr(c), channel_list)
246 else:
247 groups = (self.gaddr(chan),)
248 if cb is None:
249 cb = self.recv_cb
A R Karthick338268f2016-06-21 17:12:13 -0700250 return sniff(prn = cb, count=count, timeout = timeout,
251 lfilter = lambda p: IP in p and p[IP].dst in groups, iface = self.iface)
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800252
253 def stop(self):
254 if self.streams:
255 self.streams.stop()
256 self.state = self.Stopped
257
258 def get_state(self, chan):
259 return self.channel_states[chan][0]
260
261 def set_state(self, chan, state):
262 self.channel_states[chan][0] = state
263
264if __name__ == '__main__':
Chetan Gaonkercd86bdd2016-03-17 00:08:12 -0700265 num = 5
266 start = 0
267 ssm_list = []
268 for i in xrange(2):
269 channels = Channels(num, start)
270 ssm_list += channels.channels
271 start += num
272 igmpChannel = IgmpChannel()
273 igmpChannel.igmp_load_ssm_config(ssm_list)
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800274 channels.start()
275 for i in range(num):
276 channels.join(i)
277 for i in range(num):
278 channels.recv(i)
279 for i in range(num):
280 channels.leave(i)
281 channels.stop()