Beispiel #1
0
    def add_table_miss_flow(self,
                            datapath,
                            priority,
                            match,
                            actions,
                            buffer_id=None):
        parser = datapath.ofproto_parser
        match = parser.OFPMatch()
        ofp_helper.add_flow_goto_next(datapath, 0, self.goto_table_priority,
                                      match)

        ofproto = datapath.ofproto
        parser = datapath.ofproto_parser
        inst = [
            parser.OFPInstructionActions(ofproto.OFPIT_APPLY_ACTIONS, actions)
        ]
        if buffer_id:
            mod = parser.OFPFlowMod(datapath=datapath,
                                    buffer_id=buffer_id,
                                    priority=priority,
                                    match=match,
                                    table_id=1,
                                    instructions=inst)
        else:
            mod = parser.OFPFlowMod(datapath=datapath,
                                    priority=priority,
                                    table_id=1,
                                    match=match,
                                    instructions=inst)
        datapath.send_msg(mod)
Beispiel #2
0
 def add_passby_flow(self, apply_table_id):
     switch_list = get_switch(self.topology_api_app, None)
     for switch in switch_list:
         datapath = switch.dp
         parser = datapath.ofproto_parser
         match = parser.OFPMatch()
         ofp_helper.add_flow_goto_next(datapath, apply_table_id,
                                       self.service_control_priority, match)
Beispiel #3
0
    def init_table_miss(self, datapath):
        switch_list = get_switch(self.topology_api_app, None)
        parser = datapath.ofproto_parser
        match = parser.OFPMatch()

        if self.count == 1:
            pass
        else:
            for i in range(0, self.count - 1):
                ofp_helper.add_flow_goto_next(datapath, i,
                                              self.goto_table_priority, match)
Beispiel #4
0
 def init_table_miss(self, datapath):
     switch_list = get_switch(self.topology_api_app, None)
     parser = datapath.ofproto_parser
     match = parser.OFPMatch()
     experiment_action = [parser.OFPActionOutput(4)]
     # outside - inside (Table 0)
     # ofp_helper.add_flow_with_next(datapath, table_id=0,
     #                               priority=self.goto_table_priority, match=match,
     #                               actions=experiment_action, idle_timeout=0)
     ofp_helper.add_flow_goto_next(datapath, 0, self.goto_table_priority,
                                   match)
     ofp_helper.add_flow_goto_next(datapath, 1, self.goto_table_priority,
                                   match)
     # ofp_helper.add_flow_with_next(datapath, table_id=1,
     #                               priority=self.goto_table_priority, match=match,
     #                               actions=experiment_action, idle_timeout=0)
     ofp_helper.add_flow_goto_next(datapath, 2, self.goto_table_priority,
                                   match)
     # ofp_helper.add_flow_goto_next(datapath, 3, self.goto_table_priority, match)
     ofp_helper.add_flow_with_next(
         datapath,
         table_id=service_config.service_sequence['mirror'],
         priority=self.goto_table_priority,
         match=match,
         actions=experiment_action,
         idle_timeout=0)
     ofp_helper.add_flow_goto_next(datapath, 4, self.goto_table_priority,
                                   match)
Beispiel #5
0
    def _private_to_public(self,
                           datapath,
                           buffer_id,
                           data,
                           in_port,
                           out_port,
                           pkt_ip,
                           pkt_ethernet,
                           pkt_tcp=None,
                           pkt_udp=None,
                           pkt_icmp=None):
        if pkt_ip is None:
            return

        parser = datapath.ofproto_parser
        ofproto = datapath.ofproto

        eth_dst = pkt_ethernet.dst
        eth_src = pkt_ethernet.src
        ipv4_src = pkt_ip.src
        ipv4_dst = pkt_ip.dst

        nat_port = self._get_available_port()

        if (self._is_public(ipv4_dst)
                and not self._in_public_ip_subnetwork(ipv4_dst)):
            target_ip = self.public_gateway
        elif self._in_public_ip_subnetwork(ipv4_dst):
            target_ip = ipv4_dst
        elif self._in_private_subnetwork(ipv4_dst):
            return

        if pkt_tcp:
            # Install TCP Flow Entry
            tcp_src = pkt_tcp.src_port
            tcp_dst = pkt_tcp.dst_port
            # egress
            match = parser.OFPMatch(in_port=in_port,
                                    eth_type=ether.ETH_TYPE_IP,
                                    ip_proto=inet.IPPROTO_TCP,
                                    ipv4_src=ipv4_src,
                                    ipv4_dst=ipv4_dst,
                                    tcp_src=tcp_src,
                                    tcp_dst=tcp_dst)
            actions = [
                parser.OFPActionSetField(eth_dst=IP_TO_MAC_TABLE[target_ip]),
                parser.OFPActionSetField(ipv4_src=self.public_ip),
                parser.OFPActionSetField(tcp_src=nat_port)
            ]
            forward_actions = [parser.OFPActionOutput(out_port)]

            # ingress
            match_back = parser.OFPMatch(eth_type=ether.ETH_TYPE_IP,
                                         ip_proto=inet.IPPROTO_TCP,
                                         ipv4_src=ipv4_dst,
                                         ipv4_dst=self.public_ip,
                                         tcp_src=tcp_dst,
                                         tcp_dst=nat_port)

            actions_back = [
                parser.OFPActionSetField(eth_dst=eth_src),
                parser.OFPActionSetField(ipv4_dst=ipv4_src),
                parser.OFPActionSetField(tcp_dst=tcp_src)
            ]
            forward_match_back = parser.OFPMatch(eth_type=ether.ETH_TYPE_IP,
                                                 ip_proto=inet.IPPROTO_TCP,
                                                 ipv4_src=ipv4_dst,
                                                 ipv4_dst=ipv4_src,
                                                 tcp_src=tcp_dst,
                                                 tcp_dst=tcp_src)
            forward_actions_back = [parser.OFPActionOutput(in_port)]

        elif pkt_udp:
            # Install UDP Flow Entry
            udp_src = pkt_udp.src_port
            udp_dst = pkt_udp.dst_port

            # egress, inside-to-outside
            match = parser.OFPMatch(in_port=in_port,
                                    eth_type=ether.ETH_TYPE_IP,
                                    ip_proto=inet.IPPROTO_UDP,
                                    ipv4_src=ipv4_src,
                                    ipv4_dst=ipv4_dst,
                                    udp_src=udp_src,
                                    udp_dst=udp_dst)
            actions = [
                parser.OFPActionSetField(eth_dst=IP_TO_MAC_TABLE[target_ip]),
                parser.OFPActionSetField(ipv4_src=self.public_ip),
                parser.OFPActionSetField(udp_src=nat_port)
            ]
            forward_actions = [parser.OFPActionOutput(out_port)]

            # ingress, outside-to-inside
            match_back = parser.OFPMatch(eth_type=ether.ETH_TYPE_IP,
                                         ip_proto=inet.IPPROTO_UDP,
                                         ipv4_src=ipv4_dst,
                                         ipv4_dst=self.public_ip,
                                         udp_src=udp_dst,
                                         udp_dst=nat_port)
            actions_back = [
                parser.OFPActionSetField(eth_dst=eth_src),
                parser.OFPActionSetField(ipv4_dst=ipv4_src),
                parser.OFPActionSetField(udp_dst=udp_src)
            ]

            forward_match_back = parser.OFPMatch(eth_type=ether.ETH_TYPE_IP,
                                                 ip_proto=inet.IPPROTO_UDP,
                                                 ipv4_src=ipv4_dst,
                                                 ipv4_dst=ipv4_src,
                                                 udp_src=udp_dst,
                                                 udp_dst=udp_src)
            forward_actions_back = [parser.OFPActionOutput(in_port)]
        else:
            pass
        # outside - inside set-filed (Table 0)
        ofp_helper.add_flow_with_next(datapath,
                                      table_id=self.ingress_table_id,
                                      priority=self.service_priority,
                                      match=match_back,
                                      actions=actions_back,
                                      idle_timeout=self.IDLE_TIME)
        # inside - outside go-to-next (Table 0)
        ofp_helper.add_flow_goto_next(datapath,
                                      table_id=self.ingress_table_id,
                                      priority=self.service_priority,
                                      match=match,
                                      idle_timeout=self.IDLE_TIME)
        # outside - inside out-port (Table 3)
        ofp_helper.add_flow(datapath,
                            table_id=self.forward_table_id,
                            priority=self.service_priority,
                            match=forward_match_back,
                            actions=forward_actions_back,
                            idle_timeout=self.IDLE_TIME)
        # inside - outside write-out-port(Table 3)
        ofp_helper.add_write_flow_with_next(datapath,
                                            table_id=self.forward_table_id,
                                            priority=self.service_priority,
                                            match=match,
                                            actions=forward_actions,
                                            idle_timeout=self.IDLE_TIME)
        # inside - outside set-field( Table 4)
        ofp_helper.add_flow(datapath,
                            table_id=self.egress_table_id,
                            priority=self.service_priority,
                            match=match,
                            actions=actions,
                            idle_timeout=self.IDLE_TIME)

        # send first packet back to switch
        d = None
        if buffer_id == ofproto.OFP_NO_BUFFER:
            d = data
        out = parser.OFPPacketOut(datapath=datapath,
                                  buffer_id=buffer_id,
                                  in_port=in_port,
                                  actions=actions,
                                  data=d)
        datapath.send_msg(out)