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 |
A.R Karthick | 2e99c47 | 2017-03-22 19:13:51 -0700 | [diff] [blame^] | 22 | import logging |
| 23 | logging.getLogger('scapy.runtime').setLevel(logging.ERROR) |
Chetan Gaonker | b424ff8 | 2016-03-08 12:11:12 -0800 | [diff] [blame] | 24 | from scapy.all import * |
| 25 | from McastTraffic import * |
| 26 | from IGMP import * |
| 27 | from OnosCtrl import OnosCtrl |
| 28 | from nose.tools import * |
| 29 | log.setLevel('DEBUG') |
| 30 | |
| 31 | conf.verb = 0 |
| 32 | |
| 33 | class IgmpChannel: |
| 34 | |
| 35 | IGMP_DST_MAC = "01:00:5e:00:01:01" |
| 36 | IGMP_SRC_MAC = "5a:e1:ac:ec:4d:a1" |
| 37 | IP_SRC = '1.2.3.4' |
| 38 | IP_DST = '224.0.1.1' |
| 39 | igmp_eth = Ether(dst = IGMP_DST_MAC, src = IGMP_SRC_MAC, type = ETH_P_IP) |
| 40 | igmp_ip = IP(dst = IP_DST, src = IP_SRC) |
A R Karthick | 338268f | 2016-06-21 17:12:13 -0700 | [diff] [blame] | 41 | ssm_list = [] |
Chetan Gaonker | b424ff8 | 2016-03-08 12:11:12 -0800 | [diff] [blame] | 42 | |
ChetanGaonker | 689b386 | 2016-10-17 16:25:01 -0700 | [diff] [blame] | 43 | def __init__(self, iface = 'veth0', ssm_list = [], src_list = ['1.2.3.4'], delay = 2,controller=None): |
| 44 | self.controller=controller |
Chetan Gaonker | b424ff8 | 2016-03-08 12:11:12 -0800 | [diff] [blame] | 45 | self.iface = iface |
Chetan Gaonker | cd86bdd | 2016-03-17 00:08:12 -0700 | [diff] [blame] | 46 | self.ssm_list += ssm_list |
Chetan Gaonker | b424ff8 | 2016-03-08 12:11:12 -0800 | [diff] [blame] | 47 | self.src_list = src_list |
| 48 | self.delay = delay |
ChetanGaonker | 689b386 | 2016-10-17 16:25:01 -0700 | [diff] [blame] | 49 | self.onos_ctrl = OnosCtrl('org.onosproject.igmp',controller=self.controller) |
Chetan Gaonker | b424ff8 | 2016-03-08 12:11:12 -0800 | [diff] [blame] | 50 | self.onos_ctrl.activate() |
A R Karthick | 338268f | 2016-06-21 17:12:13 -0700 | [diff] [blame] | 51 | |
Chetan Gaonker | cd86bdd | 2016-03-17 00:08:12 -0700 | [diff] [blame] | 52 | def igmp_load_ssm_config(self, ssm_list = []): |
| 53 | if not ssm_list: |
| 54 | ssm_list = self.ssm_list |
| 55 | self.ssm_table_load(ssm_list) |
Chetan Gaonker | b424ff8 | 2016-03-08 12:11:12 -0800 | [diff] [blame] | 56 | |
Chetan Gaonker | cd86bdd | 2016-03-17 00:08:12 -0700 | [diff] [blame] | 57 | def igmp_join(self, groups): |
Chetan Gaonker | b424ff8 | 2016-03-08 12:11:12 -0800 | [diff] [blame] | 58 | igmp = IGMPv3(type = IGMP_TYPE_V3_MEMBERSHIP_REPORT, max_resp_code=30, |
| 59 | gaddr='224.0.1.1') |
| 60 | for g in groups: |
Chetan Gaonker | 38737f8 | 2016-05-11 17:44:17 -0700 | [diff] [blame] | 61 | gr = IGMPv3gr(rtype=IGMP_V3_GR_TYPE_INCLUDE, mcaddr=g) |
Chetan Gaonker | b424ff8 | 2016-03-08 12:11:12 -0800 | [diff] [blame] | 62 | gr.sources = self.src_list |
| 63 | igmp.grps.append(gr) |
| 64 | |
| 65 | pkt = self.igmp_eth/self.igmp_ip/igmp |
| 66 | IGMPv3.fixup(pkt) |
| 67 | sendp(pkt, iface=self.iface) |
| 68 | if self.delay != 0: |
| 69 | time.sleep(self.delay) |
| 70 | |
| 71 | def igmp_leave(self, groups): |
| 72 | igmp = IGMPv3(type = IGMP_TYPE_V3_MEMBERSHIP_REPORT, max_resp_code=30, |
| 73 | gaddr='224.0.1.1') |
| 74 | for g in groups: |
Chetan Gaonker | 38737f8 | 2016-05-11 17:44:17 -0700 | [diff] [blame] | 75 | gr = IGMPv3gr(rtype=IGMP_V3_GR_TYPE_EXCLUDE, mcaddr=g) |
Chetan Gaonker | b424ff8 | 2016-03-08 12:11:12 -0800 | [diff] [blame] | 76 | gr.sources = self.src_list |
| 77 | igmp.grps.append(gr) |
| 78 | |
| 79 | pkt = self.igmp_eth/self.igmp_ip/igmp |
| 80 | IGMPv3.fixup(pkt) |
| 81 | sendp(pkt, iface = self.iface) |
| 82 | if self.delay != 0: |
| 83 | time.sleep(self.delay) |
| 84 | |
| 85 | def onos_load_config(self, config): |
ChetanGaonker | 689b386 | 2016-10-17 16:25:01 -0700 | [diff] [blame] | 86 | status, code = OnosCtrl.config(config,controller=self.controller) |
Chetan Gaonker | b424ff8 | 2016-03-08 12:11:12 -0800 | [diff] [blame] | 87 | if status is False: |
Chetan Gaonker | a58ab6e | 2016-03-23 15:04:20 -0700 | [diff] [blame] | 88 | log.info('JSON config request returned status %d' %code) |
Chetan Gaonker | b424ff8 | 2016-03-08 12:11:12 -0800 | [diff] [blame] | 89 | time.sleep(2) |
| 90 | |
| 91 | def ssm_table_load(self, groups): |
| 92 | ssm_dict = {'apps' : { 'org.onosproject.igmp' : { 'ssmTranslate' : [] } } } |
| 93 | ssm_xlate_list = ssm_dict['apps']['org.onosproject.igmp']['ssmTranslate'] |
| 94 | for g in groups: |
| 95 | for s in self.src_list: |
| 96 | d = {} |
| 97 | d['source'] = s |
| 98 | d['group'] = g |
| 99 | ssm_xlate_list.append(d) |
| 100 | self.onos_load_config(ssm_dict) |
| 101 | |
Chetan Gaonker | a58ab6e | 2016-03-23 15:04:20 -0700 | [diff] [blame] | 102 | def cord_port_table_load(self, cord_port_map): |
| 103 | cord_group_dict = {'apps' : { 'org.ciena.cordigmp' : { 'cordIgmpTranslate' : [] } } } |
| 104 | cord_group_xlate_list = cord_group_dict['apps']['org.ciena.cordigmp']['cordIgmpTranslate'] |
| 105 | for group, ports in cord_port_map.items(): |
| 106 | d = {} |
| 107 | d['group'] = group |
| 108 | d['inputPort'] = ports[0] |
| 109 | d['outputPort'] = ports[1] |
| 110 | cord_group_xlate_list.append(d) |
| 111 | self.onos_load_config(cord_group_dict) |
| 112 | |
Chetan Gaonker | b424ff8 | 2016-03-08 12:11:12 -0800 | [diff] [blame] | 113 | class Channels(IgmpChannel): |
| 114 | Stopped = 0 |
| 115 | Started = 1 |
| 116 | Idle = 0 |
| 117 | Joined = 1 |
Chetan Gaonker | cd86bdd | 2016-03-17 00:08:12 -0700 | [diff] [blame] | 118 | 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] | 119 | self.num = num |
Chetan Gaonker | cd86bdd | 2016-03-17 00:08:12 -0700 | [diff] [blame] | 120 | self.channel_start = channel_start |
| 121 | self.channels = self.generate(self.num, self.channel_start) |
Chetan Gaonker | cbe7964 | 2016-03-09 17:45:58 -0800 | [diff] [blame] | 122 | self.group_channel_map = {} |
Chetan Gaonker | cd86bdd | 2016-03-17 00:08:12 -0700 | [diff] [blame] | 123 | #assert_equal(len(self.channels), self.num) |
Chetan Gaonker | cbe7964 | 2016-03-09 17:45:58 -0800 | [diff] [blame] | 124 | for i in range(self.num): |
| 125 | self.group_channel_map[self.channels[i]] = i |
Chetan Gaonker | b424ff8 | 2016-03-08 12:11:12 -0800 | [diff] [blame] | 126 | self.state = self.Stopped |
| 127 | self.streams = None |
| 128 | self.channel_states = {} |
| 129 | self.last_chan = None |
Chetan Gaonker | b424ff8 | 2016-03-08 12:11:12 -0800 | [diff] [blame] | 130 | self.iface_mcast = iface_mcast |
| 131 | self.mcast_cb = mcast_cb |
| 132 | for c in range(self.num): |
| 133 | self.channel_states[c] = [self.Idle] |
Chetan Gaonker | cd86bdd | 2016-03-17 00:08:12 -0700 | [diff] [blame] | 134 | IgmpChannel.__init__(self, ssm_list = self.channels, iface=iface) |
A R Karthick | 338268f | 2016-06-21 17:12:13 -0700 | [diff] [blame] | 135 | |
Chetan Gaonker | cd86bdd | 2016-03-17 00:08:12 -0700 | [diff] [blame] | 136 | def generate(self, num, channel_start = 0): |
Chetan Gaonker | 38737f8 | 2016-05-11 17:44:17 -0700 | [diff] [blame] | 137 | start = (225 << 24) | ( ( (channel_start >> 16) & 0xff) << 16 ) | \ |
Chetan Gaonker | cd86bdd | 2016-03-17 00:08:12 -0700 | [diff] [blame] | 138 | ( ( (channel_start >> 8) & 0xff ) << 8 ) | (channel_start) & 0xff |
| 139 | start += channel_start/256 + 1 |
| 140 | end = start + num |
Chetan Gaonker | b424ff8 | 2016-03-08 12:11:12 -0800 | [diff] [blame] | 141 | group_addrs = [] |
Chetan Gaonker | cd86bdd | 2016-03-17 00:08:12 -0700 | [diff] [blame] | 142 | count = 0 |
| 143 | while count != num: |
| 144 | for i in range(start, end): |
| 145 | if i&255: |
| 146 | g = '%s.%s.%s.%s' %((i>>24) &0xff, (i>>16)&0xff, (i>>8)&0xff, i&0xff) |
| 147 | log.debug('Adding group %s' %g) |
| 148 | group_addrs.append(g) |
| 149 | count += 1 |
| 150 | start = end |
| 151 | end = start + 1 |
Chetan Gaonker | b424ff8 | 2016-03-08 12:11:12 -0800 | [diff] [blame] | 152 | return group_addrs |
| 153 | |
| 154 | def start(self): |
| 155 | if self.state == self.Stopped: |
| 156 | if self.streams: |
| 157 | self.streams.stop() |
| 158 | self.streams = McastTraffic(self.channels, iface=self.iface_mcast, cb = self.mcast_cb) |
| 159 | self.streams.start() |
| 160 | self.state = self.Started |
| 161 | |
| 162 | def join(self, chan = None): |
| 163 | if chan is None: |
| 164 | chan = random.randint(0, self.num) |
| 165 | else: |
| 166 | if chan >= self.num: |
| 167 | chan = 0 |
| 168 | |
| 169 | if self.get_state(chan) == self.Joined: |
Chetan Gaonker | cbe7964 | 2016-03-09 17:45:58 -0800 | [diff] [blame] | 170 | return chan, 0 |
Chetan Gaonker | b424ff8 | 2016-03-08 12:11:12 -0800 | [diff] [blame] | 171 | |
| 172 | groups = [self.channels[chan]] |
Chetan Gaonker | cbe7964 | 2016-03-09 17:45:58 -0800 | [diff] [blame] | 173 | join_start = monotonic.monotonic() |
Chetan Gaonker | b424ff8 | 2016-03-08 12:11:12 -0800 | [diff] [blame] | 174 | self.igmp_join(groups) |
| 175 | self.set_state(chan, self.Joined) |
| 176 | self.last_chan = chan |
Chetan Gaonker | cbe7964 | 2016-03-09 17:45:58 -0800 | [diff] [blame] | 177 | return chan, join_start |
Chetan Gaonker | b424ff8 | 2016-03-08 12:11:12 -0800 | [diff] [blame] | 178 | |
| 179 | def leave(self, chan): |
| 180 | if chan is None: |
| 181 | chan = self.last_chan |
| 182 | if chan is None or chan >= self.num: |
| 183 | return False |
| 184 | if self.get_state(chan) != self.Joined: |
| 185 | return False |
| 186 | groups = [self.channels[chan]] |
| 187 | self.igmp_leave(groups) |
| 188 | self.set_state(chan, self.Idle) |
| 189 | if chan == self.last_chan: |
| 190 | self.last_chan = None |
| 191 | return True |
A R Karthick | 338268f | 2016-06-21 17:12:13 -0700 | [diff] [blame] | 192 | |
Chetan Gaonker | b424ff8 | 2016-03-08 12:11:12 -0800 | [diff] [blame] | 193 | def join_next(self, chan = None): |
| 194 | if chan is None: |
| 195 | chan = self.last_chan |
| 196 | if chan is None: |
| 197 | return None |
| 198 | leave = chan |
| 199 | join = chan+1 |
| 200 | else: |
| 201 | leave = chan - 1 |
| 202 | join = chan |
A R Karthick | 338268f | 2016-06-21 17:12:13 -0700 | [diff] [blame] | 203 | |
Chetan Gaonker | b424ff8 | 2016-03-08 12:11:12 -0800 | [diff] [blame] | 204 | if join >= self.num: |
| 205 | join = 0 |
| 206 | |
| 207 | if leave >= 0 and leave != join: |
| 208 | self.leave(leave) |
| 209 | |
| 210 | return self.join(join) |
| 211 | |
| 212 | def jump(self): |
| 213 | chan = self.last_chan |
| 214 | if chan is not None: |
| 215 | self.leave(chan) |
| 216 | s_next = chan |
| 217 | else: |
| 218 | s_next = 0 |
Chetan Gaonker | cbe7964 | 2016-03-09 17:45:58 -0800 | [diff] [blame] | 219 | if self.num - s_next < 2: |
| 220 | s_next = 0 |
Chetan Gaonker | b424ff8 | 2016-03-08 12:11:12 -0800 | [diff] [blame] | 221 | chan = random.randint(s_next, self.num) |
| 222 | return self.join(chan) |
| 223 | |
| 224 | def gaddr(self, chan): |
Chetan Gaonker | cbe7964 | 2016-03-09 17:45:58 -0800 | [diff] [blame] | 225 | '''Return the group address for a channel''' |
Chetan Gaonker | b424ff8 | 2016-03-08 12:11:12 -0800 | [diff] [blame] | 226 | if chan >= self.num: |
| 227 | return None |
| 228 | return self.channels[chan] |
| 229 | |
Chetan Gaonker | cbe7964 | 2016-03-09 17:45:58 -0800 | [diff] [blame] | 230 | def caddr(self, group): |
| 231 | '''Return a channel given a group addr''' |
| 232 | if self.group_channel_map.has_key(group): |
| 233 | return self.group_channel_map[group] |
| 234 | return None |
| 235 | |
Chetan Gaonker | b424ff8 | 2016-03-08 12:11:12 -0800 | [diff] [blame] | 236 | def recv_cb(self, pkt): |
| 237 | '''Default channel receive callback''' |
| 238 | log.debug('Received packet from source %s, destination %s' %(pkt[IP].src, pkt[IP].dst)) |
| 239 | send_time = float(pkt[IP].payload.load) |
| 240 | recv_time = monotonic.monotonic() |
| 241 | log.debug('Packet received in %.3f usecs' %(recv_time - send_time)) |
| 242 | |
A R Karthick | 338268f | 2016-06-21 17:12:13 -0700 | [diff] [blame] | 243 | def recv(self, chan, cb = None, count = 1, timeout = 5): |
Chetan Gaonker | b424ff8 | 2016-03-08 12:11:12 -0800 | [diff] [blame] | 244 | if chan is None: |
| 245 | return None |
| 246 | if type(chan) == type([]) or type(chan) == type(()): |
| 247 | channel_list=filter(lambda c: c < self.num, chan) |
| 248 | groups = map(lambda c: self.gaddr(c), channel_list) |
| 249 | else: |
| 250 | groups = (self.gaddr(chan),) |
| 251 | if cb is None: |
| 252 | cb = self.recv_cb |
A R Karthick | 338268f | 2016-06-21 17:12:13 -0700 | [diff] [blame] | 253 | return sniff(prn = cb, count=count, timeout = timeout, |
| 254 | 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] | 255 | |
| 256 | def stop(self): |
| 257 | if self.streams: |
| 258 | self.streams.stop() |
| 259 | self.state = self.Stopped |
| 260 | |
| 261 | def get_state(self, chan): |
| 262 | return self.channel_states[chan][0] |
| 263 | |
| 264 | def set_state(self, chan, state): |
| 265 | self.channel_states[chan][0] = state |
| 266 | |
| 267 | if __name__ == '__main__': |
Chetan Gaonker | cd86bdd | 2016-03-17 00:08:12 -0700 | [diff] [blame] | 268 | num = 5 |
| 269 | start = 0 |
| 270 | ssm_list = [] |
| 271 | for i in xrange(2): |
| 272 | channels = Channels(num, start) |
| 273 | ssm_list += channels.channels |
| 274 | start += num |
| 275 | igmpChannel = IgmpChannel() |
| 276 | igmpChannel.igmp_load_ssm_config(ssm_list) |
Chetan Gaonker | b424ff8 | 2016-03-08 12:11:12 -0800 | [diff] [blame] | 277 | channels.start() |
| 278 | for i in range(num): |
| 279 | channels.join(i) |
| 280 | for i in range(num): |
| 281 | channels.recv(i) |
| 282 | for i in range(num): |
| 283 | channels.leave(i) |
| 284 | channels.stop() |