blob: b712f968ccd4bc4ca6bddd81d0cec68451e5c726 [file] [log] [blame]
Chetan Gaonkercb122cc2016-05-10 10:58:34 -07001#!/usr/bin/env python
Chetan Gaonkercfcce782016-05-10 10:10:42 -07002#
3# Copyright 2016-present Ciena Corporation
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16#
Chetan Gaonkerb424ff82016-03-08 12:11:12 -080017import threading
18import sys
19import os
20import time
21import monotonic
22import random
23from scapy.all import *
24from McastTraffic import *
25from IGMP import *
26from OnosCtrl import OnosCtrl
27from nose.tools import *
28log.setLevel('DEBUG')
29
30conf.verb = 0
31
32class IgmpChannel:
33
34 IGMP_DST_MAC = "01:00:5e:00:01:01"
35 IGMP_SRC_MAC = "5a:e1:ac:ec:4d:a1"
36 IP_SRC = '1.2.3.4'
37 IP_DST = '224.0.1.1'
38 igmp_eth = Ether(dst = IGMP_DST_MAC, src = IGMP_SRC_MAC, type = ETH_P_IP)
39 igmp_ip = IP(dst = IP_DST, src = IP_SRC)
Chetan Gaonkercd86bdd2016-03-17 00:08:12 -070040 ssm_list = []
Chetan Gaonkerb424ff82016-03-08 12:11:12 -080041
Chetan Gaonkercd86bdd2016-03-17 00:08:12 -070042 def __init__(self, iface = 'veth0', ssm_list = [], src_list = ['1.2.3.4'], delay = 2):
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
47 self.onos_ctrl = OnosCtrl('org.onosproject.igmp')
48 self.onos_ctrl.activate()
Chetan Gaonkercd86bdd2016-03-17 00:08:12 -070049
50 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:
59 gr = IGMPv3gr(rtype=IGMP_V3_GR_TYPE_EXCLUDE, mcaddr=g)
60 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:
73 gr = IGMPv3gr(rtype=IGMP_V3_GR_TYPE_INCLUDE, mcaddr=g)
74 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):
Chetan Gaonkera2b87df2016-03-31 15:41:31 -070084 status, code = OnosCtrl.config(config)
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
128 self.recv_sock = L2Socket(iface = iface, type = ETH_P_IP)
129 self.iface_mcast = iface_mcast
130 self.mcast_cb = mcast_cb
131 for c in range(self.num):
132 self.channel_states[c] = [self.Idle]
Chetan Gaonkercd86bdd2016-03-17 00:08:12 -0700133 IgmpChannel.__init__(self, ssm_list = self.channels, iface=iface)
134
135 def generate(self, num, channel_start = 0):
136 start = (224 << 24) | ( ( (channel_start >> 16) & 0xff) << 16 ) | \
137 ( ( (channel_start >> 8) & 0xff ) << 8 ) | (channel_start) & 0xff
138 start += channel_start/256 + 1
139 end = start + num
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800140 group_addrs = []
Chetan Gaonkercd86bdd2016-03-17 00:08:12 -0700141 count = 0
142 while count != num:
143 for i in range(start, end):
144 if i&255:
145 g = '%s.%s.%s.%s' %((i>>24) &0xff, (i>>16)&0xff, (i>>8)&0xff, i&0xff)
146 log.debug('Adding group %s' %g)
147 group_addrs.append(g)
148 count += 1
149 start = end
150 end = start + 1
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800151 return group_addrs
152
153 def start(self):
154 if self.state == self.Stopped:
155 if self.streams:
156 self.streams.stop()
157 self.streams = McastTraffic(self.channels, iface=self.iface_mcast, cb = self.mcast_cb)
158 self.streams.start()
159 self.state = self.Started
160
161 def join(self, chan = None):
162 if chan is None:
163 chan = random.randint(0, self.num)
164 else:
165 if chan >= self.num:
166 chan = 0
167
168 if self.get_state(chan) == self.Joined:
Chetan Gaonkercbe79642016-03-09 17:45:58 -0800169 return chan, 0
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800170
171 groups = [self.channels[chan]]
Chetan Gaonkercbe79642016-03-09 17:45:58 -0800172 join_start = monotonic.monotonic()
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800173 self.igmp_join(groups)
174 self.set_state(chan, self.Joined)
175 self.last_chan = chan
Chetan Gaonkercbe79642016-03-09 17:45:58 -0800176 return chan, join_start
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800177
178 def leave(self, chan):
179 if chan is None:
180 chan = self.last_chan
181 if chan is None or chan >= self.num:
182 return False
183 if self.get_state(chan) != self.Joined:
184 return False
185 groups = [self.channels[chan]]
186 self.igmp_leave(groups)
187 self.set_state(chan, self.Idle)
188 if chan == self.last_chan:
189 self.last_chan = None
190 return True
191
192 def join_next(self, chan = None):
193 if chan is None:
194 chan = self.last_chan
195 if chan is None:
196 return None
197 leave = chan
198 join = chan+1
199 else:
200 leave = chan - 1
201 join = chan
202
203 if join >= self.num:
204 join = 0
205
206 if leave >= 0 and leave != join:
207 self.leave(leave)
208
209 return self.join(join)
210
211 def jump(self):
212 chan = self.last_chan
213 if chan is not None:
214 self.leave(chan)
215 s_next = chan
216 else:
217 s_next = 0
Chetan Gaonkercbe79642016-03-09 17:45:58 -0800218 if self.num - s_next < 2:
219 s_next = 0
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800220 chan = random.randint(s_next, self.num)
221 return self.join(chan)
222
223 def gaddr(self, chan):
Chetan Gaonkercbe79642016-03-09 17:45:58 -0800224 '''Return the group address for a channel'''
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800225 if chan >= self.num:
226 return None
227 return self.channels[chan]
228
Chetan Gaonkercbe79642016-03-09 17:45:58 -0800229 def caddr(self, group):
230 '''Return a channel given a group addr'''
231 if self.group_channel_map.has_key(group):
232 return self.group_channel_map[group]
233 return None
234
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800235 def recv_cb(self, pkt):
236 '''Default channel receive callback'''
237 log.debug('Received packet from source %s, destination %s' %(pkt[IP].src, pkt[IP].dst))
238 send_time = float(pkt[IP].payload.load)
239 recv_time = monotonic.monotonic()
240 log.debug('Packet received in %.3f usecs' %(recv_time - send_time))
241
242 def recv(self, chan, cb = None, count = 1):
243 if chan is None:
244 return None
245 if type(chan) == type([]) or type(chan) == type(()):
246 channel_list=filter(lambda c: c < self.num, chan)
247 groups = map(lambda c: self.gaddr(c), channel_list)
248 else:
249 groups = (self.gaddr(chan),)
250 if cb is None:
251 cb = self.recv_cb
252 sniff(prn = cb, count=count, lfilter = lambda p: p[IP].dst in groups, opened_socket = self.recv_sock)
253
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()