Exemplo n.º 1
0
 def get_random_rule(self, min_in, max_in, min_out, max_out):
     # pick random switch
     s = rand.randint(self.minswitch, self.minswitch + self.switchcount - 1)
     ins = randomsubset(self.in_portnums(s), min_in, max_in)
     outs = randomsubset(self.out_portnums(s), min_out, max_out)
     
     rule = simple_port_forward_rule(ins, outs)
     
     return rule
Exemplo n.º 2
0
    def rules(self):
        ''' 
        For every clients, we add rules that cause packets with that client's 
        address to be sent in the correct direction between the switches until 
        they arrive at the destination
        '''
        for (s, c) in self.client_iter():
            headerstring = self.client_to_bitstring_address((s,c))
            for sw in self.switch_iter():
                in_ports = list(self.in_portnums(sw)) # rules will always be for all of our inports
                if s == sw: # packet is destined for a client that sw is already directly connected to
                    out_ports = [self.port_to_portnum[(sw, (s,c))]] # the port connecting s to (s,c)
                else: # need to send it to another switch
                    # we need to decide whether we go clockwise or counterclockwise
                    # if we need to do less than half the circumference of the ring clockwise, go clockwise
                    if (s - sw) % self.switchcount < self.switchcount/2: dir = 1 
                    else: dir = -1
                    out_ports = [self.port_to_portnum[(sw, (sw + dir) % self.switchcount)]]

                rule = simple_port_forward_rule(in_ports, out_ports, wildcard=headerstring)
                if out_ports[0] in in_ports:
                    print ((s,c), sw), out_ports
                yield rule
        

        if self.ADD_FLAW:
            s,sw,c = 1,2,1
            in_ports = list(self.in_portnums(sw)) # rules will always be for all of our inports
            if s == sw: # packet is destined for a client that sw is already directly connected to
                out_ports = [self.port_to_portnum[(sw, (s,c))]] # the port connecting s to (s,c)
            else: # need to send it to another switch
                # we need to decide whether we go clockwise or counterclockwise
                # if we need to do less than half the circumference of the ring clockwise, go clockwise
                if (s - sw) % self.switchcount < self.switchcount/2: dir = -1 
                else: dir = 1
                out_ports = [self.port_to_portnum[(sw, (sw + dir) % self.switchcount)]]

            rule = simple_port_forward_rule(in_ports, out_ports, wildcard=headerstring)
            if out_ports[0] in in_ports:
                print ((s,c), sw), out_ports
            yield rule