Hyunsun Moon | 9f0814b | 2015-11-04 17:34:35 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.onosproject.cordvtn; |
| 17 | |
Hyunsun Moon | c71231d | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 18 | import com.google.common.collect.Lists; |
| 19 | import com.google.common.collect.Maps; |
| 20 | import org.onlab.packet.Ethernet; |
Hyunsun Moon | 4a91515 | 2016-01-14 16:56:26 -0800 | [diff] [blame] | 21 | import org.onlab.packet.IPv4; |
Hyunsun Moon | 9f0814b | 2015-11-04 17:34:35 -0800 | [diff] [blame] | 22 | import org.onlab.packet.Ip4Address; |
Hyunsun Moon | c71231d | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 23 | import org.onlab.packet.Ip4Prefix; |
| 24 | import org.onlab.packet.IpAddress; |
| 25 | import org.onlab.packet.IpPrefix; |
| 26 | import org.onlab.packet.MacAddress; |
Hyunsun Moon | 4a91515 | 2016-01-14 16:56:26 -0800 | [diff] [blame] | 27 | import org.onlab.packet.TpPort; |
Hyunsun Moon | 6d24734 | 2016-02-12 12:48:47 -0800 | [diff] [blame] | 28 | import org.onlab.packet.VlanId; |
Hyunsun Moon | 9f0814b | 2015-11-04 17:34:35 -0800 | [diff] [blame] | 29 | import org.onlab.util.ItemNotFoundException; |
| 30 | import org.onosproject.core.ApplicationId; |
Hyunsun Moon | c71231d | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 31 | import org.onosproject.core.DefaultGroupId; |
| 32 | import org.onosproject.core.GroupId; |
| 33 | import org.onosproject.mastership.MastershipService; |
| 34 | import org.onosproject.net.Device; |
Hyunsun Moon | 9f0814b | 2015-11-04 17:34:35 -0800 | [diff] [blame] | 35 | import org.onosproject.net.DeviceId; |
Hyunsun Moon | c71231d | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 36 | import org.onosproject.net.Host; |
Hyunsun Moon | 5a4346f | 2016-01-15 11:41:14 -0800 | [diff] [blame] | 37 | import org.onosproject.net.Port; |
Hyunsun Moon | c71231d | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 38 | import org.onosproject.net.PortNumber; |
Hyunsun Moon | 9f0814b | 2015-11-04 17:34:35 -0800 | [diff] [blame] | 39 | import org.onosproject.net.behaviour.ExtensionTreatmentResolver; |
Hyunsun Moon | c71231d | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 40 | import org.onosproject.net.device.DeviceService; |
Hyunsun Moon | 9f0814b | 2015-11-04 17:34:35 -0800 | [diff] [blame] | 41 | import org.onosproject.net.driver.DefaultDriverData; |
| 42 | import org.onosproject.net.driver.DefaultDriverHandler; |
| 43 | import org.onosproject.net.driver.Driver; |
| 44 | import org.onosproject.net.driver.DriverHandler; |
| 45 | import org.onosproject.net.driver.DriverService; |
Hyunsun Moon | c71231d | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 46 | import org.onosproject.net.flow.DefaultFlowRule; |
Hyunsun Moon | 9f0814b | 2015-11-04 17:34:35 -0800 | [diff] [blame] | 47 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 48 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
Hyunsun Moon | c71231d | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 49 | import org.onosproject.net.flow.FlowRule; |
| 50 | import org.onosproject.net.flow.FlowRuleOperations; |
| 51 | import org.onosproject.net.flow.FlowRuleOperationsContext; |
| 52 | import org.onosproject.net.flow.FlowRuleService; |
Hyunsun Moon | 9f0814b | 2015-11-04 17:34:35 -0800 | [diff] [blame] | 53 | import org.onosproject.net.flow.TrafficSelector; |
| 54 | import org.onosproject.net.flow.TrafficTreatment; |
Hyunsun Moon | c71231d | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 55 | import org.onosproject.net.flow.criteria.Criterion; |
| 56 | import org.onosproject.net.flow.criteria.EthCriterion; |
| 57 | import org.onosproject.net.flow.criteria.IPCriterion; |
| 58 | import org.onosproject.net.flow.criteria.PortCriterion; |
Hyunsun Moon | 9f0814b | 2015-11-04 17:34:35 -0800 | [diff] [blame] | 59 | import org.onosproject.net.flow.instructions.ExtensionPropertyException; |
| 60 | import org.onosproject.net.flow.instructions.ExtensionTreatment; |
Hyunsun Moon | c71231d | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 61 | import org.onosproject.net.flow.instructions.Instruction; |
| 62 | import org.onosproject.net.flow.instructions.Instructions; |
Hyunsun Moon | 6d24734 | 2016-02-12 12:48:47 -0800 | [diff] [blame] | 63 | import org.onosproject.net.flow.instructions.L2ModificationInstruction; |
Hyunsun Moon | c71231d | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 64 | import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModEtherInstruction; |
| 65 | import org.onosproject.net.group.DefaultGroupBucket; |
| 66 | import org.onosproject.net.group.DefaultGroupDescription; |
| 67 | import org.onosproject.net.group.DefaultGroupKey; |
| 68 | import org.onosproject.net.group.Group; |
| 69 | import org.onosproject.net.group.GroupBucket; |
| 70 | import org.onosproject.net.group.GroupBuckets; |
| 71 | import org.onosproject.net.group.GroupDescription; |
| 72 | import org.onosproject.net.group.GroupKey; |
| 73 | import org.onosproject.net.group.GroupService; |
| 74 | import org.onosproject.openstackswitching.OpenstackNetwork; |
| 75 | import org.onosproject.openstackswitching.OpenstackSubnet; |
Hyunsun Moon | 9f0814b | 2015-11-04 17:34:35 -0800 | [diff] [blame] | 76 | import org.slf4j.Logger; |
| 77 | |
Hyunsun Moon | 4161e6f | 2016-01-07 01:32:31 -0800 | [diff] [blame] | 78 | import java.util.ArrayList; |
Hyunsun Moon | 9f0814b | 2015-11-04 17:34:35 -0800 | [diff] [blame] | 79 | import java.util.List; |
Hyunsun Moon | c71231d | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 80 | import java.util.Map; |
Hyunsun Moon | c71231d | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 81 | import java.util.Objects; |
| 82 | import java.util.Set; |
| 83 | import java.util.stream.Collectors; |
Hyunsun Moon | 9f0814b | 2015-11-04 17:34:35 -0800 | [diff] [blame] | 84 | |
Hyunsun Moon | 9f0814b | 2015-11-04 17:34:35 -0800 | [diff] [blame] | 85 | import static com.google.common.base.Preconditions.checkNotNull; |
Hyunsun Moon | c71231d | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 86 | import static org.onosproject.net.Device.Type.SWITCH; |
| 87 | import static org.onosproject.net.flow.criteria.Criterion.Type.IN_PORT; |
| 88 | import static org.onosproject.net.flow.criteria.Criterion.Type.IPV4_DST; |
| 89 | import static org.onosproject.net.flow.criteria.Criterion.Type.IPV4_SRC; |
Hyunsun Moon | 9f0814b | 2015-11-04 17:34:35 -0800 | [diff] [blame] | 90 | import static org.onosproject.net.flow.instructions.ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_SET_TUNNEL_DST; |
Hyunsun Moon | c71231d | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 91 | import static org.onosproject.net.flow.instructions.L2ModificationInstruction.L2SubType.ETH_DST; |
Hyunsun Moon | 6d24734 | 2016-02-12 12:48:47 -0800 | [diff] [blame] | 92 | import static org.onosproject.net.flow.instructions.L2ModificationInstruction.L2SubType.VLAN_PUSH; |
Hyunsun Moon | 9f0814b | 2015-11-04 17:34:35 -0800 | [diff] [blame] | 93 | import static org.slf4j.LoggerFactory.getLogger; |
| 94 | |
| 95 | /** |
Hyunsun Moon | c71231d | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 96 | * Populates rules for CORD VTN service. |
Hyunsun Moon | 9f0814b | 2015-11-04 17:34:35 -0800 | [diff] [blame] | 97 | */ |
Hyunsun Moon | c71231d | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 98 | public class CordVtnRuleInstaller { |
| 99 | |
Hyunsun Moon | 9f0814b | 2015-11-04 17:34:35 -0800 | [diff] [blame] | 100 | protected final Logger log = getLogger(getClass()); |
| 101 | |
Hyunsun Moon | 4a91515 | 2016-01-14 16:56:26 -0800 | [diff] [blame] | 102 | private static final int TABLE_FIRST = 0; |
| 103 | private static final int TABLE_IN_PORT = 1; |
| 104 | private static final int TABLE_ACCESS_TYPE = 2; |
| 105 | private static final int TABLE_IN_SERVICE = 3; |
| 106 | private static final int TABLE_DST_IP = 4; |
| 107 | private static final int TABLE_TUNNEL_IN = 5; |
Hyunsun Moon | 6d24734 | 2016-02-12 12:48:47 -0800 | [diff] [blame] | 108 | private static final int TABLE_Q_IN_Q = 6; |
Hyunsun Moon | c71231d | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 109 | |
Hyunsun Moon | d52bffc | 2016-01-29 18:57:05 -0800 | [diff] [blame] | 110 | private static final int MANAGEMENT_PRIORITY = 55000; |
Hyunsun Moon | 6d24734 | 2016-02-12 12:48:47 -0800 | [diff] [blame] | 111 | private static final int VSG_PRIORITY = 55000; |
Hyunsun Moon | d52bffc | 2016-01-29 18:57:05 -0800 | [diff] [blame] | 112 | private static final int HIGH_PRIORITY = 50000; |
Hyunsun Moon | 9f0814b | 2015-11-04 17:34:35 -0800 | [diff] [blame] | 113 | private static final int DEFAULT_PRIORITY = 5000; |
Hyunsun Moon | d52bffc | 2016-01-29 18:57:05 -0800 | [diff] [blame] | 114 | private static final int LOW_PRIORITY = 4000; |
Hyunsun Moon | c71231d | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 115 | private static final int LOWEST_PRIORITY = 0; |
Hyunsun Moon | 4a91515 | 2016-01-14 16:56:26 -0800 | [diff] [blame] | 116 | |
| 117 | private static final int VXLAN_UDP_PORT = 4789; |
Hyunsun Moon | 6d24734 | 2016-02-12 12:48:47 -0800 | [diff] [blame] | 118 | private static final VlanId VLAN_WAN = VlanId.vlanId((short) 500); |
| 119 | |
| 120 | private static final String PORT_NAME = "portName"; |
| 121 | private static final String DATA_PLANE_INTF = "dataPlaneIntf"; |
| 122 | private static final String S_TAG = "stag"; |
Hyunsun Moon | 9f0814b | 2015-11-04 17:34:35 -0800 | [diff] [blame] | 123 | |
| 124 | private final ApplicationId appId; |
Hyunsun Moon | c71231d | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 125 | private final FlowRuleService flowRuleService; |
| 126 | private final DeviceService deviceService; |
Hyunsun Moon | 9f0814b | 2015-11-04 17:34:35 -0800 | [diff] [blame] | 127 | private final DriverService driverService; |
Hyunsun Moon | c71231d | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 128 | private final GroupService groupService; |
| 129 | private final MastershipService mastershipService; |
Hyunsun Moon | 9f0814b | 2015-11-04 17:34:35 -0800 | [diff] [blame] | 130 | private final String tunnelType; |
| 131 | |
| 132 | /** |
Hyunsun Moon | c71231d | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 133 | * Creates a new rule populator. |
Hyunsun Moon | 9f0814b | 2015-11-04 17:34:35 -0800 | [diff] [blame] | 134 | * |
| 135 | * @param appId application id |
Hyunsun Moon | c71231d | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 136 | * @param flowRuleService flow rule service |
| 137 | * @param deviceService device service |
Hyunsun Moon | 9f0814b | 2015-11-04 17:34:35 -0800 | [diff] [blame] | 138 | * @param driverService driver service |
Jian Li | dfba739 | 2016-01-22 16:46:58 -0800 | [diff] [blame] | 139 | * @param groupService group service |
| 140 | * @param mastershipService mastership service |
Hyunsun Moon | 9f0814b | 2015-11-04 17:34:35 -0800 | [diff] [blame] | 141 | * @param tunnelType tunnel type |
| 142 | */ |
| 143 | public CordVtnRuleInstaller(ApplicationId appId, |
Hyunsun Moon | c71231d | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 144 | FlowRuleService flowRuleService, |
| 145 | DeviceService deviceService, |
Hyunsun Moon | 9f0814b | 2015-11-04 17:34:35 -0800 | [diff] [blame] | 146 | DriverService driverService, |
Hyunsun Moon | c71231d | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 147 | GroupService groupService, |
| 148 | MastershipService mastershipService, |
Hyunsun Moon | 9f0814b | 2015-11-04 17:34:35 -0800 | [diff] [blame] | 149 | String tunnelType) { |
| 150 | this.appId = appId; |
Hyunsun Moon | c71231d | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 151 | this.flowRuleService = flowRuleService; |
| 152 | this.deviceService = deviceService; |
Hyunsun Moon | 9f0814b | 2015-11-04 17:34:35 -0800 | [diff] [blame] | 153 | this.driverService = driverService; |
Hyunsun Moon | c71231d | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 154 | this.groupService = groupService; |
| 155 | this.mastershipService = mastershipService; |
Hyunsun Moon | 9f0814b | 2015-11-04 17:34:35 -0800 | [diff] [blame] | 156 | this.tunnelType = checkNotNull(tunnelType); |
| 157 | } |
| 158 | |
| 159 | /** |
Hyunsun Moon | c71231d | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 160 | * Installs table miss rule to a give device. |
Hyunsun Moon | 9f0814b | 2015-11-04 17:34:35 -0800 | [diff] [blame] | 161 | * |
Hyunsun Moon | c71231d | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 162 | * @param deviceId device id to install the rules |
Hyunsun Moon | 133fd79 | 2016-02-09 01:55:48 -0800 | [diff] [blame] | 163 | * @param dpIntf data plane interface name |
| 164 | * @param dpIp data plane ip address |
Hyunsun Moon | 9f0814b | 2015-11-04 17:34:35 -0800 | [diff] [blame] | 165 | */ |
Hyunsun Moon | 133fd79 | 2016-02-09 01:55:48 -0800 | [diff] [blame] | 166 | public void init(DeviceId deviceId, String dpIntf, IpAddress dpIp) { |
Hyunsun Moon | c71231d | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 167 | // default is drop packets which can be accomplished without |
| 168 | // a table miss entry for all table. |
Hyunsun Moon | 4a91515 | 2016-01-14 16:56:26 -0800 | [diff] [blame] | 169 | PortNumber tunnelPort = getTunnelPort(deviceId); |
Hyunsun Moon | 133fd79 | 2016-02-09 01:55:48 -0800 | [diff] [blame] | 170 | PortNumber dpPort = getDpPort(deviceId, dpIntf); |
Hyunsun Moon | 4a91515 | 2016-01-14 16:56:26 -0800 | [diff] [blame] | 171 | |
Hyunsun Moon | 133fd79 | 2016-02-09 01:55:48 -0800 | [diff] [blame] | 172 | processFirstTable(deviceId, dpPort, dpIp); |
| 173 | processInPortTable(deviceId, tunnelPort, dpPort); |
| 174 | processAccessTypeTable(deviceId, dpPort); |
Hyunsun Moon | 6d24734 | 2016-02-12 12:48:47 -0800 | [diff] [blame] | 175 | processQInQTable(deviceId, dpPort); |
Hyunsun Moon | 9f0814b | 2015-11-04 17:34:35 -0800 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | /** |
Hyunsun Moon | c71231d | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 179 | * Populates basic rules that connect a VM to the other VMs in the system. |
Hyunsun Moon | 9f0814b | 2015-11-04 17:34:35 -0800 | [diff] [blame] | 180 | * |
Hyunsun Moon | c71231d | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 181 | * @param host host |
Hyunsun Moon | c71231d | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 182 | * @param tunnelIp tunnel ip |
| 183 | * @param vNet openstack network |
Hyunsun Moon | 9f0814b | 2015-11-04 17:34:35 -0800 | [diff] [blame] | 184 | */ |
Hyunsun Moon | b77b60f | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 185 | public void populateBasicConnectionRules(Host host, IpAddress tunnelIp, OpenstackNetwork vNet) { |
Hyunsun Moon | c71231d | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 186 | checkNotNull(host); |
| 187 | checkNotNull(vNet); |
Hyunsun Moon | 9f0814b | 2015-11-04 17:34:35 -0800 | [diff] [blame] | 188 | |
Hyunsun Moon | c71231d | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 189 | DeviceId deviceId = host.location().deviceId(); |
| 190 | if (!mastershipService.isLocalMaster(deviceId)) { |
| 191 | return; |
Hyunsun Moon | 9f0814b | 2015-11-04 17:34:35 -0800 | [diff] [blame] | 192 | } |
| 193 | |
Hyunsun Moon | c71231d | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 194 | PortNumber inPort = host.location().port(); |
| 195 | MacAddress dstMac = host.mac(); |
Hyunsun Moon | b77b60f | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 196 | IpAddress hostIp = host.ipAddresses().stream().findFirst().get(); |
Hyunsun Moon | c71231d | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 197 | long tunnelId = Long.parseLong(vNet.segmentId()); |
Hyunsun Moon | 9f0814b | 2015-11-04 17:34:35 -0800 | [diff] [blame] | 198 | |
Hyunsun Moon | c71231d | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 199 | OpenstackSubnet subnet = vNet.subnets().stream() |
| 200 | .findFirst() |
| 201 | .orElse(null); |
| 202 | |
| 203 | if (subnet == null) { |
| 204 | log.error("Failed to get subnet for {}", host.id()); |
| 205 | return; |
| 206 | } |
| 207 | |
| 208 | populateLocalInPortRule(deviceId, inPort, hostIp); |
| 209 | populateDirectAccessRule(Ip4Prefix.valueOf(subnet.cidr()), Ip4Prefix.valueOf(subnet.cidr())); |
Hyunsun Moon | 81ab23b | 2016-02-01 23:00:56 -0800 | [diff] [blame] | 210 | populateServiceIsolationRule(Ip4Prefix.valueOf(subnet.cidr())); |
Hyunsun Moon | c71231d | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 211 | populateDstIpRule(deviceId, inPort, dstMac, hostIp, tunnelId, tunnelIp); |
| 212 | populateTunnelInRule(deviceId, inPort, dstMac, tunnelId); |
Hyunsun Moon | 9f0814b | 2015-11-04 17:34:35 -0800 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | /** |
Hyunsun Moon | d5f6c3b | 2016-02-18 15:37:22 -0800 | [diff] [blame] | 216 | * Removes all rules related to a given service VM host. |
Hyunsun Moon | c71231d | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 217 | * |
| 218 | * @param host host to be removed |
| 219 | */ |
| 220 | public void removeBasicConnectionRules(Host host) { |
| 221 | checkNotNull(host); |
| 222 | |
| 223 | DeviceId deviceId = host.location().deviceId(); |
| 224 | MacAddress mac = host.mac(); |
| 225 | PortNumber port = host.location().port(); |
| 226 | IpAddress ip = host.ipAddresses().stream().findFirst().orElse(null); |
| 227 | |
| 228 | if (!mastershipService.isLocalMaster(deviceId)) { |
| 229 | return; |
| 230 | } |
| 231 | |
| 232 | for (FlowRule flowRule : flowRuleService.getFlowRulesById(appId)) { |
| 233 | if (flowRule.deviceId().equals(deviceId)) { |
| 234 | PortNumber inPort = getInPort(flowRule); |
| 235 | if (inPort != null && inPort.equals(port)) { |
| 236 | processFlowRule(false, flowRule); |
| 237 | continue; |
| 238 | } |
Hyunsun Moon | d5f6c3b | 2016-02-18 15:37:22 -0800 | [diff] [blame] | 239 | |
| 240 | PortNumber output = getOutputFromTreatment(flowRule); |
| 241 | if (output != null && output.equals(host.location().port())) { |
| 242 | processFlowRule(false, flowRule); |
| 243 | } |
Hyunsun Moon | c71231d | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | MacAddress dstMac = getDstMacFromTreatment(flowRule); |
| 247 | if (dstMac != null && dstMac.equals(mac)) { |
| 248 | processFlowRule(false, flowRule); |
| 249 | continue; |
| 250 | } |
| 251 | |
| 252 | dstMac = getDstMacFromSelector(flowRule); |
| 253 | if (dstMac != null && dstMac.equals(mac)) { |
| 254 | processFlowRule(false, flowRule); |
| 255 | continue; |
| 256 | } |
| 257 | |
| 258 | IpPrefix dstIp = getDstIpFromSelector(flowRule); |
| 259 | if (dstIp != null && dstIp.equals(ip.toIpPrefix())) { |
| 260 | processFlowRule(false, flowRule); |
| 261 | } |
Hyunsun Moon | c71231d | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | // TODO uninstall same network access rule in access table if no vm exists in the network |
| 265 | } |
| 266 | |
| 267 | /** |
Hyunsun Moon | 4161e6f | 2016-01-07 01:32:31 -0800 | [diff] [blame] | 268 | * Populates service dependency rules. |
| 269 | * |
| 270 | * @param tService tenant cord service |
| 271 | * @param pService provider cord service |
Hyunsun Moon | 640f183e | 2016-02-10 17:02:37 -0800 | [diff] [blame] | 272 | * @param isBidirectional true to enable bidirectional connection between two services |
Hyunsun Moon | 4161e6f | 2016-01-07 01:32:31 -0800 | [diff] [blame] | 273 | */ |
Hyunsun Moon | 640f183e | 2016-02-10 17:02:37 -0800 | [diff] [blame] | 274 | public void populateServiceDependencyRules(CordService tService, CordService pService, |
| 275 | boolean isBidirectional) { |
Hyunsun Moon | 4161e6f | 2016-01-07 01:32:31 -0800 | [diff] [blame] | 276 | checkNotNull(tService); |
| 277 | checkNotNull(pService); |
| 278 | |
| 279 | Ip4Prefix srcRange = tService.serviceIpRange().getIp4Prefix(); |
| 280 | Ip4Prefix dstRange = pService.serviceIpRange().getIp4Prefix(); |
| 281 | Ip4Address serviceIp = pService.serviceIp().getIp4Address(); |
| 282 | |
| 283 | Map<DeviceId, GroupId> outGroups = Maps.newHashMap(); |
| 284 | Map<DeviceId, Set<PortNumber>> inPorts = Maps.newHashMap(); |
| 285 | |
| 286 | for (Device device : deviceService.getAvailableDevices(SWITCH)) { |
| 287 | GroupId groupId = createServiceGroup(device.id(), pService); |
| 288 | outGroups.put(device.id(), groupId); |
| 289 | |
| 290 | Set<PortNumber> vms = tService.hosts().keySet() |
| 291 | .stream() |
| 292 | .filter(host -> host.location().deviceId().equals(device.id())) |
| 293 | .map(host -> host.location().port()) |
| 294 | .collect(Collectors.toSet()); |
| 295 | inPorts.put(device.id(), vms); |
| 296 | } |
| 297 | |
| 298 | populateIndirectAccessRule(srcRange, serviceIp, outGroups); |
| 299 | populateDirectAccessRule(srcRange, dstRange); |
Hyunsun Moon | 640f183e | 2016-02-10 17:02:37 -0800 | [diff] [blame] | 300 | if (isBidirectional) { |
| 301 | populateDirectAccessRule(dstRange, srcRange); |
| 302 | } |
Hyunsun Moon | 4161e6f | 2016-01-07 01:32:31 -0800 | [diff] [blame] | 303 | populateInServiceRule(inPorts, outGroups); |
| 304 | } |
| 305 | |
| 306 | /** |
Hyunsun Moon | c71231d | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 307 | * Removes service dependency rules. |
| 308 | * |
| 309 | * @param tService tenant cord service |
| 310 | * @param pService provider cord service |
| 311 | */ |
| 312 | public void removeServiceDependencyRules(CordService tService, CordService pService) { |
| 313 | checkNotNull(tService); |
| 314 | checkNotNull(pService); |
| 315 | |
| 316 | Ip4Prefix srcRange = tService.serviceIpRange().getIp4Prefix(); |
| 317 | Ip4Prefix dstRange = pService.serviceIpRange().getIp4Prefix(); |
| 318 | IpPrefix serviceIp = pService.serviceIp().toIpPrefix(); |
| 319 | |
| 320 | Map<DeviceId, GroupId> outGroups = Maps.newHashMap(); |
| 321 | GroupKey groupKey = new DefaultGroupKey(pService.id().id().getBytes()); |
| 322 | |
| 323 | deviceService.getAvailableDevices(SWITCH).forEach(device -> { |
| 324 | Group group = groupService.getGroup(device.id(), groupKey); |
| 325 | if (group != null) { |
| 326 | outGroups.put(device.id(), group.id()); |
| 327 | } |
| 328 | }); |
| 329 | |
| 330 | for (FlowRule flowRule : flowRuleService.getFlowRulesById(appId)) { |
| 331 | IpPrefix dstIp = getDstIpFromSelector(flowRule); |
| 332 | IpPrefix srcIp = getSrcIpFromSelector(flowRule); |
| 333 | |
| 334 | if (dstIp != null && dstIp.equals(serviceIp)) { |
| 335 | processFlowRule(false, flowRule); |
| 336 | continue; |
| 337 | } |
| 338 | |
| 339 | if (dstIp != null && srcIp != null) { |
| 340 | if (dstIp.equals(dstRange) && srcIp.equals(srcRange)) { |
| 341 | processFlowRule(false, flowRule); |
| 342 | continue; |
| 343 | } |
| 344 | |
| 345 | if (dstIp.equals(srcRange) && srcIp.equals(dstRange)) { |
| 346 | processFlowRule(false, flowRule); |
| 347 | continue; |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | GroupId groupId = getGroupIdFromTreatment(flowRule); |
| 352 | if (groupId != null && groupId.equals(outGroups.get(flowRule.deviceId()))) { |
| 353 | processFlowRule(false, flowRule); |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | // TODO remove the group if it is not in use |
| 358 | } |
| 359 | |
| 360 | /** |
Hyunsun Moon | 4161e6f | 2016-01-07 01:32:31 -0800 | [diff] [blame] | 361 | * Updates group buckets for a given service to all devices. |
| 362 | * |
| 363 | * @param service cord service |
| 364 | */ |
| 365 | public void updateServiceGroup(CordService service) { |
| 366 | checkNotNull(service); |
| 367 | |
| 368 | GroupKey groupKey = getGroupKey(service.id()); |
| 369 | |
| 370 | for (Device device : deviceService.getAvailableDevices(SWITCH)) { |
| 371 | DeviceId deviceId = device.id(); |
| 372 | if (!mastershipService.isLocalMaster(deviceId)) { |
| 373 | continue; |
| 374 | } |
| 375 | |
| 376 | Group group = groupService.getGroup(deviceId, groupKey); |
| 377 | if (group == null) { |
Hyunsun Moon | b77b60f | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 378 | log.trace("No group exists for service {} in {}, do nothing.", service.id(), deviceId); |
Hyunsun Moon | 4161e6f | 2016-01-07 01:32:31 -0800 | [diff] [blame] | 379 | continue; |
| 380 | } |
| 381 | |
| 382 | List<GroupBucket> oldBuckets = group.buckets().buckets(); |
| 383 | List<GroupBucket> newBuckets = getServiceGroupBuckets( |
| 384 | deviceId, service.segmentationId(), service.hosts()).buckets(); |
| 385 | |
| 386 | if (oldBuckets.equals(newBuckets)) { |
| 387 | continue; |
| 388 | } |
| 389 | |
| 390 | List<GroupBucket> bucketsToRemove = new ArrayList<>(oldBuckets); |
| 391 | bucketsToRemove.removeAll(newBuckets); |
| 392 | if (!bucketsToRemove.isEmpty()) { |
| 393 | groupService.removeBucketsFromGroup( |
| 394 | deviceId, |
| 395 | groupKey, |
| 396 | new GroupBuckets(bucketsToRemove), |
| 397 | groupKey, appId); |
| 398 | } |
| 399 | |
| 400 | List<GroupBucket> bucketsToAdd = new ArrayList<>(newBuckets); |
| 401 | bucketsToAdd.removeAll(oldBuckets); |
| 402 | if (!bucketsToAdd.isEmpty()) { |
| 403 | groupService.addBucketsToGroup( |
| 404 | deviceId, |
| 405 | groupKey, |
| 406 | new GroupBuckets(bucketsToAdd), |
| 407 | groupKey, appId); |
| 408 | } |
| 409 | } |
| 410 | } |
| 411 | |
| 412 | /** |
Hyunsun Moon | d52bffc | 2016-01-29 18:57:05 -0800 | [diff] [blame] | 413 | * Populates flow rules for management network access. |
| 414 | * |
| 415 | * @param host host which has management network interface |
| 416 | * @param mService management network service |
| 417 | */ |
| 418 | public void populateManagementNetworkRules(Host host, CordService mService) { |
| 419 | checkNotNull(mService); |
| 420 | |
| 421 | DeviceId deviceId = host.location().deviceId(); |
| 422 | IpAddress hostIp = host.ipAddresses().stream().findFirst().get(); |
| 423 | |
Hyunsun Moon | 6d24734 | 2016-02-12 12:48:47 -0800 | [diff] [blame] | 424 | if (!mastershipService.isLocalMaster(deviceId)) { |
| 425 | return; |
| 426 | } |
| 427 | |
Hyunsun Moon | d52bffc | 2016-01-29 18:57:05 -0800 | [diff] [blame] | 428 | TrafficSelector selector = DefaultTrafficSelector.builder() |
| 429 | .matchEthType(Ethernet.TYPE_ARP) |
| 430 | .matchArpTpa(mService.serviceIp().getIp4Address()) |
| 431 | .build(); |
| 432 | |
| 433 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 434 | .setOutput(PortNumber.LOCAL) |
| 435 | .build(); |
| 436 | |
| 437 | FlowRule flowRule = DefaultFlowRule.builder() |
| 438 | .fromApp(appId) |
| 439 | .withSelector(selector) |
| 440 | .withTreatment(treatment) |
| 441 | .withPriority(MANAGEMENT_PRIORITY) |
| 442 | .forDevice(deviceId) |
| 443 | .forTable(TABLE_FIRST) |
| 444 | .makePermanent() |
| 445 | .build(); |
| 446 | |
| 447 | processFlowRule(true, flowRule); |
| 448 | |
| 449 | selector = DefaultTrafficSelector.builder() |
| 450 | .matchInPort(PortNumber.LOCAL) |
| 451 | .matchEthType(Ethernet.TYPE_ARP) |
| 452 | .matchArpTpa(hostIp.getIp4Address()) |
| 453 | .build(); |
| 454 | |
| 455 | treatment = DefaultTrafficTreatment.builder() |
| 456 | .setOutput(host.location().port()) |
| 457 | .build(); |
| 458 | |
| 459 | flowRule = DefaultFlowRule.builder() |
| 460 | .fromApp(appId) |
| 461 | .withSelector(selector) |
| 462 | .withTreatment(treatment) |
| 463 | .withPriority(MANAGEMENT_PRIORITY) |
| 464 | .forDevice(deviceId) |
| 465 | .forTable(TABLE_FIRST) |
| 466 | .makePermanent() |
| 467 | .build(); |
| 468 | |
| 469 | processFlowRule(true, flowRule); |
| 470 | |
| 471 | selector = DefaultTrafficSelector.builder() |
| 472 | .matchInPort(PortNumber.LOCAL) |
| 473 | .matchEthType(Ethernet.TYPE_IPV4) |
| 474 | .matchIPDst(mService.serviceIpRange()) |
| 475 | .build(); |
| 476 | |
| 477 | treatment = DefaultTrafficTreatment.builder() |
| 478 | .transition(TABLE_DST_IP) |
| 479 | .build(); |
| 480 | |
| 481 | flowRule = DefaultFlowRule.builder() |
| 482 | .fromApp(appId) |
| 483 | .withSelector(selector) |
| 484 | .withTreatment(treatment) |
| 485 | .withPriority(MANAGEMENT_PRIORITY) |
| 486 | .forDevice(deviceId) |
| 487 | .forTable(TABLE_FIRST) |
| 488 | .makePermanent() |
| 489 | .build(); |
| 490 | |
| 491 | processFlowRule(true, flowRule); |
| 492 | |
| 493 | selector = DefaultTrafficSelector.builder() |
| 494 | .matchEthType(Ethernet.TYPE_IPV4) |
| 495 | .matchIPDst(mService.serviceIp().toIpPrefix()) |
| 496 | .build(); |
| 497 | |
| 498 | treatment = DefaultTrafficTreatment.builder() |
| 499 | .setOutput(PortNumber.LOCAL) |
| 500 | .build(); |
| 501 | |
| 502 | flowRule = DefaultFlowRule.builder() |
| 503 | .fromApp(appId) |
| 504 | .withSelector(selector) |
| 505 | .withTreatment(treatment) |
| 506 | .withPriority(MANAGEMENT_PRIORITY) |
| 507 | .forDevice(deviceId) |
| 508 | .forTable(TABLE_ACCESS_TYPE) |
| 509 | .makePermanent() |
| 510 | .build(); |
| 511 | |
| 512 | processFlowRule(true, flowRule); |
| 513 | } |
| 514 | |
| 515 | /** |
| 516 | * Removes management network access rules. |
| 517 | * |
| 518 | * @param host host to be removed |
| 519 | * @param mService service for management network |
| 520 | */ |
| 521 | public void removeManagementNetworkRules(Host host, CordService mService) { |
| 522 | checkNotNull(mService); |
| 523 | |
Hyunsun Moon | 6d24734 | 2016-02-12 12:48:47 -0800 | [diff] [blame] | 524 | if (!mastershipService.isLocalMaster(host.location().deviceId())) { |
| 525 | return; |
| 526 | } |
Hyunsun Moon | d5f6c3b | 2016-02-18 15:37:22 -0800 | [diff] [blame] | 527 | // TODO remove management network specific rules |
Hyunsun Moon | d52bffc | 2016-01-29 18:57:05 -0800 | [diff] [blame] | 528 | } |
| 529 | |
| 530 | /** |
Hyunsun Moon | 6d24734 | 2016-02-12 12:48:47 -0800 | [diff] [blame] | 531 | * Populates rules for vSG VM. |
| 532 | * |
| 533 | * @param vSgHost vSG host |
| 534 | * @param vSgIps set of ip addresses of vSGs running inside the vSG VM |
| 535 | */ |
| 536 | public void populateSubscriberGatewayRules(Host vSgHost, Set<IpAddress> vSgIps) { |
| 537 | VlanId serviceVlan = getServiceVlan(vSgHost); |
| 538 | PortNumber dpPort = getDpPort(vSgHost); |
| 539 | |
| 540 | if (serviceVlan == null || dpPort == null) { |
| 541 | log.warn("Failed to populate rules for vSG VM {}", vSgHost.id()); |
| 542 | return; |
| 543 | } |
| 544 | |
| 545 | // for traffics with s-tag, strip the tag and take through the vSG VM |
| 546 | TrafficSelector selector = DefaultTrafficSelector.builder() |
Hyunsun Moon | d5f6c3b | 2016-02-18 15:37:22 -0800 | [diff] [blame] | 547 | .matchInPort(dpPort) |
Hyunsun Moon | 6d24734 | 2016-02-12 12:48:47 -0800 | [diff] [blame] | 548 | .matchVlanId(serviceVlan) |
| 549 | .build(); |
| 550 | |
| 551 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
Hyunsun Moon | 6d24734 | 2016-02-12 12:48:47 -0800 | [diff] [blame] | 552 | .setOutput(vSgHost.location().port()) |
| 553 | .build(); |
| 554 | |
| 555 | FlowRule flowRule = DefaultFlowRule.builder() |
| 556 | .fromApp(appId) |
| 557 | .withSelector(selector) |
| 558 | .withTreatment(treatment) |
| 559 | .withPriority(DEFAULT_PRIORITY) |
| 560 | .forDevice(vSgHost.location().deviceId()) |
| 561 | .forTable(TABLE_Q_IN_Q) |
| 562 | .makePermanent() |
| 563 | .build(); |
| 564 | |
| 565 | processFlowRule(true, flowRule); |
| 566 | |
| 567 | // for traffics with customer vlan, tag with the service vlan based on input port with |
| 568 | // lower priority to avoid conflict with WAN tag |
| 569 | selector = DefaultTrafficSelector.builder() |
| 570 | .matchInPort(vSgHost.location().port()) |
Hyunsun Moon | d5f6c3b | 2016-02-18 15:37:22 -0800 | [diff] [blame] | 571 | .matchVlanId(serviceVlan) |
Hyunsun Moon | 6d24734 | 2016-02-12 12:48:47 -0800 | [diff] [blame] | 572 | .build(); |
| 573 | |
| 574 | treatment = DefaultTrafficTreatment.builder() |
Hyunsun Moon | 6d24734 | 2016-02-12 12:48:47 -0800 | [diff] [blame] | 575 | .setOutput(dpPort) |
| 576 | .build(); |
| 577 | |
| 578 | flowRule = DefaultFlowRule.builder() |
| 579 | .fromApp(appId) |
| 580 | .withSelector(selector) |
| 581 | .withTreatment(treatment) |
Hyunsun Moon | d5f6c3b | 2016-02-18 15:37:22 -0800 | [diff] [blame] | 582 | .withPriority(DEFAULT_PRIORITY) |
Hyunsun Moon | 6d24734 | 2016-02-12 12:48:47 -0800 | [diff] [blame] | 583 | .forDevice(vSgHost.location().deviceId()) |
| 584 | .forTable(TABLE_Q_IN_Q) |
| 585 | .makePermanent() |
| 586 | .build(); |
| 587 | |
| 588 | processFlowRule(true, flowRule); |
| 589 | |
| 590 | // for traffic coming from WAN, tag 500 and take through the vSG VM |
| 591 | // based on destination ip |
| 592 | vSgIps.stream().forEach(ip -> { |
| 593 | TrafficSelector downstream = DefaultTrafficSelector.builder() |
| 594 | .matchEthType(Ethernet.TYPE_IPV4) |
| 595 | .matchIPDst(ip.toIpPrefix()) |
| 596 | .build(); |
| 597 | |
| 598 | TrafficTreatment downstreamTreatment = DefaultTrafficTreatment.builder() |
| 599 | .pushVlan() |
| 600 | .setVlanId(VLAN_WAN) |
| 601 | .setEthDst(vSgHost.mac()) |
| 602 | .setOutput(vSgHost.location().port()) |
| 603 | .build(); |
| 604 | |
| 605 | FlowRule downstreamFlowRule = DefaultFlowRule.builder() |
| 606 | .fromApp(appId) |
| 607 | .withSelector(downstream) |
| 608 | .withTreatment(downstreamTreatment) |
| 609 | .withPriority(DEFAULT_PRIORITY) |
| 610 | .forDevice(vSgHost.location().deviceId()) |
| 611 | .forTable(TABLE_DST_IP) |
| 612 | .makePermanent() |
| 613 | .build(); |
| 614 | |
| 615 | processFlowRule(true, downstreamFlowRule); |
| 616 | }); |
| 617 | |
| 618 | // remove downstream flow rules for the vSG not shown in vSgIps |
| 619 | for (FlowRule rule : flowRuleService.getFlowRulesById(appId)) { |
| 620 | if (!rule.deviceId().equals(vSgHost.location().deviceId())) { |
| 621 | continue; |
| 622 | } |
| 623 | PortNumber output = getOutputFromTreatment(rule); |
| 624 | if (output == null || !output.equals(vSgHost.location().port()) || |
| 625 | !isVlanPushFromTreatment(rule)) { |
| 626 | continue; |
| 627 | } |
| 628 | |
| 629 | IpPrefix dstIp = getDstIpFromSelector(rule); |
| 630 | if (dstIp != null && !vSgIps.contains(dstIp.address())) { |
| 631 | processFlowRule(false, rule); |
| 632 | } |
| 633 | } |
| 634 | } |
| 635 | |
| 636 | /** |
Hyunsun Moon | 4a91515 | 2016-01-14 16:56:26 -0800 | [diff] [blame] | 637 | * Populates default rules on the first table. |
Hyunsun Moon | 133fd79 | 2016-02-09 01:55:48 -0800 | [diff] [blame] | 638 | * It includes the rules for shuttling vxlan-encapped packets between ovs and |
| 639 | * linux stack,and external network connectivity. |
Hyunsun Moon | c71231d | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 640 | * |
| 641 | * @param deviceId device id |
Hyunsun Moon | 133fd79 | 2016-02-09 01:55:48 -0800 | [diff] [blame] | 642 | * @param dpPort data plane interface port number |
| 643 | * @param dpIp data plane ip address |
Hyunsun Moon | c71231d | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 644 | */ |
Hyunsun Moon | 133fd79 | 2016-02-09 01:55:48 -0800 | [diff] [blame] | 645 | private void processFirstTable(DeviceId deviceId, PortNumber dpPort, IpAddress dpIp) { |
Hyunsun Moon | 4a91515 | 2016-01-14 16:56:26 -0800 | [diff] [blame] | 646 | // take vxlan packet out onto the physical port |
| 647 | TrafficSelector selector = DefaultTrafficSelector.builder() |
| 648 | .matchInPort(PortNumber.LOCAL) |
| 649 | .build(); |
Hyunsun Moon | c71231d | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 650 | |
Hyunsun Moon | 4a91515 | 2016-01-14 16:56:26 -0800 | [diff] [blame] | 651 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
Hyunsun Moon | 133fd79 | 2016-02-09 01:55:48 -0800 | [diff] [blame] | 652 | .setOutput(dpPort) |
Hyunsun Moon | 4a91515 | 2016-01-14 16:56:26 -0800 | [diff] [blame] | 653 | .build(); |
Hyunsun Moon | c71231d | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 654 | |
Hyunsun Moon | 4a91515 | 2016-01-14 16:56:26 -0800 | [diff] [blame] | 655 | FlowRule flowRule = DefaultFlowRule.builder() |
| 656 | .fromApp(appId) |
| 657 | .withSelector(selector) |
| 658 | .withTreatment(treatment) |
Hyunsun Moon | d52bffc | 2016-01-29 18:57:05 -0800 | [diff] [blame] | 659 | .withPriority(HIGH_PRIORITY) |
Hyunsun Moon | 4a91515 | 2016-01-14 16:56:26 -0800 | [diff] [blame] | 660 | .forDevice(deviceId) |
| 661 | .forTable(TABLE_FIRST) |
| 662 | .makePermanent() |
| 663 | .build(); |
Hyunsun Moon | c71231d | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 664 | |
Hyunsun Moon | 4a91515 | 2016-01-14 16:56:26 -0800 | [diff] [blame] | 665 | processFlowRule(true, flowRule); |
Hyunsun Moon | c71231d | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 666 | |
Hyunsun Moon | 4a91515 | 2016-01-14 16:56:26 -0800 | [diff] [blame] | 667 | // take a vxlan encap'd packet through the Linux stack |
| 668 | selector = DefaultTrafficSelector.builder() |
Hyunsun Moon | 133fd79 | 2016-02-09 01:55:48 -0800 | [diff] [blame] | 669 | .matchInPort(dpPort) |
Hyunsun Moon | 4a91515 | 2016-01-14 16:56:26 -0800 | [diff] [blame] | 670 | .matchEthType(Ethernet.TYPE_IPV4) |
| 671 | .matchIPProtocol(IPv4.PROTOCOL_UDP) |
| 672 | .matchUdpDst(TpPort.tpPort(VXLAN_UDP_PORT)) |
| 673 | .build(); |
Hyunsun Moon | c71231d | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 674 | |
Hyunsun Moon | 4a91515 | 2016-01-14 16:56:26 -0800 | [diff] [blame] | 675 | treatment = DefaultTrafficTreatment.builder() |
| 676 | .setOutput(PortNumber.LOCAL) |
| 677 | .build(); |
Hyunsun Moon | c71231d | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 678 | |
Hyunsun Moon | 4a91515 | 2016-01-14 16:56:26 -0800 | [diff] [blame] | 679 | flowRule = DefaultFlowRule.builder() |
| 680 | .fromApp(appId) |
| 681 | .withSelector(selector) |
| 682 | .withTreatment(treatment) |
Hyunsun Moon | d52bffc | 2016-01-29 18:57:05 -0800 | [diff] [blame] | 683 | .withPriority(HIGH_PRIORITY) |
Hyunsun Moon | 4a91515 | 2016-01-14 16:56:26 -0800 | [diff] [blame] | 684 | .forDevice(deviceId) |
| 685 | .forTable(TABLE_FIRST) |
| 686 | .makePermanent() |
| 687 | .build(); |
| 688 | |
| 689 | processFlowRule(true, flowRule); |
| 690 | |
Hyunsun Moon | 133fd79 | 2016-02-09 01:55:48 -0800 | [diff] [blame] | 691 | // take a packet to the data plane ip through Linux stack |
Hyunsun Moon | b4f8508 | 2016-01-15 20:11:41 -0800 | [diff] [blame] | 692 | selector = DefaultTrafficSelector.builder() |
Hyunsun Moon | 133fd79 | 2016-02-09 01:55:48 -0800 | [diff] [blame] | 693 | .matchInPort(dpPort) |
Hyunsun Moon | b4f8508 | 2016-01-15 20:11:41 -0800 | [diff] [blame] | 694 | .matchEthType(Ethernet.TYPE_IPV4) |
Hyunsun Moon | 133fd79 | 2016-02-09 01:55:48 -0800 | [diff] [blame] | 695 | .matchIPDst(dpIp.toIpPrefix()) |
Hyunsun Moon | b4f8508 | 2016-01-15 20:11:41 -0800 | [diff] [blame] | 696 | .build(); |
| 697 | |
| 698 | treatment = DefaultTrafficTreatment.builder() |
| 699 | .setOutput(PortNumber.LOCAL) |
| 700 | .build(); |
| 701 | |
| 702 | flowRule = DefaultFlowRule.builder() |
| 703 | .fromApp(appId) |
| 704 | .withSelector(selector) |
| 705 | .withTreatment(treatment) |
Hyunsun Moon | d52bffc | 2016-01-29 18:57:05 -0800 | [diff] [blame] | 706 | .withPriority(HIGH_PRIORITY) |
Hyunsun Moon | b4f8508 | 2016-01-15 20:11:41 -0800 | [diff] [blame] | 707 | .forDevice(deviceId) |
| 708 | .forTable(TABLE_FIRST) |
| 709 | .makePermanent() |
| 710 | .build(); |
| 711 | |
| 712 | processFlowRule(true, flowRule); |
| 713 | |
| 714 | // take an arp packet from physical through Linux stack |
| 715 | selector = DefaultTrafficSelector.builder() |
Hyunsun Moon | 133fd79 | 2016-02-09 01:55:48 -0800 | [diff] [blame] | 716 | .matchInPort(dpPort) |
Hyunsun Moon | b4f8508 | 2016-01-15 20:11:41 -0800 | [diff] [blame] | 717 | .matchEthType(Ethernet.TYPE_ARP) |
Hyunsun Moon | 6d24734 | 2016-02-12 12:48:47 -0800 | [diff] [blame] | 718 | .matchArpTpa(dpIp.getIp4Address()) |
Hyunsun Moon | b4f8508 | 2016-01-15 20:11:41 -0800 | [diff] [blame] | 719 | .build(); |
| 720 | |
| 721 | treatment = DefaultTrafficTreatment.builder() |
| 722 | .setOutput(PortNumber.LOCAL) |
| 723 | .build(); |
| 724 | |
| 725 | flowRule = DefaultFlowRule.builder() |
| 726 | .fromApp(appId) |
| 727 | .withSelector(selector) |
| 728 | .withTreatment(treatment) |
Hyunsun Moon | d52bffc | 2016-01-29 18:57:05 -0800 | [diff] [blame] | 729 | .withPriority(HIGH_PRIORITY) |
Hyunsun Moon | b4f8508 | 2016-01-15 20:11:41 -0800 | [diff] [blame] | 730 | .forDevice(deviceId) |
| 731 | .forTable(TABLE_FIRST) |
| 732 | .makePermanent() |
| 733 | .build(); |
| 734 | |
| 735 | processFlowRule(true, flowRule); |
| 736 | |
Hyunsun Moon | 4a91515 | 2016-01-14 16:56:26 -0800 | [diff] [blame] | 737 | // take all else to the next table |
| 738 | selector = DefaultTrafficSelector.builder() |
| 739 | .build(); |
| 740 | |
| 741 | treatment = DefaultTrafficTreatment.builder() |
| 742 | .transition(TABLE_IN_PORT) |
| 743 | .build(); |
| 744 | |
| 745 | flowRule = DefaultFlowRule.builder() |
| 746 | .fromApp(appId) |
| 747 | .withSelector(selector) |
| 748 | .withTreatment(treatment) |
| 749 | .withPriority(LOWEST_PRIORITY) |
| 750 | .forDevice(deviceId) |
| 751 | .forTable(TABLE_FIRST) |
| 752 | .makePermanent() |
| 753 | .build(); |
| 754 | |
| 755 | processFlowRule(true, flowRule); |
Hyunsun Moon | 6d24734 | 2016-02-12 12:48:47 -0800 | [diff] [blame] | 756 | |
| 757 | // take all vlan tagged packet to the Q_IN_Q table |
| 758 | selector = DefaultTrafficSelector.builder() |
| 759 | .matchVlanId(VlanId.ANY) |
| 760 | .build(); |
| 761 | |
| 762 | treatment = DefaultTrafficTreatment.builder() |
| 763 | .transition(TABLE_Q_IN_Q) |
| 764 | .build(); |
| 765 | |
| 766 | flowRule = DefaultFlowRule.builder() |
| 767 | .fromApp(appId) |
| 768 | .withSelector(selector) |
| 769 | .withTreatment(treatment) |
| 770 | .withPriority(VSG_PRIORITY) |
| 771 | .forDevice(deviceId) |
| 772 | .forTable(TABLE_FIRST) |
| 773 | .makePermanent() |
| 774 | .build(); |
| 775 | |
| 776 | processFlowRule(true, flowRule); |
Hyunsun Moon | c71231d | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 777 | } |
| 778 | |
| 779 | /** |
Hyunsun Moon | 133fd79 | 2016-02-09 01:55:48 -0800 | [diff] [blame] | 780 | * Forward table miss packets in ACCESS_TYPE table to data plane port. |
Hyunsun Moon | c71231d | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 781 | * |
| 782 | * @param deviceId device id |
Hyunsun Moon | 133fd79 | 2016-02-09 01:55:48 -0800 | [diff] [blame] | 783 | * @param dpPort data plane interface port number |
Hyunsun Moon | c71231d | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 784 | */ |
Hyunsun Moon | 133fd79 | 2016-02-09 01:55:48 -0800 | [diff] [blame] | 785 | private void processAccessTypeTable(DeviceId deviceId, PortNumber dpPort) { |
Hyunsun Moon | c71231d | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 786 | TrafficSelector selector = DefaultTrafficSelector.builder() |
| 787 | .build(); |
| 788 | |
| 789 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
Hyunsun Moon | 133fd79 | 2016-02-09 01:55:48 -0800 | [diff] [blame] | 790 | .setOutput(dpPort) |
Hyunsun Moon | c71231d | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 791 | .build(); |
| 792 | |
| 793 | FlowRule flowRule = DefaultFlowRule.builder() |
| 794 | .fromApp(appId) |
| 795 | .withSelector(selector) |
| 796 | .withTreatment(treatment) |
| 797 | .withPriority(LOWEST_PRIORITY) |
| 798 | .forDevice(deviceId) |
| 799 | .forTable(TABLE_ACCESS_TYPE) |
| 800 | .makePermanent() |
| 801 | .build(); |
| 802 | |
| 803 | processFlowRule(true, flowRule); |
| 804 | } |
| 805 | |
| 806 | /** |
Hyunsun Moon | 4a91515 | 2016-01-14 16:56:26 -0800 | [diff] [blame] | 807 | * Populates default rules for IN_PORT table. |
| 808 | * All packets from tunnel port are forwarded to TUNNEL_ID table and all packets |
Hyunsun Moon | 133fd79 | 2016-02-09 01:55:48 -0800 | [diff] [blame] | 809 | * from data plane interface port to ACCESS_TYPE table. |
Hyunsun Moon | c71231d | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 810 | * |
| 811 | * @param deviceId device id to install the rules |
Hyunsun Moon | 4a91515 | 2016-01-14 16:56:26 -0800 | [diff] [blame] | 812 | * @param tunnelPort tunnel port number |
Hyunsun Moon | 133fd79 | 2016-02-09 01:55:48 -0800 | [diff] [blame] | 813 | * @param dpPort data plane interface port number |
Hyunsun Moon | c71231d | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 814 | */ |
Hyunsun Moon | 133fd79 | 2016-02-09 01:55:48 -0800 | [diff] [blame] | 815 | private void processInPortTable(DeviceId deviceId, PortNumber tunnelPort, PortNumber dpPort) { |
Hyunsun Moon | c71231d | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 816 | checkNotNull(tunnelPort); |
| 817 | |
| 818 | TrafficSelector selector = DefaultTrafficSelector.builder() |
| 819 | .matchInPort(tunnelPort) |
| 820 | .build(); |
Hyunsun Moon | 4a91515 | 2016-01-14 16:56:26 -0800 | [diff] [blame] | 821 | |
Hyunsun Moon | c71231d | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 822 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 823 | .transition(TABLE_TUNNEL_IN) |
| 824 | .build(); |
| 825 | |
| 826 | FlowRule flowRule = DefaultFlowRule.builder() |
| 827 | .fromApp(appId) |
| 828 | .withSelector(selector) |
| 829 | .withTreatment(treatment) |
| 830 | .withPriority(DEFAULT_PRIORITY) |
| 831 | .forDevice(deviceId) |
| 832 | .forTable(TABLE_IN_PORT) |
| 833 | .makePermanent() |
| 834 | .build(); |
| 835 | |
| 836 | processFlowRule(true, flowRule); |
Hyunsun Moon | 4a91515 | 2016-01-14 16:56:26 -0800 | [diff] [blame] | 837 | |
| 838 | selector = DefaultTrafficSelector.builder() |
Hyunsun Moon | 133fd79 | 2016-02-09 01:55:48 -0800 | [diff] [blame] | 839 | .matchInPort(dpPort) |
Hyunsun Moon | 4a91515 | 2016-01-14 16:56:26 -0800 | [diff] [blame] | 840 | .build(); |
| 841 | |
| 842 | treatment = DefaultTrafficTreatment.builder() |
| 843 | .transition(TABLE_DST_IP) |
| 844 | .build(); |
| 845 | |
| 846 | flowRule = DefaultFlowRule.builder() |
| 847 | .fromApp(appId) |
| 848 | .withSelector(selector) |
| 849 | .withTreatment(treatment) |
| 850 | .withPriority(DEFAULT_PRIORITY) |
| 851 | .forDevice(deviceId) |
| 852 | .forTable(TABLE_IN_PORT) |
| 853 | .makePermanent() |
| 854 | .build(); |
| 855 | |
| 856 | processFlowRule(true, flowRule); |
Hyunsun Moon | c71231d | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 857 | } |
| 858 | |
| 859 | /** |
Hyunsun Moon | 6d24734 | 2016-02-12 12:48:47 -0800 | [diff] [blame] | 860 | * Populates default rules for Q_IN_Q table. |
| 861 | * |
| 862 | * @param deviceId device id |
| 863 | * @param dpPort data plane interface port number |
| 864 | */ |
| 865 | private void processQInQTable(DeviceId deviceId, PortNumber dpPort) { |
| 866 | // for traffic going out to WAN, strip vid 500 and take through data plane interface |
| 867 | TrafficSelector selector = DefaultTrafficSelector.builder() |
| 868 | .matchVlanId(VLAN_WAN) |
| 869 | .build(); |
| 870 | |
| 871 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 872 | .popVlan() |
| 873 | .setOutput(dpPort) |
| 874 | .build(); |
| 875 | |
| 876 | FlowRule flowRule = DefaultFlowRule.builder() |
| 877 | .fromApp(appId) |
| 878 | .withSelector(selector) |
| 879 | .withTreatment(treatment) |
| 880 | .withPriority(DEFAULT_PRIORITY) |
| 881 | .forDevice(deviceId) |
| 882 | .forTable(TABLE_Q_IN_Q) |
| 883 | .makePermanent() |
| 884 | .build(); |
| 885 | |
| 886 | processFlowRule(true, flowRule); |
| 887 | |
| 888 | selector = DefaultTrafficSelector.builder() |
| 889 | .matchVlanId(VLAN_WAN) |
| 890 | .matchEthType(Ethernet.TYPE_ARP) |
| 891 | .build(); |
| 892 | |
| 893 | treatment = DefaultTrafficTreatment.builder() |
| 894 | .setOutput(PortNumber.CONTROLLER) |
| 895 | .build(); |
| 896 | |
| 897 | flowRule = DefaultFlowRule.builder() |
| 898 | .fromApp(appId) |
| 899 | .withSelector(selector) |
| 900 | .withTreatment(treatment) |
| 901 | .withPriority(HIGH_PRIORITY) |
| 902 | .forDevice(deviceId) |
| 903 | .forTable(TABLE_Q_IN_Q) |
| 904 | .makePermanent() |
| 905 | .build(); |
| 906 | |
| 907 | processFlowRule(true, flowRule); |
| 908 | } |
| 909 | |
| 910 | /** |
Hyunsun Moon | c71231d | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 911 | * Populates rules for local in port in IN_PORT table. |
| 912 | * Flows from a given in port, whose source IP is service IP transition |
| 913 | * to DST_TYPE table. Other flows transition to IN_SERVICE table. |
| 914 | * |
| 915 | * @param deviceId device id to install the rules |
| 916 | * @param inPort in port |
| 917 | * @param srcIp source ip |
| 918 | */ |
| 919 | private void populateLocalInPortRule(DeviceId deviceId, PortNumber inPort, IpAddress srcIp) { |
| 920 | TrafficSelector selector = DefaultTrafficSelector.builder() |
| 921 | .matchInPort(inPort) |
| 922 | .matchEthType(Ethernet.TYPE_IPV4) |
| 923 | .matchIPSrc(srcIp.toIpPrefix()) |
| 924 | .build(); |
Hyunsun Moon | 4a91515 | 2016-01-14 16:56:26 -0800 | [diff] [blame] | 925 | |
Hyunsun Moon | c71231d | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 926 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 927 | .transition(TABLE_ACCESS_TYPE) |
| 928 | .build(); |
| 929 | |
| 930 | |
| 931 | FlowRule flowRule = DefaultFlowRule.builder() |
| 932 | .fromApp(appId) |
| 933 | .withSelector(selector) |
| 934 | .withTreatment(treatment) |
| 935 | .withPriority(DEFAULT_PRIORITY) |
| 936 | .forDevice(deviceId) |
| 937 | .forTable(TABLE_IN_PORT) |
| 938 | .makePermanent() |
| 939 | .build(); |
| 940 | |
| 941 | processFlowRule(true, flowRule); |
| 942 | |
| 943 | selector = DefaultTrafficSelector.builder() |
| 944 | .matchInPort(inPort) |
| 945 | .build(); |
Hyunsun Moon | 4a91515 | 2016-01-14 16:56:26 -0800 | [diff] [blame] | 946 | |
Hyunsun Moon | c71231d | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 947 | treatment = DefaultTrafficTreatment.builder() |
| 948 | .transition(TABLE_IN_SERVICE) |
| 949 | .build(); |
| 950 | |
| 951 | flowRule = DefaultFlowRule.builder() |
| 952 | .fromApp(appId) |
| 953 | .withSelector(selector) |
| 954 | .withTreatment(treatment) |
Hyunsun Moon | d52bffc | 2016-01-29 18:57:05 -0800 | [diff] [blame] | 955 | .withPriority(LOW_PRIORITY) |
Hyunsun Moon | c71231d | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 956 | .forDevice(deviceId) |
| 957 | .forTable(TABLE_IN_PORT) |
| 958 | .makePermanent() |
| 959 | .build(); |
| 960 | |
| 961 | processFlowRule(true, flowRule); |
| 962 | } |
| 963 | |
| 964 | /** |
| 965 | * Populates direct VM access rules for ACCESS_TYPE table. |
| 966 | * These rules are installed to all devices. |
| 967 | * |
| 968 | * @param srcRange source ip range |
| 969 | * @param dstRange destination ip range |
| 970 | */ |
| 971 | private void populateDirectAccessRule(Ip4Prefix srcRange, Ip4Prefix dstRange) { |
| 972 | TrafficSelector selector = DefaultTrafficSelector.builder() |
| 973 | .matchEthType(Ethernet.TYPE_IPV4) |
| 974 | .matchIPSrc(srcRange) |
| 975 | .matchIPDst(dstRange) |
| 976 | .build(); |
Hyunsun Moon | 4a91515 | 2016-01-14 16:56:26 -0800 | [diff] [blame] | 977 | |
Hyunsun Moon | c71231d | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 978 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 979 | .transition(TABLE_DST_IP) |
| 980 | .build(); |
| 981 | |
| 982 | for (Device device : deviceService.getAvailableDevices(SWITCH)) { |
| 983 | FlowRule flowRuleDirect = DefaultFlowRule.builder() |
| 984 | .fromApp(appId) |
| 985 | .withSelector(selector) |
| 986 | .withTreatment(treatment) |
Hyunsun Moon | 81ab23b | 2016-02-01 23:00:56 -0800 | [diff] [blame] | 987 | .withPriority(DEFAULT_PRIORITY) |
| 988 | .forDevice(device.id()) |
| 989 | .forTable(TABLE_ACCESS_TYPE) |
| 990 | .makePermanent() |
| 991 | .build(); |
| 992 | |
| 993 | processFlowRule(true, flowRuleDirect); |
| 994 | } |
| 995 | } |
| 996 | |
| 997 | /** |
| 998 | * Populates drop rules that does not match any direct access rules but has |
| 999 | * destination to a different service network in ACCESS_TYPE table. |
| 1000 | * |
| 1001 | * @param dstRange destination ip range |
| 1002 | */ |
| 1003 | private void populateServiceIsolationRule(Ip4Prefix dstRange) { |
| 1004 | TrafficSelector selector = DefaultTrafficSelector.builder() |
| 1005 | .matchEthType(Ethernet.TYPE_IPV4) |
| 1006 | .matchIPDst(dstRange) |
| 1007 | .build(); |
| 1008 | |
| 1009 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 1010 | .drop() |
| 1011 | .build(); |
| 1012 | |
| 1013 | for (Device device : deviceService.getAvailableDevices(SWITCH)) { |
| 1014 | FlowRule flowRuleDirect = DefaultFlowRule.builder() |
| 1015 | .fromApp(appId) |
| 1016 | .withSelector(selector) |
| 1017 | .withTreatment(treatment) |
Hyunsun Moon | d52bffc | 2016-01-29 18:57:05 -0800 | [diff] [blame] | 1018 | .withPriority(LOW_PRIORITY) |
Hyunsun Moon | c71231d | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 1019 | .forDevice(device.id()) |
| 1020 | .forTable(TABLE_ACCESS_TYPE) |
| 1021 | .makePermanent() |
| 1022 | .build(); |
| 1023 | |
| 1024 | processFlowRule(true, flowRuleDirect); |
| 1025 | } |
| 1026 | } |
| 1027 | |
| 1028 | /** |
| 1029 | * Populates indirect service access rules for ACCESS_TYPE table. |
| 1030 | * These rules are installed to all devices. |
| 1031 | * |
| 1032 | * @param srcRange source range |
| 1033 | * @param serviceIp service ip |
| 1034 | * @param outGroups list of output group |
| 1035 | */ |
| 1036 | private void populateIndirectAccessRule(Ip4Prefix srcRange, Ip4Address serviceIp, |
| 1037 | Map<DeviceId, GroupId> outGroups) { |
| 1038 | TrafficSelector selector = DefaultTrafficSelector.builder() |
| 1039 | .matchEthType(Ethernet.TYPE_IPV4) |
| 1040 | .matchIPSrc(srcRange) |
| 1041 | .matchIPDst(serviceIp.toIpPrefix()) |
| 1042 | .build(); |
| 1043 | |
| 1044 | for (Map.Entry<DeviceId, GroupId> outGroup : outGroups.entrySet()) { |
| 1045 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 1046 | .group(outGroup.getValue()) |
| 1047 | .build(); |
| 1048 | |
| 1049 | FlowRule flowRule = DefaultFlowRule.builder() |
| 1050 | .fromApp(appId) |
| 1051 | .withSelector(selector) |
| 1052 | .withTreatment(treatment) |
Hyunsun Moon | 81ab23b | 2016-02-01 23:00:56 -0800 | [diff] [blame] | 1053 | .withPriority(HIGH_PRIORITY) |
Hyunsun Moon | c71231d | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 1054 | .forDevice(outGroup.getKey()) |
| 1055 | .forTable(TABLE_ACCESS_TYPE) |
| 1056 | .makePermanent() |
| 1057 | .build(); |
| 1058 | |
| 1059 | processFlowRule(true, flowRule); |
| 1060 | } |
| 1061 | } |
| 1062 | |
| 1063 | /** |
| 1064 | * Populates flow rules for IN_SERVICE table. |
| 1065 | * |
| 1066 | * @param inPorts list of inports related to the service for each device |
| 1067 | * @param outGroups set of output groups |
| 1068 | */ |
| 1069 | private void populateInServiceRule(Map<DeviceId, Set<PortNumber>> inPorts, Map<DeviceId, GroupId> outGroups) { |
| 1070 | checkNotNull(inPorts); |
| 1071 | checkNotNull(outGroups); |
| 1072 | |
| 1073 | for (Map.Entry<DeviceId, Set<PortNumber>> entry : inPorts.entrySet()) { |
| 1074 | Set<PortNumber> ports = entry.getValue(); |
| 1075 | DeviceId deviceId = entry.getKey(); |
| 1076 | |
| 1077 | GroupId groupId = outGroups.get(deviceId); |
| 1078 | if (groupId == null) { |
| 1079 | continue; |
| 1080 | } |
| 1081 | |
| 1082 | ports.stream().forEach(port -> { |
| 1083 | TrafficSelector selector = DefaultTrafficSelector.builder() |
| 1084 | .matchInPort(port) |
| 1085 | .build(); |
Hyunsun Moon | 4a91515 | 2016-01-14 16:56:26 -0800 | [diff] [blame] | 1086 | |
Hyunsun Moon | c71231d | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 1087 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 1088 | .group(groupId) |
| 1089 | .build(); |
| 1090 | |
| 1091 | FlowRule flowRule = DefaultFlowRule.builder() |
| 1092 | .fromApp(appId) |
| 1093 | .withSelector(selector) |
| 1094 | .withTreatment(treatment) |
| 1095 | .withPriority(DEFAULT_PRIORITY) |
| 1096 | .forDevice(deviceId) |
| 1097 | .forTable(TABLE_IN_SERVICE) |
| 1098 | .makePermanent() |
| 1099 | .build(); |
| 1100 | |
| 1101 | processFlowRule(true, flowRule); |
| 1102 | }); |
| 1103 | } |
| 1104 | } |
| 1105 | |
| 1106 | /** |
| 1107 | * Populates flow rules for DST_IP table. |
| 1108 | * |
| 1109 | * @param deviceId device id |
| 1110 | * @param inPort in port |
| 1111 | * @param dstMac mac address |
| 1112 | * @param dstIp destination ip |
| 1113 | * @param tunnelId tunnel id |
| 1114 | * @param tunnelIp tunnel remote ip |
| 1115 | */ |
| 1116 | private void populateDstIpRule(DeviceId deviceId, PortNumber inPort, MacAddress dstMac, |
| 1117 | IpAddress dstIp, long tunnelId, IpAddress tunnelIp) { |
| 1118 | TrafficSelector selector = DefaultTrafficSelector.builder() |
| 1119 | .matchEthType(Ethernet.TYPE_IPV4) |
| 1120 | .matchIPDst(dstIp.toIpPrefix()) |
| 1121 | .build(); |
Hyunsun Moon | 4a91515 | 2016-01-14 16:56:26 -0800 | [diff] [blame] | 1122 | |
Hyunsun Moon | c71231d | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 1123 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 1124 | .setEthDst(dstMac) |
| 1125 | .setOutput(inPort) |
| 1126 | .build(); |
| 1127 | |
| 1128 | FlowRule flowRule = DefaultFlowRule.builder() |
| 1129 | .fromApp(appId) |
| 1130 | .withSelector(selector) |
| 1131 | .withTreatment(treatment) |
| 1132 | .withPriority(DEFAULT_PRIORITY) |
| 1133 | .forDevice(deviceId) |
| 1134 | .forTable(TABLE_DST_IP) |
| 1135 | .makePermanent() |
| 1136 | .build(); |
| 1137 | |
| 1138 | processFlowRule(true, flowRule); |
| 1139 | |
| 1140 | for (Device device : deviceService.getAvailableDevices(SWITCH)) { |
| 1141 | if (device.id().equals(deviceId)) { |
| 1142 | continue; |
| 1143 | } |
| 1144 | |
| 1145 | ExtensionTreatment tunnelDst = getTunnelDst(device.id(), tunnelIp.getIp4Address()); |
| 1146 | if (tunnelDst == null) { |
| 1147 | continue; |
| 1148 | } |
| 1149 | |
| 1150 | treatment = DefaultTrafficTreatment.builder() |
| 1151 | .setEthDst(dstMac) |
| 1152 | .setTunnelId(tunnelId) |
| 1153 | .extension(tunnelDst, device.id()) |
| 1154 | .setOutput(getTunnelPort(device.id())) |
| 1155 | .build(); |
| 1156 | |
| 1157 | flowRule = DefaultFlowRule.builder() |
| 1158 | .fromApp(appId) |
| 1159 | .withSelector(selector) |
| 1160 | .withTreatment(treatment) |
| 1161 | .withPriority(DEFAULT_PRIORITY) |
| 1162 | .forDevice(device.id()) |
| 1163 | .forTable(TABLE_DST_IP) |
| 1164 | .makePermanent() |
| 1165 | .build(); |
| 1166 | |
| 1167 | processFlowRule(true, flowRule); |
| 1168 | } |
| 1169 | } |
| 1170 | |
| 1171 | /** |
| 1172 | * Populates flow rules for TUNNEL_ID table. |
| 1173 | * |
| 1174 | * @param deviceId device id |
| 1175 | * @param inPort in port |
| 1176 | * @param mac mac address |
| 1177 | * @param tunnelId tunnel id |
| 1178 | */ |
| 1179 | private void populateTunnelInRule(DeviceId deviceId, PortNumber inPort, MacAddress mac, long tunnelId) { |
| 1180 | TrafficSelector selector = DefaultTrafficSelector.builder() |
| 1181 | .matchTunnelId(tunnelId) |
| 1182 | .matchEthDst(mac) |
| 1183 | .build(); |
| 1184 | |
| 1185 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 1186 | .setOutput(inPort) |
| 1187 | .build(); |
| 1188 | |
| 1189 | FlowRule flowRule = DefaultFlowRule.builder() |
| 1190 | .fromApp(appId) |
| 1191 | .withSelector(selector) |
| 1192 | .withTreatment(treatment) |
| 1193 | .withPriority(DEFAULT_PRIORITY) |
| 1194 | .forDevice(deviceId) |
| 1195 | .forTable(TABLE_TUNNEL_IN) |
| 1196 | .makePermanent() |
| 1197 | .build(); |
| 1198 | |
| 1199 | processFlowRule(true, flowRule); |
| 1200 | } |
| 1201 | |
| 1202 | /** |
| 1203 | * Installs or uninstall a given rule. |
| 1204 | * |
| 1205 | * @param install true to install, false to uninstall |
| 1206 | * @param rule rule |
| 1207 | */ |
| 1208 | private void processFlowRule(boolean install, FlowRule rule) { |
| 1209 | FlowRuleOperations.Builder oBuilder = FlowRuleOperations.builder(); |
| 1210 | oBuilder = install ? oBuilder.add(rule) : oBuilder.remove(rule); |
| 1211 | |
| 1212 | flowRuleService.apply(oBuilder.build(new FlowRuleOperationsContext() { |
| 1213 | @Override |
| 1214 | public void onError(FlowRuleOperations ops) { |
| 1215 | log.error(String.format("Failed %s, %s", ops.toString(), rule.toString())); |
| 1216 | } |
| 1217 | })); |
| 1218 | } |
| 1219 | |
| 1220 | /** |
| 1221 | * Returns tunnel port of the device. |
| 1222 | * |
| 1223 | * @param deviceId device id |
| 1224 | * @return tunnel port number, or null if no tunnel port exists on a given device |
| 1225 | */ |
| 1226 | private PortNumber getTunnelPort(DeviceId deviceId) { |
Hyunsun Moon | 5a4346f | 2016-01-15 11:41:14 -0800 | [diff] [blame] | 1227 | Port port = deviceService.getPorts(deviceId).stream() |
Hyunsun Moon | 6d24734 | 2016-02-12 12:48:47 -0800 | [diff] [blame] | 1228 | .filter(p -> p.annotations().value(PORT_NAME).contains(tunnelType)) |
| 1229 | .findFirst().orElse(null); |
Hyunsun Moon | 5a4346f | 2016-01-15 11:41:14 -0800 | [diff] [blame] | 1230 | |
| 1231 | return port == null ? null : port.number(); |
Hyunsun Moon | c71231d | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 1232 | } |
| 1233 | |
| 1234 | /** |
Hyunsun Moon | 133fd79 | 2016-02-09 01:55:48 -0800 | [diff] [blame] | 1235 | * Returns data plane interface port name of a given device. |
Hyunsun Moon | 4a91515 | 2016-01-14 16:56:26 -0800 | [diff] [blame] | 1236 | * |
| 1237 | * @param deviceId device id |
Hyunsun Moon | 133fd79 | 2016-02-09 01:55:48 -0800 | [diff] [blame] | 1238 | * @param dpIntf data plane interface port name |
| 1239 | * @return data plane interface port number, or null if no such port exists |
Hyunsun Moon | 4a91515 | 2016-01-14 16:56:26 -0800 | [diff] [blame] | 1240 | */ |
Hyunsun Moon | 133fd79 | 2016-02-09 01:55:48 -0800 | [diff] [blame] | 1241 | private PortNumber getDpPort(DeviceId deviceId, String dpIntf) { |
Hyunsun Moon | 5a4346f | 2016-01-15 11:41:14 -0800 | [diff] [blame] | 1242 | Port port = deviceService.getPorts(deviceId).stream() |
Hyunsun Moon | 6d24734 | 2016-02-12 12:48:47 -0800 | [diff] [blame] | 1243 | .filter(p -> p.annotations().value(PORT_NAME).contains(dpIntf) && |
| 1244 | p.isEnabled()) |
| 1245 | .findFirst().orElse(null); |
Hyunsun Moon | 5a4346f | 2016-01-15 11:41:14 -0800 | [diff] [blame] | 1246 | |
| 1247 | return port == null ? null : port.number(); |
Hyunsun Moon | 4a91515 | 2016-01-14 16:56:26 -0800 | [diff] [blame] | 1248 | } |
| 1249 | |
Hyunsun Moon | 6d24734 | 2016-02-12 12:48:47 -0800 | [diff] [blame] | 1250 | /** Returns data plane interface port number of a given host. |
| 1251 | * |
| 1252 | * @param host host |
| 1253 | * @return port number, or null |
| 1254 | */ |
| 1255 | private PortNumber getDpPort(Host host) { |
| 1256 | String portName = host.annotations().value(DATA_PLANE_INTF); |
| 1257 | return portName == null ? null : getDpPort(host.location().deviceId(), portName); |
| 1258 | } |
| 1259 | |
| 1260 | /** |
| 1261 | * Returns service vlan from a given host. |
| 1262 | * |
| 1263 | * @param host host |
| 1264 | * @return vlan id, or null |
| 1265 | */ |
| 1266 | private VlanId getServiceVlan(Host host) { |
| 1267 | String serviceVlan = host.annotations().value(S_TAG); |
| 1268 | return serviceVlan == null ? null : VlanId.vlanId(Short.parseShort(serviceVlan)); |
| 1269 | } |
| 1270 | |
Hyunsun Moon | 4a91515 | 2016-01-14 16:56:26 -0800 | [diff] [blame] | 1271 | /** |
Hyunsun Moon | c71231d | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 1272 | * Returns the inport from a given flow rule if the rule contains the match of it. |
| 1273 | * |
| 1274 | * @param flowRule flow rule |
| 1275 | * @return port number, or null if the rule doesn't have inport match |
| 1276 | */ |
| 1277 | private PortNumber getInPort(FlowRule flowRule) { |
| 1278 | Criterion criterion = flowRule.selector().getCriterion(IN_PORT); |
| 1279 | if (criterion != null && criterion instanceof PortCriterion) { |
| 1280 | PortCriterion port = (PortCriterion) criterion; |
| 1281 | return port.port(); |
| 1282 | } else { |
| 1283 | return null; |
| 1284 | } |
| 1285 | } |
| 1286 | |
| 1287 | /** |
| 1288 | * Returns the destination mac address from a given flow rule if the rule |
| 1289 | * contains the instruction of it. |
| 1290 | * |
| 1291 | * @param flowRule flow rule |
| 1292 | * @return mac address, or null if the rule doesn't have destination mac instruction |
| 1293 | */ |
| 1294 | private MacAddress getDstMacFromTreatment(FlowRule flowRule) { |
| 1295 | Instruction instruction = flowRule.treatment().allInstructions().stream() |
| 1296 | .filter(inst -> inst instanceof ModEtherInstruction && |
| 1297 | ((ModEtherInstruction) inst).subtype().equals(ETH_DST)) |
| 1298 | .findFirst() |
| 1299 | .orElse(null); |
| 1300 | |
| 1301 | if (instruction == null) { |
Hyunsun Moon | 9f0814b | 2015-11-04 17:34:35 -0800 | [diff] [blame] | 1302 | return null; |
| 1303 | } |
| 1304 | |
Hyunsun Moon | c71231d | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 1305 | return ((ModEtherInstruction) instruction).mac(); |
Hyunsun Moon | 9f0814b | 2015-11-04 17:34:35 -0800 | [diff] [blame] | 1306 | } |
| 1307 | |
| 1308 | /** |
Hyunsun Moon | c71231d | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 1309 | * Returns the destination mac address from a given flow rule if the rule |
| 1310 | * contains the match of it. |
Hyunsun Moon | 9f0814b | 2015-11-04 17:34:35 -0800 | [diff] [blame] | 1311 | * |
Hyunsun Moon | c71231d | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 1312 | * @param flowRule flow rule |
| 1313 | * @return mac address, or null if the rule doesn't have destination mac match |
Hyunsun Moon | 9f0814b | 2015-11-04 17:34:35 -0800 | [diff] [blame] | 1314 | */ |
Hyunsun Moon | c71231d | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 1315 | private MacAddress getDstMacFromSelector(FlowRule flowRule) { |
| 1316 | Criterion criterion = flowRule.selector().getCriterion(Criterion.Type.ETH_DST); |
| 1317 | if (criterion != null && criterion instanceof EthCriterion) { |
| 1318 | EthCriterion eth = (EthCriterion) criterion; |
| 1319 | return eth.mac(); |
| 1320 | } else { |
| 1321 | return null; |
| 1322 | } |
| 1323 | } |
| 1324 | |
| 1325 | /** |
| 1326 | * Returns the destination IP from a given flow rule if the rule contains |
| 1327 | * the match of it. |
| 1328 | * |
| 1329 | * @param flowRule flow rule |
| 1330 | * @return ip prefix, or null if the rule doesn't have ip match |
| 1331 | */ |
| 1332 | private IpPrefix getDstIpFromSelector(FlowRule flowRule) { |
| 1333 | Criterion criterion = flowRule.selector().getCriterion(IPV4_DST); |
| 1334 | if (criterion != null && criterion instanceof IPCriterion) { |
| 1335 | IPCriterion ip = (IPCriterion) criterion; |
| 1336 | return ip.ip(); |
| 1337 | } else { |
| 1338 | return null; |
| 1339 | } |
| 1340 | } |
| 1341 | |
| 1342 | /** |
| 1343 | * Returns the source IP from a given flow rule if the rule contains |
| 1344 | * the match of it. |
| 1345 | * |
| 1346 | * @param flowRule flow rule |
| 1347 | * @return ip prefix, or null if the rule doesn't have ip match |
| 1348 | */ |
| 1349 | private IpPrefix getSrcIpFromSelector(FlowRule flowRule) { |
| 1350 | Criterion criterion = flowRule.selector().getCriterion(IPV4_SRC); |
| 1351 | if (criterion != null && criterion instanceof IPCriterion) { |
| 1352 | IPCriterion ip = (IPCriterion) criterion; |
| 1353 | return ip.ip(); |
| 1354 | } else { |
| 1355 | return null; |
| 1356 | } |
| 1357 | } |
| 1358 | |
| 1359 | /** |
| 1360 | * Returns the group ID from a given flow rule if the rule contains the |
| 1361 | * treatment of it. |
| 1362 | * |
| 1363 | * @param flowRule flow rule |
| 1364 | * @return group id, or null if the rule doesn't have group instruction |
| 1365 | */ |
| 1366 | private GroupId getGroupIdFromTreatment(FlowRule flowRule) { |
| 1367 | Instruction instruction = flowRule.treatment().allInstructions().stream() |
| 1368 | .filter(inst -> inst instanceof Instructions.GroupInstruction) |
| 1369 | .findFirst() |
| 1370 | .orElse(null); |
| 1371 | |
| 1372 | if (instruction == null) { |
| 1373 | return null; |
| 1374 | } |
| 1375 | |
| 1376 | return ((Instructions.GroupInstruction) instruction).groupId(); |
Hyunsun Moon | 9f0814b | 2015-11-04 17:34:35 -0800 | [diff] [blame] | 1377 | } |
| 1378 | |
| 1379 | /** |
Hyunsun Moon | d52bffc | 2016-01-29 18:57:05 -0800 | [diff] [blame] | 1380 | * Returns the output port number from a given flow rule. |
| 1381 | * |
| 1382 | * @param flowRule flow rule |
| 1383 | * @return port number, or null if the rule does not have output instruction |
| 1384 | */ |
| 1385 | private PortNumber getOutputFromTreatment(FlowRule flowRule) { |
| 1386 | Instruction instruction = flowRule.treatment().allInstructions().stream() |
Hyunsun Moon | 6d24734 | 2016-02-12 12:48:47 -0800 | [diff] [blame] | 1387 | .filter(inst -> inst instanceof Instructions.OutputInstruction) |
Hyunsun Moon | d52bffc | 2016-01-29 18:57:05 -0800 | [diff] [blame] | 1388 | .findFirst() |
| 1389 | .orElse(null); |
| 1390 | |
| 1391 | if (instruction == null) { |
| 1392 | return null; |
| 1393 | } |
| 1394 | |
| 1395 | return ((Instructions.OutputInstruction) instruction).port(); |
| 1396 | } |
| 1397 | |
| 1398 | /** |
Hyunsun Moon | 6d24734 | 2016-02-12 12:48:47 -0800 | [diff] [blame] | 1399 | * Returns if a given flow rule has vlan push instruction or not. |
| 1400 | * |
| 1401 | * @param flowRule flow rule |
| 1402 | * @return true if it includes vlan push, or false |
| 1403 | */ |
| 1404 | private boolean isVlanPushFromTreatment(FlowRule flowRule) { |
| 1405 | Instruction instruction = flowRule.treatment().allInstructions().stream() |
| 1406 | .filter(inst -> inst instanceof L2ModificationInstruction) |
| 1407 | .filter(inst -> ((L2ModificationInstruction) inst).subtype().equals(VLAN_PUSH)) |
| 1408 | .findAny() |
| 1409 | .orElse(null); |
| 1410 | |
| 1411 | return instruction != null; |
| 1412 | } |
| 1413 | |
| 1414 | /** |
Hyunsun Moon | 4a91515 | 2016-01-14 16:56:26 -0800 | [diff] [blame] | 1415 | * Creates a new group for a given service. |
| 1416 | * |
| 1417 | * @param deviceId device id to create a group |
| 1418 | * @param service cord service |
| 1419 | * @return group id, or null if it fails to create |
| 1420 | */ |
| 1421 | private GroupId createServiceGroup(DeviceId deviceId, CordService service) { |
| 1422 | checkNotNull(service); |
| 1423 | |
| 1424 | GroupKey groupKey = getGroupKey(service.id()); |
| 1425 | Group group = groupService.getGroup(deviceId, groupKey); |
| 1426 | GroupId groupId = getGroupId(service.id(), deviceId); |
| 1427 | |
| 1428 | if (group != null) { |
| 1429 | log.debug("Group {} is already exist in {}", service.id(), deviceId); |
| 1430 | return groupId; |
| 1431 | } |
| 1432 | |
| 1433 | GroupBuckets buckets = getServiceGroupBuckets(deviceId, service.segmentationId(), service.hosts()); |
| 1434 | GroupDescription groupDescription = new DefaultGroupDescription( |
| 1435 | deviceId, |
| 1436 | GroupDescription.Type.SELECT, |
| 1437 | buckets, |
| 1438 | groupKey, |
| 1439 | groupId.id(), |
| 1440 | appId); |
| 1441 | |
| 1442 | groupService.addGroup(groupDescription); |
| 1443 | |
| 1444 | return groupId; |
| 1445 | } |
| 1446 | |
| 1447 | /** |
| 1448 | * Returns group buckets for a given device. |
| 1449 | * |
| 1450 | * @param deviceId device id |
| 1451 | * @param tunnelId tunnel id |
| 1452 | * @param hosts list of host |
| 1453 | * @return group buckets |
| 1454 | */ |
| 1455 | private GroupBuckets getServiceGroupBuckets(DeviceId deviceId, long tunnelId, Map<Host, IpAddress> hosts) { |
| 1456 | List<GroupBucket> buckets = Lists.newArrayList(); |
| 1457 | |
| 1458 | for (Map.Entry<Host, IpAddress> entry : hosts.entrySet()) { |
| 1459 | Host host = entry.getKey(); |
| 1460 | Ip4Address remoteIp = entry.getValue().getIp4Address(); |
| 1461 | DeviceId hostDevice = host.location().deviceId(); |
| 1462 | |
| 1463 | TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment |
| 1464 | .builder() |
| 1465 | .setEthDst(host.mac()); |
| 1466 | |
| 1467 | if (deviceId.equals(hostDevice)) { |
| 1468 | tBuilder.setOutput(host.location().port()); |
| 1469 | } else { |
| 1470 | ExtensionTreatment tunnelDst = getTunnelDst(deviceId, remoteIp); |
| 1471 | if (tunnelDst == null) { |
| 1472 | continue; |
| 1473 | } |
| 1474 | |
| 1475 | tBuilder.extension(tunnelDst, deviceId) |
| 1476 | .setTunnelId(tunnelId) |
| 1477 | .setOutput(getTunnelPort(hostDevice)); |
| 1478 | } |
| 1479 | |
| 1480 | buckets.add(DefaultGroupBucket.createSelectGroupBucket(tBuilder.build())); |
| 1481 | } |
| 1482 | |
| 1483 | return new GroupBuckets(buckets); |
| 1484 | } |
| 1485 | |
| 1486 | /** |
| 1487 | * Returns globally unique group ID. |
| 1488 | * |
| 1489 | * @param serviceId service id |
| 1490 | * @param deviceId device id |
| 1491 | * @return group id |
| 1492 | */ |
| 1493 | private GroupId getGroupId(CordServiceId serviceId, DeviceId deviceId) { |
| 1494 | return new DefaultGroupId(Objects.hash(serviceId, deviceId)); |
| 1495 | } |
| 1496 | |
| 1497 | /** |
| 1498 | * Returns group key of a service. |
| 1499 | * |
| 1500 | * @param serviceId service id |
| 1501 | * @return group key |
| 1502 | */ |
| 1503 | private GroupKey getGroupKey(CordServiceId serviceId) { |
| 1504 | return new DefaultGroupKey(serviceId.id().getBytes()); |
| 1505 | } |
| 1506 | |
| 1507 | /** |
Hyunsun Moon | 9f0814b | 2015-11-04 17:34:35 -0800 | [diff] [blame] | 1508 | * Returns extension instruction to set tunnel destination. |
| 1509 | * |
| 1510 | * @param deviceId device id |
| 1511 | * @param remoteIp tunnel destination address |
| 1512 | * @return extension treatment or null if it fails to get instruction |
| 1513 | */ |
Hyunsun Moon | c71231d | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 1514 | private ExtensionTreatment getTunnelDst(DeviceId deviceId, Ip4Address remoteIp) { |
Hyunsun Moon | 9f0814b | 2015-11-04 17:34:35 -0800 | [diff] [blame] | 1515 | try { |
| 1516 | Driver driver = driverService.getDriver(deviceId); |
Hyunsun Moon | c71231d | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 1517 | DefaultDriverData driverData = new DefaultDriverData(driver, deviceId); |
| 1518 | DriverHandler handler = new DefaultDriverHandler(driverData); |
| 1519 | ExtensionTreatmentResolver resolver = handler.behaviour(ExtensionTreatmentResolver.class); |
Hyunsun Moon | 9f0814b | 2015-11-04 17:34:35 -0800 | [diff] [blame] | 1520 | |
Hyunsun Moon | c71231d | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 1521 | ExtensionTreatment treatment = |
| 1522 | resolver.getExtensionInstruction(NICIRA_SET_TUNNEL_DST.type()); |
Hyunsun Moon | 9f0814b | 2015-11-04 17:34:35 -0800 | [diff] [blame] | 1523 | treatment.setPropertyValue("tunnelDst", remoteIp); |
| 1524 | |
| 1525 | return treatment; |
Hyunsun Moon | c71231d | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 1526 | } catch (ItemNotFoundException | UnsupportedOperationException | |
| 1527 | ExtensionPropertyException e) { |
| 1528 | log.error("Failed to get extension instruction {}", deviceId); |
Hyunsun Moon | 9f0814b | 2015-11-04 17:34:35 -0800 | [diff] [blame] | 1529 | return null; |
| 1530 | } |
| 1531 | } |
| 1532 | } |
Hyunsun Moon | c71231d | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 1533 | |