|
54 | 54 | import com.cloud.vm.VirtualMachine; |
55 | 55 | import com.cloud.vm.VirtualMachineProfile; |
56 | 56 | import com.cloud.vm.dao.NicDao; |
57 | | -import com.midokura.midonet.client.MidonetApi; |
58 | | -import com.midokura.midonet.client.dto.DtoRule; |
59 | | -import com.midokura.midonet.client.resource.Bridge; |
60 | | -import com.midokura.midonet.client.resource.BridgePort; |
61 | | -import com.midokura.midonet.client.resource.DhcpHost; |
62 | | -import com.midokura.midonet.client.resource.DhcpSubnet; |
63 | | -import com.midokura.midonet.client.resource.Port; |
64 | | -import com.midokura.midonet.client.resource.ResourceCollection; |
65 | | -import com.midokura.midonet.client.resource.Route; |
66 | | -import com.midokura.midonet.client.resource.Router; |
67 | | -import com.midokura.midonet.client.resource.RouterPort; |
68 | | -import com.midokura.midonet.client.resource.Rule; |
69 | | -import com.midokura.midonet.client.resource.RuleChain; |
| 57 | +import org.midonet.client.MidonetApi; |
| 58 | +import org.midonet.client.exception.HttpInternalServerError; |
| 59 | +import org.midonet.client.dto.DtoRule; |
| 60 | +import org.midonet.client.dto.DtoRule.DtoRange; |
| 61 | +import org.midonet.client.resource.Bridge; |
| 62 | +import org.midonet.client.resource.BridgePort; |
| 63 | +import org.midonet.client.resource.DhcpHost; |
| 64 | +import org.midonet.client.resource.DhcpSubnet; |
| 65 | +import org.midonet.client.resource.Port; |
| 66 | +import org.midonet.client.resource.ResourceCollection; |
| 67 | +import org.midonet.client.resource.Route; |
| 68 | +import org.midonet.client.resource.Router; |
| 69 | +import org.midonet.client.resource.RouterPort; |
| 70 | +import org.midonet.client.resource.Rule; |
| 71 | +import org.midonet.client.resource.RuleChain; |
70 | 72 | import com.sun.jersey.core.util.MultivaluedMapImpl; |
71 | 73 | import org.apache.log4j.Logger; |
72 | 74 | import org.springframework.stereotype.Component; |
@@ -420,7 +422,9 @@ public boolean addDhcpEntry(Network network, NicProfile nic, VirtualMachineProfi |
420 | 422 | sub.subnetLength(cidrInfo.second()); |
421 | 423 | sub.subnetPrefix(cidrInfo.first()); |
422 | 424 | sub.defaultGateway(network.getGateway()); |
423 | | - sub.dnsServerAddr(dest.getDataCenter().getDns1()); |
| 425 | + List<String> dcs = new ArrayList<String>(); |
| 426 | + dcs.add(dest.getDataCenter().getDns1()); |
| 427 | + sub.dnsServerAddrs(dcs); |
424 | 428 |
|
425 | 429 | sub.create(); |
426 | 430 | } |
@@ -529,10 +533,8 @@ private void addMidonetStaticNAT(RuleChain preFilter, RuleChain preNat, RuleChai |
529 | 533 | .nwDstAddress(floatingIp) |
530 | 534 | .nwDstLength(32) |
531 | 535 | .nwProto(SimpleFirewallRule.stringToProtocolNumber("icmp")) |
532 | | - .tpSrcStart(0) |
533 | | - .tpSrcEnd(0) |
534 | | - .tpDstStart(0) |
535 | | - .tpDstEnd(0) |
| 536 | + .tpSrc(new DtoRange<Integer>(0,0)) |
| 537 | + .tpDst(new DtoRange<Integer>(0,0)) |
536 | 538 | .position(2) |
537 | 539 | .create(); |
538 | 540 |
|
@@ -685,52 +687,56 @@ public boolean applyFWRules(Network config, List<? extends FirewallRule> rulesTo |
685 | 687 | } |
686 | 688 |
|
687 | 689 | for (FirewallRule rule : rulesToApply) { |
688 | | - IpAddress dstIp = _networkModel.getIp(rule.getSourceIpAddressId()); |
689 | | - FirewallRuleTO ruleTO = new FirewallRuleTO(rule, null, dstIp.getAddress().addr()); |
690 | | - |
691 | | - // Convert to string representation |
692 | | - SimpleFirewallRule fwRule = new SimpleFirewallRule(ruleTO); |
693 | | - String[] ruleStrings = fwRule.toStringArray(); |
694 | | - |
695 | | - if (rule.getState() == FirewallRule.State.Revoke) { |
696 | | - // Lookup in existingRules, delete if present |
697 | | - for(String revokeRuleString : ruleStrings){ |
698 | | - Rule foundRule = existingRules.get(revokeRuleString); |
699 | | - if(foundRule != null){ |
700 | | - foundRule.delete(); |
| 690 | + if (rule.getState() == FirewallRule.State.Revoke || rule.getState() == FirewallRule.State.Add) { |
| 691 | + IpAddress dstIp = _networkModel.getIp(rule.getSourceIpAddressId()); |
| 692 | + FirewallRuleTO ruleTO = new FirewallRuleTO(rule, null, dstIp.getAddress().addr()); |
| 693 | + |
| 694 | + // Convert to string representation |
| 695 | + SimpleFirewallRule fwRule = new SimpleFirewallRule(ruleTO); |
| 696 | + String[] ruleStrings = fwRule.toStringArray(); |
| 697 | + |
| 698 | + if (rule.getState() == FirewallRule.State.Revoke) { |
| 699 | + // Lookup in existingRules, delete if present |
| 700 | + for(String revokeRuleString : ruleStrings){ |
| 701 | + Rule foundRule = existingRules.get(revokeRuleString); |
| 702 | + if(foundRule != null){ |
| 703 | + foundRule.delete(); |
| 704 | + } |
701 | 705 | } |
702 | | - } |
703 | | - } else if (rule.getState() == FirewallRule.State.Add) { |
704 | | - // Lookup in existingRules, add if not present |
705 | | - for(int i = 0; i < ruleStrings.length; i++){ |
706 | | - String ruleString = ruleStrings[i]; |
707 | | - Rule foundRule = existingRules.get(ruleString); |
708 | | - if(foundRule == null){ |
709 | | - // Get the cidr for the related entry in the Source Cidrs list |
710 | | - String relatedCidr = fwRule.sourceCidrs.get(i); |
711 | | - Pair<String,Integer> cidrParts = NetUtils.getCidr(relatedCidr); |
712 | | - |
713 | | - // Create rule with correct proto, cidr, ACCEPT, dst IP |
714 | | - Rule toApply = preFilter.addRule() |
715 | | - .type(DtoRule.Jump) |
716 | | - .jumpChainId(preNat.getId()) |
717 | | - .position(1) |
718 | | - .nwSrcAddress(cidrParts.first()) |
719 | | - .nwSrcLength(cidrParts.second()) |
720 | | - .nwDstAddress(ruleTO.getSrcIp()) |
721 | | - .nwDstLength(32) |
722 | | - .nwProto(SimpleFirewallRule.stringToProtocolNumber(rule.getProtocol())); |
| 706 | + } else if (rule.getState() == FirewallRule.State.Add) { |
| 707 | + // Lookup in existingRules, add if not present |
| 708 | + for(int i = 0; i < ruleStrings.length; i++){ |
| 709 | + String ruleString = ruleStrings[i]; |
| 710 | + Rule foundRule = existingRules.get(ruleString); |
| 711 | + if(foundRule == null){ |
| 712 | + // Get the cidr for the related entry in the Source Cidrs list |
| 713 | + String relatedCidr = fwRule.sourceCidrs.get(i); |
| 714 | + Pair<String,Integer> cidrParts = NetUtils.getCidr(relatedCidr); |
| 715 | + |
| 716 | + // Create rule with correct proto, cidr, ACCEPT, dst IP |
| 717 | + Rule toApply = preFilter.addRule() |
| 718 | + .type(DtoRule.Jump) |
| 719 | + .jumpChainId(preNat.getId()) |
| 720 | + .position(1) |
| 721 | + .nwSrcAddress(cidrParts.first()) |
| 722 | + .nwSrcLength(cidrParts.second()) |
| 723 | + .nwDstAddress(ruleTO.getSrcIp()) |
| 724 | + .nwDstLength(32) |
| 725 | + .nwProto(SimpleFirewallRule.stringToProtocolNumber(rule.getProtocol())); |
| 726 | + |
| 727 | + if(rule.getProtocol().equals("icmp")){ |
| 728 | + // ICMP rules - reuse port fields |
| 729 | + // (-1, -1) means "allow all ICMP", so we don't set tpSrc / tpDst |
| 730 | + if(fwRule.icmpType != -1 | fwRule.icmpCode != -1){ |
| 731 | + toApply.tpSrc(new DtoRange(fwRule.icmpType, fwRule.icmpType)) |
| 732 | + .tpDst(new DtoRange(fwRule.icmpCode, fwRule.icmpCode)); |
| 733 | + } |
| 734 | + } else { |
| 735 | + toApply.tpDst(new DtoRange(fwRule.dstPortStart, fwRule.dstPortEnd)); |
| 736 | + } |
723 | 737 |
|
724 | | - if(rule.getProtocol().equals("icmp")){ |
725 | | - // ICMP rules - reuse port fields |
726 | | - toApply.tpSrcStart(fwRule.icmpType).tpSrcEnd(fwRule.icmpType) |
727 | | - .tpDstStart(fwRule.icmpCode).tpDstEnd(fwRule.icmpCode); |
728 | | - } else { |
729 | | - toApply.tpDstStart(fwRule.dstPortStart) |
730 | | - .tpDstEnd(fwRule.dstPortEnd); |
| 738 | + toApply.create(); |
731 | 739 | } |
732 | | - |
733 | | - toApply.create(); |
734 | 740 | } |
735 | 741 | } |
736 | 742 | } |
@@ -969,8 +975,11 @@ public boolean applyPFRules(Network network, List<PortForwardingRule> rules) |
969 | 975 | // Rules in the preNat table |
970 | 976 | Map<String, Rule> existingPreNatRules = new HashMap<String, Rule>(); |
971 | 977 | for (Rule existingRule : preNat.getRules()) { |
972 | | - String ruleString = new SimpleFirewallRule(existingRule).toStringArray()[0]; |
973 | | - existingPreNatRules.put(ruleString, existingRule); |
| 978 | + // The "port forwarding" rules we're interested in are dnat rules where src / dst ports are specified |
| 979 | + if(existingRule.getType().equals(DtoRule.DNAT) && existingRule.getTpDst() != null){ |
| 980 | + String ruleString = new SimpleFirewallRule(existingRule).toStringArray()[0]; |
| 981 | + existingPreNatRules.put(ruleString, existingRule); |
| 982 | + } |
974 | 983 | } |
975 | 984 |
|
976 | 985 | /* |
@@ -1054,8 +1063,7 @@ public boolean applyPFRules(Network network, List<PortForwardingRule> rules) |
1054 | 1063 | .flowAction(DtoRule.Accept) |
1055 | 1064 | .nwDstAddress(publicIp) |
1056 | 1065 | .nwDstLength(32) |
1057 | | - .tpDstStart(pubPortStart) |
1058 | | - .tpDstEnd(pubPortEnd) |
| 1066 | + .tpDst(new DtoRange(pubPortStart, pubPortEnd)) |
1059 | 1067 | .natTargets(preTargets) |
1060 | 1068 | .nwProto(SimpleFirewallRule.stringToProtocolNumber(rule.getProtocol())) |
1061 | 1069 | .position(1); |
@@ -1119,7 +1127,8 @@ private static Map<Service, Map<Capability, String>> setCapabilities() { |
1119 | 1127 | capabilities.put(Service.Gateway, null); |
1120 | 1128 |
|
1121 | 1129 | // L3 Support : DHCP |
1122 | | - capabilities.put(Service.Dhcp, null); |
| 1130 | + Map<Capability, String> dhcpCapabilities = new HashMap<Capability, String>(); |
| 1131 | + capabilities.put(Service.Dhcp, dhcpCapabilities); |
1123 | 1132 |
|
1124 | 1133 | // L3 Support : SourceNat |
1125 | 1134 | Map<Capability, String> sourceNatCapabilities = new HashMap<Capability, String>(); |
@@ -1360,7 +1369,7 @@ protected RuleChain getOrInitEgressACLFilter(Network network) { |
1360 | 1369 | int pos = 1; |
1361 | 1370 | // If it is ARP, accept it |
1362 | 1371 | egressChain.addRule().type(DtoRule.Accept) |
1363 | | - .dlType((short)0x0806) |
| 1372 | + .dlType(0x0806) |
1364 | 1373 | .position(pos++) |
1365 | 1374 | .create(); |
1366 | 1375 |
|
@@ -1419,7 +1428,7 @@ private void connectBridgeToRouter(Network network, Bridge netBridge, Router net |
1419 | 1428 |
|
1420 | 1429 | // If it is ARP, accept it |
1421 | 1430 | inc.addRule().type(DtoRule.Accept) |
1422 | | - .dlType((short)0x0806) |
| 1431 | + .dlType(0x0806) |
1423 | 1432 | .position(pos++) |
1424 | 1433 | .create(); |
1425 | 1434 |
|
@@ -1483,7 +1492,13 @@ private Bridge getOrCreateNetworkBridge(long networkID, String accountUuid){ |
1483 | 1492 |
|
1484 | 1493 | String networkUUIDStr = String.valueOf(networkID); |
1485 | 1494 |
|
1486 | | - netBridge = api.addBridge().tenantId(accountUuid).name(networkUUIDStr).create(); |
| 1495 | + s_logger.debug("Attempting to create guest network bridge"); |
| 1496 | + try { |
| 1497 | + netBridge = api.addBridge().tenantId(accountUuid).name(networkUUIDStr).create(); |
| 1498 | + } catch (HttpInternalServerError ex) { |
| 1499 | + s_logger.warn("Bridge creation failed, retrying bridge get in case it now exists.", ex); |
| 1500 | + netBridge = getNetworkBridge(networkID, accountUuid); |
| 1501 | + } |
1487 | 1502 | } |
1488 | 1503 | return netBridge; |
1489 | 1504 | } |
|
0 commit comments