use of org.midonet.client.resource.BridgePort in project cloudstack by apache.
the class MidoNetElement method getOrCreatePublicBridgePorts.
private Port[] getOrCreatePublicBridgePorts(NicProfile nic, Bridge publicBridge, Router providerRouter) {
Port[] ports = new Port[2];
BridgePort bridgeUplink = null;
RouterPort providerDownlink = null;
// Check if the ports and connection already exist
for (Port peerPort : publicBridge.getPeerPorts()) {
if (peerPort != null && peerPort instanceof RouterPort) {
RouterPort checkPort = (RouterPort) peerPort;
// Check it's a port on the providerRouter with the right gateway address
if (checkPort.getDeviceId().compareTo(providerRouter.getId()) == 0 && checkPort.getPortAddress().equals(nic.getIPv4Gateway())) {
providerDownlink = checkPort;
bridgeUplink = (BridgePort) api.getPort(checkPort.getPeerId());
break;
}
}
}
// Create the ports and connection if they don't exist
if (providerDownlink == null) {
String cidr = NetUtils.ipAndNetMaskToCidr(nic.getIPv4Gateway(), nic.getIPv4Netmask());
String cidrSubnet = NetUtils.getCidrSubNet(cidr);
int cidrSize = (int) NetUtils.getCidrSize(NetUtils.cidr2Netmask(cidr));
String gateway = nic.getIPv4Gateway();
// Add interior port on router side, with network details
providerDownlink = providerRouter.addInteriorRouterPort().networkAddress(cidrSubnet).networkLength(cidrSize).portAddress(gateway).create();
bridgeUplink = publicBridge.addInteriorPort().create();
// Link them up
providerDownlink.link(bridgeUplink.getId()).update();
}
ports[0] = bridgeUplink;
ports[1] = providerDownlink;
return ports;
}
use of org.midonet.client.resource.BridgePort in project cloudstack by apache.
the class MidoNetElement method connectBridgeToRouter.
private void connectBridgeToRouter(Network network, Bridge netBridge, Router netRouter) {
boolean isVpc = getIsVpc(network);
long id = getRouterId(network, isVpc);
String routerName = getRouterName(isVpc, id);
String accountIdStr = getAccountUuid(network);
// Add interior port on bridge side
BridgePort bridgePort = netBridge.addInteriorPort().create();
// Add interior port on router side, with network details
RouterPort routerPort = netRouter.addInteriorRouterPort();
String cidr = network.getCidr();
String cidrSubnet = NetUtils.getCidrSubNet(cidr);
int cidrSize = (int) NetUtils.getCidrSize(NetUtils.cidr2Netmask(cidr));
routerPort.networkAddress(cidrSubnet);
routerPort.networkLength(cidrSize);
routerPort.portAddress(network.getGateway());
// implemented via chains on the router port to that network.
if (getIsVpc(network)) {
// Create ACL filter chain for traffic coming INTO the network
// (outbound from the port
int pos = 1;
RuleChain inc = api.addChain().name(getChainName(String.valueOf(network.getId()), routerName, RuleChainCode.ACL_INGRESS)).tenantId(accountIdStr).create();
// If it is ARP, accept it
inc.addRule().type(DtoRule.Accept).dlType(0x0806).position(pos++).create();
// If it is ICMP to the router, accept that
inc.addRule().type(DtoRule.Accept).nwProto(SimpleFirewallRule.stringToProtocolNumber("icmp")).nwDstAddress(network.getGateway()).nwDstLength(32).position(pos++).create();
// If it is connection tracked, accept that as well
inc.addRule().type(DtoRule.Accept).matchReturnFlow(true).position(pos++).create();
inc.addRule().type(DtoRule.Drop).position(pos).create();
//
RuleChain out = api.addChain().name(getChainName(String.valueOf(network.getId()), routerName, RuleChainCode.ACL_EGRESS)).tenantId(accountIdStr).create();
// Creating the first default rule here that does nothing
// but start connection tracking.
out.addRule().type(DtoRule.Accept).matchForwardFlow(true).position(1).create();
routerPort.outboundFilterId(inc.getId());
routerPort.inboundFilterId(out.getId());
}
routerPort.create();
// Link them up
bridgePort.link(routerPort.getId()).update();
// Set up default route from router to subnet
netRouter.addRoute().type("Normal").weight(100).srcNetworkAddr("0.0.0.0").srcNetworkLength(0).dstNetworkAddr(cidrSubnet).dstNetworkLength(cidrSize).nextHopPort(routerPort.getId()).nextHopGateway(null).create();
}
use of org.midonet.client.resource.BridgePort in project cloudstack by apache.
the class MidoNetVifDriver method plug.
@Override
public LibvirtVMDef.InterfaceDef plug(NicTO nic, String guestOsType, String nicAdapter) throws InternalErrorException, LibvirtException {
if (s_logger.isDebugEnabled()) {
s_logger.debug("nic=" + nic);
}
LibvirtVMDef.InterfaceDef intf = new LibvirtVMDef.InterfaceDef();
String trafficLabel = nic.getName();
if (nic.getBroadcastType() == Networks.BroadcastDomainType.Mido && (nic.getType() == Networks.TrafficType.Guest || nic.getType() == Networks.TrafficType.Public)) {
/*
* create the tap.
*/
String tapName = addTap();
/*
* grab the tenant id and the network id from the Broadcast URI.
* We need to pluck the values out of the String. The string
* should look like "mido://[tenant_id].[bridge_name]"
*/
MultivaluedMap qNet = new MultivaluedMapImpl();
String nicAuthority = nic.getBroadcastUri().getAuthority();
String tenantId = nicAuthority.split("\\.")[0];
qNet.add("tenant_id", tenantId);
String url = nicAuthority.split("\\.")[1];
String netName = url.split(":")[0];
MidonetApi api = new MidonetApi(_midoApiLocation);
api.enableLogging();
for (Bridge b : api.getBridges(qNet)) {
if (b.getName().equals(netName)) {
for (BridgePort p : b.getPorts()) {
UUID pvif = p.getVifId();
if (pvif != null && p.getVifId().toString().equals(nic.getUuid())) {
getMyHost(api).addHostInterfacePort().interfaceName(tapName).portId(p.getId()).create();
break;
}
}
}
}
intf.defEthernet(tapName, nic.getMac(), getGuestNicModel(guestOsType, nicAdapter), "");
} else {
throw new InternalErrorException("Only NICs of BroadcastDomain type Mido are supported by the MidoNetVifDriver");
}
return intf;
}
Aggregations