blob: 90d4f3cd41cc3c5c30e53540fb50d25552532b7c [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)
24
25 def __init__(self, iface = 'veth0', src_list = ['1.2.3.4'], delay = 2):
26 self.iface = iface
27 self.src_list = src_list
28 self.delay = delay
29 self.onos_ctrl = OnosCtrl('org.onosproject.igmp')
30 self.onos_ctrl.activate()
31
32 def igmp_join(self, groups):
33 self.ssm_table_load(groups)
34 igmp = IGMPv3(type = IGMP_TYPE_V3_MEMBERSHIP_REPORT, max_resp_code=30,
35 gaddr='224.0.1.1')
36 for g in groups:
37 gr = IGMPv3gr(rtype=IGMP_V3_GR_TYPE_EXCLUDE, mcaddr=g)
38 gr.sources = self.src_list
39 igmp.grps.append(gr)
40
41 pkt = self.igmp_eth/self.igmp_ip/igmp
42 IGMPv3.fixup(pkt)
43 sendp(pkt, iface=self.iface)
44 if self.delay != 0:
45 time.sleep(self.delay)
46
47 def igmp_leave(self, groups):
48 igmp = IGMPv3(type = IGMP_TYPE_V3_MEMBERSHIP_REPORT, max_resp_code=30,
49 gaddr='224.0.1.1')
50 for g in groups:
51 gr = IGMPv3gr(rtype=IGMP_V3_GR_TYPE_INCLUDE, mcaddr=g)
52 gr.sources = self.src_list
53 igmp.grps.append(gr)
54
55 pkt = self.igmp_eth/self.igmp_ip/igmp
56 IGMPv3.fixup(pkt)
57 sendp(pkt, iface = self.iface)
58 if self.delay != 0:
59 time.sleep(self.delay)
60
61 def onos_load_config(self, config):
62 status, code = self.onos_ctrl.config(config)
63 if status is False:
64 log.info('JSON config request for app %s returned status %d' %code)
65 time.sleep(2)
66
67 def ssm_table_load(self, groups):
68 ssm_dict = {'apps' : { 'org.onosproject.igmp' : { 'ssmTranslate' : [] } } }
69 ssm_xlate_list = ssm_dict['apps']['org.onosproject.igmp']['ssmTranslate']
70 for g in groups:
71 for s in self.src_list:
72 d = {}
73 d['source'] = s
74 d['group'] = g
75 ssm_xlate_list.append(d)
76 self.onos_load_config(ssm_dict)
77
78class Channels(IgmpChannel):
79 Stopped = 0
80 Started = 1
81 Idle = 0
82 Joined = 1
83 def __init__(self, num, iface = 'veth0', iface_mcast = 'veth2', mcast_cb = None):
84 self.num = num
85 self.channels = self.generate(self.num)
86 self.state = self.Stopped
87 self.streams = None
88 self.channel_states = {}
89 self.last_chan = None
90 self.recv_sock = L2Socket(iface = iface, type = ETH_P_IP)
91 self.iface_mcast = iface_mcast
92 self.mcast_cb = mcast_cb
93 for c in range(self.num):
94 self.channel_states[c] = [self.Idle]
95
96 IgmpChannel.__init__(self, iface=iface)
97
98 def generate(self, num):
99 start = (224 << 24) | 1
100 end = start + num + num/256
101 group_addrs = []
102 for i in range(start, end):
103 if i&255:
104 g = '%s.%s.%s.%s' %((i>>24) &0xff, (i>>16)&0xff, (i>>8)&0xff, i&0xff)
105 log.debug('Adding group %s' %g)
106 group_addrs.append(g)
107 return group_addrs
108
109 def start(self):
110 if self.state == self.Stopped:
111 if self.streams:
112 self.streams.stop()
113 self.streams = McastTraffic(self.channels, iface=self.iface_mcast, cb = self.mcast_cb)
114 self.streams.start()
115 self.state = self.Started
116
117 def join(self, chan = None):
118 if chan is None:
119 chan = random.randint(0, self.num)
120 else:
121 if chan >= self.num:
122 chan = 0
123
124 if self.get_state(chan) == self.Joined:
125 return chan
126
127 groups = [self.channels[chan]]
128 self.igmp_join(groups)
129 self.set_state(chan, self.Joined)
130 self.last_chan = chan
131 return chan
132
133 def leave(self, chan):
134 if chan is None:
135 chan = self.last_chan
136 if chan is None or chan >= self.num:
137 return False
138 if self.get_state(chan) != self.Joined:
139 return False
140 groups = [self.channels[chan]]
141 self.igmp_leave(groups)
142 self.set_state(chan, self.Idle)
143 if chan == self.last_chan:
144 self.last_chan = None
145 return True
146
147 def join_next(self, chan = None):
148 if chan is None:
149 chan = self.last_chan
150 if chan is None:
151 return None
152 leave = chan
153 join = chan+1
154 else:
155 leave = chan - 1
156 join = chan
157
158 if join >= self.num:
159 join = 0
160
161 if leave >= 0 and leave != join:
162 self.leave(leave)
163
164 return self.join(join)
165
166 def jump(self):
167 chan = self.last_chan
168 if chan is not None:
169 self.leave(chan)
170 s_next = chan
171 else:
172 s_next = 0
173 chan = random.randint(s_next, self.num)
174 return self.join(chan)
175
176 def gaddr(self, chan):
177 if chan >= self.num:
178 return None
179 return self.channels[chan]
180
181 def recv_cb(self, pkt):
182 '''Default channel receive callback'''
183 log.debug('Received packet from source %s, destination %s' %(pkt[IP].src, pkt[IP].dst))
184 send_time = float(pkt[IP].payload.load)
185 recv_time = monotonic.monotonic()
186 log.debug('Packet received in %.3f usecs' %(recv_time - send_time))
187
188 def recv(self, chan, cb = None, count = 1):
189 if chan is None:
190 return None
191 if type(chan) == type([]) or type(chan) == type(()):
192 channel_list=filter(lambda c: c < self.num, chan)
193 groups = map(lambda c: self.gaddr(c), channel_list)
194 else:
195 groups = (self.gaddr(chan),)
196 if cb is None:
197 cb = self.recv_cb
198 sniff(prn = cb, count=count, lfilter = lambda p: p[IP].dst in groups, opened_socket = self.recv_sock)
199
200 def stop(self):
201 if self.streams:
202 self.streams.stop()
203 self.state = self.Stopped
204
205 def get_state(self, chan):
206 return self.channel_states[chan][0]
207
208 def set_state(self, chan, state):
209 self.channel_states[chan][0] = state
210
211if __name__ == '__main__':
212 num = 2
213 channels = Channels(num)
214 channels.start()
215 for i in range(num):
216 channels.join(i)
217 for i in range(num):
218 channels.recv(i)
219 for i in range(num):
220 channels.leave(i)
221 channels.stop()