Beispiel #1
0
def CountResultRule(FieldInfoList, FileList, LAll, RIDOrderList,
                    CombinationNum, stride_s, cluster_n):
    if len(FileList) != len(RIDOrderList):
        print('Error')
        return []
    CountResultList = []
    for m in range(len(FileList)):
        if LAll == 104:  # 5-tuple
            RuleList, linenum = Init(
                FileList[m])  # RuleList[i] <- [RID, sa, da, sp, dp, prtcl]
        elif LAll == 253:  # OpenFlow1.0
            RuleList, linenum = Init_OpenFlow(
                FileList[m])  # RuleList[i] <- [RID, in_port...]
        N = len(RuleList)

        OldAllFieldList = [''.join(RuleList[n][1:]) for n in range(N)]
        OldwildcardMatrixCount = CountWildcardMatrix(OldAllFieldList, LAll, N,
                                                     stride_s, cluster_n)

        tmpAllFieldList = [
            ''.join(RuleList[n][1:])
            for n in RIDOrderList[m][CombinationNum - 1]
        ]
        wildcardMatrixCount = CountWildcardMatrix(tmpAllFieldList, LAll, N,
                                                  stride_s, cluster_n)
        CountResultList.append(
            (FileList[m],
             (wildcardMatrixCount * stride_s * cluster_n) / (LAll * N),
             (OldwildcardMatrixCount * stride_s * cluster_n) / (LAll * N)))
    return CountResultList
Beispiel #2
0
def Combination_ViolentSearch(FieldInfoList, FileList, LAll, CombinationNum,
                              stride_s, cluster_n):
    ResultList = []
    for m in range(len(FileList)):
        if LAll == 104:  # 5-tuple
            RuleList, linenum = Init(
                FileList[m])  # RuleList[i] <- [RID, sa, da, sp, dp, prtcl]
        elif LAll == 253:  # OpenFlow1.0
            RuleList, linenum = Init_OpenFlow(
                FileList[m])  # RuleList[i] <- [RID, in_port...]
        N = len(RuleList)

        if N < cluster_n:
            ResultList.append((FileList[m], 0.0, ()))
            continue

        tmpNList = np.arange(N).tolist()
        permutationList = list(permutations(tmpNList, N))  # N! kinds
        print('len(permutationList)', len(permutationList), N)
        MaxwildcardMatrixCount = 0
        maxItem = ()
        for Item in permutationList:
            tmpAllFieldList = [''.join(RuleList[n][1:]) for n in Item]
            wildcardMatrixCount = CountWildcardMatrix(tmpAllFieldList, LAll, N,
                                                      stride_s, cluster_n)
            if MaxwildcardMatrixCount < wildcardMatrixCount:
                MaxwildcardMatrixCount = wildcardMatrixCount
                maxItem = Item
        M = MaxwildcardMatrixCount * stride_s * cluster_n
        ResultList.append((FileList[m], M / (LAll * N), maxItem))
        print('maxItem', [''.join(RuleList[n][1:]) for n in maxItem])
        print('ViolentSearch', FileList[m], M / (LAll * N))
    return ResultList
Beispiel #3
0
def MaxCompressedPresent(DataFlag, FieldInfoList, FileList, LAll):
    ResultList = []
    for m in range(len(FileList)):
        if LAll == 104:  # 5-tuple
            RuleList, linenum = Init(
                FileList[m])  # RuleList[i] <- [RID, sa, da, sp, dp, prtcl]
        elif LAll == 253:  # OpenFlow1.0
            RuleList, linenum = Init_OpenFlow(
                FileList[m])  # RuleList[i] <- [RID, in_port...]
        N = len(RuleList)
        M = 0
        for n in range(len(FieldInfoList)):
            for i in range(N):
                wildcardFlag = RuleList[i][n + 1].find('*')
                if wildcardFlag != -1:
                    M += len(RuleList[i][n + 1]) - wildcardFlag
        ResultList.append((FileList[m], M / (LAll * N)))
    return ResultList
Beispiel #4
0
def OrderRuleList(FieldInfoList, FileList, LAll, RIDOrderList, CombinationNum,
                  stride_s, cluster_n):
    if len(FileList) != len(RIDOrderList):
        print('Error')
        return False
    for m in range(len(FileList)):
        if LAll == 104:  # 5-tuple
            RuleList, linenum = Init(
                FileList[m])  # RuleList[i] <- [RID, sa, da, sp, dp, prtcl]
        elif LAll == 253:  # OpenFlow1.0
            RuleList, linenum = Init_OpenFlow(
                FileList[m])  # RuleList[i] <- [RID, in_port...]
        fw = open(
            'Order_' + str(CombinationNum) + '_' + str(stride_s) + '_' +
            str(cluster_n) + FileList[m], 'w')
        for n in RIDOrderList[m][CombinationNum - 1]:
            fw.write('\t'.join(RuleList[n][1:]) + '\n')
        fw.close()
    return True
Beispiel #5
0
def CombinationMix_MaxPadding(FieldInfoList, FileList, LAll, CombinationNum,
                              stride_s, cluster_n):
    ResultList = []
    RIDOrderList = []
    WOMPList = []  # without MaxPadding

    for m in range(len(FileList)):
        if LAll == 104:  # 5-tuple
            RuleList, linenum = Init(
                FileList[m])  # RuleList[i] <- [RID, sa, da, sp, dp, prtcl]
        elif LAll == 253:  # OpenFlow1.0
            RuleList, linenum = Init_OpenFlow(
                FileList[m])  # RuleList[i] <- [RID, in_port...]
        N = len(RuleList)
        v_CombinationResult = []
        AllRuleSet = set([item[0] for item in RuleList])  # Init: all RID
        RuleElseSet = AllRuleSet

        v_foundFieldDic = {}
        v_RulepDicOld = {}
        M_mp_All = 0
        for cv in range(CombinationNum):
            v_foundFieldDicNew, v_RulepDicNew, v_tmpresultItem = CombinationFind(
                RuleList, N, FieldInfoList, LAll, v_foundFieldDic,
                v_RulepDicOld, stride_s, cluster_n)
            if v_RulepDicNew == {}:  # can not find field to compress
                break
            #------------------------------------------------------------------
            h_foundFieldDic = v_foundFieldDicNew
            h_RulepDicOld = v_RulepDicNew
            h_CombinationResult = [(tuple(v_RulepDicNew.keys()),
                                    v_tmpresultItem, [])]  # Add the first item
            for ch in range(CombinationNum - 1):
                h_foundFieldDicNew, h_RulepDicNew, h_tmpresultItem = CombinationFind(
                    RuleList, N, FieldInfoList, LAll, h_foundFieldDic,
                    h_RulepDicOld, stride_s, cluster_n)
                if h_RulepDicNew == {}:  # can not find field to compress
                    break

                #---Max Padding---
                m_MaxPaddingResult = []
                #print('T->', len(h_RulepDicNew), len(h_RulepDicOld))
                if (len(h_RulepDicNew) / len(h_RulepDicOld)) < (2 / 3):
                    m_foundFieldDic = h_foundFieldDic
                    tmpRuleElseSet = set(h_RulepDicOld) - set(h_RulepDicNew)
                    m_RulepDicOld = dict(
                        zip(tmpRuleElseSet, [1] * len(tmpRuleElseSet)))
                    for cm in range(len(FieldInfoList) - len(m_foundFieldDic)):
                        m_foundFieldDicNew, m_RulepDicNew, m_tmpresultItem = CombinationFind(
                            RuleList, N, FieldInfoList, LAll, m_foundFieldDic,
                            m_RulepDicOld, stride_s, cluster_n)
                        if m_RulepDicNew == {}:
                            break
                        m_foundFieldDic = m_foundFieldDicNew
                        m_RulepDicOld = m_RulepDicNew
                        m_MaxPaddingResult.append(
                            (tuple(m_RulepDicNew.keys()), m_tmpresultItem))
                #-----------------

                h_foundFieldDic = h_foundFieldDicNew
                h_RulepDicOld = h_RulepDicNew  #!!!
                h_CombinationResult.append(
                    (tuple(h_RulepDicNew.keys()), h_tmpresultItem,
                     m_MaxPaddingResult)
                )  # tmpresultItem <- (Lm, FieldName, p*Np / LAll*N, p, Np)

            M = 0
            h_tmpResultDic = {}
            tmplist = []
            for ch in range(len(h_CombinationResult)):
                '''
                FieldName = h_CombinationResult[ch][1][1]
                pNum = h_CombinationResult[ch][1][-1]
                pLen = h_CombinationResult[ch][1][-2]
                print(cv, ch, pLen, pNum, FieldName)
                '''
                tmplist = list(h_CombinationResult[ch][0])
                tmplist.sort()
                M = 0
                for cr in range(ch, 0, -1):
                    tmplt = list(
                        set(h_CombinationResult[cr - 1][0]) -
                        set(h_CombinationResult[cr][0]))
                    tmplt.sort()
                    tmplist += tmplt
                    tmpNum = h_CombinationResult[cr][1][-1]
                    tmpLen = h_CombinationResult[cr][1][-2]
                    M += (tmpLen - tmpLen % stride_s) * (
                        tmpNum - tmpNum % cluster_n
                    )  # each field length of five-tuple is a power of 2, find wildcard-PE from behind
                tmpNum = h_CombinationResult[0][1][-1]
                tmpLen = h_CombinationResult[0][1][-2]
                M += (tmpLen - tmpLen % stride_s) * (tmpNum -
                                                     tmpNum % cluster_n)
                h_tmpResultDic[ch] = (tuple(tmplist), M)

            #---Max Padding---
            M_mp = 0
            for ch in range(len(h_CombinationResult)):
                m_MaxPaddingResult = h_CombinationResult[ch][2]
                if m_MaxPaddingResult != []:
                    for cm in range(len(m_MaxPaddingResult)):
                        tmpNum = m_MaxPaddingResult[cm][1][-1]
                        tmpLen = m_MaxPaddingResult[cm][1][-2]
                        #print('MP',ch, cm, tmpLen, tmpNum, m_MaxPaddingResult[cm][1][1])
                        M_mp += (tmpLen - tmpLen % stride_s) * (
                            tmpNum - tmpNum % cluster_n)
            M += M_mp
            h_tmpResultDic[len(h_CombinationResult) - 1] = (tuple(tmplist), M)
            M_mp_All += M_mp
            #-----------------

            for ch in range(len(h_CombinationResult), CombinationNum):
                h_tmpResultDic[ch] = (tuple(tmplist), M)
            #------------------------------------------------------------------
            RuleElseSet = RuleElseSet - set(v_RulepDicNew)
            v_RulepDicOld = dict(zip(RuleElseSet, [1] * len(RuleElseSet)))
            v_CombinationResult.append(h_tmpResultDic)
            if v_RulepDicOld == {}:
                break
        #print(v_CombinationResult)
        #print('len(v_CombinationResult)', len(v_CombinationResult))
        M = 0
        v_tmpResultDic = {}
        v_tmpRIDOrderDic = {}
        tmpcurrentlist = list(AllRuleSet)
        for cv in range(len(v_CombinationResult)):
            # Order RID
            tmpList = []
            for cr in range(0, cv):
                tmpDic = v_CombinationResult[cr]
                tmplt = list(tmpDic[cv][0])  # has been sorted !
                tmpList += tmplt
            tmplt = list(AllRuleSet - set(tmpList))
            tmplt.sort()
            tmpcurrentlist = tmpList + tmplt
            # Compute M
            M = 0
            for cN in range(0, cv + 1):
                M += v_CombinationResult[cN][cv][1]
            '''
            #tmpAllFieldList = [''.join(RuleList[n][1:]) for n in tmpcurrentlist]
            #wildcardMatrixCount = CountWildcardMatrix(tmpAllFieldList, L, N, stride_s, cluster_n)
            #print('wildcardMatrixCount',wildcardMatrixCount)
            #M = wildcardMatrixCount * stride_s * cluster_n
            '''
            v_tmpResultDic[cv] = M / (LAll * N)
            v_tmpRIDOrderDic[cv] = tuple(tmpcurrentlist)
        for cv in range(len(v_CombinationResult), CombinationNum):
            # Order RID
            tmpList = []
            for cr in range(0, len(v_CombinationResult)):
                tmpDic = v_CombinationResult[cr]
                tmplt = list(tmpDic[cv][0])  # has been sorted !
                tmpList += tmplt
            tmplt = list(AllRuleSet - set(tmpList))
            tmplt.sort()
            tmpcurrentlist = tmpList + tmplt
            # Compute M
            M = 0
            for cN in range(0, len(v_CombinationResult)):
                M += v_CombinationResult[cN][cv][1]
            v_tmpResultDic[cv] = M / (LAll * N)
            v_tmpRIDOrderDic[cv] = tuple(tmpcurrentlist)

        print('Mix', FileList[m], M / (LAll * N), len(tmpcurrentlist), N,
              N / linenum, len(v_CombinationResult))
        #print(v_tmpResultDic)
        ResultList.append(v_tmpResultDic)
        RIDOrderList.append(v_tmpRIDOrderDic)
        WOMPList.append((M - M_mp_All) / (LAll * N))
    return (ResultList, RIDOrderList, WOMPList)
Beispiel #6
0
def CombinationVertical(FieldInfoList, FileList, LAll, CombinationNum,
                        stride_s, cluster_n):
    ResultList = []
    RIDOrderList = []

    for m in range(len(FileList)):
        if LAll == 104:  # 5-tuple
            RuleList, linenum = Init(
                FileList[m])  # RuleList[i] <- [RID, sa, da, sp, dp, prtcl]
        elif LAll == 253:  # OpenFlow1.0
            RuleList, linenum = Init_OpenFlow(
                FileList[m])  # RuleList[i] <- [RID, in_port...]
        N = len(RuleList)
        CombinationResult = []
        AllRuleSet = set([item[0] for item in RuleList])  # Init: all RID
        RuleElseSet = AllRuleSet

        RulepDicOld = {}

        foundFieldDic = {}
        RulepDicOld = {}
        for c in range(CombinationNum):
            foundFieldDicNew, RulepDicNew, tmpresultItem = CombinationFind(
                RuleList, N, FieldInfoList, LAll, foundFieldDic, RulepDicOld,
                stride_s, cluster_n
            )  # tmpresultItem <- (Lm, FieldName, p*Np / LAll*N, p, Np)
            if RulepDicNew == {}:
                break
            RuleElseSet = RuleElseSet - set(RulepDicNew)
            RulepDicOld = dict(zip(RuleElseSet, [1] * len(RuleElseSet)))
            CombinationResult.append(
                (tuple(RulepDicNew.keys()), tmpresultItem))
            #print tmpresultItem
            if RulepDicOld == {}:
                break

        M = 0
        tmpResultDic = {}
        tmpRIDOrderDic = {}
        tmpList = []
        tmpcurrentlist = list(AllRuleSet)
        for c in range(len(CombinationResult)):
            # Order RID
            tmplt = list(CombinationResult[c][0])
            tmplt.sort()
            tmpList += tmplt
            tmplt = list(AllRuleSet - set(tmpList))
            tmplt.sort()
            tmpcurrentlist = tmpList + tmplt  # current RID order for c
            # Compute M
            tmpNum = CombinationResult[c][1][-1]
            tmpLen = CombinationResult[c][1][-2]
            M += (tmpLen - tmpLen % stride_s) * (tmpNum - tmpNum % cluster_n)
            '''
            #tmpAllFieldList = [''.join(RuleList[n][1:]) for n in tmpcurrentlist]
            #wildcardMatrixCount = CountWildcardMatrix(tmpAllFieldList, L, N, stride_s, cluster_n)
            ##print('wildcardMatrixCount',wildcardMatrixCount)
            #M = wildcardMatrixCount * stride_s * cluster_n
            '''
            tmpResultDic[c] = M / (LAll * N)
            tmpRIDOrderDic[c] = tmpcurrentlist
        for c in range(len(CombinationResult), CombinationNum):
            tmpResultDic[c] = M / (LAll * N)
            tmpRIDOrderDic[c] = tmpcurrentlist
        print('Vertical', FileList[m], M / (LAll * N))
        ResultList.append(tmpResultDic)
        RIDOrderList.append(tmpRIDOrderDic)
    return (ResultList, RIDOrderList)
Beispiel #7
0
def CombinationHorizontal(FieldInfoList, FileList, LAll, CombinationNum,
                          stride_s, cluster_n):
    ResultList = []
    RIDOrderList = []

    for m in range(len(FileList)):
        if LAll == 104:  # 5-tuple
            RuleList, linenum = Init(
                FileList[m])  # RuleList[i] <- [RID, sa, da, sp, dp, prtcl]
        elif LAll == 253:  # OpenFlow1.0
            RuleList, linenum = Init_OpenFlow(
                FileList[m])  # RuleList[i] <- [RID, in_port...]
        N = len(RuleList)
        CombinationResult = []
        AllRuleSet = set([item[0] for item in RuleList])  # Init: all RID

        foundFieldDic = {}
        RulepDicOld = {}
        for c in range(CombinationNum):
            foundFieldDicNew, RulepDicNew, tmpresultItem = CombinationFind(
                RuleList, N, FieldInfoList, LAll, foundFieldDic, RulepDicOld,
                stride_s, cluster_n
            )  # tmpresultItem <- (Lm, FieldName, p*Np / LAll*N, p, Np)
            if RulepDicNew == {}:
                break
            foundFieldDic = foundFieldDicNew
            RulepDicOld = RulepDicNew  #!!!
            #print foundFieldDic
            CombinationResult.append(
                (tuple(RulepDicNew.keys()), tmpresultItem))

        M = 0
        tmpResultDic = {}
        tmpRIDOrderDic = {}
        RuleElseList = list(AllRuleSet - set(CombinationResult[0]))
        RuleElseList.sort()
        tmplist = []
        #print('RuleElseList', len(RuleElseList))
        for c in range(len(CombinationResult)):
            tmplist = list(CombinationResult[c][0])
            tmplist.sort()
            M = 0
            for cr in range(c, 0, -1):
                tmplt = list(
                    set(CombinationResult[cr - 1][0]) -
                    set(CombinationResult[cr][0]))
                tmplt.sort()
                tmplist += tmplt
                tmpNum = CombinationResult[cr][1][-1]
                tmpLen = CombinationResult[cr][1][-2]
                M += (tmpLen - tmpLen % stride_s) * (
                    tmpNum - tmpNum % cluster_n
                )  # each field length of five-tuple is a power of 2, find wildcard-PE from behind
            tmpNum = CombinationResult[0][1][-1]
            tmpLen = CombinationResult[0][1][-2]
            M += (tmpLen - tmpLen % stride_s) * (tmpNum - tmpNum % cluster_n)
            tmplist += RuleElseList  # current RID order for c
            '''
            #tmpAllFieldList = [''.join(RuleList[n][1:]) for n in tmplist]
            ##print('tmpAllFieldList', len(tmpAllFieldList), N)
            #wildcardMatrixCount = CountWildcardMatrix(tmpAllFieldList, L, len(tmpAllFieldList), stride_s, cluster_n)
            ##print('wildcardMatrixCount', wildcardMatrixCount)
            #M = wildcardMatrixCount * stride_s * cluster_n
            '''
            tmpResultDic[c] = M / (LAll * N)
            tmpRIDOrderDic[c] = tmplist
        for c in range(len(CombinationResult), CombinationNum):
            tmpResultDic[c] = M / (LAll * N)
            tmpRIDOrderDic[c] = tmplist
        print('Horizontal', FileList[m], M / (LAll * N))
        #print(tmpResultDic)
        ResultList.append(tmpResultDic)
        RIDOrderList.append(tmpRIDOrderDic)
    return (ResultList, RIDOrderList)