Chetan Gaonker | cfcce78 | 2016-05-10 10:10:42 -0700 | [diff] [blame] | 1 | # |
| 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 Gaonker | b424ff8 | 2016-03-08 12:11:12 -0800 | [diff] [blame] | 16 | import threading |
| 17 | import sys |
| 18 | import os |
| 19 | import time |
| 20 | import monotonic |
| 21 | import random |
| 22 | from scapy.all import * |
| 23 | from McastTraffic import * |
| 24 | from IGMP import * |
| 25 | from OnosCtrl import OnosCtrl |
| 26 | from nose.tools import * |
| 27 | log.setLevel('DEBUG') |
| 28 | |
| 29 | conf.verb = 0 |
| 30 | |
| 31 | class 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 Gaonker | cd86bdd | 2016-03-17 00:08:12 -0700 | [diff] [blame] | 39 | ssm_list = [] |
Chetan Gaonker | b424ff8 | 2016-03-08 12:11:12 -0800 | [diff] [blame] | 40 | |
Chetan Gaonker | cd86bdd | 2016-03-17 00:08:12 -0700 | [diff] [blame] | 41 | 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] | 42 | self.iface = iface |
Chetan Gaonker | cd86bdd | 2016-03-17 00:08:12 -0700 | [diff] [blame] | 43 | self.ssm_list += ssm_list |
Chetan Gaonker | b424ff8 | 2016-03-08 12:11:12 -0800 | [diff] [blame] | 44 | self.src_list = src_list |
| 45 | self.delay = delay |
| 46 | self.onos_ctrl = OnosCtrl('org.onosproject.igmp') |
| 47 | self.onos_ctrl.activate() |
Chetan Gaonker | cd86bdd | 2016-03-17 00:08:12 -0700 | [diff] [blame] | 48 | |
| 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 Gaonker | b424ff8 | 2016-03-08 12:11:12 -0800 | [diff] [blame] | 53 | |
Chetan Gaonker | cd86bdd | 2016-03-17 00:08:12 -0700 | [diff] [blame] | 54 | def igmp_join(self, groups): |
Chetan Gaonker | b424ff8 | 2016-03-08 12:11:12 -0800 | [diff] [blame] | 55 | igmp = IGMPv3(type = IGMP_TYPE_V3_MEMBERSHIP_REPORT, max_resp_code=30, |
| 56 | gaddr='224.0.1.1') |
| 57 | for g in groups: |
Chetan Gaonker | 38737f8 | 2016-05-11 17:44:17 -0700 | [diff] [blame] | 58 | gr = IGMPv3gr(rtype=IGMP_V3_GR_TYPE_INCLUDE, mcaddr=g) |
Chetan Gaonker | b424ff8 | 2016-03-08 12:11:12 -0800 | [diff] [blame] | 59 | 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 Gaonker | 38737f8 | 2016-05-11 17:44:17 -0700 | [diff] [blame] | 72 | gr = IGMPv3gr(rtype=IGMP_V3_GR_TYPE_EXCLUDE, mcaddr=g) |
Chetan Gaonker | b424ff8 | 2016-03-08 12:11:12 -0800 | [diff] [blame] | 73 | 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 Gaonker | a2b87df | 2016-03-31 15:41:31 -0700 | [diff] [blame] | 83 | status, code = OnosCtrl.config(config) |
Chetan Gaonker | b424ff8 | 2016-03-08 12:11:12 -0800 | [diff] [blame] | 84 | if status is False: |
Chetan Gaonker | a58ab6e | 2016-03-23 15:04:20 -0700 | [diff] [blame] | 85 | log.info('JSON config request returned status %d' %code) |
Chetan Gaonker | b424ff8 | 2016-03-08 12:11:12 -0800 | [diff] [blame] | 86 | 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 Gaonker | a58ab6e | 2016-03-23 15:04:20 -0700 | [diff] [blame] | 99 | 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 Gaonker | b424ff8 | 2016-03-08 12:11:12 -0800 | [diff] [blame] | 110 | class Channels(IgmpChannel): |
| 111 | Stopped = 0 |
| 112 | Started = 1 |
| 113 | Idle = 0 |
| 114 | Joined = 1 |
Chetan Gaonker | cd86bdd | 2016-03-17 00:08:12 -0700 | [diff] [blame] | 115 | 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] | 116 | self.num = num |
Chetan Gaonker | cd86bdd | 2016-03-17 00:08:12 -0700 | [diff] [blame] | 117 | self.channel_start = channel_start |
| 118 | self.channels = self.generate(self.num, self.channel_start) |
Chetan Gaonker | cbe7964 | 2016-03-09 17:45:58 -0800 | [diff] [blame] | 119 | self.group_channel_map = {} |
Chetan Gaonker | cd86bdd | 2016-03-17 00:08:12 -0700 | [diff] [blame] | 120 | #assert_equal(len(self.channels), self.num) |
Chetan Gaonker | cbe7964 | 2016-03-09 17:45:58 -0800 | [diff] [blame] | 121 | for i in range(self.num): |
| 122 | self.group_channel_map[self.channels[i]] = i |
Chetan Gaonker | b424ff8 | 2016-03-08 12:11:12 -0800 | [diff] [blame] | 123 | 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 Gaonker | cd86bdd | 2016-03-17 00:08:12 -0700 | [diff] [blame] | 132 | IgmpChannel.__init__(self, ssm_list = self.channels, iface=iface) |
| 133 | |
| 134 | def generate(self, num, channel_start = 0): |
Chetan Gaonker | 38737f8 | 2016-05-11 17:44:17 -0700 | [diff] [blame] | 135 | start = (225 << 24) | ( ( (channel_start >> 16) & 0xff) << 16 ) | \ |
Chetan Gaonker | cd86bdd | 2016-03-17 00:08:12 -0700 | [diff] [blame] | 136 | ( ( (channel_start >> 8) & 0xff ) << 8 ) | (channel_start) & 0xff |
| 137 | start += channel_start/256 + 1 |
| 138 | end = start + num |
Chetan Gaonker | b424ff8 | 2016-03-08 12:11:12 -0800 | [diff] [blame] | 139 | group_addrs = [] |
Chetan Gaonker | cd86bdd | 2016-03-17 00:08:12 -0700 | [diff] [blame] | 140 | 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 Gaonker | b424ff8 | 2016-03-08 12:11:12 -0800 | [diff] [blame] | 150 | 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 Gaonker | cbe7964 | 2016-03-09 17:45:58 -0800 | [diff] [blame] | 168 | return chan, 0 |
Chetan Gaonker | b424ff8 | 2016-03-08 12:11:12 -0800 | [diff] [blame] | 169 | |
| 170 | groups = [self.channels[chan]] |
Chetan Gaonker | cbe7964 | 2016-03-09 17:45:58 -0800 | [diff] [blame] | 171 | join_start = monotonic.monotonic() |
Chetan Gaonker | b424ff8 | 2016-03-08 12:11:12 -0800 | [diff] [blame] | 172 | self.igmp_join(groups) |
| 173 | self.set_state(chan, self.Joined) |
| 174 | self.last_chan = chan |
Chetan Gaonker | cbe7964 | 2016-03-09 17:45:58 -0800 | [diff] [blame] | 175 | return chan, join_start |
Chetan Gaonker | b424ff8 | 2016-03-08 12:11:12 -0800 | [diff] [blame] | 176 | |
| 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 Gaonker | cbe7964 | 2016-03-09 17:45:58 -0800 | [diff] [blame] | 217 | if self.num - s_next < 2: |
| 218 | s_next = 0 |
Chetan Gaonker | b424ff8 | 2016-03-08 12:11:12 -0800 | [diff] [blame] | 219 | chan = random.randint(s_next, self.num) |
| 220 | return self.join(chan) |
| 221 | |
| 222 | def gaddr(self, chan): |
Chetan Gaonker | cbe7964 | 2016-03-09 17:45:58 -0800 | [diff] [blame] | 223 | '''Return the group address for a channel''' |
Chetan Gaonker | b424ff8 | 2016-03-08 12:11:12 -0800 | [diff] [blame] | 224 | if chan >= self.num: |
| 225 | return None |
| 226 | return self.channels[chan] |
| 227 | |
Chetan Gaonker | cbe7964 | 2016-03-09 17:45:58 -0800 | [diff] [blame] | 228 | 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 Gaonker | b424ff8 | 2016-03-08 12:11:12 -0800 | [diff] [blame] | 234 | 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 Gaonker | 38737f8 | 2016-05-11 17:44:17 -0700 | [diff] [blame] | 251 | sniff(prn = cb, count=count, lfilter = lambda p: IP in p and p[IP].dst in groups, opened_socket = self.recv_sock) |
Chetan Gaonker | b424ff8 | 2016-03-08 12:11:12 -0800 | [diff] [blame] | 252 | |
| 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 | |
| 264 | if __name__ == '__main__': |
Chetan Gaonker | cd86bdd | 2016-03-17 00:08:12 -0700 | [diff] [blame] | 265 | 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 Gaonker | b424ff8 | 2016-03-08 12:11:12 -0800 | [diff] [blame] | 274 | 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() |