Chetan Gaonker | b424ff8 | 2016-03-08 12:11:12 -0800 | [diff] [blame] | 1 | import threading |
| 2 | import sys |
| 3 | import os |
| 4 | import time |
| 5 | import monotonic |
| 6 | import random |
| 7 | from scapy.all import * |
| 8 | from McastTraffic import * |
| 9 | from IGMP import * |
| 10 | from OnosCtrl import OnosCtrl |
| 11 | from nose.tools import * |
| 12 | log.setLevel('DEBUG') |
| 13 | |
| 14 | conf.verb = 0 |
| 15 | |
| 16 | class 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 Gaonker | cd86bdd | 2016-03-17 00:08:12 -0700 | [diff] [blame] | 24 | ssm_list = [] |
Chetan Gaonker | b424ff8 | 2016-03-08 12:11:12 -0800 | [diff] [blame] | 25 | |
Chetan Gaonker | cd86bdd | 2016-03-17 00:08:12 -0700 | [diff] [blame] | 26 | def __init__(self, iface = 'veth0', ssm_list = [], src_list = ['1.2.3.4'], delay = 2): |
Chetan Gaonker | b424ff8 | 2016-03-08 12:11:12 -0800 | [diff] [blame] | 27 | self.iface = iface |
Chetan Gaonker | cd86bdd | 2016-03-17 00:08:12 -0700 | [diff] [blame] | 28 | self.ssm_list += ssm_list |
Chetan Gaonker | b424ff8 | 2016-03-08 12:11:12 -0800 | [diff] [blame] | 29 | self.src_list = src_list |
| 30 | self.delay = delay |
| 31 | self.onos_ctrl = OnosCtrl('org.onosproject.igmp') |
| 32 | self.onos_ctrl.activate() |
Chetan Gaonker | cd86bdd | 2016-03-17 00:08:12 -0700 | [diff] [blame] | 33 | |
| 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 Gaonker | b424ff8 | 2016-03-08 12:11:12 -0800 | [diff] [blame] | 38 | |
Chetan Gaonker | cd86bdd | 2016-03-17 00:08:12 -0700 | [diff] [blame] | 39 | def igmp_join(self, groups): |
Chetan Gaonker | b424ff8 | 2016-03-08 12:11:12 -0800 | [diff] [blame] | 40 | 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 Gaonker | a2b87df | 2016-03-31 15:41:31 -0700 | [diff] [blame] | 68 | status, code = OnosCtrl.config(config) |
Chetan Gaonker | b424ff8 | 2016-03-08 12:11:12 -0800 | [diff] [blame] | 69 | if status is False: |
Chetan Gaonker | a58ab6e | 2016-03-23 15:04:20 -0700 | [diff] [blame] | 70 | log.info('JSON config request returned status %d' %code) |
Chetan Gaonker | b424ff8 | 2016-03-08 12:11:12 -0800 | [diff] [blame] | 71 | 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 Gaonker | a58ab6e | 2016-03-23 15:04:20 -0700 | [diff] [blame] | 84 | 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 Gaonker | b424ff8 | 2016-03-08 12:11:12 -0800 | [diff] [blame] | 95 | class Channels(IgmpChannel): |
| 96 | Stopped = 0 |
| 97 | Started = 1 |
| 98 | Idle = 0 |
| 99 | Joined = 1 |
Chetan Gaonker | cd86bdd | 2016-03-17 00:08:12 -0700 | [diff] [blame] | 100 | def __init__(self, num, channel_start = 0, iface = 'veth0', iface_mcast = 'veth2', mcast_cb = None): |
Chetan Gaonker | b424ff8 | 2016-03-08 12:11:12 -0800 | [diff] [blame] | 101 | self.num = num |
Chetan Gaonker | cd86bdd | 2016-03-17 00:08:12 -0700 | [diff] [blame] | 102 | self.channel_start = channel_start |
| 103 | self.channels = self.generate(self.num, self.channel_start) |
Chetan Gaonker | cbe7964 | 2016-03-09 17:45:58 -0800 | [diff] [blame] | 104 | self.group_channel_map = {} |
Chetan Gaonker | cd86bdd | 2016-03-17 00:08:12 -0700 | [diff] [blame] | 105 | #assert_equal(len(self.channels), self.num) |
Chetan Gaonker | cbe7964 | 2016-03-09 17:45:58 -0800 | [diff] [blame] | 106 | for i in range(self.num): |
| 107 | self.group_channel_map[self.channels[i]] = i |
Chetan Gaonker | b424ff8 | 2016-03-08 12:11:12 -0800 | [diff] [blame] | 108 | 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 Gaonker | cd86bdd | 2016-03-17 00:08:12 -0700 | [diff] [blame] | 117 | 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 Gaonker | b424ff8 | 2016-03-08 12:11:12 -0800 | [diff] [blame] | 124 | group_addrs = [] |
Chetan Gaonker | cd86bdd | 2016-03-17 00:08:12 -0700 | [diff] [blame] | 125 | 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 Gaonker | b424ff8 | 2016-03-08 12:11:12 -0800 | [diff] [blame] | 135 | 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 Gaonker | cbe7964 | 2016-03-09 17:45:58 -0800 | [diff] [blame] | 153 | return chan, 0 |
Chetan Gaonker | b424ff8 | 2016-03-08 12:11:12 -0800 | [diff] [blame] | 154 | |
| 155 | groups = [self.channels[chan]] |
Chetan Gaonker | cbe7964 | 2016-03-09 17:45:58 -0800 | [diff] [blame] | 156 | join_start = monotonic.monotonic() |
Chetan Gaonker | b424ff8 | 2016-03-08 12:11:12 -0800 | [diff] [blame] | 157 | self.igmp_join(groups) |
| 158 | self.set_state(chan, self.Joined) |
| 159 | self.last_chan = chan |
Chetan Gaonker | cbe7964 | 2016-03-09 17:45:58 -0800 | [diff] [blame] | 160 | return chan, join_start |
Chetan Gaonker | b424ff8 | 2016-03-08 12:11:12 -0800 | [diff] [blame] | 161 | |
| 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 Gaonker | cbe7964 | 2016-03-09 17:45:58 -0800 | [diff] [blame] | 202 | if self.num - s_next < 2: |
| 203 | s_next = 0 |
Chetan Gaonker | b424ff8 | 2016-03-08 12:11:12 -0800 | [diff] [blame] | 204 | chan = random.randint(s_next, self.num) |
| 205 | return self.join(chan) |
| 206 | |
| 207 | def gaddr(self, chan): |
Chetan Gaonker | cbe7964 | 2016-03-09 17:45:58 -0800 | [diff] [blame] | 208 | '''Return the group address for a channel''' |
Chetan Gaonker | b424ff8 | 2016-03-08 12:11:12 -0800 | [diff] [blame] | 209 | if chan >= self.num: |
| 210 | return None |
| 211 | return self.channels[chan] |
| 212 | |
Chetan Gaonker | cbe7964 | 2016-03-09 17:45:58 -0800 | [diff] [blame] | 213 | 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 Gaonker | b424ff8 | 2016-03-08 12:11:12 -0800 | [diff] [blame] | 219 | 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 | |
| 249 | if __name__ == '__main__': |
Chetan Gaonker | cd86bdd | 2016-03-17 00:08:12 -0700 | [diff] [blame] | 250 | 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 Gaonker | b424ff8 | 2016-03-08 12:11:12 -0800 | [diff] [blame] | 259 | 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() |