Beispiel #1
0
    def process_link_status(self, sock, eventmask):
        """

        Args:
            sock:
            eventmask:

        Returns:

        """
        try:
            if sock is not None:
                if isinstance(sock, int) and eventmask == zmq.POLLIN:
                    if sock != self.linksock.fileno():
                        self.logger.warn(
                            "Got a unexpected socket event, the sock is not expected ")
                        return
                    data = self.linksock.recv(65535)
                    interface, state = self._decode_link_state(data)
                    if interface is None:
                        self.logger.debug("Fail to decode interface state:%s", state)
                        return
                    self.logger.warn("receive msg: %s is %s", interface, state)
                    Mcast.interface_state_change(interface=interface, state=state)

        except Exception as e:
            self.logger.warn("Exception happens when handle link status, error:" + str(e) + ", The Trace back is:\n" +
                             traceback.format_exc())
        return
Beispiel #2
0
 def fsmStateRecipientEnterStateEstablished(self, event):
     self.lastchangetime = time.time()
     self.SendHalMsg(L2tpv3Session.ADD_SESSION)
     self.updateSessionRecord()
     # if multicast session ,send mcast join
     for avp in self.avps_icrq:
         if isinstance(avp, L2tpv3CableLabsAvps.DepiRemoteMulticastJoin):
             address = (self.connection.localAddr, avp.src_ip, avp.group_ip,
                        0)
             try:
                 mcast = Mcast.findMcastInstance(address=address)
                 if mcast is None:
                     mcast = Mcast(address=address)
                 mcast.join(session=(self.connection.localAddr,
                                     self.connection.remoteAddr,
                                     self.localSessionId,
                                     self.remoteSessionId))
                 self.mcast.append(address)
                 if mcast.status != Mcast.JOINED:
                     self.logger.warn(
                         "Session [%d, %d] mcast join failed %s:",
                         self.localSessionId, self.remoteSessionId, address)
             except Exception as e:
                 self.logger.warn(
                     "Session [%d, %d] mcast join failed %s: %s",
                     self.localSessionId, self.remoteSessionId, address,
                     str(e))
Beispiel #3
0
    def fsmStateRecipientLeaveStateEstablished(self, event):
        self.SendHalMsg(L2tpv3Session.DEL_SESSION)
        # if multicast session ,send mcast leave

        for avp in self.avps_cdn:
            if isinstance(avp, L2tpv3CableLabsAvps.DepiRemoteMulticastLeave):
                address = (self.connection.localAddr, avp.src_ip, avp.group_ip,
                           0)
                if address in self.mcast:
                    self.mcast.remove(address)
                    try:
                        mcast = Mcast.findMcastInstance(address=address)
                        if mcast is not None:
                            mcast.leave(session=(self.connection.localAddr,
                                                 self.connection.remoteAddr,
                                                 self.localSessionId,
                                                 self.remoteSessionId))
                    except Exception as e:
                        self.logger.warn(
                            "Session [%d, %d] mcast join failed %s: %s",
                            self.localSessionId, self.remoteSessionId, address,
                            str(e))
                else:
                    self.logger.warn(
                        "mcast address[%s] is not joined in session[0x%x]",
                        address, self.localSessionId)

        while len(self.mcast):
            address = self.mcast.pop(0)
            self.logger.warn(
                "mcast address[%s] is not in session[0x%x] cdn avps",
                str(address), self.localSessionId)
            try:
                mcast = Mcast.findMcastInstance(address=address)
                if mcast is not None:
                    mcast.leave(session=(self.connection.localAddr,
                                         self.connection.remoteAddr,
                                         self.localSessionId,
                                         self.remoteSessionId))
            except Exception as e:
                self.logger.warn("Session [%d, %d] mcast join failed %s: %s",
                                 self.localSessionId, self.remoteSessionId,
                                 address, str(e))
Beispiel #4
0
 def test_join(self):
     session = ("127.0.0.1", "127.0.0.1", 1233, 12323)
     mcast = Mcast(address=("127.0.0.1", "5.5.5.1", "229.1.1.255", 0))
     self.assertIn(("127.0.0.1", "5.5.5.1", "229.1.1.255", 0),
                   Mcast.McastDb.keys())
     self.assertIn(mcast, Mcast.McastDb.values())
     mcast.join(session)
     self.assertEqual(mcast.status, Mcast.JOINED)
     time.sleep(0.1)
     mcast.close()
     self.assertEqual(mcast.status, Mcast.LEAVED)
     self.assertNotIn(("127.0.0.1", "5.5.5.1", "229.1.1.255", 0),
                      Mcast.McastDb.keys())
     self.assertNotIn(mcast, Mcast.McastDb.values())
Beispiel #5
0
    def test__handleMcastInfo(self):
        cmd = l2tpMsg.L2tpCommandReq()
        cmd.cmd = l2tpMsg.MCAST_INFO
        msg = self.api._handleMsg(cmd)
        self.assertEqual(msg.rsp, l2tpMsg.SUCCESS)

        session_1 = ("127.0.0.1", "127.0.0.1", 1, 1)
        session_2 = ("127.0.0.1", "127.0.0.1", 2, 2)
        session_3 = ("127.0.0.1", "127.0.0.1", 3, 3)
        address = ("127.0.0.1", "5.5.5.1", "229.1.1.255", 0)
        mcast = Mcast(address=address)
        mcast.join(session_1)
        time.sleep(0.1)
        mcast.join(session_2)
        msg = self.api._handleMsg(cmd)
        print msg
        self.assertEqual(msg.rsp, l2tpMsg.SUCCESS)

        Mcast.McastDb[("127.0.0.1", "5.5.5.1", "225.1.1.255", 0)] = "test"
        msg = self.api._handleMsg(cmd)
        print msg
        data = []
        print "********"
        for mcastInfo in msg.mcast_info.mcastInfoList:
            mcast_line = L2tpMcastList()
            mcast_line.Grp = mcastInfo.grp
            mcast_line.Src = mcastInfo.src
            mcast_line.LocalIp = mcastInfo.local_ip
            mcast_line.interface = mcastInfo.interface
            mcast_line.status = mcastInfo.status
            mcast_line.last_change_time = mcastInfo.lastchange
            for session in mcastInfo.session:
                mcast_line.Session.append(session)
            print mcast_line.Grp, mcast_line.Src, mcast_line.LocalIp, mcast_line.LocalIp, mcast_line.interface,\
                mcast_line.status, mcast_line.last_change_time, len(mcast_line.Session)
            item = (mcast_line.interface, mcast_line.LocalIp, mcast_line.Grp,
                    mcast_line.Src, mcast_line.status,
                    str(len(mcast_line.Session)), mcast_line.last_change_time)
            data.append(item)
            del mcast_line
            printval = {"header": L2tpMcastList.HEADER, "data": data}
            output = testL2tpv3API.format_print(printval)
            print output
            del data[:]
        self.assertEqual(msg.rsp, l2tpMsg.SUCCESS)
        Mcast.McastDb.pop(("127.0.0.1", "5.5.5.1", "225.1.1.255", 0))

        for key in Mcast.McastDb.keys():
            Mcast.McastDb[key].close()
Beispiel #6
0
    def test_join_1(self):
        session = ("127.0.0.1", "127.0.0.1", 1233, 12323)
        address_1 = ("127.0.0.1", "5.5.5.1", "229.1.1.255", 0)
        address_2 = ("127.0.0.1", "7.5.5.2", "229.1.1.255", 0)
        address_3 = ("127.0.0.1", "3.5.5.2", "228.1.1.255", 0)

        mcast = Mcast(address=address_1)
        self.assertIn(("127.0.0.1", "5.5.5.1", "229.1.1.255", 0),
                      Mcast.McastDb.keys())
        self.assertIn(mcast, Mcast.McastDb.values())
        mcast.rejoin()
        self.assertEqual(mcast.status, Mcast.NOT_JOINED)
        mcast.join(session)
        self.assertEqual(mcast.status, Mcast.JOINED)
        time.sleep(0.1)
        # no operation in joined status
        mcast.join(session)
        self.assertEqual(mcast.status, Mcast.JOINED)

        mcast_3 = Mcast(address=address_3)
        self.assertIn(address_3, Mcast.McastDb.keys())
        time.sleep(0.1)
        # no operation in joined status
        mcast_3.join(session)
        self.assertEqual(mcast_3.status, Mcast.JOINED)

        try:
            mcast_test = Mcast.findMcastInstance(None)
        except McastException as e:
            pass

        mcast_test = Mcast.findMcastInstance(address_1)

        self.assertEqual(mcast_test.status, Mcast.JOINED)
        self.assertEqual(mcast_test, mcast)

        try:
            mcast = Mcast(address=address_1)
        except McastException as e:
            self.assertEqual(
                str(e),
                "init address %s was already initiated" % str(address_1))

        mcast_test = Mcast.findMcastInstance(address_2)
        self.assertIsNone(mcast_test)

        mcast_2 = Mcast(address=address_2)
        self.assertIn(("127.0.0.1", "5.5.5.1", "229.1.1.255", 0),
                      Mcast.McastDb.keys())
        self.assertIn(mcast_2, Mcast.McastDb.values())
        mcast_2.join(session)
        self.assertEqual(mcast_2.status, Mcast.JOINED)

        time.sleep(0.1)
        mcast_3.close()
        mcast.close()
        self.assertEqual(mcast.status, Mcast.LEAVED)
        time.sleep(1)
        self.assertNotIn(("127.0.0.1", "5.5.5.1", "229.1.1.255", 0),
                         Mcast.McastDb.keys())
        self.assertNotIn(Mcast, Mcast.McastDb.values())
        for key in Mcast.McastDb.keys():
            Mcast.McastDb[key].close()
        time.sleep(1)
        self.assertEqual(len(Mcast.McastDb), 0)
Beispiel #7
0
    def test_Mcast_error(self):
        session = ("127.0.0.1", "127.0.0.1", 1233, 12323)
        try:
            mcast = Mcast(address=("127.0.0.8", "5.5.5.1", "229.1.1.255", 0))
        except Exception as e:
            self.assertIsInstance(e, McastException)
        try:
            mcast = Mcast(None)
        except Exception as e:
            self.assertIsInstance(e, McastException)
        try:
            mcast = Mcast(address=("127.0.0.1", "5.5.5.1"))
        except Exception as e:
            self.assertIsInstance(e, McastException)

        try:
            mcast = Mcast.findMcastInstance(address=("127.0.0.1", "5.5.5.1"))
        except McastException as e:
            self.assertEqual(
                str(e), "init address %s is not expected" % str(
                    ("127.0.0.1", "5.5.5.1")))

        mcast = Mcast(address=("127.0.0.1", "5.5.5.1", "229.1.1.255", 1))
        mcast.join(session)
        self.assertEqual(mcast.status, Mcast.NOT_JOINED)

        mcast = Mcast(address=("127.0.0.1", "5.5.5.1", "229.1.1.255", 60002))
        mcast.join(session)
        self.assertEqual(mcast.status, Mcast.JOINED)
        mcast.leave(session)
Beispiel #8
0
 def test_mld_join(self):
     session_1 = ("::1", "::1", 1, 1)
     session_2 = ("::1", "::1", 2, 2)
     session_3 = ("::1", "::1", 3, 3)
     address = ("::1", "2001::1", "ff15:7079:7468:6f6e:6465:6d6f:6d63:6173",
                0)
     mcast = Mcast(address=address)
     self.assertIn(address, Mcast.McastDb.keys())
     self.assertIn(mcast, Mcast.McastDb.values())
     mcast.join(session_1)
     time.sleep(0.1)
     self.assertEqual(mcast.status, Mcast.JOINED)
     self.assertIn(session_1, mcast.sessionList)
     mcast.join(session_2)
     self.assertIn(session_1, mcast.sessionList)
     self.assertIn(session_2, mcast.sessionList)
     mcast.leave(session_1)
     time.sleep(0.1)
     self.assertNotIn(session_1, mcast.sessionList)
     self.assertIn(session_2, mcast.sessionList)
     self.assertEqual(mcast.status, Mcast.JOINED)
     mcast.leave(session_3)
     self.assertNotIn(session_1, mcast.sessionList)
     self.assertIn(session_2, mcast.sessionList)
     self.assertEqual(mcast.status, Mcast.JOINED)
     mcast.leave(session_2)
     time.sleep(0.1)
     self.assertEqual(0, len(mcast.sessionList))
     self.assertEqual(mcast.status, Mcast.LEAVED)
Beispiel #9
0
 def test_interface_check(self):
     address = ("127.0.0.1", "5.5.5.1", "229.1.1.255", 0)
     session_1 = ("127.0.0.1", "127.0.0.1", 1, 1)
     mcast = Mcast(address=address)
     mcast.join(session_1)
     time.sleep(1)
     mcast.rejoin()
     mcast.interface_state_change("lo", 'UP')
     self.assertEqual(mcast.interfaceList['lo'], Mcast.interface_up)
     mcast.interface_state_change("lo", 'DOWN')
     self.assertEqual(mcast.interfaceList['lo'], Mcast.interface_down)
     mcast.interface_state_change("lo", 'UP')
     self.assertEqual(mcast.interfaceList['lo'], Mcast.interface_up)
     for key in mcast.McastDb.keys():
         mcast.McastDb[key].close()
Beispiel #10
0
 def test_multi_session_join(self):
     session_1 = ("127.0.0.1", "127.0.0.1", 1, 1)
     session_2 = ("127.0.0.1", "127.0.0.1", 2, 2)
     session_3 = ("127.0.0.1", "127.0.0.1", 3, 3)
     address = ("127.0.0.1", "5.5.5.1", "229.1.1.255", 0)
     mcast = Mcast(address=address)
     self.assertIn(("127.0.0.1", "5.5.5.1", "229.1.1.255", 0),
                   Mcast.McastDb.keys())
     self.assertIn(mcast, Mcast.McastDb.values())
     mcast.join(session_1)
     time.sleep(0.1)
     self.assertEqual(mcast.status, Mcast.JOINED)
     self.assertIn(session_1, mcast.sessionList)
     mcast.join(session_2)
     self.assertIn(session_1, mcast.sessionList)
     self.assertIn(session_2, mcast.sessionList)
     mcast.leave(session_1)
     time.sleep(0.1)
     self.assertNotIn(session_1, mcast.sessionList)
     self.assertIn(session_2, mcast.sessionList)
     self.assertEqual(mcast.status, Mcast.JOINED)
     mcast.leave(session_3)
     self.assertNotIn(session_1, mcast.sessionList)
     self.assertIn(session_2, mcast.sessionList)
     self.assertEqual(mcast.status, Mcast.JOINED)
     mcast.leave(session_2)
     time.sleep(0.1)
     self.assertEqual(0, len(mcast.sessionList))
     self.assertEqual(mcast.status, Mcast.LEAVED)