Merge pull request #5 from cgaonker/master

Adding more IGMP test cases
diff --git a/src/test/builder/buildFsm.sh b/src/test/builder/buildFsm.sh
index aee58ca..51fd8d3 100644
--- a/src/test/builder/buildFsm.sh
+++ b/src/test/builder/buildFsm.sh
@@ -4,8 +4,13 @@
     odir = "./"
 fi
 
-##Generate TLS authentication state machine
+##Generate TLS authentication Test state machine
 python yamlFsm.py -p TlsAuthHolder -f noseTlsAuthTest.yaml > ${odir}/noseTlsAuthHolder.py
 
 ##Generate PAP authentication state machine
 python yamlFsm.py -p PAPAuthHolder -f nosePAPTest.yaml > ${odir}/nosePAPAuthHolder.py
+
+
+##Generate DNS test state machine
+python yamlFsm.py -p DnsHolder -f noseDnsTest.yaml > ${odir}/noseDnsHolder.py
+
diff --git a/src/test/igmp/igmpTest.py b/src/test/igmp/igmpTest.py
index 60d314e..28c243a 100644
--- a/src/test/igmp/igmpTest.py
+++ b/src/test/igmp/igmpTest.py
@@ -3,12 +3,28 @@
 from nose.twistedtools import reactor, deferred
 from twisted.internet import defer
 from scapy.all import *
-import time
+import time, monotonic
 import os, sys
+import tempfile
+import json
+import random
+import threading
+
 CORD_TEST_UTILS = 'utils'
 test_root = os.getenv('CORD_TEST_ROOT') or './'
 sys.path.append(test_root + CORD_TEST_UTILS)
 from IGMP import *
+from McastTraffic import *
+from Stats import Stats
+log.setLevel('INFO')
+
+IGMP_DST_MAC = "01:00:5e:00:01:01"
+IGMP_SRC_MAC = "5a:e1:ac:ec:4d:a1"
+IP_SRC = '1.2.3.4'
+IP_DST = '224.0.1.1'
+
+igmp_eth = Ether(dst = IGMP_DST_MAC, src = IGMP_SRC_MAC, type = ETH_P_IP)
+igmp_ip = IP(dst = IP_DST, src = IP_SRC)
 
 class IGMPTestState:
 
@@ -19,78 +35,286 @@
             self.groups = groups
             self.group_map = {} ##create a send/recv count map
             for g in groups:
-                self.group_map[g] = [0, 0]
-
-      def update(self, group, tx = 0, rx = 0):
+                self.group_map[g] = (Stats(), Stats())
+            
+      def update(self, group, tx = 0, rx = 0, t = 0):
+            self.counter += 1
             index = 0 if rx == 0 else 1
             v = tx if rx == 0 else rx
             if self.group_map.has_key(group):
-                self.group_map[group][index] += v
+                  self.group_map[group][index].update(packets = v, t = t)
 
       def update_state(self):
           self.state = self.state ^ 1
-          self.counter += 1
 
 class igmp_exchange(unittest.TestCase):
+
+    IGMP_TEST_TIMEOUT = 5
+    MCAST_TRAFFIC_TIMEOUT = 10
+    max_packets = 100
     
-    def igmp_verify(self, igmpState):
+    def onos_ssm_table_load(self, groups, src_list):
+          ssm_dict = {'apps' : { 'org.onosproject.igmp' : { 'ssmTranslate' : [] } } }
+          ssm_xlate_list = ssm_dict['apps']['org.onosproject.igmp']['ssmTranslate']
+          for g in groups:
+                for s in src_list:
+                      d = {}
+                      d['source'] = s
+                      d['group'] = g
+                      ssm_xlate_list.append(d)
+          json_dict = json.JSONEncoder().encode(ssm_dict)
+          with tempfile.NamedTemporaryFile(delete=False) as temp:
+                temp.write(json_dict)
+                temp.flush()
+                temp.close()
+          log.debug('Loading SSM config in file %s to ONOS.' %temp.name)
+          os.system('./igmp_ssm_load.sh %s' %temp.name)
+          os.unlink(temp.name)
+          ##Wait for ONOS to populate the SSM map before sending join.Huh
+          time.sleep(2)
+
+    def igmp_verify_join(self, igmpStateList):
+        sendState, recvState = igmpStateList
         ## check if the send is received for the groups
-        for g in igmpState.groups:
-            tx = igmpState.group_map[g][0]
-            rx = igmpState.group_map[g][1]
+        for g in sendState.groups:
+            tx_stats = sendState.group_map[g][0]  
+            tx = tx_stats.count
             assert_greater(tx, 0)
-            assert_equal(tx, rx)
-        print 'IGMP test verification success'
+            rx_stats = recvState.group_map[g][1]
+            rx = rx_stats.count
+            assert_greater(rx, 0)
+            log.info('Receive stats %s for group %s' %(rx_stats, g))
+            
+        log.info('IGMP test verification success')
 
-    def igmp_send(self, igmpState, iface = 'veth2'):
-        dst_mac = '01:00:5e:00:01:01'
-        src_mac = '02:88:b4:e4:90:77'
-        src_ip = '1.2.3.4'
-        eth = Ether(dst = dst_mac, src = src_mac)
-        data = repr(time.time())
-        for g in igmpState.groups:
-            ip = IP(dst = g, src = src_ip)
-            sendp(eth/ip/data, iface=iface)
-            igmpState.update(g, tx = 1)
+    def igmp_verify_leave(self, igmpStateList, leave_groups):
+        sendState, recvState = igmpStateList[0], igmpStateList[1]
+        ## check if the send is received for the groups
+        for g in sendState.groups:
+            tx_stats = sendState.group_map[g][0]
+            rx_stats = recvState.group_map[g][1]
+            tx = tx_stats.count
+            rx = rx_stats.count                   
+            assert_greater(tx, 0)
+            if g not in leave_groups:
+                log.info('Received %d packets for group %s' %(rx, g))
+        for g in leave_groups:
+            rx = recvState.group_map[g][1].count
+            assert_equal(rx, 0)
+            
+        log.info('IGMP test verification success')
 
-        ##done sending. Bounce the states
-        igmpState.update_state()
+    def mcast_traffic_timer(self):
+          self.mcastTraffic.stopReceives()
+
+    def send_mcast_cb(self, send_state):
+        for g in send_state.groups:
+            send_state.update(g, tx = 1)
         return 0
 
+    ##Runs in the context of twisted reactor thread
     def igmp_recv(self, igmpState, iface = 'veth0'):
-        for g in igmpState.groups:
-            igmpState.update(g, rx = 1)
-        ##done receiving. Bounce the state back to send
-        igmpState.update_state()
+        p = self.recv_socket.recv()
+        send_time = float(p.payload.load)
+        recv_time = monotonic.monotonic()
+        #log.info( 'Recv in %.6f secs' %(recv_time - send_time))
+        igmpState.update(p.dst, rx = 1, t = recv_time - send_time)
         return 0
 
-    def send_igmp_join(self, igmpState, iface = 'veth0'):
-        src_list = ['1.2.3.4']
-        for g in igmpState.groups:
-            igmp = IGMP(mtype = IGMPV3_REPORT, 
-                        group = g,
-                        rtype = IGMP_EXCLUDE,
-                        src_list = src_list)
-            sendp(igmp.scapify(), iface = iface)
+    def send_igmp_join(self, groups, src_list = ['1.2.3.4'], iface = 'veth0', delay = 2):
+        self.onos_ssm_table_load(groups, src_list)
+        igmp = IGMPv3(type = IGMP_TYPE_V3_MEMBERSHIP_REPORT, max_resp_code=30,
+                      gaddr='224.0.1.1')
+        for g in groups:
+              gr = IGMPv3gr(rtype=IGMP_V3_GR_TYPE_EXCLUDE, mcaddr=g)
+              gr.sources = src_list
+              igmp.grps.append(gr)
 
-    @deferred(timeout=10)
-    def test_igmp_sendrecv(self):
+        pkt = igmp_eth/igmp_ip/igmp
+        IGMPv3.fixup(pkt)
+        sendp(pkt, iface=iface)
+        if delay != 0:
+            time.sleep(delay)
+
+    def send_igmp_leave(self, groups, src_list = ['1.2.3.4'], iface = 'veth0', delay = 2):
+        igmp = IGMPv3(type = IGMP_TYPE_V3_MEMBERSHIP_REPORT, max_resp_code=30,
+                      gaddr='224.0.1.1')
+        for g in groups:
+              gr = IGMPv3gr(rtype=IGMP_V3_GR_TYPE_INCLUDE, mcaddr=g)
+              gr.sources = src_list
+              igmp.grps.append(gr)
+
+        pkt = igmp_eth/igmp_ip/igmp
+        IGMPv3.fixup(pkt)
+        sendp(pkt, iface = iface)
+        if delay != 0:
+            time.sleep(delay)
+
+    @deferred(timeout=MCAST_TRAFFIC_TIMEOUT+10)
+    def test_igmp_join_verify_traffic(self):
         groups = ['224.0.1.1', '225.0.0.1']
-        def igmp_srp_task(stateObject):
-            if stateObject.state == 0:
-                result = self.igmp_send(stateObject)
-            else:
-                result = self.igmp_recv(stateObject)
-
-            if stateObject.counter < 20:
-                reactor.callLater(0, igmp_srp_task, stateObject)
-            else:
-                self.igmp_verify(stateObject)
-                stateObject.df.callback(0)
-
         df = defer.Deferred()
         igmpState = IGMPTestState(groups = groups, df = df)
-        self.send_igmp_join(igmpState)
-        reactor.callLater(0, igmp_srp_task, igmpState)
+        igmpStateRecv = IGMPTestState(groups = groups, df = df)
+        igmpStateList = (igmpState, igmpStateRecv)
+        mcastTraffic = McastTraffic(groups, iface= 'veth2', cb = self.send_mcast_cb, arg = igmpState)
+        self.df = df
+        self.mcastTraffic = mcastTraffic
+        self.recv_socket = L3PacketSocket(iface = 'veth0', type = ETH_P_IP)
+
+        def igmp_srp_task(stateList):
+            igmpSendState, igmpRecvState = stateList
+            if not mcastTraffic.isRecvStopped():
+                result = self.igmp_recv(igmpRecvState)
+                reactor.callLater(0, igmp_srp_task, stateList)
+            else:
+                self.mcastTraffic.stop()
+                self.recv_socket.close()
+                self.igmp_verify_join(stateList)
+                self.df.callback(0)
+
+        self.send_igmp_join(groups)
+        mcastTraffic.start()
+        self.test_timer = reactor.callLater(self.MCAST_TRAFFIC_TIMEOUT, self.mcast_traffic_timer)
+        reactor.callLater(0, igmp_srp_task, igmpStateList)
         return df
 
+    @deferred(timeout=MCAST_TRAFFIC_TIMEOUT+10)
+    def test_igmp_leave_verify_traffic(self):
+        groups = ['224.0.1.10', '225.0.0.10']
+        leave_groups = ['224.0.1.10']
+        df = defer.Deferred()
+        igmpState = IGMPTestState(groups = groups, df = df)
+        igmpStateRecv = IGMPTestState(groups = groups, df = df)
+        igmpStateList = (igmpState, igmpStateRecv)
+        mcastTraffic = McastTraffic(groups, iface= 'veth2', cb = self.send_mcast_cb, 
+                                    arg = igmpState)
+        self.df = df
+        self.mcastTraffic = mcastTraffic
+        self.recv_socket = L3PacketSocket(iface = 'veth0', type = ETH_P_IP)
+        
+        def igmp_srp_task(stateList):
+            igmpSendState, igmpRecvState = stateList
+            if not mcastTraffic.isRecvStopped():
+                result = self.igmp_recv(igmpRecvState)
+                reactor.callLater(0, igmp_srp_task, stateList)
+            else:
+                self.mcastTraffic.stop()
+                self.recv_socket.close()
+                self.igmp_verify_leave(stateList, leave_groups)
+                self.df.callback(0)
+
+        self.send_igmp_join(groups)
+        self.send_igmp_leave(leave_groups, delay = 3)
+        mcastTraffic.start()
+        self.test_timer = reactor.callLater(self.MCAST_TRAFFIC_TIMEOUT, self.mcast_traffic_timer)
+        reactor.callLater(0, igmp_srp_task, igmpStateList)
+        return df
+
+    @deferred(timeout=100)
+    def test_igmp_leave_join_loop(self):
+        self.groups = ['226.0.1.1', '227.0.0.1', '228.0.0.1', '229.0.0.1', '230.0.0.1' ]
+        self.src_list = ['3.4.5.6', '7.8.9.10']
+        df = defer.Deferred()
+        self.df = df
+        self.iterations = 0
+        self.num_groups = len(self.groups)
+        self.MAX_TEST_ITERATIONS = 10
+
+        def igmp_srp_task(v):
+              if self.iterations < self.MAX_TEST_ITERATIONS:
+                    if v == 1:
+                          ##join test
+                          self.num_groups = random.randint(0, len(self.groups))
+                          self.send_igmp_join(self.groups[:self.num_groups],
+                                              src_list = self.src_list,
+                                              iface = 'veth0', delay = 0)
+                    else:
+                          self.send_igmp_leave(self.groups[:self.num_groups],
+                                               src_list = self.src_list,
+                                               iface = 'veth0', delay = 0)
+                    self.iterations += 1
+                    v ^= 1
+                    reactor.callLater(1.0 + 0.5*self.num_groups,
+                                      igmp_srp_task, v)
+              else:
+                    self.df.callback(0)
+
+        reactor.callLater(0, igmp_srp_task, 1)
+        return df
+
+    def igmp_join_task(self, intf, groups, state, src_list = ['1.2.3.4']):
+          self.onos_ssm_table_load(groups, src_list)
+          igmp = IGMPv3(type = IGMP_TYPE_V3_MEMBERSHIP_REPORT, max_resp_code=30,
+                        gaddr='224.0.1.1')
+          for g in groups:
+                gr = IGMPv3gr(rtype = IGMP_V3_GR_TYPE_EXCLUDE, mcaddr = g)
+                gr.sources = src_list
+                igmp.grps.append(gr)
+
+          for g in groups:
+                state.group_map[g][0].update(1, t = monotonic.monotonic())
+
+          pkt = igmp_eth/igmp_ip/igmp
+          IGMPv3.fixup(pkt)
+          sendp(pkt, iface=intf)
+          log.debug('Returning from join task')
+
+    def igmp_recv_task(self, intf, groups, join_state):
+          recv_socket = L3PacketSocket(iface = intf, type = ETH_P_IP)
+          group_map = {}
+          for g in groups:
+                group_map[g] = [0,0]
+
+          while True:
+                p = recv_socket.recv()
+                if p.dst in groups and group_map[p.dst][0] == 0:
+                      group_map[p.dst][0] += 1
+                      group_map[p.dst][1] = monotonic.monotonic()
+                      c = 0
+                      for g in groups:
+                            c += group_map[g][0]
+                      if c == len(groups):
+                            break
+          for g in groups:
+                join_start = join_state.group_map[g][0].start
+                recv_time = group_map[g][1] * 1000000
+                delta = (recv_time - join_start)
+                log.info('Join for group %s received in %.3f usecs' %
+                         (g, delta))
+
+          recv_socket.close()
+          log.debug('Returning from recv task')
+          
+    def group_latency_check(self, groups):
+          tasks = []
+          self.send_igmp_leave(groups = groups)
+          join_state = IGMPTestState(groups = groups)
+          tasks.append(threading.Thread(target=self.igmp_join_task, args = ('veth0', groups, join_state,)))
+          traffic_state = IGMPTestState(groups = groups)
+          mcast_traffic = McastTraffic(groups, iface= 'veth2', cb = self.send_mcast_cb, 
+                                       arg = traffic_state)
+          mcast_traffic.start()
+          tasks.append(threading.Thread(target=self.igmp_recv_task, args = ('veth0', groups, join_state)))
+          for t in tasks:
+                t.start()
+          for t in tasks:
+                t.join()
+
+          mcast_traffic.stop()
+          self.send_igmp_leave(groups = groups)
+          return
+
+    def test_igmp_1group_join_latency(self):
+          groups = ['239.0.1.1']
+          self.group_latency_check(groups)
+
+    def test_igmp_2group_join_latency(self):
+          groups = ['239.0.1.1', '240.0.1.1']
+          self.group_latency_check(groups)
+
+    def test_igmp_Ngroup_join_latency(self):
+          groups = ['239.0.1.1', '240.0.1.1', '241.0.1.1', '242.0.1.1']
+          self.group_latency_check(groups)
+
+          
diff --git a/src/test/igmp/igmp_ssm_load.sh b/src/test/igmp/igmp_ssm_load.sh
new file mode 100755
index 0000000..b2e9623
--- /dev/null
+++ b/src/test/igmp/igmp_ssm_load.sh
@@ -0,0 +1,17 @@
+#!/usr/bin/env bash
+json="$1"
+controller="$2"
+if [ x"$json" = "x" ]; then
+  echo "No json file specified. Exiting"
+  exit 127
+fi
+if [ x"$controller" = "x" ]; then
+    controller=`ovs-vsctl show | egrep "Controller|tcp" | grep -v ptcp | sed 's,Controller,,g' | sed 's,\",,g' | tr -s ' '|awk -F":" '{print $2}'`
+    #echo "Controller at $controller"
+fi
+#echo "Loading ssm translate json file $json to controller at $controller"
+curl --fail -sSL --user karaf:karaf \
+    -X POST -H 'Content-Type:application/json' \
+    http://$controller:8181/onos/v1/network/configuration/ -d@$json
+
+
diff --git a/src/test/utils/EapolAAA.py b/src/test/utils/EapolAAA.py
index ea26164..f7a8a2d 100644
--- a/src/test/utils/EapolAAA.py
+++ b/src/test/utils/EapolAAA.py
@@ -1,7 +1,7 @@
 ####  Authentication parameters
+from scapy.all import *
 from socket import *
 from struct import *
-import scapy
 import sys
 from nose.tools import assert_equal, assert_not_equal, assert_raises, assert_true
 
@@ -31,9 +31,6 @@
 cCertMsg = '\x0b\x00\x00\x03\x00\x00\x00'
 TLS_LENGTH_INCLUDED = 0x80
 
-def ethernet_header(src, dst, req_type):
-    return dst+src+pack("!H", req_type)
-
 class EapolPacket(object):
     
     def __init__(self, intf = 'veth0'):
@@ -45,7 +42,7 @@
         self.s = socket(AF_PACKET, SOCK_RAW, htons(ETHERTYPE_PAE))
         self.s.bind((self.intf, ETHERTYPE_PAE))
         self.mymac = self.s.getsockname()[4]
-        self.llheader = ethernet_header(self.mymac, PAE_GROUP_ADDR, ETHERTYPE_PAE)
+        self.llheader = Ether(dst = PAE_GROUP_ADDR, src = self.mymac, type = ETHERTYPE_PAE)
 
     def cleanup(self):
         if self.s is not None:
@@ -53,13 +50,10 @@
             self.s = None
             
     def eapol(self, req_type, payload=""):
-        return pack("!BBH", EAPOL_VERSION, req_type, len(payload))+payload
+        return EAPOL(version = EAPOL_VERSION, type = req_type)/payload
 
     def eap(self, code, pkt_id, req_type=0, data=""):
-        if code in [EAP_SUCCESS, EAP_FAILURE]:
-            return pack("!BBH", code, pkt_id, 4)
-        else:
-            return pack("!BBHB", code, pkt_id, 5+len(data), req_type)+data
+        return EAP(code = code, id = pkt_id, type = req_type)/data
 
     def eapTLS(self, code, pkt_id, flags = TLS_LENGTH_INCLUDED, data=""):
         req_type = EAP_TYPE_TLS
@@ -73,7 +67,7 @@
             return pack("!BBHB", code, pkt_id, 5+len(flags_str)+len(data), req_type) + flags_str + data
 
     def eapol_send(self, eapol_type, eap_payload):
-        return self.s.send(self.llheader + self.eapol(eapol_type, eap_payload))
+        return sendp(self.llheader/self.eapol(eapol_type, eap_payload), iface=self.intf)
 
     def eapol_recv(self):
         p = self.s.recv(self.max_payload_size)[14:]
diff --git a/src/test/utils/IGMP.py b/src/test/utils/IGMP.py
index 95ce1ec..1b24de3 100644
--- a/src/test/utils/IGMP.py
+++ b/src/test/utils/IGMP.py
@@ -3,146 +3,232 @@
 from scapy.all import *
 from itertools import *
 
-IGMPV3_REPORT = 0x22
-IGMP_LEAVE = 0x17
-IGMP_EXCLUDE = 0x04
-IGMP_INCLUDE = 0x03
+IGMP_TYPE_MEMBERSHIP_QUERY     = 0x11
+IGMP_TYPE_V3_MEMBERSHIP_REPORT = 0x22
+IGMP_TYPE_V1_MEMBERSHIP_REPORT = 0x12
+IGMP_TYPE_V2_MEMBERSHIP_REPORT = 0x16
+IGMP_TYPE_V2_LEAVE_GROUP       = 0x17
+
+IGMP_V3_GR_TYPE_INCLUDE           = 0x01
+IGMP_V3_GR_TYPE_EXCLUDE           = 0x02
+IGMP_V3_GR_TYPE_CHANGE_TO_INCLUDE = 0x03
+IGMP_V3_GR_TYPE_CHANGE_TO_EXCLUDE = 0x04
+IGMP_V3_GR_TYPE_ALLOW_NEW         = 0x05
+IGMP_V3_GR_TYPE_BLOCK_OLD         = 0x06
+
+"""
 IGMPV3_ALL_ROUTERS = '224.0.0.22'
 IGMPv3 = 3
 IP_SRC = '1.2.3.4'
 ETHERTYPE_IP = 0x0800
 IGMP_DST_MAC = "01:00:5e:00:01:01"
 IGMP_SRC_MAC = "5a:e1:ac:ec:4d:a1"
+"""
 
-class IGMP:
 
-    def __init__(self, mtype = None, group = '', rtype = None, src_list = []):
-        self.version = IGMPv3
-        self.mtype = mtype
-        self.group = group
-        self.src_list= src_list
-        self.rtype = rtype
+class IGMPv3gr(Packet):
+    """IGMPv3 Group Record, used in membership report"""
 
-    def checksum(self, msg):
-        s = 0
-        for i in range(0, len(msg), 2):
-            w = ord(msg[i]) + (ord(msg[i+1]) << 8)
-            c = s + w
-            s = (c & 0xffff) + (c >> 16)
-        return ~s & 0xffff
+    name = "IGMPv3gr"
 
-    def update_igmp_checksum(self, pkt):
-        cs = self.checksum(pkt)
-        #print 'igmp checksum: ' + str(hex(cs))
-        m = []
-        for x in pkt:        
-            m.append(ord(x))
-        higher = (cs >> 8) & 0xff
-        lower = cs & 0xff
-        m[2] = lower
-        m[3] = higher
-        m = pack("%dB" % len(m), *m)
-        return m
+    igmp_v3_gr_types = {
+        IGMP_V3_GR_TYPE_INCLUDE: "Include Mode",
+        IGMP_V3_GR_TYPE_EXCLUDE: "Exclude Mode",
+        IGMP_V3_GR_TYPE_CHANGE_TO_INCLUDE: "Change to Include Mode",
+        IGMP_V3_GR_TYPE_CHANGE_TO_EXCLUDE: "Change to Exclude Mode",
+        IGMP_V3_GR_TYPE_ALLOW_NEW: "Allow New Sources",
+        IGMP_V3_GR_TYPE_BLOCK_OLD: "Block Old Sources"
+    }
 
-    def update_ip_checksum(self, pkt):
-        cs = self.checksum(pkt)
-        #print 'ip hdr checksum: ' + str(hex(cs))
-        m = []
-        for x in pkt:        
-            m.append(ord(x))
-        higher = (cs >> 8) & 0xff
-        lower = cs & 0xff
-        m[10] = lower
-        m[11] = higher
-        m = pack("%dB" % len(m), *m)
-        return m
+    fields_desc = [
+        ByteEnumField("rtype", IGMP_V3_GR_TYPE_INCLUDE, igmp_v3_gr_types),
+        ByteField("aux_data_len", 0),
+        FieldLenField("numsrc", None, count_of="sources"),
+        IPField("mcaddr", "0.0.0.0"),
+        FieldListField("sources", None, IPField("src", "0.0.0.0"), "numsrc")
+    ]
 
-    def build_ip_hdr(self, s, d):
-        ip_ihl_len = 0x46 #8 bits
-        ip_dscp = 0xc0 #8 bits
-        ip_hdr_total_len = 0x0028 #16 bits
-        ip_id = 0x0000 #16 bits
-        ip_flags = 0x4000 #16 bits
-        ip_ttl = 1 #8 bits
-        ip_protocol = 0x02 #8 bits
-        ip_cs = 0x0000 #16 bits (should filled by kernel but seems not???)
-        #ip_src #32 bits
-        #ip_dst #32 bits
-        ip_options = 0x94040000 #32 bits
-        #total len 24 bytes
-        ip_header = pack('!BBHHHBBH4s4sI', ip_ihl_len, ip_dscp, ip_hdr_total_len,
-                         ip_id, ip_flags, ip_ttl, ip_protocol, ip_cs, inet_aton(s),
-                         inet_aton(d), ip_options)
-        return ip_header
-
-    def dump_packet(self, data):
-        i = 0
-        for x in data:
-            if i == 4:
-                print ''
-                i = 0
-            i += 1
-            sys.stdout.write(' %0.2x' % ord(x))
-        print ''
-
-    def build_igmp(self, msg_type = None, group = None, record_type = None, src_list = None):
-        msg_type = self.mtype if msg_type == None else msg_type
-        group = self.group if group == None else group
-        record_type = self.rtype if record_type == None else record_type
-        src_list = self.src_list if src_list == None else src_list
-        if msg_type == IGMP_LEAVE:
-            pkt = pack('!BBH4s', msg_type, 0, 0, inet_aton(group))
-        elif msg_type == IGMPV3_REPORT:
-            pkt = pack('!BBHHHBBH', msg_type, 0x00, 0x0000, 0x0000, 0x0001, record_type,
-                       0x00, len(src_list))
-            pkt += pack('!4s', inet_aton(group))
-            for a in src_list:
-                pkt += pack('!4s', inet_aton(a))
-        else:
-            print 'unsupported report type: ' + str(msg_type)
-            return None
+    def post_build(self, pkt, payload):
+        pkt += payload
+        if self.aux_data_len != 0:
+            print "WARNING: Auxiliary Data Length must be zero (0)"
         return pkt
 
-    def build_join_msg(self, group = None, record_type = None, src_list = None):
-        return self.build_igmp(msg_type = IGMPV3_REPORT, 
-                               group = group, 
-                               record_type = record_type, 
-                               src_list = src_list)
 
-    def build_leave_msg(self, group = None):
-        return self.build_igmp(msg_type = IGMPV3_REPORT, 
-                               group = group, 
-                               record_type = IGMP_INCLUDE, 
-                               src_list = [])
+class IGMPv3(Packet):
 
-    def build_ip_igmp(self, 
-                      src = IP_SRC,
-                      msg_type = None, 
-                      group = None, 
-                      record_type = None, 
-                      src_list = None):
+    name = "IGMPv3"
 
-        igmp = self.build_igmp(msg_type = msg_type,
-                               group = group,
-                               record_type = record_type,
-                               src_list = src_list)
-        igmp = self.update_igmp_checksum(igmp)
-        ip_hdr = self.build_ip_hdr(src, IGMPV3_ALL_ROUTERS)
-        p = ip_hdr + igmp
-        p = self.update_ip_checksum(p)
-        return p
+    igmp_v3_types = {
+        IGMP_TYPE_MEMBERSHIP_QUERY: "Membership Query",
+        IGMP_TYPE_V3_MEMBERSHIP_REPORT: " Version 3 Mebership Report",
+        IGMP_TYPE_V2_MEMBERSHIP_REPORT: " Version 2 Mebership Report",
+        IGMP_TYPE_V1_MEMBERSHIP_REPORT: " Version 1 Mebership Report",
+        IGMP_TYPE_V2_LEAVE_GROUP: "Version 2 Leave Group"
+    }
 
-    def scapify(self, 
-                src = IP_SRC, 
-                msg_type = None,
-                group = None,
-                record_type = None,
-                src_list = None):
+    fields_desc = [
+        ByteEnumField("type", IGMP_TYPE_MEMBERSHIP_QUERY, igmp_v3_types),
+        ByteField("max_resp_code", 0),
+        XShortField("checksum", None),
+        #IPField("group_address", "0.0.0.0"),
 
-        ip_igmp = self.build_ip_igmp(src = src,
-                                     msg_type = msg_type,
-                                     group = group,
-                                     record_type = record_type,
-                                     src_list = src_list)
-        eth = Ether(dst = IGMP_DST_MAC, src = IGMP_SRC_MAC, type = ETHERTYPE_IP)
-        return eth/ip_igmp
+        # membership query fields
+        ConditionalField(IPField("gaddr", "0.0.0.0"), lambda pkt: pkt.type == IGMP_TYPE_MEMBERSHIP_QUERY),
+        ConditionalField(BitField("resv", 0, 4), lambda pkt: pkt.type == IGMP_TYPE_MEMBERSHIP_QUERY),
+        ConditionalField(BitField("s", 0, 1), lambda pkt: pkt.type == IGMP_TYPE_MEMBERSHIP_QUERY),
+        ConditionalField(BitField("qrv", 0, 3), lambda pkt: pkt.type == IGMP_TYPE_MEMBERSHIP_QUERY),
+        ConditionalField(ByteField("qqic", 0), lambda pkt: pkt.type == IGMP_TYPE_MEMBERSHIP_QUERY),
+        ConditionalField(FieldLenField("numsrc", None, count_of="srcs"), lambda pkt: pkt.type == IGMP_TYPE_MEMBERSHIP_QUERY),
+        ConditionalField(FieldListField("srcs", None, IPField("src", "0.0.0.0"), "numsrc"), lambda pkt: pkt.type == IGMP_TYPE_MEMBERSHIP_QUERY),
+
+        # membership report fields
+        ConditionalField(ShortField("resv2", 0), lambda pkt: pkt.type == IGMP_TYPE_V3_MEMBERSHIP_REPORT),
+        ConditionalField(FieldLenField("numgrp", None, count_of="grps"), lambda pkt: pkt.type == IGMP_TYPE_V3_MEMBERSHIP_REPORT),
+        ConditionalField(PacketListField("grps", [], IGMPv3gr), lambda pkt: pkt.type == IGMP_TYPE_V3_MEMBERSHIP_REPORT)
+
+        # TODO: v2 and v3 membership reports?
+
+    ]
+
+    def post_build(self, pkt, payload):
+
+        pkt += payload
+
+        if self.type in [IGMP_TYPE_V3_MEMBERSHIP_REPORT,]: # max_resp_code field is reserved (0)
+            mrc = 0
+        else:
+            mrc = self.encode_float(self.max_resp_code)
+        pkt = pkt[:1] + chr(mrc) + pkt[2:]
+
+        if self.checksum is None:
+            chksum = checksum(pkt)
+            pkt = pkt[:2] + chr(chksum >> 8) + chr(chksum & 0xff) + pkt[4:]
+
+        return pkt
+
+    def encode_float(self, value):
+        """Encode max response time value per RFC 3376."""
+        if value < 128:
+            return value
+        if value > 31743:
+            return 255
+        exp = 0
+        value >>= 3
+        while value > 31:
+            exp += 1
+            value >>= 1
+        return 0x80 | (exp << 4) | (value & 0xf)
+
+
+    def decode_float(self, code):
+        if code < 128:
+            return code
+        mant = code & 0xf
+        exp = (code >> 4) & 0x7
+        return (mant | 0x10) << (exp + 3)
+
+    @staticmethod
+    def is_valid_mcaddr(ip):
+        byte1 = atol(ip) >> 24 & 0xff
+        return (byte1 & 0xf0) == 0xe0
+
+    @staticmethod
+    def fixup(pkt):
+        """Fixes up the underlying IP() and Ether() headers."""
+        assert pkt.haslayer(IGMPv3), "This packet is not an IGMPv4 packet; cannot fix it up"
+
+        igmp = pkt.getlayer(IGMPv3)
+
+        if pkt.haslayer(IP):
+            ip = pkt.getlayer(IP)
+            ip.ttl = 1
+            ip.proto = 2
+            ip.tos = 0xc0
+            ip.options = [IPOption_Router_Alert()]
+
+            if igmp.type == IGMP_TYPE_MEMBERSHIP_QUERY:
+                if igmp.gaddr == "0.0.0.0":
+                    ip.dst = "224.0.0.1"
+                else:
+                    assert IGMPv3.is_valid_mcaddr(igmp.gaddr), "IGMP membership query with invalid mcast address"
+                    ip.dst = igmp.gaddr
+
+            elif igmp.type == IGMP_TYPE_V2_LEAVE_GROUP and IGMPv3.is_valid_mcaddr(igmp.gaddr):
+                ip.dst = "224.0.0.2"
+
+            elif (igmp.type in (IGMP_TYPE_V1_MEMBERSHIP_REPORT, IGMP_TYPE_V2_MEMBERSHIP_REPORT) and
+                  IGMPv3.is_valid_mcaddr(igmp.gaddr)):
+                ip.dst = igmp.gaddr
+
+           # We do not need to fixup the ether layer, it is done by scapy
+           #
+           # if pkt.haslayer(Ether):
+           #     eth = pkt.getlayer(Ether)
+           #     ip_long = atol(ip.dst)
+           #     ether.dst = '01:00:5e:%02x:%02x:%02x' % ( (ip_long >> 16) & 0x7f, (ip_long >> 8) & 0xff, ip_long & 0xff )
+
+
+        return pkt
+
+
+bind_layers(IP,       IGMPv3,   frag=0, proto=2, ttl=1, tos=0xc0)
+bind_layers(IGMPv3,   IGMPv3gr, frag=0, proto=2)
+bind_layers(IGMPv3gr, IGMPv3gr, frag=0, proto=2)
+
+
+if __name__ == "__main__":
+
+    print "test float encoding"
+    from math import log
+    max_expected_error = 1.0 / (2<<3) # four bit precision
+    p = IGMPv3()
+    for v in range(0, 31745):
+        c = p.encode_float(v)
+        d = p.decode_float(c)
+        rel_err = float(v-d)/v if v!=0 else 0.0
+        assert rel_err <= max_expected_error
+
+    print "construct membership query - general query"
+    mq = IGMPv3(type=IGMP_TYPE_MEMBERSHIP_QUERY, max_resp_code=120)
+    hexdump(str(mq))
+
+    print "construct membership query - group-specific query"
+    mq = IGMPv3(type=IGMP_TYPE_MEMBERSHIP_QUERY, max_resp_code=120, gaddr="224.0.0.1")
+    hexdump(str(mq))
+
+    print "construct membership query - group-and-source-specific query"
+    mq = IGMPv3(type=IGMP_TYPE_MEMBERSHIP_QUERY, max_resp_code=120, gaddr="224.0.0.1")
+    mq.srcs = ['1.2.3.4', '5.6.7.8']
+    hexdump(str(mq))
+
+    print "fixup"
+    mq = IGMPv3(type=IGMP_TYPE_MEMBERSHIP_QUERY)
+    mq.srcs = ['1.2.3.4', '5.6.7.8']
+    pkt = Ether() / IP() / mq
+    print "before fixup:"
+    hexdump(str(pkt))
+
+    print "after fixup:"
+    IGMPv3.fixup(pkt)
+    hexdump(str(pkt))
+
+    print "construct v3 membership report - join a single group"
+    mr = IGMPv3(type=IGMP_TYPE_V3_MEMBERSHIP_REPORT, max_resp_code=30, gaddr="224.0.0.1")
+    mr.grps = [IGMPv3gr( rtype=IGMP_V3_GR_TYPE_EXCLUDE, mcaddr="229.10.20.30")]
+    hexdump(mr)
+
+    print "construct v3 membership report - join two groups"
+    mr = IGMPv3(type=IGMP_TYPE_V3_MEMBERSHIP_REPORT, max_resp_code=30, gaddr="224.0.0.1")
+    mr.grps = [
+        IGMPv3gr(rtype=IGMP_V3_GR_TYPE_EXCLUDE, mcaddr="229.10.20.30"),
+        IGMPv3gr(rtype=IGMP_V3_GR_TYPE_EXCLUDE, mcaddr="229.10.20.31")
+    ]
+    hexdump(mr)
+
+    print "construct v3 membership report - leave a group"
+    mr = IGMPv3(type=IGMP_TYPE_V3_MEMBERSHIP_REPORT, max_resp_code=30, gaddr="224.0.0.1")
+    mr.grps = [IGMPv3gr(rtype=IGMP_V3_GR_TYPE_INCLUDE, mcaddr="229.10.20.30")]
+    hexdump(mr)
+
+    print "all ok"
diff --git a/src/test/utils/McastTraffic.py b/src/test/utils/McastTraffic.py
new file mode 100644
index 0000000..2bd28e2
--- /dev/null
+++ b/src/test/utils/McastTraffic.py
@@ -0,0 +1,49 @@
+import threading
+import sys
+import os
+import time
+import monotonic
+from scapy.all import *
+
+class McastTraffic(threading.Thread):
+
+    dst_mac = '01:00:5e:00:01:01'
+    src_mac = '02:88:b4:e4:90:77'
+    src_ip = '1.2.3.4'
+    SEND_STATE = 1
+    RECV_STATE = 2
+    def __init__(self, addrs, iface = 'eth0', cb = None, arg = None):
+        threading.Thread.__init__(self)
+        self.addrs = addrs
+        self.iface = iface
+        self.cb = cb
+        self.arg = arg
+        self.state = self.SEND_STATE | self.RECV_STATE
+
+    def run(self):
+        eth = Ether(dst = self.dst_mac, src = self.src_mac)
+        while self.state & self.SEND_STATE:
+            for addr in self.addrs:
+                #data = repr(time.time())
+                data = repr(monotonic.monotonic())
+                ip = IP(dst = addr, src = self.src_ip)
+                sendp(eth/ip/data, iface = self.iface)
+            if self.cb:
+                self.cb(self.arg)
+
+    def stop(self):
+        self.state = 0
+
+    def stopReceives(self):
+        self.state &= ~self.RECV_STATE
+
+    def stopSends(self):
+        self.state &= ~self.SEND_STATE
+
+    def isRecvStopped(self):
+        return False if self.state & self.RECV_STATE else True
+
+    def isSendStopped(self):
+        return False if self.state & self.SEND_STATE else True
+
+    
diff --git a/src/test/utils/Stats.py b/src/test/utils/Stats.py
new file mode 100644
index 0000000..3d8e40c
--- /dev/null
+++ b/src/test/utils/Stats.py
@@ -0,0 +1,34 @@
+from math import sqrt
+
+class Stats:
+      def __init__(self):
+            self.count = 0
+            self.start = 0
+            self.delta = 0
+            self.min = 0
+            self.max = 0
+            self.delta_squares = 0
+
+      def update(self, packets = 0, t = 0):
+            self.count += packets
+            t *= 1000000 ##convert to usecs
+            if self.start == 0:
+                  self.start = t
+            self.delta += t
+            self.delta_squares += t*t
+            if self.min == 0 or t < self.min:
+                  self.min = t
+            if self.max == 0 or t > self.max:
+                  self.max = t
+
+      def __repr__(self):
+            if self.count == 0:
+                  self.count = 1
+            mean = self.delta/self.count
+            mean_square = mean*mean
+            delta_square_mean = self.delta_squares/self.count
+            std_mean = sqrt(delta_square_mean - mean_square)
+            r = 'Avg %.3f usecs, Std deviation %.3f usecs, Min %.3f, Max %.3f for %d packets\n' %(
+                  mean, std_mean, self.min, self.max, self.count)
+            return r
+