blob: ccfebb591c842f13b0cea12c3319f803e65be38f [file] [log] [blame]
Chetan Gaonkercfcce782016-05-10 10:10:42 -07001#
2# 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
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# 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)
Chetan Gaonkercd86bdd2016-03-17 00:08:12 -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()
Chetan Gaonkercd86bdd2016-03-17 00:08:12 -070048
49 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
127 self.recv_sock = L2Socket(iface = iface, type = ETH_P_IP)
128 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)
133
134 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
190
191 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
201
202 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
241 def recv(self, chan, cb = None, count = 1):
242 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
Chetan Gaonker38737f82016-05-11 17:44:17 -0700251 sniff(prn = cb, count=count, lfilter = lambda p: IP in p and p[IP].dst in groups, opened_socket = self.recv_sock)
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()