Ejemplo n.º 1
0
    def _add_chain_rule(self, chain, action, **kwargs):

        nw_proto = kwargs.get("nw_proto")
        src_addr = kwargs.pop("src_addr", None)
        dst_addr = kwargs.pop("dst_addr", None)
        src_port_from = kwargs.pop("src_port_from", None)
        src_port_to = kwargs.pop("src_port_to", None)
        dst_port_from = kwargs.pop("dst_port_from", None)
        dst_port_to = kwargs.pop("dst_port_to", None)

        # Convert to the keys and values that midonet client understands
        if src_addr:
            kwargs["nw_src_addr"], kwargs["nw_src_length"] = net_util.net_addr(
                src_addr)

        if dst_addr:
            kwargs["nw_dst_addr"], kwargs["nw_dst_length"] = net_util.net_addr(
                dst_addr)

        kwargs["tp_src"] = {"start": src_port_from, "end": src_port_to}

        kwargs["tp_dst"] = {"start": dst_port_from, "end": dst_port_to}

        if nw_proto == 1:  # ICMP
            # Overwrite port fields regardless of the direction
            kwargs["tp_src"] = {"start": src_port_from, "end": src_port_from}
            kwargs["tp_dst"] = {"start": dst_port_to, "end": dst_port_to}

        return self.client.add_chain_rule(chain, action=action, **kwargs)
Ejemplo n.º 2
0
    def _link_bridge_to_router(self, router, bridge_port_id, net_addr, net_len,
                               gw_ip, metadata_gw_ip):
        router_port = self.client.add_router_port(
            router, port_address=gw_ip, network_address=net_addr,
            network_length=net_len)
        self.client.link(router_port, bridge_port_id)
        self.client.add_router_route(router, type='Normal',
                                     src_network_addr='0.0.0.0',
                                     src_network_length=0,
                                     dst_network_addr=net_addr,
                                     dst_network_length=net_len,
                                     next_hop_port=router_port.get_id(),
                                     weight=100)

        if metadata_gw_ip:
            # Add a route for the metadata server.
            # Not all VM images supports DHCP option 121.  Add a route for the
            # Metadata server in the router to forward the packet to the bridge
            # that will send them to the Metadata Proxy.
            net_addr, net_len = net_util.net_addr(METADATA_DEFAULT_IP)
            self.client.add_router_route(
                router, type='Normal', src_network_addr=net_addr,
                src_network_length=net_len,
                dst_network_addr=net_addr,
                dst_network_length=32,
                next_hop_port=router_port.get_id(),
                next_hop_gateway=metadata_gw_ip)
Ejemplo n.º 3
0
    def _link_bridge_to_gw_router(self, bridge, gw_router, gw_ip, cidr):
        """Link a bridge to the gateway router

        :param bridge:  bridge
        :param gw_router: gateway router to link to
        :param gw_ip: IP address of gateway
        :param cidr: network CIDR
        """
        net_addr, net_len = net_util.net_addr(cidr)

        # create a port on the gateway router
        gw_port = self.client.add_router_port(gw_router, port_address=gw_ip,
                                              network_address=net_addr,
                                              network_length=net_len)

        # create a bridge port, then link it to the router.
        port = self.client.add_bridge_port(bridge)
        self.client.link(gw_port, port.get_id())

        # add a route for the subnet in the gateway router
        self.client.add_router_route(gw_router, type='Normal',
                                     src_network_addr='0.0.0.0',
                                     src_network_length=0,
                                     dst_network_addr=net_addr,
                                     dst_network_length=net_len,
                                     next_hop_port=gw_port.get_id(),
                                     weight=100)
Ejemplo n.º 4
0
    def add_router_interface(self, context, router_id, interface_info):
        """Handle router linking with network."""
        LOG.debug(
            _(
                "MidonetPluginV2.add_router_interface called: "
                "router_id=%(router_id)s "
                "interface_info=%(interface_info)r"
            ),
            {"router_id": router_id, "interface_info": interface_info},
        )

        with context.session.begin(subtransactions=True):
            info = super(MidonetPluginV2, self).add_router_interface(context, router_id, interface_info)

        try:
            subnet = self._get_subnet(context, info["subnet_id"])
            cidr = subnet["cidr"]
            net_addr, net_len = net_util.net_addr(cidr)
            router = self.client.get_router(router_id)

            # Get the metadata GW IP
            metadata_gw_ip = None
            rport_qry = context.session.query(models_v2.Port)
            dhcp_ports = rport_qry.filter_by(
                network_id=subnet["network_id"], device_owner=constants.DEVICE_OWNER_DHCP
            ).all()
            if dhcp_ports and dhcp_ports[0].fixed_ips:
                metadata_gw_ip = dhcp_ports[0].fixed_ips[0].ip_address
            else:
                LOG.warn(
                    _("DHCP agent is not working correctly. No port " "to reach the Metadata server on this network")
                )
            # Link the router and the bridge
            port = super(MidonetPluginV2, self).get_port(context, info["port_id"])
            self._link_bridge_to_router(router, port, net_addr, net_len, subnet["gateway_ip"], metadata_gw_ip)
        except Exception:
            LOG.error(
                _(
                    "Failed to create MidoNet resources to add router "
                    "interface. info=%(info)s, router_id=%(router_id)s"
                ),
                {"info": info, "router_id": router_id},
            )
            with excutils.save_and_reraise_exception():
                with context.session.begin(subtransactions=True):
                    self.remove_router_interface(context, router_id, info)

        LOG.debug(_("MidonetPluginV2.add_router_interface exiting: " "info=%r"), info)
        return info
Ejemplo n.º 5
0
    def create_dhcp(self, bridge, gateway_ip, cidr):
        """Create a new DHCP entry

        :param bridge: bridge object to add dhcp to
        :param gateway_ip: IP address of gateway
        :param cidr: subnet represented as x.x.x.x/y
        :returns: newly created dhcp
        """
        LOG.debug(
            _("MidoClient.create_dhcp called: bridge=%(bridge)s, " "cidr=%(cidr)s, gateway_ip=%(gateway_ip)s"),
            {"bridge": bridge, "cidr": cidr, "gateway_ip": gateway_ip},
        )
        net_addr, net_len = net_util.net_addr(cidr)
        return (
            bridge.add_dhcp_subnet().default_gateway(gateway_ip).subnet_prefix(net_addr).subnet_length(net_len).create()
        )
Ejemplo n.º 6
0
    def delete_dhcp(self, bridge, cidr):
        """Delete a DHCP entry

        :param bridge: bridge to remove DHCP from
        :param cidr: subnet represented as x.x.x.x/y
        """
        LOG.debug(
            "MidoClient.delete_dhcp called: bridge=%(bridge)s, " "cidr=%(cidr)s", {"bridge": bridge, "cidr": cidr}
        )
        dhcp_subnets = bridge.get_dhcp_subnets()
        net_addr, net_len = net_util.net_addr(cidr)
        if not dhcp_subnets:
            raise MidonetApiException(msg=_("Tried to delete non-existent DHCP"))
        for dhcp in dhcp_subnets:
            if dhcp.get_subnet_prefix() == net_addr and dhcp.get_subnet_length() == str(net_len):
                dhcp.delete()
                break
Ejemplo n.º 7
0
    def add_router_interface(self, context, router_id, interface_info):
        """Handle router linking with network."""
        LOG.debug(_("MidonetPluginV2.add_router_interface called: "
                    "router_id=%(router_id)s "
                    "interface_info=%(interface_info)r"),
                  {'router_id': router_id, 'interface_info': interface_info})

        with context.session.begin(subtransactions=True):
            info = super(MidonetPluginV2, self).add_router_interface(
                context, router_id, interface_info)

        try:
            subnet = self._get_subnet(context, info["subnet_id"])
            cidr = subnet["cidr"]
            net_addr, net_len = net_util.net_addr(cidr)
            router = self.client.get_router(router_id)

            # Get the metadatat GW IP
            metadata_gw_ip = None
            rport_qry = context.session.query(models_v2.Port)
            dhcp_ports = rport_qry.filter_by(
                network_id=subnet["network_id"],
                device_owner='network:dhcp').all()
            if dhcp_ports and dhcp_ports[0].fixed_ips:
                metadata_gw_ip = dhcp_ports[0].fixed_ips[0].ip_address
            else:
                LOG.warn(_("DHCP agent is not working correctly. No port "
                           "to reach the Metadata server on this network"))
            # Link the router and the bridge
            self._link_bridge_to_router(router, info["port_id"], net_addr,
                                        net_len, subnet["gateway_ip"],
                                        metadata_gw_ip)
        except Exception:
            LOG.error(_("Failed to create MidoNet resources to add router "
                        "interface. info=%(info)s, router_id=%(router_id)s"),
                      {"info": info, "router_id": router_id})
            with excutils.save_and_reraise_exception():
                with context.session.begin(subtransactions=True):
                    self.remove_router_interface(context, router_id, info)

        LOG.debug(_("MidonetPluginV2.add_router_interface exiting: "
                    "info=%r"), info)
        return info
Ejemplo n.º 8
0
    def create_dhcp(self, bridge, gateway_ip, cidr):
        """Create a new DHCP entry

        :param bridge: bridge object to add dhcp to
        :param gateway_ip: IP address of gateway
        :param cidr: subnet represented as x.x.x.x/y
        :returns: newly created dhcp
        """
        LOG.debug(
            _("MidoClient.create_dhcp called: bridge=%(bridge)s, "
              "cidr=%(cidr)s, gateway_ip=%(gateway_ip)s"), {
                  'bridge': bridge,
                  'cidr': cidr,
                  'gateway_ip': gateway_ip
              })
        net_addr, net_len = net_util.net_addr(cidr)
        return bridge.add_dhcp_subnet().default_gateway(
            gateway_ip).subnet_prefix(net_addr).subnet_length(
                net_len).create()
Ejemplo n.º 9
0
    def delete_dhcp(self, bridge, cidr):
        """Delete a DHCP entry

        :param bridge: bridge to remove DHCP from
        :param cidr: subnet represented as x.x.x.x/y
        """
        LOG.debug(
            _("MidoClient.delete_dhcp called: bridge=%(bridge)s, "
              "cidr=%(cidr)s"), {
                  'bridge': bridge,
                  'cidr': cidr
              })
        dhcp_subnets = bridge.get_dhcp_subnets()
        net_addr, net_len = net_util.net_addr(cidr)
        if not dhcp_subnets:
            raise MidonetApiException(
                msg=_("Tried to delete non-existent DHCP"))
        for dhcp in dhcp_subnets:
            if dhcp.get_subnet_prefix() == net_addr:
                dhcp.delete()
                break