예제 #1
0
    def install_rule(self, path, rule):
        act = [parser.OFPActionOutput(proto.OFPP_NORMAL, 0)]

        match = parser.OFPMatch(eth_type=ether_types.ETH_TYPE_IP,
                                ip_proto=1,
                                ipv4_src=rule[0],
                                ipv4_dst=rule[1])
        self.logger.debug("installing rule: %s", match)
        self.send_flow(path, 100, match, act)
예제 #2
0
def match(match_fields):
    """Return OpenFlow matches from dict.

    Args:
        match_fields (dict): match fields and values.
    Returns:
        ryu.ofproto.ofproto_v1_3_parser.OFPMatch: matches.
    """
    return parser.OFPMatch(**match_fields)
예제 #3
0
    def unknown_flow(self, ev):
        switch = ev.msg.datapath
        data = ev.msg.data
        parsed_data = packet.Packet(data)
        in_port = ev.msg.match['in_port']
        first_eth = parsed_data.get_protocols(eth.ethernet)[0]

        #Calling function to update arp and mac tables
        self.learn(switch, in_port, parsed_data)

        # Send where?
        if first_eth.dst in self.mac_tables[switch.id]:
            out_port = self.mac_tables[switch.id][first_eth.dst]
        else:
            out_port = of13.OFPP_FLOOD

        # Flow definition
        match = parser13.OFPMatch(in_port=in_port, eth_dst=first_eth.dst)
        actions = [parser13.OFPActionOutput(out_port)]
        instr = [
            parser13.OFPInstructionActions(of13.OFPIT_APPLY_ACTIONS, actions)
        ]

        # Do we use BID?
        if ev.msg.buffer_id == of13.OFP_NO_BUFFER:  # We got no buffer_id
            # ..so we send actual data
            self.send_packet_out(switch,
                                 out_port=out_port,
                                 data=data,
                                 actions=actions,
                                 in_port=in_port)
            if out_port != of13.OFPP_FLOOD:
                # ...and install a rule if it isn't a FLOOD
                self.send_new_flow(switch, match=match, instr=instr)
        else:  # We have buffer id
            if out_port != of13.OFPP_FLOOD:
                # ...and are not FLOODing
                # so we install a rule and send packet out in one line
                self.send_new_flow(switch,
                                   match=match,
                                   instr=instr,
                                   buffer_id=ev.msg.buffer_id)
            else:
                # or just send a packet out, without a rule
                self.send_packet_out(switch,
                                     out_port=out_port,
                                     buffer_id=ev.msg.buffer_id,
                                     actions=actions,
                                     in_port=in_port)

        #Printing flow stats for every packet in.
        print "New flow stats reply with SW.ID : current flows stats: \n", self.flows, "\n"

        #Printing name and path of complete flow stat file for every packet in
        new_file_t = self.flow_stat_file[-1]
        print "For complete flow stats replies please open Sam-" + str(
            new_file_t) + ".csv" + " in " + os.getcwd() + "/ \n\n\n"
예제 #4
0
 def tcp_udp_flows(self, vlan_id, nfvip, eth_type, nat_flows):
     return (
         # Learn from coprocessor port/do inbound translation.
         (self.FROM_COPRO_TABLE, parser.OFPMatch(eth_type=eth_type, vlan_vid=vlan_id),
          nat_flows(nfvip)),
         # Packets from coprocessor go to tuple inbound table.
         (self.INTF_TABLE, parser.OFPMatch(
             eth_type=eth_type, ip_proto=socket.IPPROTO_TCP, in_port=self.COPROPORT, vlan_vid=vlan_id),
          [parser.OFPInstructionGotoTable(self.FROM_COPRO_TABLE)]),
         (self.INTF_TABLE, parser.OFPMatch(
             eth_type=eth_type, ip_proto=socket.IPPROTO_UDP, in_port=self.COPROPORT, vlan_vid=vlan_id),
          [parser.OFPInstructionGotoTable(self.FROM_COPRO_TABLE)]),
         # Packets from fake interface go outbound table.
         (self.INTF_TABLE, parser.OFPMatch(
             eth_type=eth_type, ip_proto=socket.IPPROTO_TCP, in_port=self.FAKEPORT, vlan_vid=vlan_id),
          [parser.OFPInstructionGotoTable(self.TO_COPRO_TABLE)]),
         (self.INTF_TABLE, parser.OFPMatch(
             eth_type=eth_type, ip_proto=socket.IPPROTO_UDP, in_port=self.FAKEPORT, vlan_vid=vlan_id),
          [parser.OFPInstructionGotoTable(self.TO_COPRO_TABLE)]))
예제 #5
0
    def switch_features_handler(self, ev):
        datapath = ev.msg.datapath
        ofproto = datapath.ofproto
        parser = datapath.ofproto_parser

        # install the table-miss flow entry.
        match = parser.OFPMatch()
        actions = [parser.OFPActionOutput(ofproto.OFPP_CONTROLLER,
                                          ofproto.OFPCML_NO_BUFFER)]
        self.add_flow(datapath, 0, match, actions)
예제 #6
0
파일: pipette.py 프로젝트: cglewis/pipette
 def ipv6_flows(self, vlan_id, nfvip):
     return (
         (self.INTF_TABLE,
          parser.OFPMatch(eth_type=ether.ETH_TYPE_IPV6,
                          vlan_vid=vlan_id,
                          ip_proto=socket.IPPROTO_ICMPV6,
                          icmpv6_type=icmpv6.ND_NEIGHBOR_SOLICIT),
          self.apply_actions([parser.OFPActionOutput(
              ofp.OFPP_CONTROLLER)])), ) + self.tcp_udp_flows(
                  vlan_id, nfvip, ether.ETH_TYPE_IPV6, self.natv6_flows)
예제 #7
0
    def switch_features_handler(self, ev):
        dp = ev.msg.datapath

        ## init (per-data path) variables
        self.MAC_TO_PORT[dp.id] = {}
        self.pkt_count[dp.id] = 0

        ## Note: check example for syntax how-to.
        #self.example()

        ## some debug output
        print("")
        print("")
        print("/// Switch connected. ID: {}".format(dp.id))

        ## default "all to controller" flow
        match = parser.OFPMatch()
        action = [parser.OFPActionOutput(ofproto.OFPP_CONTROLLER)]
        self.program_flow(dp,
                          match,
                          action,
                          priority=0,
                          idle_timeout=0,
                          hard_timeout=0)

        ## Directly connect port 1 and 2 with proactive flow rules
        match = parser.OFPMatch(in_port=1)
        action = [parser.OFPActionOutput(2)]
        self.program_flow(dp,
                          match,
                          action,
                          priority=100,
                          idle_timeout=0,
                          hard_timeout=0)

        match = parser.OFPMatch(in_port=2)
        action = [parser.OFPActionOutput(1)]
        self.program_flow(dp,
                          match,
                          action,
                          priority=100,
                          idle_timeout=0,
                          hard_timeout=0)
예제 #8
0
	def switch_features_handler(self, event):

		""" switch sent his features, check if Beba supported """
		msg = event.msg
		datapath = msg.datapath

		LOG.info("Configuring switch %d..." % datapath.id)

		""" Set table 0 as stateful """
		req = bebaparser.OFPExpMsgConfigureStatefulTable(
				datapath=datapath,
				table_id=0,
				stateful=1)
		datapath.send_msg(req)

		""" Set lookup extractor = {eth_dst} """
		req = bebaparser.OFPExpMsgKeyExtract(datapath=datapath,
				command=bebaproto.OFPSC_EXP_SET_L_EXTRACTOR,
				fields=[ofproto.OXM_OF_ETH_SRC],
				table_id=0)
		datapath.send_msg(req)

		""" Set update extractor = {eth_dst}  """
		req = bebaparser.OFPExpMsgKeyExtract(datapath=datapath,
				command=bebaproto.OFPSC_EXP_SET_U_EXTRACTOR,
				fields=[ofproto.OXM_OF_ETH_SRC],
				table_id=0)
		datapath.send_msg(req)

		###########################################################################################

		""" Set HF[1]=PKT_LEN [byte]"""
		req = bebaparser.OFPExpMsgHeaderFieldExtract(
				datapath=datapath,
				table_id=0,
				extractor_id=1,
				field=bebaproto.OXM_EXP_PKT_LEN
			)
		datapath.send_msg(req)

		""" Update function: var( [count] , [value_to_be_varianced] , [avg_value] , [var_value]) = (IO1 , IN1 , IO2, IO3) has 4 inputs and 3 outputs
        	OUT1 = FDV[0] = count
       		OUT2 = FDV[1] = avg(IN1)*1000
       		OUT3 = FDV[2] = var(IN1)
       	"""
		match = ofparser.OFPMatch()
		actions = [ofparser.OFPActionOutput(ofproto.OFPP_FLOOD),
			bebaparser.OFPExpActionSetDataVariable(table_id=0, opcode=bebaproto.OPCODE_VAR, output_fd_id=0, operand_1_hf_id=1, operand_2_fd_id=1, operand_3_fd_id=2)]
		self.add_flow(datapath=datapath,
				table_id=0,
				priority=0,
				match=match,
				actions=actions)

		""" $ sudo watch --color -n1 dpctl tcp:127.0.0.1:6634 stats-state -c
예제 #9
0
    def test_reply_ttl_invalid_message_with_rate_limit(self):
        pkt = packet.Packet()
        pkt.add_protocol(ethernet.ethernet(dst='aa:bb:cc:dd:ee:ff'))
        pkt.add_protocol(ipv4.ipv4(proto=in_proto.IPPROTO_UDP))
        pkt.add_protocol(udp.udp())
        pkt.serialize()

        lswitch = l2.LogicalSwitch(
            id='lswitch1',
            topic='topic1',
            unique_key=9,
            version=1,
        )
        self.app.db_store.update(lswitch)

        lrouter = l3.LogicalRouter(
            id='lrouter1',
            topic='topic1',
            version=1,
            unique_key=22,
            ports=[
                l3.LogicalRouterPort(
                    id='lrouter1-port1',
                    unique_key=55,
                    topic='topic1',
                    mac='aa:bb:cc:dd:ee:ff',
                    network='10.0.0.1/24',
                    lswitch='lswitch1',
                ),
            ],
        )
        self.app.db_store.update(lrouter)

        event = ofp_event.EventOFPMsgBase(
            msg=ofproto_parser.OFPPacketIn(
                datapath=mock.Mock(),
                reason=self.app.ofproto.OFPR_INVALID_TTL,
                match=ofproto_parser.OFPMatch(
                    metadata=lswitch.unique_key,
                    reg5=lrouter.unique_key,
                ),
                data=pkt.data,
            )
        )

        with mock.patch("dragonflow.controller.common."
                        "icmp_error_generator.generate") as icmp_error:
            for _ in range(self.app.conf.router_ttl_invalid_max_rate * 2):
                self.app.packet_in_handler(event)

            self.assertEqual(self.app.conf.router_ttl_invalid_max_rate,
                             icmp_error.call_count)
            icmp_error.assert_called_with(icmp.ICMP_TIME_EXCEEDED,
                                          icmp.ICMP_TTL_EXPIRED_CODE,
                                          mock.ANY, "10.0.0.1", mock.ANY)
    def _switch_features_handler(self, ev):
        datapath = ev.msg.datapath
        self.datapaths[datapath.id] = datapath
        self.statistics.append({})

        # All packets with a destination address in the IP address range 22.0.0.0/8 are expected to arrive at host H2
        match = parser.OFPMatch(eth_type=ether_types.ETH_TYPE_IP,
                                ipv4_src=('11.0.0.0', '255.0.0.0'),
                                ipv4_dst=('22.0.0.0', '255.0.0.0'))
        actions = [parser.OFPActionOutput(2)]
        self.program_flow(datapath,
                          match,
                          actions,
                          priority=20,
                          hard_timeout=0,
                          idle_timeout=0)

        # All packets with a destination address in the IP address range 33.0.0.0/8 are expected to arrive at host H3
        match = parser.OFPMatch(eth_type=ether_types.ETH_TYPE_IP,
                                ipv4_src=('11.0.0.0', '255.0.0.0'),
                                ipv4_dst=('33.0.0.0', '255.0.0.0'))
        actions = [parser.OFPActionOutput(3)]
        self.program_flow(datapath,
                          match,
                          actions,
                          priority=20,
                          hard_timeout=0,
                          idle_timeout=0)

        # traffic destined to 44.0.0.0/8 should be forwarded in such a way that the overall traffic is evenly distributed among the two hosts H2 and H3
        # set group table
        self.send_group_mod(datapath)
        match = parser.OFPMatch(eth_type=ether_types.ETH_TYPE_IP,
                                ipv4_src=('11.0.0.0', '255.0.0.0'),
                                ipv4_dst=('44.0.0.0', '255.0.0.0'))
        actions = [parser.OFPActionGroup(group_id=1)]
        self.program_flow(datapath,
                          match,
                          actions,
                          priority=10,
                          hard_timeout=0,
                          idle_timeout=0)
예제 #11
0
    def _create_match(self, rule):
        """Create an OFPMatch instance based on the contents of an
        ACL_ENTRY.

        :param rule: The rule entry to create an OFPMatch instance from.
        :return: The OFPMatch instance.
        """
        match = ofp13_parser.OFPMatch()
        ip_version = self._return_ip_version(rule.ip_src, rule.ip_dst)
        # Match IP layer (layer 3)
        if ip_version == 4:
            # Match IPv4
            match.append_field(ofproto_v1_3.OXM_OF_ETH_TYPE,
                               ethernet.ether.ETH_TYPE_IP)
            if rule.ip_src != self._RULE_WILDCARD:
                match.append_field(ofproto_v1_3.OXM_OF_IPV4_SRC,
                                   int(IPAddress(rule.ip_src)))
            if rule.ip_dst != self._RULE_WILDCARD:
                match.append_field(ofproto_v1_3.OXM_OF_IPV4_DST,
                                   int(IPAddress(rule.ip_dst)))
        else:
            # Match IPv6
            match.append_field(ofproto_v1_3.OXM_OF_ETH_TYPE,
                               ethernet.ether.ETH_TYPE_IPV6)
            if rule.ip_src != self._RULE_WILDCARD:
                match.append_field(ofproto_v1_3.OXM_OF_IPV6_SRC,
                                   IPAddress(rule.ip_src).words)
            if rule.ip_dst != self._RULE_WILDCARD:
                match.append_field(ofproto_v1_3.OXM_OF_IPV6_DST,
                                   IPAddress(rule.ip_dst).words)

        # Match transport layer (layer 4)
        if rule.tp_proto != self._RULE_WILDCARD:
            if rule.tp_proto == self._RULE_TCP:
                # Match TCP
                match.append_field(ofproto_v1_3.OXM_OF_IP_PROTO,
                                   ipv4.inet.IPPROTO_TCP)  # covers IPv6
                if rule.port_src != self._RULE_WILDCARD:
                    match.append_field(ofproto_v1_3.OXM_OF_TCP_SRC,
                                       int(rule.port_src))
                if rule.port_dst != self._RULE_WILDCARD:
                    match.append_field(ofproto_v1_3.OXM_OF_TCP_DST,
                                       int(rule.port_dst))
            elif rule.tp_proto == self._RULE_UDP:
                # Match UDP
                match.append_field(ofproto_v1_3.OXM_OF_IP_PROTO,
                                   ipv4.inet.IPPROTO_UDP)  # covers IPv6
                if rule.port_src != self._RULE_WILDCARD:
                    match.append_field(ofproto_v1_3.OXM_OF_UDP_SRC,
                                       int(rule.port_src))
                if rule.port_dst != self._RULE_WILDCARD:
                    match.append_field(ofproto_v1_3.OXM_OF_UDP_DST,
                                       int(rule.port_dst))
        return match
예제 #12
0
 def send_midpoint_flows_for_path(self, in_path):
     """
     Gets list of link and then based on them it sends flows only to the switches in the midpoints.
     That is the switch in the middle of path not at the endpoints
     Note that it only takes care of nodes in the middle very well.
     :type in_path: list
     :param in_path: list of link objects which collectively is called a path.
     """
     u_dpids = self.find_unique_dpid_inlinklist(in_path)
     for temp_dpid in u_dpids:
         ports = self.find_ports_for_dpid(temp_dpid, in_path)
         if len(ports) == 2:
             match = ofproto_v1_3_parser.OFPMatch(in_port=ports[0])
             actions = [ofproto_v1_3_parser.OFPActionOutput(port=ports[1])]
             self.add_flow(self.get_dp_switch_with_id(temp_dpid), 1, match, actions)
             match = ofproto_v1_3_parser.OFPMatch(in_port=ports[1])
             actions = [ofproto_v1_3_parser.OFPActionOutput(port=ports[0])]
             self.add_flow(self.get_dp_switch_with_id(temp_dpid), 1, match, actions)
         elif len(ports) > 2:
             print("Need to be implemented.")
예제 #13
0
    def launch_countermeasures(self, datapath, src_ip):
        """ Deploy a block rule based on IP source addresses """
        match = parser.OFPMatch(eth_type=ether_types.ETH_TYPE_IP,
                                ipv4_src=src_ip)

        # An empty action list indicates a drop rule
        self.set_flow(datapath, match, [], priority=2)

        warn = "   WARNING! Traffic limit exceeded for IP {:s}!".format(src_ip)
        warn += " Dropping packets!"
        print(warn)
예제 #14
0
    def load_arp_icmp(self):
        """
        Enable ARP and ICMP protocol
        """
        match = ofparser.OFPMatch(eth_type=0x0806)
        actions = [ofparser.OFPActionOutput(ofp.OFPP_FLOOD)]
        self.add_flow(datapath=self.datapath,
                      table_id=0,
                      priority=100,
                      match=match,
                      actions=actions)

        # ICMP packets flooding - simple, TEMPORARY and dull solution.
        match = ofparser.OFPMatch(eth_type=0x0800, ip_proto=1)
        actions = [ofparser.OFPActionOutput(ofp.OFPP_FLOOD)]
        self.add_flow(datapath=self.datapath,
                      table_id=0,
                      priority=1,
                      match=match,
                      actions=actions)
예제 #15
0
 def test29(self, datapath):
     self.send_table_mod(datapath)
     self.send_key_lookup(datapath)
     self.send_key_update(datapath)
     act_type = osproto.OFPAT_EXP_SET_GLOBAL_STATE
     data = struct.pack('!I4x', act_type)
     a = ofparser.OFPActionExperimenterUnknown(experimenter=0XBEBABEBA,
                                               data=data)
     actions = [a]
     match = ofparser.OFPMatch(in_port=5, eth_type=0x800, ip_proto=1)
     self.add_flow(datapath, 100, match, actions)
예제 #16
0
    def install_rule(self, **kwargs):

        ofproto = self.datapath.ofproto

        match = parser.OFPMatch(**kwargs.get('match'))

        action = parser.OFPActionOutput(kwargs.get('action').get('output'))

        self.wrapper.set_flow(self.datapath,
                              match, [action],
                              priority=priority)
예제 #17
0
    def send_flows_for_path(self, in_link_path):
        """
        Gets list of back up link and then based on them it sends flows to the switch.
        Note that it takes care of nodes in the middle very well. But for the endpoints, it assumes that the
        host is connected to port 1.
        :param in_link_path: List of link objects (a path)
        """
        u_dpids = self.find_unique_dpid_inlinklist(in_link_path)
        visited_dpids = []
        for temp_dpid in u_dpids:
            ports = self.find_ports_for_dpid(temp_dpid, in_link_path)
            if len(ports) == 2:
                visited_dpids.append(temp_dpid)
                match = ofproto_v1_3_parser.OFPMatch(in_port=ports[0])
                actions = [ofproto_v1_3_parser.OFPActionOutput(port=ports[1])]
                self.add_flow(self.get_dp_switch_with_id(temp_dpid), 1, match,
                              actions)
                match = ofproto_v1_3_parser.OFPMatch(in_port=ports[1])
                actions = [ofproto_v1_3_parser.OFPActionOutput(port=ports[0])]
                self.add_flow(self.get_dp_switch_with_id(temp_dpid), 1, match,
                              actions)
            elif len(ports) > 2:
                visited_dpids.append(temp_dpid)
                print("Need to be implemented.")

        end_points = [x for x in u_dpids if x not in visited_dpids]
        if len(end_points) > 2:
            print(
                "There is something wrong. There is two endpoints for a link")

        for temp_dpid_endpoint in end_points:
            other_port = self.find_ports_for_dpid(temp_dpid_endpoint,
                                                  in_link_path)
            match = ofproto_v1_3_parser.OFPMatch(in_port=1)
            actions = [ofproto_v1_3_parser.OFPActionOutput(port=other_port[0])]
            self.add_flow(self.get_dp_switch_with_id(temp_dpid_endpoint), 1,
                          match, actions)
            match = ofproto_v1_3_parser.OFPMatch()
            actions = [ofproto_v1_3_parser.OFPActionOutput(port=1)]
            self.add_flow(self.get_dp_switch_with_id(temp_dpid_endpoint), 1,
                          match, actions)
예제 #18
0
 def _flow_stats_requester(self):
     while True:
         for dp in self.switchports.keys():
             switch = self.switches[dp]  #Getting datapath from dpid
             cookie = cookie_mask = 0
             match = parser13.OFPMatch()
             msg = parser13.OFPFlowStatsRequest(switch, 0, of13.OFPTT_ALL,
                                                of13.OFPP_ANY,
                                                of13.OFPG_ANY, cookie,
                                                cookie_mask, match)
             switch.send_msg(msg)
         hub.sleep(3)  # Sleeping for three seconds
예제 #19
0
    def _switch_features_handler(self, ev):
        # to make sure host H2 and H3 receive an equal share of the overall traffic
        # N2_address_range = IPNetwork("22.0.0.0/9")
        # N3_address_range = IPNetwork("22.128.0.0/9")
        datapath = ev.msg.datapath

        match = parser.OFPMatch( # from N1 to N2
            eth_type = ether_types.ETH_TYPE_IP,
            ipv4_src = ('11.0.0.0', '255.0.0.0'),
            ipv4_dst = ('22.0.0.0', '255.128.0.0')
        )
        actions = [parser.OFPActionOutput(2)]
        self.program_flow(datapath, match, actions, priority = 10, hard_timeout = 0, idle_timeout = 0)

        match = parser.OFPMatch( # from N1 to N3
            eth_type = ether_types.ETH_TYPE_IP,
            ipv4_src = ('11.0.0.0', '255.0.0.0'),
            ipv4_dst = ('22.128.0.0', '255.128.0.0')
        )
        actions = [parser.OFPActionOutput(3)]
        self.program_flow(datapath, match, actions, priority = 10, hard_timeout = 0, idle_timeout = 0)
예제 #20
0
    def install_stateless(self, datapath):
        ################################ TAB 0: FWD ###################################
        match = ofparser.OFPMatch(eth_type=0x800, in_port=HOST_PORT)
        self.add_flow(datapath=datapath, priority=0, table_id=0, match=match, actions=[])

        match = ofparser.OFPMatch(eth_type=0x800, ip_dscp=0)
        actions = [ofparser.OFPActionOutput(1 if datapath.id != 8 else 2)]
        self.add_flow(datapath=datapath, priority=100, table_id=0, match=match, actions=actions)

        # If: sw[i] is connected to the host -> deliver the packet 
        match = ofparser.OFPMatch(eth_type=0x800, ipv4_dst=server_dict[datapath.id])
        actions = [ofparser.OFPActionOutput(HOST_PORT)]
        self.add_flow(datapath=datapath, priority=10, table_id=0, match=match, actions=actions)

        # Else: forward it to the other ports
        for p in CORE_PORTS:
            # If ip_dst does not belong to the host directly connected to me,
            #   drop the packet because it is already been forwarded
            match = ofparser.OFPMatch(in_port=p, eth_type=0x800)
            actions = [ofparser.OFPActionOutput(1 if p == 2 else 2)]
            self.add_flow(datapath=datapath, priority=0, table_id=0, match=match, actions=actions)
예제 #21
0
 def _switch_features_handler(self, ev):
     dp = ev.msg.datapath
     # r: src, dst, sid, in_port, out_port
     rules = [r for r in ROUTING_TABLE if r[2] == dp.id]
     for r in rules:
         match = parser.OFPMatch(
             eth_type = ether_types.ETH_TYPE_IP,
             ipv4_src = r[0],
             ipv4_dst = r[1]
         )
         actions = [parser.OFPActionOutput(r[4])]
         self.program_flow(dp, match, actions, priority=10, hard_timeout=0, idle_timeout=0)
예제 #22
0
def match_from_dict(match_dict):
    kwargs = {}
    for of_match, field in match_dict.items():
        of_match = OLD_MATCH_FIELDS.get(of_match, of_match)
        test_config_condition(of_match not in MATCH_FIELDS, 'Unknown match field: %s' % of_match)
        try:
            encoded_field = MATCH_FIELDS[of_match](field)
        except TypeError:
            raise InvalidConfigError('%s cannot be type %s' % (of_match, type(field)))
        kwargs[of_match] = encoded_field

    return parser.OFPMatch(**kwargs)
예제 #23
0
    def packet_in(self, event):
        msg = event.msg
        path = msg.datapath
        inpt = msg.match['in_port']

        pckt = packet.Packet(msg.data)
        eth = pckt.get_protocols(ethernet.ethernet)[0]
        arpp = pckt.get_protocols(arp.arp)[0]

        acts = [parser.OFPActionOutput(inpt)]
        mtch = parser.OFPMatch(in_port=inpt,
                               ipv4_dst=arpp.src_ip,
                               ip_proto=0,
                               eth_type=ether_types.ETH_TYPE_IP)
        self.send_flow(path, 1, mtch, acts)

        dest = eth.dst
        sorc = eth.src

        dpid = path.id
        self.mac_tab.setdefault(dpid, {})
        self.mac_tab[dpid][sorc] = inpt

        outp = self.get_port(dest, dpid)
        acts = [parser.OFPActionOutput(outp)]

        if outp != proto.OFPP_FLOOD:
            mtch = parser.OFPMatch(in_port=outp, eth_dst=dest)
            self.send_flow(path, 1, mtch, acts)

        data = None
        if msg.buffer_id == proto.OFP_NO_BUFFER:
            data = msg.data

        out = parser.OFPPacketOut(datapath=path,
                                  buffer_id=msg.buffer_id,
                                  in_port=inpt,
                                  actions=acts,
                                  data=data)
        path.send_msg(out)
    def remove_auth_flows(self, src_ip):
        """ removes the flows that capture knock sequence 
                (identified with src_ip and priority) """
        match_ipv4 = ofproto_v1_3_parser.OFPMatch()
        match_ipv4.append_field(ofproto_v1_3.OXM_OF_ETH_TYPE,
                                ether_types.ETH_TYPE_IP)
        match_ipv4.append_field(ofproto_v1_3.OXM_OF_IPV4_SRC,
                                int(IPAddress(src_ip)))
        match_ipv4.append_field(ofproto_v1_3.OXM_OF_IPV4_DST,
                                int(IPAddress(self.server_ipv4_address)))

        for id in self.datapaths:
            delete_flow(self.datapaths[id], 3, match_ipv4)
예제 #25
0
    def clear_table(self, datapath, table_id):
        """
        Cleans all table values

        Parameters:
            - datapath  = datapath to use
            - table_id  = id of the table
        """
        empty_match = ofparser.OFPMatch()
        instructions = []
        flow_mod = self.remove_table_flows(datapath, table_id, empty_match,
                                           instructions)
        datapath.send_msg(flow_mod)
예제 #26
0
    def install_edges(self, datapath):
        # Static routes configuration
        if datapath.id == 5:
            match = ofparser.OFPMatch(in_port=HOST_PORT)
            actions = [ofparser.OFPActionOutput(2)]
            self.add_flow(datapath=datapath,
                          priority=0,
                          table_id=0,
                          match=match,
                          actions=actions)

            match = ofparser.OFPMatch(in_port=HOST_PORT,
                                      eth_type=0x800,
                                      ipv4_dst=addrs[0])
            actions = [ofparser.OFPActionOutput(1)]
            self.add_flow(datapath=datapath,
                          priority=10,
                          table_id=0,
                          match=match,
                          actions=actions)
        else:
            match = ofparser.OFPMatch(in_port=HOST_PORT)
            actions = [ofparser.OFPActionOutput(1)]
            self.add_flow(datapath=datapath,
                          priority=0,
                          table_id=0,
                          match=match,
                          actions=actions)

        if datapath.id == 1 or datapath.id == 3:
            match = ofparser.OFPMatch(in_port=HOST_PORT,
                                      eth_type=0x800,
                                      ipv4_dst=addrs[1])
            actions = [ofparser.OFPActionOutput(2)]
            self.add_flow(datapath=datapath,
                          priority=10,
                          table_id=0,
                          match=match,
                          actions=actions)
        elif datapath.id == 7:
            match = ofparser.OFPMatch(in_port=HOST_PORT,
                                      eth_type=0x800,
                                      ipv4_dst=addrs[0])
            actions = [ofparser.OFPActionOutput(2)]
            self.add_flow(datapath=datapath,
                          priority=10,
                          table_id=0,
                          match=match,
                          actions=actions)

        # Dumbly forward ALL
        for p in CORE_PORTS:
            match = ofparser.OFPMatch(in_port=p)
            actions = [ofparser.OFPActionOutput(1 if p == 2 else 2)]
            self.add_flow(datapath=datapath,
                          priority=5,
                          table_id=0,
                          match=match,
                          actions=actions)
예제 #27
0
    def switch_features_handler(self, event):
        """ Switche sent his features, check if Beba supported """
        msg = event.msg
        datapath = msg.datapath
        devices.append(datapath)

        LOG.info("Configuring switch %d..." % datapath.id)
        """ Set table 0 as stateful """
        req = bebaparser.OFPExpMsgConfigureStatefulTable(datapath=datapath,
                                                         table_id=0,
                                                         stateful=1)
        datapath.send_msg(req)
        """ Set lookup extractor = {eth_dst} """
        req = bebaparser.OFPExpMsgKeyExtract(
            datapath=datapath,
            command=bebaproto.OFPSC_EXP_SET_L_EXTRACTOR,
            fields=[ofp.OXM_OF_ETH_DST],
            table_id=0)
        datapath.send_msg(req)
        """ Set update extractor = {eth_src}  """
        req = bebaparser.OFPExpMsgKeyExtract(
            datapath=datapath,
            command=bebaproto.OFPSC_EXP_SET_U_EXTRACTOR,
            fields=[ofp.OXM_OF_ETH_SRC],
            table_id=0)
        datapath.send_msg(req)

        # for each input port, for each state
        for i in range(1, N + 1):
            for s in range(N + 1):
                match = ofparser.OFPMatch(in_port=i, state=s)
                if s == 0:
                    out_port = ofp.OFPP_FLOOD
                else:
                    out_port = s

                actions = [
                    bebaparser.OFPExpActionSetState(state=i,
                                                    table_id=0,
                                                    hard_timeout=10),
                    ofparser.OFPActionOutput(out_port)
                    #,ofparser.OFPActionOutput(ofp.OFPP_CONTROLLER,
                    #ofp.OFPCML_NO_BUFFER)
                ]

                # Triggers flow mod
                self.add_flow(datapath=datapath,
                              table_id=0,
                              priority=0,
                              match=match,
                              actions=actions)
 def add_tcp_table(self, datapath):
     ofproto = datapath.ofproto
     parser = datapath.ofproto_parser
     inst = [parser.OFPInstructionGotoTable(1)]
     match = parser.OFPMatch()
     mod = parser.OFPFlowMod(datapath=datapath,
                             table_id=0,
                             priority=0,
                             match=match,
                             idle_timeout=0,
                             hard_timeout=0,
                             instructions=inst)
     datapath.send_msg(mod)
     self.Flowcounter[datapath.id] += 1
예제 #29
0
    def test13(self, datapath):
        self.send_table_mod(datapath)
        (global_state, global_state_mask
         ) = osparser.masked_global_state_from_str("*1*1*1*1*0*0*1*1*1*1*1*1*")
        actions = [ofparser.OFPActionOutput(6, 0)]
        match = ofparser.OFPMatch(
            in_port=5,
            eth_type=0x800,
            ip_proto=1,
            global_state=osparser.masked_global_state_from_str(
                "*1*1*1*1*0*0*1*1*1*1*1*1*"))
        self.add_flow(datapath, 200, match, actions)

        actions = [
            osparser.OFPExpActionSetGlobalState(
                global_state=global_state, global_state_mask=global_state_mask)
        ]
        match = ofparser.OFPMatch(in_port=5, eth_type=0x800, ip_proto=1)
        self.add_flow(datapath, 100, match, actions)

        actions = [ofparser.OFPActionOutput(5, 0)]
        match = ofparser.OFPMatch(in_port=6, eth_type=0x800, ip_proto=1)
        self.add_flow(datapath, 200, match, actions)
예제 #30
0
    def install_forward(self, datapath):

        match = ofparser.OFPMatch()
        actions = [ofparser.OFPActionOutput(ofproto.OFPP_FLOOD)]
        inst = [
            ofparser.OFPInstructionActions(ofproto.OFPIT_APPLY_ACTIONS,
                                           actions)
        ]
        mod = ofparser.OFPFlowMod(datapath=datapath,
                                  table_id=0,
                                  priority=10,
                                  match=match,
                                  instructions=inst)
        datapath.send_msg(mod)