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