A R Karthick | 338268f | 2016-06-21 17:12:13 -0700 | [diff] [blame] | 1 | # |
Chetan Gaonker | cfcce78 | 2016-05-10 10:10:42 -0700 | [diff] [blame] | 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 |
A R Karthick | 338268f | 2016-06-21 17:12:13 -0700 | [diff] [blame] | 7 | # |
Chetan Gaonker | cfcce78 | 2016-05-10 10:10:42 -0700 | [diff] [blame] | 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
A R Karthick | 338268f | 2016-06-21 17:12:13 -0700 | [diff] [blame] | 9 | # |
Chetan Gaonker | cfcce78 | 2016-05-10 10:10:42 -0700 | [diff] [blame] | 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) |
A R Karthick | 338268f | 2016-06-21 17:12:13 -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() |
A R Karthick | 338268f | 2016-06-21 17:12:13 -0700 | [diff] [blame] | 48 | |
Chetan Gaonker | cd86bdd | 2016-03-17 00:08:12 -0700 | [diff] [blame] | 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 |
Chetan Gaonker | b424ff8 | 2016-03-08 12:11:12 -0800 | [diff] [blame] | 127 | self.iface_mcast = iface_mcast |
| 128 | self.mcast_cb = mcast_cb |
| 129 | for c in range(self.num): |
| 130 | self.channel_states[c] = [self.Idle] |
Chetan Gaonker | cd86bdd | 2016-03-17 00:08:12 -0700 | [diff] [blame] | 131 | IgmpChannel.__init__(self, ssm_list = self.channels, iface=iface) |
A R Karthick | 338268f | 2016-06-21 17:12:13 -0700 | [diff] [blame] | 132 | |
Chetan Gaonker | cd86bdd | 2016-03-17 00:08:12 -0700 | [diff] [blame] | 133 | def generate(self, num, channel_start = 0): |
Chetan Gaonker | 38737f8 | 2016-05-11 17:44:17 -0700 | [diff] [blame] | 134 | start = (225 << 24) | ( ( (channel_start >> 16) & 0xff) << 16 ) | \ |
Chetan Gaonker | cd86bdd | 2016-03-17 00:08:12 -0700 | [diff] [blame] | 135 | ( ( (channel_start >> 8) & 0xff ) << 8 ) | (channel_start) & 0xff |
| 136 | start += channel_start/256 + 1 |
| 137 | end = start + num |
Chetan Gaonker | b424ff8 | 2016-03-08 12:11:12 -0800 | [diff] [blame] | 138 | group_addrs = [] |
Chetan Gaonker | cd86bdd | 2016-03-17 00:08:12 -0700 | [diff] [blame] | 139 | count = 0 |
| 140 | while count != num: |
| 141 | for i in range(start, end): |
| 142 | if i&255: |
| 143 | g = '%s.%s.%s.%s' %((i>>24) &0xff, (i>>16)&0xff, (i>>8)&0xff, i&0xff) |
| 144 | log.debug('Adding group %s' %g) |
| 145 | group_addrs.append(g) |
| 146 | count += 1 |
| 147 | start = end |
| 148 | end = start + 1 |
Chetan Gaonker | b424ff8 | 2016-03-08 12:11:12 -0800 | [diff] [blame] | 149 | return group_addrs |
| 150 | |
| 151 | def start(self): |
| 152 | if self.state == self.Stopped: |
| 153 | if self.streams: |
| 154 | self.streams.stop() |
| 155 | self.streams = McastTraffic(self.channels, iface=self.iface_mcast, cb = self.mcast_cb) |
| 156 | self.streams.start() |
| 157 | self.state = self.Started |
| 158 | |
| 159 | def join(self, chan = None): |
| 160 | if chan is None: |
| 161 | chan = random.randint(0, self.num) |
| 162 | else: |
| 163 | if chan >= self.num: |
| 164 | chan = 0 |
| 165 | |
| 166 | if self.get_state(chan) == self.Joined: |
Chetan Gaonker | cbe7964 | 2016-03-09 17:45:58 -0800 | [diff] [blame] | 167 | return chan, 0 |
Chetan Gaonker | b424ff8 | 2016-03-08 12:11:12 -0800 | [diff] [blame] | 168 | |
| 169 | groups = [self.channels[chan]] |
Chetan Gaonker | cbe7964 | 2016-03-09 17:45:58 -0800 | [diff] [blame] | 170 | join_start = monotonic.monotonic() |
Chetan Gaonker | b424ff8 | 2016-03-08 12:11:12 -0800 | [diff] [blame] | 171 | self.igmp_join(groups) |
| 172 | self.set_state(chan, self.Joined) |
| 173 | self.last_chan = chan |
Chetan Gaonker | cbe7964 | 2016-03-09 17:45:58 -0800 | [diff] [blame] | 174 | return chan, join_start |
Chetan Gaonker | b424ff8 | 2016-03-08 12:11:12 -0800 | [diff] [blame] | 175 | |
| 176 | def leave(self, chan): |
| 177 | if chan is None: |
| 178 | chan = self.last_chan |
| 179 | if chan is None or chan >= self.num: |
| 180 | return False |
| 181 | if self.get_state(chan) != self.Joined: |
| 182 | return False |
| 183 | groups = [self.channels[chan]] |
| 184 | self.igmp_leave(groups) |
| 185 | self.set_state(chan, self.Idle) |
| 186 | if chan == self.last_chan: |
| 187 | self.last_chan = None |
| 188 | return True |
A R Karthick | 338268f | 2016-06-21 17:12:13 -0700 | [diff] [blame] | 189 | |
Chetan Gaonker | b424ff8 | 2016-03-08 12:11:12 -0800 | [diff] [blame] | 190 | def join_next(self, chan = None): |
| 191 | if chan is None: |
| 192 | chan = self.last_chan |
| 193 | if chan is None: |
| 194 | return None |
| 195 | leave = chan |
| 196 | join = chan+1 |
| 197 | else: |
| 198 | leave = chan - 1 |
| 199 | join = chan |
A R Karthick | 338268f | 2016-06-21 17:12:13 -0700 | [diff] [blame] | 200 | |
Chetan Gaonker | b424ff8 | 2016-03-08 12:11:12 -0800 | [diff] [blame] | 201 | if join >= self.num: |
| 202 | join = 0 |
| 203 | |
| 204 | if leave >= 0 and leave != join: |
| 205 | self.leave(leave) |
| 206 | |
| 207 | return self.join(join) |
| 208 | |
| 209 | def jump(self): |
| 210 | chan = self.last_chan |
| 211 | if chan is not None: |
| 212 | self.leave(chan) |
| 213 | s_next = chan |
| 214 | else: |
| 215 | s_next = 0 |
Chetan Gaonker | cbe7964 | 2016-03-09 17:45:58 -0800 | [diff] [blame] | 216 | if self.num - s_next < 2: |
| 217 | s_next = 0 |
Chetan Gaonker | b424ff8 | 2016-03-08 12:11:12 -0800 | [diff] [blame] | 218 | chan = random.randint(s_next, self.num) |
| 219 | return self.join(chan) |
| 220 | |
| 221 | def gaddr(self, chan): |
Chetan Gaonker | cbe7964 | 2016-03-09 17:45:58 -0800 | [diff] [blame] | 222 | '''Return the group address for a channel''' |
Chetan Gaonker | b424ff8 | 2016-03-08 12:11:12 -0800 | [diff] [blame] | 223 | if chan >= self.num: |
| 224 | return None |
| 225 | return self.channels[chan] |
| 226 | |
Chetan Gaonker | cbe7964 | 2016-03-09 17:45:58 -0800 | [diff] [blame] | 227 | def caddr(self, group): |
| 228 | '''Return a channel given a group addr''' |
| 229 | if self.group_channel_map.has_key(group): |
| 230 | return self.group_channel_map[group] |
| 231 | return None |
| 232 | |
Chetan Gaonker | b424ff8 | 2016-03-08 12:11:12 -0800 | [diff] [blame] | 233 | def recv_cb(self, pkt): |
| 234 | '''Default channel receive callback''' |
| 235 | log.debug('Received packet from source %s, destination %s' %(pkt[IP].src, pkt[IP].dst)) |
| 236 | send_time = float(pkt[IP].payload.load) |
| 237 | recv_time = monotonic.monotonic() |
| 238 | log.debug('Packet received in %.3f usecs' %(recv_time - send_time)) |
| 239 | |
A R Karthick | 338268f | 2016-06-21 17:12:13 -0700 | [diff] [blame] | 240 | def recv(self, chan, cb = None, count = 1, timeout = 5): |
Chetan Gaonker | b424ff8 | 2016-03-08 12:11:12 -0800 | [diff] [blame] | 241 | if chan is None: |
| 242 | return None |
| 243 | if type(chan) == type([]) or type(chan) == type(()): |
| 244 | channel_list=filter(lambda c: c < self.num, chan) |
| 245 | groups = map(lambda c: self.gaddr(c), channel_list) |
| 246 | else: |
| 247 | groups = (self.gaddr(chan),) |
| 248 | if cb is None: |
| 249 | cb = self.recv_cb |
A R Karthick | 338268f | 2016-06-21 17:12:13 -0700 | [diff] [blame] | 250 | return sniff(prn = cb, count=count, timeout = timeout, |
| 251 | lfilter = lambda p: IP in p and p[IP].dst in groups, iface = self.iface) |
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() |