Exemplo n.º 1
0
def flowident_from_ofp_match(m):
    return FlowIdent(srcip=m.nw_src,
                     dstip=m.nw_dst,
                     ipproto=m.nw_proto,
                     sport=m.tp_src,
                     dport=m.tp_dst,
                     srcmac=m.dl_src,
                     dstmac=m.dl_dst,
                     vlan=m.dl_vlan)
Exemplo n.º 2
0
    def __makeflow(self):
        if haveIPAddrGen:
            srcip = str(IPv4Address(ipaddrgen.generate_addressv4(self.ipsrcgen)))
            dstip = str(IPv4Address(ipaddrgen.generate_addressv4(self.ipdstgen)))
        else:
            srcip = str(IPAddress(int(self.ipsrc) + random.randint(0,self.ipsrc.numhosts-1)))
            dstip = str(IPAddress(int(self.ipdst) + random.randint(0,self.ipdst.numhosts-1)))

        ipproto = self.ipproto
        sport = dport = 0
        if ipproto == IPPROTO_ICMP:
            # std way that netflow encodes icmp type/code:
            # type in high-order byte of dport, 
            # code in low-order byte
            t = next(self.icmptype)
            c = next(self.icmpcode)
            dport = t << 8 | c
            # print 'icmp t,c,dport',hex(t),hex(c),hex(dport)
        else:
            if self.sport:
                sport = next(self.sport)
            if self.dport:
                dport = next(self.dport)
                
        flet = Flowlet(FlowIdent(srcip, dstip, ipproto, sport, dport))
        flet.iptos = next(self.iptos)
        flet.flowstart = flet.flowend = fscore().now

        if flet.ipproto == IPPROTO_TCP:
            flet.ackflow = not self.autoack

            tcpflags = next(self.tcpflags)
            flaglist = tcpflags.split('|')
            xtcpflags = 0x0
            for f in flaglist:
                if f == 'FIN':
                    xtcpflags |= 0x01
                elif f == 'SYN':
                    xtcpflags |= 0x02
                elif f == 'RST':
                    xtcpflags |= 0x04
                elif f == 'PUSH' or f == 'PSH':
                    xtcpflags |= 0x08
                elif f == 'ACK':
                    xtcpflags |= 0x10
                elif f == 'URG':
                    xtcpflags |= 0x20
                elif f == 'ECE':
                    xtcpflags |= 0x40
                elif f == 'CWR':
                    xtcpflags |= 0x80
                else:
                    raise InvalidFlowConfiguration('Invalid TCP flags mnemonic ' + f)

            flet.tcpflags = xtcpflags
        return flet
Exemplo n.º 3
0
 def callback(self):
     # pass oneself from srcnode to dstnode, performing action at each router
     # at end, set done to True
     f = SubtractiveFlowlet(FlowIdent(self.ipsrcfilt,
                                      self.ipdstfilt,
                                      ipproto=self.ipprotofilt),
                            action=self.action)
     self.logger.info('Subtractive generator callback')
     fscore().topology.node(self.srcnode).flowlet_arrival(
         f, 'subtractor', self.dstnode)
Exemplo n.º 4
0
 def controller_to_switch(self, switchname, mesg):
     '''Ferry an OF message from controller to switch'''
     if not self.started:
         # self.logger.debug("OF controller-to-switch deferred message {}".format(mesg))
         evid = 'deferred controller->switch send'
         fscore().after(0, evid, self.controller_to_switch, switchname, mesg)
     else:
         # self.logger.debug("OF controller-to-switch {}->{}: {}".format(self.name, switchname, mesg))
         link = self.switch_links[switchname][1]
         link.flowlet_arrival(OpenflowMessage(FlowIdent(), mesg), self.name, switchname)
Exemplo n.º 5
0
def packet_to_flowlet(pkt):
    '''Translate a POX packet to an fs flowlet'''
    try:
        return getattr(pkt, "origflet")
    except AttributeError,e:
        log = get_logger()
        flet = None
        ip = pkt.find('ipv4')
        if ip is None:
            flet = PoxFlowlet(FlowIdent())
            log.debug("Received non-IP packet {} from POX: there's no direct translation to fs".format(str(pkt.payload)))
        else:
            dport = sport = tcpflags = 0
            if ip.protocol == IPPROTO_TCP:
                tcp = ip.payload
                sport = tcp.srcport
                dport = tcp.dstport
                tcpflags = tcp.flags
                log.debug("Translating POX TCP packet to fs {}".format(tcp))
            elif ip.protocol == IPPROTO_UDP:
                udp = ip.payload
                sport = udp.srcport
                dport = udp.dstport
                log.debug("Translating POX UDP packet to fs {}".format(udp))
            elif ip.protocol == IPPROTO_ICMP:
                icmp = ip.payload
                dport = (icmp.type << 8) | icmp.code
                log.debug("Translating POX ICMP packet to fs {}".format(icmp))
            else:
                log.warn("Received unhandled IPv4 packet {} from POX: can't translate to fs".format(str(ip.payload)))

            flet = PoxFlowlet(FlowIdent(srcip=ip.srcip, dstip=ip.dstip, ipproto=ip.protocol, sport=sport, dport=dport))
            flet.tcpflags = tcpflags
            flet.iptos = ip.tos

        flet.srcmac = pkt.src
        flet.dstmac = pkt.dst
        flet.pkts = 1
        flet.bytes = len(pkt)
        flet.origpkt = pkt
        return flet
Exemplo n.º 6
0
    def __makeflow(self):
        while True:
            if haveIPAddrGen:
                srcip = str(ipaddr.IPv4Address(ipaddrgen.generate_addressv4(self.ipsrcgen)))
                dstip = str(ipaddr.IPv4Address(ipaddrgen.generate_addressv4(self.ipdstgen)))
            else:
                # srcip = str(ipaddr.IPAddress(int(self.srcnet) + random.randint(0,self.srcnet.numhosts-1)))
                # dstip = str(ipaddr.IPAddress(int(self.dstnet) + random.randint(0,self.dstnet.numhosts-1)))
                srcip = str(ipaddr.IPAddress(int(self.srcnet) + random.randint(0, 2)))
                dstip = str(ipaddr.IPAddress(int(self.dstnet) + random.randint(0, 2)))

            ipproto = next(self.ipproto)
            sport = next(self.srcports)
            dport = next(self.dstports)
            fsize = int(next(self.flowsizerv))
            flet = Flowlet(FlowIdent(srcip, dstip, ipproto, sport, dport), bytes=fsize)
            
            flet.iptos = next(self.iptosrv)
            if flet.key not in self.activeflows:
                break

        return flet
Exemplo n.º 7
0
class TestFlowlet(FsTestBase):
    def setUp(self):
        self.ident1 = FlowIdent(srcip="1.1.1.1",dstip="2.2.2.2",ipproto=6, dport=80, sport=10000)
        self.ident2 = FlowIdent(str(ipaddr.IPAddress('10.0.1.1')), str(ipaddr.IPAddress('192.168.5.2')), 17, 5, 42)

    def testFlowIdent(self):
        ftfwd2 = self.ident2.mkreverse().mkreverse()
        self.assertEqual(self.ident2.key, ftfwd2.key)

    def testBuildFlowlet(self):
        f1 = Flowlet(self.ident1)
        f1.flowstart = time.time()
        f1.flowend = time.time() + 10
        self.assertEqual(repr(f1.key), repr(self.ident1))

    def testCopy(self):
        # NB: shallow copy of f1; flow key will be identical
        f1 = Flowlet(self.ident2)
        f2 = copy.copy(f1)
        # test whether FlowIdent keys referred to by each flowlet
        # are the same object
        self.assertIs(f1.key, f2.key)

    def testAdd(self):
        f1 = Flowlet(self.ident1)
        f1.pkts = 1 
        f1.bytes = 1 
        f2 = copy.copy(f1)
        f2.pkts = 1
        f2.bytes = 1
        f1 += f2
        self.assertEqual(f1.pkts, 2)
        self.assertEqual(f1.bytes, 2)
        self.assertEqual(f2.pkts, 1)
        self.assertEqual(f2.bytes, 1)

    def testSubtractive(self):
        f1 = SubtractiveFlowlet(self.ident1, "removeuniform(0.001)")
Exemplo n.º 8
0
 def setUp(self):
     self.ident1 = FlowIdent(srcip="1.1.1.1",dstip="2.2.2.2",ipproto=6, dport=80, sport=10000)
     self.ident2 = FlowIdent(str(ipaddr.IPAddress('10.0.1.1')), str(ipaddr.IPAddress('192.168.5.2')), 17, 5, 42)
Exemplo n.º 9
0
 def send(self, ofmessage):
     '''Callback function for POX SoftwareSwitch to send an outgoing OF message
     to controller.'''
     if not self.started:
         # self.logger.debug("OF switch-to-controller deferred message {}".format(ofmessage))
         evid = 'deferred switch->controller send'
         fscore().after(0.0, evid, self.send, ofmessage)
     else:
         # self.logger.debug("OF switch-to-controller {} - {}".format(str(self.controller_links[self.controller_name]), ofmessage))
         clink = self.controller_links[self.controller_name]
         self.controller_links[self.controller_name].flowlet_arrival(OpenflowMessage(FlowIdent(), ofmessage), self.name, self.controller_name)