Matteo Scandolo | 48d3d2d | 2017-08-08 13:05:27 -0700 | [diff] [blame] | 1 | |
| 2 | # Copyright 2017-present Open Networking Foundation |
| 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 | |
| 16 | |
Chetan Gaonker | cfcce78 | 2016-05-10 10:10:42 -0700 | [diff] [blame] | 17 | # |
| 18 | # Copyright 2016-present Ciena Corporation |
| 19 | # |
| 20 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 21 | # you may not use this file except in compliance with the License. |
| 22 | # You may obtain a copy of the License at |
| 23 | # |
| 24 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 25 | # |
| 26 | # Unless required by applicable law or agreed to in writing, software |
| 27 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 28 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 29 | # See the License for the specific language governing permissions and |
| 30 | # limitations under the License. |
| 31 | # |
Chetan Gaonker | eb2b24b | 2016-03-01 14:04:45 -0800 | [diff] [blame] | 32 | import threading |
| 33 | import sys |
| 34 | import os |
| 35 | import time |
| 36 | import monotonic |
| 37 | from scapy.all import * |
| 38 | |
| 39 | class McastTraffic(threading.Thread): |
Chetan Gaonker | 4b85300 | 2016-03-28 15:17:28 -0700 | [diff] [blame] | 40 | DST_MAC_DEFAULT = '01:00:5e:00:01:01' |
| 41 | SRC_MAC_DEFAULT = '02:88:b4:e4:90:77' |
| 42 | SRC_IP_DEFAULT = '1.2.3.4' |
Chetan Gaonker | eb2b24b | 2016-03-01 14:04:45 -0800 | [diff] [blame] | 43 | SEND_STATE = 1 |
| 44 | RECV_STATE = 2 |
Chetan Gaonker | 4b85300 | 2016-03-28 15:17:28 -0700 | [diff] [blame] | 45 | |
| 46 | def __init__(self, addrs, iface = 'eth0', dst_mac = DST_MAC_DEFAULT, src_mac = SRC_MAC_DEFAULT, |
| 47 | src_ip = SRC_IP_DEFAULT, cb = None, arg = None): |
Chetan Gaonker | eb2b24b | 2016-03-01 14:04:45 -0800 | [diff] [blame] | 48 | threading.Thread.__init__(self) |
| 49 | self.addrs = addrs |
| 50 | self.iface = iface |
Chetan Gaonker | 4b85300 | 2016-03-28 15:17:28 -0700 | [diff] [blame] | 51 | self.dst_mac = dst_mac |
| 52 | self.src_mac = src_mac |
| 53 | self.src_ip = src_ip |
Chetan Gaonker | eb2b24b | 2016-03-01 14:04:45 -0800 | [diff] [blame] | 54 | self.cb = cb |
| 55 | self.arg = arg |
| 56 | self.state = self.SEND_STATE | self.RECV_STATE |
| 57 | |
| 58 | def run(self): |
| 59 | eth = Ether(dst = self.dst_mac, src = self.src_mac) |
| 60 | while self.state & self.SEND_STATE: |
| 61 | for addr in self.addrs: |
| 62 | #data = repr(time.time()) |
| 63 | data = repr(monotonic.monotonic()) |
| 64 | ip = IP(dst = addr, src = self.src_ip) |
| 65 | sendp(eth/ip/data, iface = self.iface) |
| 66 | if self.cb: |
| 67 | self.cb(self.arg) |
| 68 | |
| 69 | def stop(self): |
| 70 | self.state = 0 |
| 71 | |
| 72 | def stopReceives(self): |
| 73 | self.state &= ~self.RECV_STATE |
| 74 | |
| 75 | def stopSends(self): |
| 76 | self.state &= ~self.SEND_STATE |
| 77 | |
| 78 | def isRecvStopped(self): |
| 79 | return False if self.state & self.RECV_STATE else True |
| 80 | |
| 81 | def isSendStopped(self): |
| 82 | return False if self.state & self.SEND_STATE else True |
| 83 | |