blob: e0b11c87a121dd19ff014cd0c77d5bcee54cc382 [file] [log] [blame]
Chetan Gaonkerb424ff82016-03-08 12:11:12 -08001import threading
2import sys
3import os
4import time
5import monotonic
6import random
7from scapy.all import *
8from McastTraffic import *
9from IGMP import *
10from OnosCtrl import OnosCtrl
11from nose.tools import *
12log.setLevel('DEBUG')
13
14conf.verb = 0
15
16class IgmpChannel:
17
18 IGMP_DST_MAC = "01:00:5e:00:01:01"
19 IGMP_SRC_MAC = "5a:e1:ac:ec:4d:a1"
20 IP_SRC = '1.2.3.4'
21 IP_DST = '224.0.1.1'
22 igmp_eth = Ether(dst = IGMP_DST_MAC, src = IGMP_SRC_MAC, type = ETH_P_IP)
23 igmp_ip = IP(dst = IP_DST, src = IP_SRC)
Chetan Gaonkercd86bdd2016-03-17 00:08:12 -070024 ssm_list = []
Chetan Gaonkerb424ff82016-03-08 12:11:12 -080025
Chetan Gaonkercd86bdd2016-03-17 00:08:12 -070026 def __init__(self, iface = 'veth0', ssm_list = [], src_list = ['1.2.3.4'], delay = 2):
Chetan Gaonkerb424ff82016-03-08 12:11:12 -080027 self.iface = iface
Chetan Gaonkercd86bdd2016-03-17 00:08:12 -070028 self.ssm_list += ssm_list
Chetan Gaonkerb424ff82016-03-08 12:11:12 -080029 self.src_list = src_list
30 self.delay = delay
31 self.onos_ctrl = OnosCtrl('org.onosproject.igmp')
32 self.onos_ctrl.activate()
Chetan Gaonkercd86bdd2016-03-17 00:08:12 -070033
34 def igmp_load_ssm_config(self, ssm_list = []):
35 if not ssm_list:
36 ssm_list = self.ssm_list
37 self.ssm_table_load(ssm_list)
Chetan Gaonkerb424ff82016-03-08 12:11:12 -080038
Chetan Gaonkercd86bdd2016-03-17 00:08:12 -070039 def igmp_join(self, groups):
Chetan Gaonkerb424ff82016-03-08 12:11:12 -080040 igmp = IGMPv3(type = IGMP_TYPE_V3_MEMBERSHIP_REPORT, max_resp_code=30,
41 gaddr='224.0.1.1')
42 for g in groups:
43 gr = IGMPv3gr(rtype=IGMP_V3_GR_TYPE_EXCLUDE, mcaddr=g)
44 gr.sources = self.src_list
45 igmp.grps.append(gr)
46
47 pkt = self.igmp_eth/self.igmp_ip/igmp
48 IGMPv3.fixup(pkt)
49 sendp(pkt, iface=self.iface)
50 if self.delay != 0:
51 time.sleep(self.delay)
52
53 def igmp_leave(self, groups):
54 igmp = IGMPv3(type = IGMP_TYPE_V3_MEMBERSHIP_REPORT, max_resp_code=30,
55 gaddr='224.0.1.1')
56 for g in groups:
57 gr = IGMPv3gr(rtype=IGMP_V3_GR_TYPE_INCLUDE, mcaddr=g)
58 gr.sources = self.src_list
59 igmp.grps.append(gr)
60
61 pkt = self.igmp_eth/self.igmp_ip/igmp
62 IGMPv3.fixup(pkt)
63 sendp(pkt, iface = self.iface)
64 if self.delay != 0:
65 time.sleep(self.delay)
66
67 def onos_load_config(self, config):
Chetan Gaonkera2b87df2016-03-31 15:41:31 -070068 status, code = OnosCtrl.config(config)
Chetan Gaonkerb424ff82016-03-08 12:11:12 -080069 if status is False:
Chetan Gaonkera58ab6e2016-03-23 15:04:20 -070070 log.info('JSON config request returned status %d' %code)
Chetan Gaonkerb424ff82016-03-08 12:11:12 -080071 time.sleep(2)
72
73 def ssm_table_load(self, groups):
74 ssm_dict = {'apps' : { 'org.onosproject.igmp' : { 'ssmTranslate' : [] } } }
75 ssm_xlate_list = ssm_dict['apps']['org.onosproject.igmp']['ssmTranslate']
76 for g in groups:
77 for s in self.src_list:
78 d = {}
79 d['source'] = s
80 d['group'] = g
81 ssm_xlate_list.append(d)
82 self.onos_load_config(ssm_dict)
83
Chetan Gaonkera58ab6e2016-03-23 15:04:20 -070084 def cord_port_table_load(self, cord_port_map):
85 cord_group_dict = {'apps' : { 'org.ciena.cordigmp' : { 'cordIgmpTranslate' : [] } } }
86 cord_group_xlate_list = cord_group_dict['apps']['org.ciena.cordigmp']['cordIgmpTranslate']
87 for group, ports in cord_port_map.items():
88 d = {}
89 d['group'] = group
90 d['inputPort'] = ports[0]
91 d['outputPort'] = ports[1]
92 cord_group_xlate_list.append(d)
93 self.onos_load_config(cord_group_dict)
94
Chetan Gaonkerb424ff82016-03-08 12:11:12 -080095class Channels(IgmpChannel):
96 Stopped = 0
97 Started = 1
98 Idle = 0
99 Joined = 1
Chetan Gaonkercd86bdd2016-03-17 00:08:12 -0700100 def __init__(self, num, channel_start = 0, iface = 'veth0', iface_mcast = 'veth2', mcast_cb = None):
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800101 self.num = num
Chetan Gaonkercd86bdd2016-03-17 00:08:12 -0700102 self.channel_start = channel_start
103 self.channels = self.generate(self.num, self.channel_start)
Chetan Gaonkercbe79642016-03-09 17:45:58 -0800104 self.group_channel_map = {}
Chetan Gaonkercd86bdd2016-03-17 00:08:12 -0700105 #assert_equal(len(self.channels), self.num)
Chetan Gaonkercbe79642016-03-09 17:45:58 -0800106 for i in range(self.num):
107 self.group_channel_map[self.channels[i]] = i
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800108 self.state = self.Stopped
109 self.streams = None
110 self.channel_states = {}
111 self.last_chan = None
112 self.recv_sock = L2Socket(iface = iface, type = ETH_P_IP)
113 self.iface_mcast = iface_mcast
114 self.mcast_cb = mcast_cb
115 for c in range(self.num):
116 self.channel_states[c] = [self.Idle]
Chetan Gaonkercd86bdd2016-03-17 00:08:12 -0700117 IgmpChannel.__init__(self, ssm_list = self.channels, iface=iface)
118
119 def generate(self, num, channel_start = 0):
120 start = (224 << 24) | ( ( (channel_start >> 16) & 0xff) << 16 ) | \
121 ( ( (channel_start >> 8) & 0xff ) << 8 ) | (channel_start) & 0xff
122 start += channel_start/256 + 1
123 end = start + num
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800124 group_addrs = []
Chetan Gaonkercd86bdd2016-03-17 00:08:12 -0700125 count = 0
126 while count != num:
127 for i in range(start, end):
128 if i&255:
129 g = '%s.%s.%s.%s' %((i>>24) &0xff, (i>>16)&0xff, (i>>8)&0xff, i&0xff)
130 log.debug('Adding group %s' %g)
131 group_addrs.append(g)
132 count += 1
133 start = end
134 end = start + 1
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800135 return group_addrs
136
137 def start(self):
138 if self.state == self.Stopped:
139 if self.streams:
140 self.streams.stop()
141 self.streams = McastTraffic(self.channels, iface=self.iface_mcast, cb = self.mcast_cb)
142 self.streams.start()
143 self.state = self.Started
144
145 def join(self, chan = None):
146 if chan is None:
147 chan = random.randint(0, self.num)
148 else:
149 if chan >= self.num:
150 chan = 0
151
152 if self.get_state(chan) == self.Joined:
Chetan Gaonkercbe79642016-03-09 17:45:58 -0800153 return chan, 0
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800154
155 groups = [self.channels[chan]]
Chetan Gaonkercbe79642016-03-09 17:45:58 -0800156 join_start = monotonic.monotonic()
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800157 self.igmp_join(groups)
158 self.set_state(chan, self.Joined)
159 self.last_chan = chan
Chetan Gaonkercbe79642016-03-09 17:45:58 -0800160 return chan, join_start
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800161
162 def leave(self, chan):
163 if chan is None:
164 chan = self.last_chan
165 if chan is None or chan >= self.num:
166 return False
167 if self.get_state(chan) != self.Joined:
168 return False
169 groups = [self.channels[chan]]
170 self.igmp_leave(groups)
171 self.set_state(chan, self.Idle)
172 if chan == self.last_chan:
173 self.last_chan = None
174 return True
175
176 def join_next(self, chan = None):
177 if chan is None:
178 chan = self.last_chan
179 if chan is None:
180 return None
181 leave = chan
182 join = chan+1
183 else:
184 leave = chan - 1
185 join = chan
186
187 if join >= self.num:
188 join = 0
189
190 if leave >= 0 and leave != join:
191 self.leave(leave)
192
193 return self.join(join)
194
195 def jump(self):
196 chan = self.last_chan
197 if chan is not None:
198 self.leave(chan)
199 s_next = chan
200 else:
201 s_next = 0
Chetan Gaonkercbe79642016-03-09 17:45:58 -0800202 if self.num - s_next < 2:
203 s_next = 0
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800204 chan = random.randint(s_next, self.num)
205 return self.join(chan)
206
207 def gaddr(self, chan):
Chetan Gaonkercbe79642016-03-09 17:45:58 -0800208 '''Return the group address for a channel'''
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800209 if chan >= self.num:
210 return None
211 return self.channels[chan]
212
Chetan Gaonkercbe79642016-03-09 17:45:58 -0800213 def caddr(self, group):
214 '''Return a channel given a group addr'''
215 if self.group_channel_map.has_key(group):
216 return self.group_channel_map[group]
217 return None
218
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800219 def recv_cb(self, pkt):
220 '''Default channel receive callback'''
221 log.debug('Received packet from source %s, destination %s' %(pkt[IP].src, pkt[IP].dst))
222 send_time = float(pkt[IP].payload.load)
223 recv_time = monotonic.monotonic()
224 log.debug('Packet received in %.3f usecs' %(recv_time - send_time))
225
226 def recv(self, chan, cb = None, count = 1):
227 if chan is None:
228 return None
229 if type(chan) == type([]) or type(chan) == type(()):
230 channel_list=filter(lambda c: c < self.num, chan)
231 groups = map(lambda c: self.gaddr(c), channel_list)
232 else:
233 groups = (self.gaddr(chan),)
234 if cb is None:
235 cb = self.recv_cb
236 sniff(prn = cb, count=count, lfilter = lambda p: p[IP].dst in groups, opened_socket = self.recv_sock)
237
238 def stop(self):
239 if self.streams:
240 self.streams.stop()
241 self.state = self.Stopped
242
243 def get_state(self, chan):
244 return self.channel_states[chan][0]
245
246 def set_state(self, chan, state):
247 self.channel_states[chan][0] = state
248
249if __name__ == '__main__':
Chetan Gaonkercd86bdd2016-03-17 00:08:12 -0700250 num = 5
251 start = 0
252 ssm_list = []
253 for i in xrange(2):
254 channels = Channels(num, start)
255 ssm_list += channels.channels
256 start += num
257 igmpChannel = IgmpChannel()
258 igmpChannel.igmp_load_ssm_config(ssm_list)
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800259 channels.start()
260 for i in range(num):
261 channels.join(i)
262 for i in range(num):
263 channels.recv(i)
264 for i in range(num):
265 channels.leave(i)
266 channels.stop()