blob: cc18e2dfd146600cdf621987297a1ea27c5d6622 [file] [log] [blame]
Matteo Scandolo48d3d2d2017-08-08 13:05:27 -07001
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 Gaonkercfcce782016-05-10 10:10:42 -070017#
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 Gaonkereb2b24b2016-03-01 14:04:45 -080032import threading
33import sys
34import os
35import time
36import monotonic
37from scapy.all import *
38
39class McastTraffic(threading.Thread):
Chetan Gaonker4b853002016-03-28 15:17:28 -070040 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 Gaonkereb2b24b2016-03-01 14:04:45 -080043 SEND_STATE = 1
44 RECV_STATE = 2
Chetan Gaonker4b853002016-03-28 15:17:28 -070045
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 Gaonkereb2b24b2016-03-01 14:04:45 -080048 threading.Thread.__init__(self)
49 self.addrs = addrs
50 self.iface = iface
Chetan Gaonker4b853002016-03-28 15:17:28 -070051 self.dst_mac = dst_mac
52 self.src_mac = src_mac
53 self.src_ip = src_ip
Chetan Gaonkereb2b24b2016-03-01 14:04:45 -080054 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