sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 1 | /* |
Jian Li | 2694976 | 2018-03-30 15:46:37 +0900 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
| 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 | */ |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 16 | |
| 17 | package org.onosproject.openstacknetworking.impl; |
| 18 | |
| 19 | import com.google.common.base.Strings; |
sangho | 2e97be0 | 2017-07-03 18:18:27 +0900 | [diff] [blame] | 20 | import com.google.common.collect.Maps; |
| 21 | import com.google.common.collect.Sets; |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 22 | import org.apache.felix.scr.annotations.Activate; |
| 23 | import org.apache.felix.scr.annotations.Component; |
| 24 | import org.apache.felix.scr.annotations.Deactivate; |
sangho | 0248ca2 | 2017-05-31 13:22:47 +0900 | [diff] [blame] | 25 | import org.apache.felix.scr.annotations.Modified; |
| 26 | import org.apache.felix.scr.annotations.Property; |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 27 | import org.apache.felix.scr.annotations.Reference; |
| 28 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 29 | import org.onlab.packet.Ethernet; |
| 30 | import org.onlab.packet.IPv4; |
| 31 | import org.onlab.packet.Ip4Address; |
| 32 | import org.onlab.packet.Ip4Prefix; |
| 33 | import org.onlab.packet.IpPrefix; |
| 34 | import org.onlab.packet.TpPort; |
Jian Li | e8b28db | 2018-10-17 14:04:09 +0900 | [diff] [blame^] | 35 | import org.onlab.packet.VlanId; |
sangho | 0248ca2 | 2017-05-31 13:22:47 +0900 | [diff] [blame] | 36 | import org.onlab.util.Tools; |
| 37 | import org.onosproject.cfg.ComponentConfigService; |
sangho | 1aaa788 | 2017-05-31 13:22:47 +0900 | [diff] [blame] | 38 | import org.onosproject.cluster.ClusterService; |
| 39 | import org.onosproject.cluster.LeadershipService; |
| 40 | import org.onosproject.cluster.NodeId; |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 41 | import org.onosproject.core.ApplicationId; |
| 42 | import org.onosproject.core.CoreService; |
| 43 | import org.onosproject.mastership.MastershipService; |
sangho | 1aaa788 | 2017-05-31 13:22:47 +0900 | [diff] [blame] | 44 | import org.onosproject.net.DeviceId; |
| 45 | import org.onosproject.net.driver.DriverService; |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 46 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 47 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 48 | import org.onosproject.net.flow.TrafficSelector; |
sangho | 1aaa788 | 2017-05-31 13:22:47 +0900 | [diff] [blame] | 49 | import org.onosproject.net.flow.TrafficTreatment; |
| 50 | import org.onosproject.net.flow.criteria.ExtensionSelector; |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 51 | import org.onosproject.openstacknetworking.api.InstancePort; |
Jian Li | 9d35bd6 | 2018-10-13 01:43:24 +0900 | [diff] [blame] | 52 | import org.onosproject.openstacknetworking.api.InstancePortAdminService; |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 53 | import org.onosproject.openstacknetworking.api.InstancePortEvent; |
| 54 | import org.onosproject.openstacknetworking.api.InstancePortListener; |
sangho | dc37537 | 2017-06-08 10:41:30 +0900 | [diff] [blame] | 55 | import org.onosproject.openstacknetworking.api.OpenstackFlowRuleService; |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 56 | import org.onosproject.openstacknetworking.api.OpenstackNetworkEvent; |
| 57 | import org.onosproject.openstacknetworking.api.OpenstackNetworkListener; |
| 58 | import org.onosproject.openstacknetworking.api.OpenstackNetworkService; |
| 59 | import org.onosproject.openstacknetworking.api.OpenstackSecurityGroupEvent; |
| 60 | import org.onosproject.openstacknetworking.api.OpenstackSecurityGroupListener; |
| 61 | import org.onosproject.openstacknetworking.api.OpenstackSecurityGroupService; |
Jian Li | 9d35bd6 | 2018-10-13 01:43:24 +0900 | [diff] [blame] | 62 | import org.onosproject.openstacknetworking.api.PreCommitPortService; |
Jian Li | 2694976 | 2018-03-30 15:46:37 +0900 | [diff] [blame] | 63 | import org.onosproject.openstacknetworking.util.RulePopulatorUtil; |
sangho | 3dd2a8b | 2017-07-19 15:54:31 +0900 | [diff] [blame] | 64 | import org.onosproject.openstacknode.api.OpenstackNode; |
sangho | 1aaa788 | 2017-05-31 13:22:47 +0900 | [diff] [blame] | 65 | import org.onosproject.openstacknode.api.OpenstackNodeEvent; |
| 66 | import org.onosproject.openstacknode.api.OpenstackNodeListener; |
sangho | 3dd2a8b | 2017-07-19 15:54:31 +0900 | [diff] [blame] | 67 | import org.onosproject.openstacknode.api.OpenstackNodeService; |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 68 | import org.openstack4j.model.network.Port; |
| 69 | import org.openstack4j.model.network.SecurityGroup; |
| 70 | import org.openstack4j.model.network.SecurityGroupRule; |
| 71 | import org.openstack4j.openstack.networking.domain.NeutronSecurityGroupRule; |
sangho | 0248ca2 | 2017-05-31 13:22:47 +0900 | [diff] [blame] | 72 | import org.osgi.service.component.ComponentContext; |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 73 | import org.slf4j.Logger; |
| 74 | |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 75 | import java.util.Collections; |
sangho | 0248ca2 | 2017-05-31 13:22:47 +0900 | [diff] [blame] | 76 | import java.util.Dictionary; |
sangho | 2e97be0 | 2017-07-03 18:18:27 +0900 | [diff] [blame] | 77 | import java.util.Map; |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 78 | import java.util.Objects; |
| 79 | import java.util.Set; |
| 80 | import java.util.concurrent.ExecutorService; |
| 81 | import java.util.stream.Collectors; |
| 82 | |
| 83 | import static java.util.concurrent.Executors.newSingleThreadExecutor; |
| 84 | import static org.onlab.util.Tools.groupedThreads; |
sangho | dc37537 | 2017-06-08 10:41:30 +0900 | [diff] [blame] | 85 | import static org.onosproject.openstacknetworking.api.Constants.ACL_TABLE; |
sangho | 1aaa788 | 2017-05-31 13:22:47 +0900 | [diff] [blame] | 86 | import static org.onosproject.openstacknetworking.api.Constants.CT_TABLE; |
| 87 | import static org.onosproject.openstacknetworking.api.Constants.ERROR_TABLE; |
Jian Li | 9d35bd6 | 2018-10-13 01:43:24 +0900 | [diff] [blame] | 88 | import static org.onosproject.openstacknetworking.api.Constants.JUMP_TABLE; |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 89 | import static org.onosproject.openstacknetworking.api.Constants.OPENSTACK_NETWORKING_APP_ID; |
| 90 | import static org.onosproject.openstacknetworking.api.Constants.PRIORITY_ACL_RULE; |
sangho | 1aaa788 | 2017-05-31 13:22:47 +0900 | [diff] [blame] | 91 | import static org.onosproject.openstacknetworking.api.Constants.PRIORITY_CT_DROP_RULE; |
| 92 | import static org.onosproject.openstacknetworking.api.Constants.PRIORITY_CT_HOOK_RULE; |
| 93 | import static org.onosproject.openstacknetworking.api.Constants.PRIORITY_CT_RULE; |
Jian Li | 9d35bd6 | 2018-10-13 01:43:24 +0900 | [diff] [blame] | 94 | import static org.onosproject.openstacknetworking.api.InstancePort.State.REMOVE_PENDING; |
| 95 | import static org.onosproject.openstacknetworking.api.OpenstackNetworkEvent.Type.OPENSTACK_PORT_PRE_REMOVE; |
Jian Li | bcc4228 | 2018-09-13 20:59:34 +0900 | [diff] [blame] | 96 | import static org.onosproject.openstacknetworking.util.RulePopulatorUtil.computeCtMaskFlag; |
| 97 | import static org.onosproject.openstacknetworking.util.RulePopulatorUtil.computeCtStateFlag; |
| 98 | import static org.onosproject.openstacknetworking.util.RulePopulatorUtil.niciraConnTrackTreatmentBuilder; |
sangho | 1aaa788 | 2017-05-31 13:22:47 +0900 | [diff] [blame] | 99 | import static org.onosproject.openstacknode.api.OpenstackNode.NodeType.COMPUTE; |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 100 | import static org.slf4j.LoggerFactory.getLogger; |
| 101 | |
| 102 | /** |
| 103 | * Populates flow rules to handle OpenStack SecurityGroups. |
| 104 | */ |
| 105 | @Component(immediate = true) |
| 106 | public class OpenstackSecurityGroupHandler { |
| 107 | |
| 108 | private final Logger log = getLogger(getClass()); |
| 109 | |
sangho | 0248ca2 | 2017-05-31 13:22:47 +0900 | [diff] [blame] | 110 | private static final boolean USE_SECURITY_GROUP = false; |
| 111 | |
| 112 | @Property(name = "useSecurityGroup", boolValue = USE_SECURITY_GROUP, |
| 113 | label = "Apply OpenStack security group rule for VM traffic") |
| 114 | private boolean useSecurityGroup = USE_SECURITY_GROUP; |
| 115 | |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 116 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 117 | protected CoreService coreService; |
| 118 | |
| 119 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Jian Li | 9d35bd6 | 2018-10-13 01:43:24 +0900 | [diff] [blame] | 120 | protected InstancePortAdminService instancePortService; |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 121 | |
| 122 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 123 | protected MastershipService mastershipService; |
| 124 | |
| 125 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Hyunsun Moon | ae51e73 | 2017-04-25 17:46:21 +0900 | [diff] [blame] | 126 | protected OpenstackNetworkService osNetService; |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 127 | |
| 128 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 129 | protected OpenstackSecurityGroupService securityGroupService; |
| 130 | |
| 131 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
sangho | dc37537 | 2017-06-08 10:41:30 +0900 | [diff] [blame] | 132 | protected OpenstackFlowRuleService osFlowRuleService; |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 133 | |
sangho | 0248ca2 | 2017-05-31 13:22:47 +0900 | [diff] [blame] | 134 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 135 | protected ComponentConfigService configService; |
| 136 | |
sangho | 3dd2a8b | 2017-07-19 15:54:31 +0900 | [diff] [blame] | 137 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 138 | protected OpenstackNodeService osNodeService; |
| 139 | |
sangho | e6457a3 | 2017-08-24 14:31:19 +0900 | [diff] [blame] | 140 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
sangho | 1aaa788 | 2017-05-31 13:22:47 +0900 | [diff] [blame] | 141 | protected DriverService driverService; |
| 142 | |
| 143 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 144 | protected LeadershipService leadershipService; |
| 145 | |
| 146 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 147 | protected ClusterService clusterService; |
| 148 | |
Jian Li | 9d35bd6 | 2018-10-13 01:43:24 +0900 | [diff] [blame] | 149 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 150 | protected PreCommitPortService preCommitPortService; |
| 151 | |
Jian Li | bcc4228 | 2018-09-13 20:59:34 +0900 | [diff] [blame] | 152 | private final InstancePortListener instancePortListener = |
| 153 | new InternalInstancePortListener(); |
| 154 | private final OpenstackNetworkListener osNetworkListener = |
| 155 | new InternalOpenstackNetworkListener(); |
| 156 | private final OpenstackNetworkListener osPortListener = |
| 157 | new InternalOpenstackPortListener(); |
| 158 | private final OpenstackSecurityGroupListener securityGroupListener = |
| 159 | new InternalSecurityGroupListener(); |
sangho | 1aaa788 | 2017-05-31 13:22:47 +0900 | [diff] [blame] | 160 | private final OpenstackNodeListener osNodeListener = new InternalNodeListener(); |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 161 | private ApplicationId appId; |
sangho | 1aaa788 | 2017-05-31 13:22:47 +0900 | [diff] [blame] | 162 | private NodeId localNodeId; |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 163 | |
| 164 | private final ExecutorService eventExecutor = newSingleThreadExecutor( |
| 165 | groupedThreads(this.getClass().getSimpleName(), "event-handler")); |
| 166 | |
| 167 | private static final String PROTO_ICMP = "ICMP"; |
| 168 | private static final String PROTO_TCP = "TCP"; |
| 169 | private static final String PROTO_UDP = "UDP"; |
| 170 | private static final String ETHTYPE_IPV4 = "IPV4"; |
| 171 | private static final String EGRESS = "EGRESS"; |
| 172 | private static final String INGRESS = "INGRESS"; |
| 173 | private static final IpPrefix IP_PREFIX_ANY = Ip4Prefix.valueOf("0.0.0.0/0"); |
| 174 | |
Jian Li | e8b28db | 2018-10-17 14:04:09 +0900 | [diff] [blame^] | 175 | private static final String VXLAN = "VXLAN"; |
| 176 | private static final String VLAN = "VLAN"; |
| 177 | |
sangho | 1aaa788 | 2017-05-31 13:22:47 +0900 | [diff] [blame] | 178 | // We expose pipeline structure to SONA application considering removing pipeline soon. |
sanghoshin | bbeb31a | 2018-09-11 17:01:01 +0800 | [diff] [blame] | 179 | private static final int GOTO_CONNTRACK_TABLE = CT_TABLE; |
| 180 | private static final int GOTO_JUMP_TABLE = JUMP_TABLE; |
sangho | 1aaa788 | 2017-05-31 13:22:47 +0900 | [diff] [blame] | 181 | |
| 182 | private static final int CT_COMMIT = 0; |
| 183 | private static final int CT_NO_COMMIT = 1; |
| 184 | private static final short CT_NO_RECIRC = -1; |
| 185 | |
| 186 | private static final int ACTION_NONE = 0; |
| 187 | private static final int ACTION_DROP = -1; |
| 188 | |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 189 | @Activate |
| 190 | protected void activate() { |
| 191 | appId = coreService.registerApplication(OPENSTACK_NETWORKING_APP_ID); |
sangho | 1aaa788 | 2017-05-31 13:22:47 +0900 | [diff] [blame] | 192 | localNodeId = clusterService.getLocalNode().id(); |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 193 | instancePortService.addListener(instancePortListener); |
| 194 | securityGroupService.addListener(securityGroupListener); |
Jian Li | bcc4228 | 2018-09-13 20:59:34 +0900 | [diff] [blame] | 195 | osNetService.addListener(osPortListener); |
| 196 | osNetService.addListener(osNetworkListener); |
sangho | 0248ca2 | 2017-05-31 13:22:47 +0900 | [diff] [blame] | 197 | configService.registerProperties(getClass()); |
sangho | 1aaa788 | 2017-05-31 13:22:47 +0900 | [diff] [blame] | 198 | osNodeService.addListener(osNodeListener); |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 199 | |
| 200 | log.info("Started"); |
| 201 | } |
| 202 | |
| 203 | @Deactivate |
| 204 | protected void deactivate() { |
| 205 | instancePortService.removeListener(instancePortListener); |
| 206 | securityGroupService.removeListener(securityGroupListener); |
Jian Li | bcc4228 | 2018-09-13 20:59:34 +0900 | [diff] [blame] | 207 | osNetService.removeListener(osNetworkListener); |
| 208 | osNetService.removeListener(osPortListener); |
sangho | 0248ca2 | 2017-05-31 13:22:47 +0900 | [diff] [blame] | 209 | configService.unregisterProperties(getClass(), false); |
sangho | 1aaa788 | 2017-05-31 13:22:47 +0900 | [diff] [blame] | 210 | osNodeService.removeListener(osNodeListener); |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 211 | eventExecutor.shutdown(); |
| 212 | |
| 213 | log.info("Stopped"); |
| 214 | } |
| 215 | |
sangho | 0248ca2 | 2017-05-31 13:22:47 +0900 | [diff] [blame] | 216 | @Modified |
| 217 | protected void modified(ComponentContext context) { |
| 218 | Dictionary<?, ?> properties = context.getProperties(); |
| 219 | Boolean flag; |
| 220 | |
| 221 | flag = Tools.isPropertyEnabled(properties, "useSecurityGroup"); |
| 222 | if (flag == null) { |
| 223 | log.info("useSecurityGroup is not configured, " + |
| 224 | "using current value of {}", useSecurityGroup); |
| 225 | } else { |
| 226 | useSecurityGroup = flag; |
| 227 | log.info("Configured. useSecurityGroup is {}", |
| 228 | useSecurityGroup ? "enabled" : "disabled"); |
| 229 | } |
| 230 | |
sangho | e6457a3 | 2017-08-24 14:31:19 +0900 | [diff] [blame] | 231 | securityGroupService.setSecurityGroupEnabled(useSecurityGroup); |
sangho | 0248ca2 | 2017-05-31 13:22:47 +0900 | [diff] [blame] | 232 | resetSecurityGroupRules(); |
| 233 | } |
| 234 | |
sangho | 1aaa788 | 2017-05-31 13:22:47 +0900 | [diff] [blame] | 235 | private void initializeConnTrackTable(DeviceId deviceId, boolean install) { |
| 236 | |
| 237 | //table=1,ip,ct_state=-trk, actions=ct(table:2) |
Jian Li | bcc4228 | 2018-09-13 20:59:34 +0900 | [diff] [blame] | 238 | long ctState = computeCtStateFlag(false, false, false); |
| 239 | long ctMask = computeCtMaskFlag(true, false, false); |
sangho | 1aaa788 | 2017-05-31 13:22:47 +0900 | [diff] [blame] | 240 | setConnTrackRule(deviceId, ctState, ctMask, CT_NO_COMMIT, (short) GOTO_CONNTRACK_TABLE, |
| 241 | ACTION_NONE, PRIORITY_CT_HOOK_RULE, install); |
| 242 | |
| 243 | //table=2,ip,nw_dst=10.10.0.2,ct_state=+trk+est,action=goto_table:3 |
Jian Li | bcc4228 | 2018-09-13 20:59:34 +0900 | [diff] [blame] | 244 | ctState = computeCtStateFlag(true, false, true); |
| 245 | ctMask = computeCtMaskFlag(true, false, true); |
sangho | 1aaa788 | 2017-05-31 13:22:47 +0900 | [diff] [blame] | 246 | setConnTrackRule(deviceId, ctState, ctMask, CT_NO_COMMIT, CT_NO_RECIRC, |
| 247 | GOTO_JUMP_TABLE, PRIORITY_CT_RULE, install); |
| 248 | |
| 249 | //table=2,ip,nw_dst=10.10.0.2,ct_state=+trk+new,action=drop |
Jian Li | bcc4228 | 2018-09-13 20:59:34 +0900 | [diff] [blame] | 250 | ctState = computeCtStateFlag(true, true, false); |
| 251 | ctMask = computeCtMaskFlag(true, true, false); |
sangho | 1aaa788 | 2017-05-31 13:22:47 +0900 | [diff] [blame] | 252 | setConnTrackRule(deviceId, ctState, ctMask, CT_NO_COMMIT, CT_NO_RECIRC, |
| 253 | ACTION_DROP, PRIORITY_CT_DROP_RULE, install); |
| 254 | } |
| 255 | |
Jian Li | bcc4228 | 2018-09-13 20:59:34 +0900 | [diff] [blame] | 256 | private void setSecurityGroupRules(InstancePort instPort, |
| 257 | Port port, boolean install) { |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 258 | port.getSecurityGroups().forEach(sgId -> { |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 259 | SecurityGroup sg = securityGroupService.securityGroup(sgId); |
| 260 | if (sg == null) { |
| 261 | log.error("Security Group Not Found : {}", sgId); |
| 262 | return; |
| 263 | } |
Jian Li | bcc4228 | 2018-09-13 20:59:34 +0900 | [diff] [blame] | 264 | sg.getRules().forEach(sgRule -> |
| 265 | updateSecurityGroupRule(instPort, port, sgRule, install)); |
Hyunsun Moon | ae51e73 | 2017-04-25 17:46:21 +0900 | [diff] [blame] | 266 | final String action = install ? "Installed " : "Removed "; |
| 267 | log.debug(action + "security group rule ID : " + sgId); |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 268 | }); |
Jian Li | 9d35bd6 | 2018-10-13 01:43:24 +0900 | [diff] [blame] | 269 | |
| 270 | if (install) { |
| 271 | preCommitPortService.subscribePreCommit(instPort.portId(), |
| 272 | OPENSTACK_PORT_PRE_REMOVE, this.getClass().getName()); |
| 273 | log.info("Subscribed the port {} on listening pre-remove event", instPort.portId()); |
| 274 | } else { |
| 275 | preCommitPortService.unsubscribePreCommit(instPort.portId(), |
| 276 | OPENSTACK_PORT_PRE_REMOVE, instancePortService, this.getClass().getName()); |
| 277 | log.info("Unsubscribed the port {} on listening pre-remove event", instPort.portId()); |
| 278 | } |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 279 | } |
| 280 | |
Jian Li | bcc4228 | 2018-09-13 20:59:34 +0900 | [diff] [blame] | 281 | private void updateSecurityGroupRule(InstancePort instPort, Port port, |
| 282 | SecurityGroupRule sgRule, boolean install) { |
sangho | 2e97be0 | 2017-07-03 18:18:27 +0900 | [diff] [blame] | 283 | |
Daniel Park | 3a14059 | 2018-06-28 18:33:10 +0900 | [diff] [blame] | 284 | if (instPort == null || port == null || sgRule == null) { |
| 285 | return; |
| 286 | } |
| 287 | |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 288 | if (sgRule.getRemoteGroupId() != null && !sgRule.getRemoteGroupId().isEmpty()) { |
| 289 | getRemoteInstPorts(port.getTenantId(), sgRule.getRemoteGroupId()) |
Jian Li | bcc4228 | 2018-09-13 20:59:34 +0900 | [diff] [blame] | 290 | .forEach(rInstPort -> { |
Jian Li | e8b28db | 2018-10-17 14:04:09 +0900 | [diff] [blame^] | 291 | populateSecurityGroupRule(sgRule, instPort, port, |
Jian Li | bcc4228 | 2018-09-13 20:59:34 +0900 | [diff] [blame] | 292 | rInstPort.ipAddress().toIpPrefix(), install); |
Jian Li | e8b28db | 2018-10-17 14:04:09 +0900 | [diff] [blame^] | 293 | populateSecurityGroupRule(sgRule, rInstPort, port, |
Jian Li | bcc4228 | 2018-09-13 20:59:34 +0900 | [diff] [blame] | 294 | instPort.ipAddress().toIpPrefix(), install); |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 295 | |
Jian Li | bcc4228 | 2018-09-13 20:59:34 +0900 | [diff] [blame] | 296 | SecurityGroupRule rSgRule = |
| 297 | new NeutronSecurityGroupRule |
| 298 | .SecurityGroupRuleConcreteBuilder() |
| 299 | .from(sgRule) |
| 300 | .direction(sgRule.getDirection().toUpperCase() |
| 301 | .equals(EGRESS) ? INGRESS : EGRESS) |
| 302 | .build(); |
Jian Li | e8b28db | 2018-10-17 14:04:09 +0900 | [diff] [blame^] | 303 | populateSecurityGroupRule(rSgRule, instPort, port, |
Jian Li | bcc4228 | 2018-09-13 20:59:34 +0900 | [diff] [blame] | 304 | rInstPort.ipAddress().toIpPrefix(), install); |
Jian Li | e8b28db | 2018-10-17 14:04:09 +0900 | [diff] [blame^] | 305 | populateSecurityGroupRule(rSgRule, rInstPort, port, |
Jian Li | bcc4228 | 2018-09-13 20:59:34 +0900 | [diff] [blame] | 306 | instPort.ipAddress().toIpPrefix(), install); |
| 307 | }); |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 308 | } else { |
Jian Li | e8b28db | 2018-10-17 14:04:09 +0900 | [diff] [blame^] | 309 | populateSecurityGroupRule(sgRule, instPort, port, |
Jian Li | bcc4228 | 2018-09-13 20:59:34 +0900 | [diff] [blame] | 310 | sgRule.getRemoteIpPrefix() == null ? IP_PREFIX_ANY : |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 311 | IpPrefix.valueOf(sgRule.getRemoteIpPrefix()), install); |
| 312 | } |
| 313 | } |
| 314 | |
Jian Li | bcc4228 | 2018-09-13 20:59:34 +0900 | [diff] [blame] | 315 | private void populateSecurityGroupRule(SecurityGroupRule sgRule, |
| 316 | InstancePort instPort, |
Jian Li | e8b28db | 2018-10-17 14:04:09 +0900 | [diff] [blame^] | 317 | Port port, |
| 318 | IpPrefix remoteIp, |
| 319 | boolean install) { |
sangho | 2e97be0 | 2017-07-03 18:18:27 +0900 | [diff] [blame] | 320 | Set<TrafficSelector> selectors = buildSelectors(sgRule, |
Jian Li | e8b28db | 2018-10-17 14:04:09 +0900 | [diff] [blame^] | 321 | Ip4Address.valueOf(instPort.ipAddress().toInetAddress()), remoteIp, port); |
sangho | 2e97be0 | 2017-07-03 18:18:27 +0900 | [diff] [blame] | 322 | if (selectors == null || selectors.isEmpty()) { |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 323 | return; |
| 324 | } |
| 325 | |
sangho | 2e97be0 | 2017-07-03 18:18:27 +0900 | [diff] [blame] | 326 | selectors.forEach(selector -> { |
| 327 | osFlowRuleService.setRule(appId, |
| 328 | instPort.deviceId(), |
| 329 | selector, |
sangho | 3dd2a8b | 2017-07-19 15:54:31 +0900 | [diff] [blame] | 330 | DefaultTrafficTreatment.builder().transition(JUMP_TABLE).build(), |
sangho | 2e97be0 | 2017-07-03 18:18:27 +0900 | [diff] [blame] | 331 | PRIORITY_ACL_RULE, |
| 332 | ACL_TABLE, |
| 333 | install); |
| 334 | }); |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | /** |
sangho | 1aaa788 | 2017-05-31 13:22:47 +0900 | [diff] [blame] | 338 | * Sets connection tracking rule using OVS extension commands. |
| 339 | * It is not so graceful, but I don't want to make it more general because it is going to be used |
Ray Milkey | c108a6b | 2017-08-23 15:23:50 -0700 | [diff] [blame] | 340 | * only here. The following is the usage of the function. |
sangho | 1aaa788 | 2017-05-31 13:22:47 +0900 | [diff] [blame] | 341 | * |
| 342 | * @param deviceId Device ID |
| 343 | * @param ctState ctState: please use RulePopulatorUtil.computeCtStateFlag() to build the value |
| 344 | * @param ctMask crMask: please use RulePopulatorUtil.computeCtMaskFlag() to build the value |
| 345 | * @param commit CT_COMMIT for commit action, CT_NO_COMMIT otherwise |
| 346 | * @param recircTable table number for recirculation after CT actions. CT_NO_RECIRC with no recirculation |
| 347 | * @param action Additional actions. ACTION_DROP, ACTION_NONE, GOTO_XXX_TABLE are supported. |
| 348 | * @param priority priority value for the rule |
| 349 | * @param install true for insertion, false for removal |
| 350 | */ |
| 351 | private void setConnTrackRule(DeviceId deviceId, long ctState, long ctMask, |
| 352 | int commit, short recircTable, |
| 353 | int action, int priority, boolean install) { |
| 354 | |
Jian Li | bcc4228 | 2018-09-13 20:59:34 +0900 | [diff] [blame] | 355 | ExtensionSelector esCtSate = RulePopulatorUtil |
| 356 | .buildCtExtensionSelector(driverService, deviceId, ctState, ctMask); |
sangho | 1aaa788 | 2017-05-31 13:22:47 +0900 | [diff] [blame] | 357 | TrafficSelector selector = DefaultTrafficSelector.builder() |
| 358 | .extension(esCtSate, deviceId) |
| 359 | .matchEthType(Ethernet.TYPE_IPV4) |
| 360 | .build(); |
| 361 | |
| 362 | TrafficTreatment.Builder tb = DefaultTrafficTreatment.builder(); |
| 363 | |
| 364 | if (commit == CT_COMMIT || recircTable > 0) { |
| 365 | RulePopulatorUtil.NiriraConnTrackTreatmentBuilder natTreatmentBuilder = |
Jian Li | bcc4228 | 2018-09-13 20:59:34 +0900 | [diff] [blame] | 366 | niciraConnTrackTreatmentBuilder(driverService, deviceId); |
sangho | 1aaa788 | 2017-05-31 13:22:47 +0900 | [diff] [blame] | 367 | natTreatmentBuilder.natAction(false); |
| 368 | if (commit == CT_COMMIT) { |
| 369 | natTreatmentBuilder.commit(true); |
| 370 | } else { |
| 371 | natTreatmentBuilder.commit(false); |
| 372 | } |
| 373 | if (recircTable > 0) { |
| 374 | natTreatmentBuilder.table(recircTable); |
| 375 | } |
| 376 | tb.extension(natTreatmentBuilder.build(), deviceId); |
| 377 | } else if (action == ACTION_DROP) { |
| 378 | tb.drop(); |
| 379 | } |
| 380 | |
sanghoshin | bbeb31a | 2018-09-11 17:01:01 +0800 | [diff] [blame] | 381 | if (action != ACTION_NONE && action != ACTION_DROP) { |
sangho | 1aaa788 | 2017-05-31 13:22:47 +0900 | [diff] [blame] | 382 | tb.transition(action); |
| 383 | } |
| 384 | |
| 385 | int tableType = ERROR_TABLE; |
| 386 | if (priority == PRIORITY_CT_RULE || priority == PRIORITY_CT_DROP_RULE) { |
| 387 | tableType = CT_TABLE; |
| 388 | } else if (priority == PRIORITY_CT_HOOK_RULE) { |
| 389 | tableType = ACL_TABLE; |
| 390 | } else { |
| 391 | log.error("Cannot an appropriate table for the conn track rule."); |
| 392 | } |
| 393 | |
| 394 | osFlowRuleService.setRule( |
| 395 | appId, |
| 396 | deviceId, |
| 397 | selector, |
| 398 | tb.build(), |
| 399 | priority, |
| 400 | tableType, |
| 401 | install); |
| 402 | } |
| 403 | |
| 404 | /** |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 405 | * Returns a set of host IP addresses engaged with supplied security group ID. |
| 406 | * It only searches a VM in the same tenant boundary. |
| 407 | * |
| 408 | * @param tenantId tenant id |
| 409 | * @param sgId security group id |
| 410 | * @return set of ip addresses |
| 411 | */ |
| 412 | private Set<InstancePort> getRemoteInstPorts(String tenantId, String sgId) { |
| 413 | Set<InstancePort> remoteInstPorts; |
| 414 | |
Hyunsun Moon | ae51e73 | 2017-04-25 17:46:21 +0900 | [diff] [blame] | 415 | remoteInstPorts = osNetService.ports().stream() |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 416 | .filter(port -> port.getTenantId().equals(tenantId)) |
| 417 | .filter(port -> port.getSecurityGroups().contains(sgId)) |
| 418 | .map(port -> instancePortService.instancePort(port.getId())) |
| 419 | .filter(instPort -> instPort != null && instPort.ipAddress() != null) |
| 420 | .collect(Collectors.toSet()); |
| 421 | |
| 422 | return Collections.unmodifiableSet(remoteInstPorts); |
| 423 | } |
| 424 | |
sangho | 2e97be0 | 2017-07-03 18:18:27 +0900 | [diff] [blame] | 425 | private Set<TrafficSelector> buildSelectors(SecurityGroupRule sgRule, |
| 426 | Ip4Address vmIp, |
Jian Li | e8b28db | 2018-10-17 14:04:09 +0900 | [diff] [blame^] | 427 | IpPrefix remoteIp, |
| 428 | Port port) { |
sangho | 2e97be0 | 2017-07-03 18:18:27 +0900 | [diff] [blame] | 429 | if (remoteIp != null && remoteIp.equals(IpPrefix.valueOf(vmIp, 32))) { |
| 430 | // do nothing if the remote IP is my IP |
| 431 | return null; |
| 432 | } |
| 433 | |
| 434 | Set<TrafficSelector> selectorSet = Sets.newHashSet(); |
| 435 | |
| 436 | TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder(); |
Jian Li | e8b28db | 2018-10-17 14:04:09 +0900 | [diff] [blame^] | 437 | buildMatches(sBuilder, sgRule, vmIp, remoteIp, port); |
sangho | 2e97be0 | 2017-07-03 18:18:27 +0900 | [diff] [blame] | 438 | |
| 439 | if (sgRule.getPortRangeMax() != null && sgRule.getPortRangeMin() != null && |
| 440 | sgRule.getPortRangeMin() < sgRule.getPortRangeMax()) { |
Jian Li | bcc4228 | 2018-09-13 20:59:34 +0900 | [diff] [blame] | 441 | Map<TpPort, TpPort> portRangeMatchMap = |
| 442 | buildPortRangeMatches(sgRule.getPortRangeMin(), |
sangho | 2e97be0 | 2017-07-03 18:18:27 +0900 | [diff] [blame] | 443 | sgRule.getPortRangeMax()); |
| 444 | portRangeMatchMap.entrySet().forEach(entry -> { |
| 445 | |
Jian Li | bcc4228 | 2018-09-13 20:59:34 +0900 | [diff] [blame] | 446 | if (sgRule.getProtocol().toUpperCase().equals(PROTO_TCP)) { |
| 447 | if (sgRule.getDirection().toUpperCase().equals(EGRESS)) { |
| 448 | sBuilder.matchTcpSrcMasked(entry.getKey(), entry.getValue()); |
| 449 | } else { |
| 450 | sBuilder.matchTcpDstMasked(entry.getKey(), entry.getValue()); |
| 451 | } |
| 452 | } else if (sgRule.getProtocol().toUpperCase().equals(PROTO_UDP)) { |
| 453 | if (sgRule.getDirection().toUpperCase().equals(EGRESS)) { |
| 454 | sBuilder.matchUdpSrcMasked(entry.getKey(), entry.getValue()); |
| 455 | } else { |
| 456 | sBuilder.matchUdpDstMasked(entry.getKey(), entry.getValue()); |
| 457 | } |
| 458 | } |
sangho | 2e97be0 | 2017-07-03 18:18:27 +0900 | [diff] [blame] | 459 | |
Jian Li | bcc4228 | 2018-09-13 20:59:34 +0900 | [diff] [blame] | 460 | selectorSet.add(sBuilder.build()); |
| 461 | } |
sangho | 2e97be0 | 2017-07-03 18:18:27 +0900 | [diff] [blame] | 462 | ); |
| 463 | } else { |
| 464 | selectorSet.add(sBuilder.build()); |
| 465 | } |
| 466 | |
| 467 | return selectorSet; |
| 468 | } |
| 469 | |
Jian Li | bcc4228 | 2018-09-13 20:59:34 +0900 | [diff] [blame] | 470 | private void buildMatches(TrafficSelector.Builder sBuilder, |
| 471 | SecurityGroupRule sgRule, |
Jian Li | e8b28db | 2018-10-17 14:04:09 +0900 | [diff] [blame^] | 472 | Ip4Address vmIp, IpPrefix remoteIp, Port port) { |
| 473 | buildTunnelId(sBuilder, port); |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 474 | buildMatchEthType(sBuilder, sgRule.getEtherType()); |
| 475 | buildMatchDirection(sBuilder, sgRule.getDirection(), vmIp); |
| 476 | buildMatchProto(sBuilder, sgRule.getProtocol()); |
| 477 | buildMatchPort(sBuilder, sgRule.getProtocol(), sgRule.getDirection(), |
sangho | 2e97be0 | 2017-07-03 18:18:27 +0900 | [diff] [blame] | 478 | sgRule.getPortRangeMin() == null ? 0 : sgRule.getPortRangeMin(), |
| 479 | sgRule.getPortRangeMax() == null ? 0 : sgRule.getPortRangeMax()); |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 480 | buildMatchRemoteIp(sBuilder, remoteIp, sgRule.getDirection()); |
| 481 | if (sgRule.getRemoteGroupId() != null && sgRule.getRemoteGroupId().isEmpty()) { |
| 482 | buildMatchRemoteIp(sBuilder, remoteIp, sgRule.getDirection()); |
| 483 | } |
| 484 | } |
| 485 | |
Jian Li | e8b28db | 2018-10-17 14:04:09 +0900 | [diff] [blame^] | 486 | private void buildTunnelId(TrafficSelector.Builder sBuilder, Port port) { |
| 487 | String segId = osNetService.segmentId(port.getNetworkId()); |
| 488 | String netType = osNetService.networkType(port.getNetworkId()); |
| 489 | |
| 490 | if (VLAN.equals(netType)) { |
| 491 | sBuilder.matchVlanId(VlanId.vlanId(segId)); |
| 492 | } else if (VXLAN.equals(netType)) { |
| 493 | sBuilder.matchTunnelId(Long.valueOf(segId)); |
| 494 | } |
| 495 | } |
| 496 | |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 497 | private void buildMatchDirection(TrafficSelector.Builder sBuilder, |
| 498 | String direction, |
| 499 | Ip4Address vmIp) { |
| 500 | if (direction.toUpperCase().equals(EGRESS)) { |
| 501 | sBuilder.matchIPSrc(IpPrefix.valueOf(vmIp, 32)); |
| 502 | } else { |
| 503 | sBuilder.matchIPDst(IpPrefix.valueOf(vmIp, 32)); |
| 504 | } |
| 505 | } |
| 506 | |
| 507 | private void buildMatchEthType(TrafficSelector.Builder sBuilder, String etherType) { |
| 508 | // Either IpSrc or IpDst (or both) is set by default, and we need to set EthType as IPv4. |
| 509 | sBuilder.matchEthType(Ethernet.TYPE_IPV4); |
| 510 | if (etherType != null && !Objects.equals(etherType, "null") && |
| 511 | !etherType.toUpperCase().equals(ETHTYPE_IPV4)) { |
| 512 | log.debug("EthType {} is not supported yet in Security Group", etherType); |
| 513 | } |
| 514 | } |
| 515 | |
Jian Li | bcc4228 | 2018-09-13 20:59:34 +0900 | [diff] [blame] | 516 | private void buildMatchRemoteIp(TrafficSelector.Builder sBuilder, |
| 517 | IpPrefix remoteIpPrefix, String direction) { |
| 518 | if (remoteIpPrefix != null && |
| 519 | !remoteIpPrefix.getIp4Prefix().equals(IP_PREFIX_ANY)) { |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 520 | if (direction.toUpperCase().equals(EGRESS)) { |
| 521 | sBuilder.matchIPDst(remoteIpPrefix); |
| 522 | } else { |
| 523 | sBuilder.matchIPSrc(remoteIpPrefix); |
| 524 | } |
| 525 | } |
| 526 | } |
| 527 | |
| 528 | private void buildMatchProto(TrafficSelector.Builder sBuilder, String protocol) { |
| 529 | if (protocol != null) { |
| 530 | switch (protocol.toUpperCase()) { |
| 531 | case PROTO_ICMP: |
| 532 | sBuilder.matchIPProtocol(IPv4.PROTOCOL_ICMP); |
| 533 | break; |
| 534 | case PROTO_TCP: |
| 535 | sBuilder.matchIPProtocol(IPv4.PROTOCOL_TCP); |
| 536 | break; |
| 537 | case PROTO_UDP: |
| 538 | sBuilder.matchIPProtocol(IPv4.PROTOCOL_UDP); |
| 539 | break; |
| 540 | default: |
| 541 | } |
| 542 | } |
| 543 | } |
| 544 | |
Jian Li | bcc4228 | 2018-09-13 20:59:34 +0900 | [diff] [blame] | 545 | private void buildMatchPort(TrafficSelector.Builder sBuilder, |
| 546 | String protocol, String direction, |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 547 | int portMin, int portMax) { |
| 548 | if (portMin > 0 && portMax > 0 && portMin == portMax) { |
| 549 | if (protocol.toUpperCase().equals(PROTO_TCP)) { |
| 550 | if (direction.toUpperCase().equals(EGRESS)) { |
| 551 | sBuilder.matchTcpSrc(TpPort.tpPort(portMax)); |
| 552 | } else { |
| 553 | sBuilder.matchTcpDst(TpPort.tpPort(portMax)); |
| 554 | } |
| 555 | } else if (protocol.toUpperCase().equals(PROTO_UDP)) { |
| 556 | if (direction.toUpperCase().equals(EGRESS)) { |
| 557 | sBuilder.matchUdpSrc(TpPort.tpPort(portMax)); |
| 558 | } else { |
| 559 | sBuilder.matchUdpDst(TpPort.tpPort(portMax)); |
| 560 | } |
| 561 | } |
| 562 | } |
| 563 | } |
| 564 | |
sangho | 0248ca2 | 2017-05-31 13:22:47 +0900 | [diff] [blame] | 565 | private void resetSecurityGroupRules() { |
| 566 | |
| 567 | if (useSecurityGroup) { |
sangho | 3dd2a8b | 2017-07-19 15:54:31 +0900 | [diff] [blame] | 568 | osNodeService.completeNodes(OpenstackNode.NodeType.COMPUTE) |
Jian Li | bcc4228 | 2018-09-13 20:59:34 +0900 | [diff] [blame] | 569 | .forEach(node -> osFlowRuleService |
| 570 | .setUpTableMissEntry(node.intgBridge(), ACL_TABLE)); |
sangho | 0248ca2 | 2017-05-31 13:22:47 +0900 | [diff] [blame] | 571 | securityGroupService.securityGroups().forEach(securityGroup -> |
| 572 | securityGroup.getRules().forEach(this::securityGroupRuleAdded)); |
sangho | 1aaa788 | 2017-05-31 13:22:47 +0900 | [diff] [blame] | 573 | osNodeService.nodes().stream() |
| 574 | .filter(node -> node.type().equals(OpenstackNode.NodeType.COMPUTE)) |
| 575 | .forEach(node -> initializeConnTrackTable(node .intgBridge(), true)); |
sangho | 0248ca2 | 2017-05-31 13:22:47 +0900 | [diff] [blame] | 576 | } else { |
sangho | 3dd2a8b | 2017-07-19 15:54:31 +0900 | [diff] [blame] | 577 | osNodeService.completeNodes(OpenstackNode.NodeType.COMPUTE) |
Jian Li | bcc4228 | 2018-09-13 20:59:34 +0900 | [diff] [blame] | 578 | .forEach(node -> osFlowRuleService |
| 579 | .connectTables(node.intgBridge(), ACL_TABLE, JUMP_TABLE)); |
sangho | 0248ca2 | 2017-05-31 13:22:47 +0900 | [diff] [blame] | 580 | securityGroupService.securityGroups().forEach(securityGroup -> |
| 581 | securityGroup.getRules().forEach(this::securityGroupRuleRemoved)); |
sangho | 1aaa788 | 2017-05-31 13:22:47 +0900 | [diff] [blame] | 582 | osNodeService.nodes().stream() |
| 583 | .filter(node -> node.type().equals(OpenstackNode.NodeType.COMPUTE)) |
| 584 | .forEach(node -> initializeConnTrackTable(node.intgBridge(), false)); |
sangho | 0248ca2 | 2017-05-31 13:22:47 +0900 | [diff] [blame] | 585 | } |
| 586 | |
Jian Li | bcc4228 | 2018-09-13 20:59:34 +0900 | [diff] [blame] | 587 | log.info("Reset security group info " + |
| 588 | (useSecurityGroup ? " with " : " without") + " Security Group"); |
sangho | 0248ca2 | 2017-05-31 13:22:47 +0900 | [diff] [blame] | 589 | } |
| 590 | |
| 591 | private void securityGroupRuleAdded(SecurityGroupRule sgRule) { |
| 592 | osNetService.ports().stream() |
Jian Li | bcc4228 | 2018-09-13 20:59:34 +0900 | [diff] [blame] | 593 | .filter(port -> port.getSecurityGroups() |
| 594 | .contains(sgRule.getSecurityGroupId())) |
sangho | 0248ca2 | 2017-05-31 13:22:47 +0900 | [diff] [blame] | 595 | .forEach(port -> { |
| 596 | updateSecurityGroupRule( |
| 597 | instancePortService.instancePort(port.getId()), |
| 598 | port, sgRule, true); |
| 599 | log.debug("Applied security group rule {} to port {}", |
| 600 | sgRule.getId(), port.getId()); |
| 601 | }); |
| 602 | } |
| 603 | |
| 604 | private void securityGroupRuleRemoved(SecurityGroupRule sgRule) { |
| 605 | osNetService.ports().stream() |
Jian Li | bcc4228 | 2018-09-13 20:59:34 +0900 | [diff] [blame] | 606 | .filter(port -> port.getSecurityGroups() |
| 607 | .contains(sgRule.getSecurityGroupId())) |
sangho | 0248ca2 | 2017-05-31 13:22:47 +0900 | [diff] [blame] | 608 | .forEach(port -> { |
| 609 | updateSecurityGroupRule( |
| 610 | instancePortService.instancePort(port.getId()), |
| 611 | port, sgRule, false); |
| 612 | log.debug("Removed security group rule {} from port {}", |
| 613 | sgRule.getId(), port.getId()); |
| 614 | }); |
| 615 | } |
| 616 | |
sangho | 2e97be0 | 2017-07-03 18:18:27 +0900 | [diff] [blame] | 617 | private int binLower(String binStr, int bits) { |
| 618 | String outBin = binStr.substring(0, 16 - bits); |
| 619 | for (int i = 0; i < bits; i++) { |
| 620 | outBin += "0"; |
| 621 | } |
| 622 | |
| 623 | return Integer.parseInt(outBin, 2); |
| 624 | } |
| 625 | |
| 626 | private int binHigher(String binStr, int bits) { |
| 627 | String outBin = binStr.substring(0, 16 - bits); |
| 628 | for (int i = 0; i < bits; i++) { |
| 629 | outBin += "1"; |
| 630 | } |
| 631 | |
| 632 | return Integer.parseInt(outBin, 2); |
| 633 | } |
| 634 | |
| 635 | private int testMasks(String binStr, int start, int end) { |
| 636 | int mask = 0; |
| 637 | for (; mask <= 16; mask++) { |
| 638 | int maskStart = binLower(binStr, mask); |
| 639 | int maskEnd = binHigher(binStr, mask); |
| 640 | if (maskStart < start || maskEnd > end) { |
| 641 | return mask - 1; |
| 642 | } |
| 643 | } |
| 644 | |
| 645 | return mask; |
| 646 | } |
| 647 | |
| 648 | private String getMask(int bits) { |
| 649 | switch (bits) { |
| 650 | case 0: return "ffff"; |
| 651 | case 1: return "fffe"; |
| 652 | case 2: return "fffc"; |
| 653 | case 3: return "fff8"; |
| 654 | case 4: return "fff0"; |
| 655 | case 5: return "ffe0"; |
| 656 | case 6: return "ffc0"; |
| 657 | case 7: return "ff80"; |
| 658 | case 8: return "ff00"; |
| 659 | case 9: return "fe00"; |
| 660 | case 10: return "fc00"; |
| 661 | case 11: return "f800"; |
| 662 | case 12: return "f000"; |
| 663 | case 13: return "e000"; |
| 664 | case 14: return "c000"; |
| 665 | case 15: return "8000"; |
| 666 | case 16: return "0000"; |
| 667 | default: return null; |
| 668 | } |
| 669 | } |
| 670 | |
| 671 | private Map<TpPort, TpPort> buildPortRangeMatches(int portMin, int portMax) { |
| 672 | |
| 673 | boolean processing = true; |
| 674 | int start = portMin; |
| 675 | Map<TpPort, TpPort> portMaskMap = Maps.newHashMap(); |
| 676 | while (processing) { |
| 677 | String minStr = Integer.toBinaryString(start); |
| 678 | String binStrMinPadded = "0000000000000000".substring(minStr.length()) + minStr; |
| 679 | |
| 680 | int mask = testMasks(binStrMinPadded, start, portMax); |
| 681 | int maskStart = binLower(binStrMinPadded, mask); |
| 682 | int maskEnd = binHigher(binStrMinPadded, mask); |
| 683 | |
| 684 | log.debug("start : {} port/mask = {} / {} ", start, getMask(mask), maskStart); |
Jian Li | bcc4228 | 2018-09-13 20:59:34 +0900 | [diff] [blame] | 685 | portMaskMap.put(TpPort.tpPort(maskStart), TpPort.tpPort( |
| 686 | Integer.parseInt(Objects.requireNonNull(getMask(mask)), 16))); |
sangho | 2e97be0 | 2017-07-03 18:18:27 +0900 | [diff] [blame] | 687 | |
| 688 | start = maskEnd + 1; |
| 689 | if (start > portMax) { |
| 690 | processing = false; |
| 691 | } |
| 692 | } |
| 693 | |
| 694 | return portMaskMap; |
| 695 | } |
| 696 | |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 697 | private class InternalInstancePortListener implements InstancePortListener { |
| 698 | |
| 699 | @Override |
| 700 | public boolean isRelevant(InstancePortEvent event) { |
| 701 | InstancePort instPort = event.subject(); |
sangho | 0248ca2 | 2017-05-31 13:22:47 +0900 | [diff] [blame] | 702 | if (!useSecurityGroup) { |
| 703 | return false; |
| 704 | } |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 705 | return mastershipService.isLocalMaster(instPort.deviceId()); |
| 706 | } |
| 707 | |
| 708 | @Override |
| 709 | public void event(InstancePortEvent event) { |
| 710 | InstancePort instPort = event.subject(); |
| 711 | switch (event.type()) { |
Jian Li | ec5c32b | 2018-07-13 14:28:58 +0900 | [diff] [blame] | 712 | case OPENSTACK_INSTANCE_PORT_UPDATED: |
Jian Li | e8b28db | 2018-10-17 14:04:09 +0900 | [diff] [blame^] | 713 | if (instPort.state() == REMOVE_PENDING) { |
| 714 | break; |
| 715 | } |
| 716 | case OPENSTACK_INSTANCE_PORT_DETECTED: |
Hyunsun Moon | ae51e73 | 2017-04-25 17:46:21 +0900 | [diff] [blame] | 717 | log.debug("Instance port detected MAC:{} IP:{}", |
| 718 | instPort.macAddress(), |
| 719 | instPort.ipAddress()); |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 720 | eventExecutor.execute(() -> { |
Hyunsun Moon | ae51e73 | 2017-04-25 17:46:21 +0900 | [diff] [blame] | 721 | setSecurityGroupRules(instPort, |
| 722 | osNetService.port(event.subject().portId()), |
| 723 | true); |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 724 | }); |
| 725 | break; |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 726 | default: |
| 727 | break; |
| 728 | } |
| 729 | } |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 730 | } |
| 731 | |
| 732 | private class InternalOpenstackPortListener implements OpenstackNetworkListener { |
| 733 | |
| 734 | @Override |
| 735 | public boolean isRelevant(OpenstackNetworkEvent event) { |
sanghoshin | bbeb31a | 2018-09-11 17:01:01 +0800 | [diff] [blame] | 736 | if (event.port() == null || Strings.isNullOrEmpty(event.port().getId())) { |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 737 | return false; |
| 738 | } |
Jian Li | bcc4228 | 2018-09-13 20:59:34 +0900 | [diff] [blame] | 739 | |
| 740 | InstancePort instPort = instancePortService.instancePort(event.port().getId()); |
| 741 | |
| 742 | if (instPort == null) { |
| 743 | return false; |
| 744 | } |
| 745 | |
| 746 | return useSecurityGroup && mastershipService.isLocalMaster(instPort.deviceId()); |
| 747 | } |
| 748 | |
| 749 | @Override |
| 750 | public void event(OpenstackNetworkEvent event) { |
| 751 | log.debug("openstack port event received {}", event); |
| 752 | Port osPort = event.port(); |
| 753 | InstancePort instPort = instancePortService.instancePort(osPort.getId()); |
| 754 | |
| 755 | switch (event.type()) { |
| 756 | case OPENSTACK_PORT_PRE_REMOVE: |
Jian Li | 9d35bd6 | 2018-10-13 01:43:24 +0900 | [diff] [blame] | 757 | instancePortService.updateInstancePort( |
| 758 | instPort.updateState(REMOVE_PENDING)); |
| 759 | eventExecutor.execute(() -> |
| 760 | setSecurityGroupRules(instPort, osPort, false) |
| 761 | ); |
Jian Li | bcc4228 | 2018-09-13 20:59:34 +0900 | [diff] [blame] | 762 | break; |
| 763 | default: |
| 764 | // do nothing for the other events |
| 765 | break; |
| 766 | } |
| 767 | } |
| 768 | } |
| 769 | |
| 770 | private class InternalOpenstackNetworkListener implements OpenstackNetworkListener { |
| 771 | |
| 772 | @Override |
| 773 | public boolean isRelevant(OpenstackNetworkEvent event) { |
| 774 | if (event.port() == null || Strings.isNullOrEmpty(event.port().getId())) { |
| 775 | return false; |
| 776 | } |
Hyunsun Moon | ae51e73 | 2017-04-25 17:46:21 +0900 | [diff] [blame] | 777 | if (event.securityGroupId() == null || |
| 778 | securityGroupService.securityGroup(event.securityGroupId()) == null) { |
| 779 | return false; |
| 780 | } |
| 781 | if (instancePortService.instancePort(event.port().getId()) == null) { |
| 782 | return false; |
| 783 | } |
Jian Li | bcc4228 | 2018-09-13 20:59:34 +0900 | [diff] [blame] | 784 | return useSecurityGroup; |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 785 | } |
| 786 | |
| 787 | @Override |
| 788 | public void event(OpenstackNetworkEvent event) { |
sanghoshin | bbeb31a | 2018-09-11 17:01:01 +0800 | [diff] [blame] | 789 | log.debug("security group event received {}", event); |
Hyunsun Moon | ae51e73 | 2017-04-25 17:46:21 +0900 | [diff] [blame] | 790 | Port osPort = event.port(); |
| 791 | InstancePort instPort = instancePortService.instancePort(osPort.getId()); |
| 792 | SecurityGroup osSg = securityGroupService.securityGroup(event.securityGroupId()); |
| 793 | |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 794 | switch (event.type()) { |
Hyunsun Moon | ae51e73 | 2017-04-25 17:46:21 +0900 | [diff] [blame] | 795 | case OPENSTACK_PORT_SECURITY_GROUP_ADDED: |
| 796 | eventExecutor.execute(() -> { |
| 797 | osSg.getRules().forEach(sgRule -> { |
| 798 | updateSecurityGroupRule(instPort, osPort, sgRule, true); |
| 799 | }); |
| 800 | log.info("Added security group {} to port {}", |
| 801 | event.securityGroupId(), event.port().getId()); |
| 802 | }); |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 803 | break; |
Hyunsun Moon | ae51e73 | 2017-04-25 17:46:21 +0900 | [diff] [blame] | 804 | case OPENSTACK_PORT_SECURITY_GROUP_REMOVED: |
| 805 | eventExecutor.execute(() -> { |
| 806 | osSg.getRules().forEach(sgRule -> { |
| 807 | updateSecurityGroupRule(instPort, osPort, sgRule, false); |
| 808 | }); |
| 809 | log.info("Removed security group {} from port {}", |
| 810 | event.securityGroupId(), event.port().getId()); |
| 811 | }); |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 812 | break; |
| 813 | default: |
Hyunsun Moon | ae51e73 | 2017-04-25 17:46:21 +0900 | [diff] [blame] | 814 | // do nothing for the other events |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 815 | break; |
| 816 | } |
| 817 | } |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 818 | } |
| 819 | |
| 820 | private class InternalSecurityGroupListener implements OpenstackSecurityGroupListener { |
| 821 | |
| 822 | @Override |
sangho | 0248ca2 | 2017-05-31 13:22:47 +0900 | [diff] [blame] | 823 | public boolean isRelevant(OpenstackSecurityGroupEvent event) { |
Jian Li | bcc4228 | 2018-09-13 20:59:34 +0900 | [diff] [blame] | 824 | return useSecurityGroup; |
sangho | 0248ca2 | 2017-05-31 13:22:47 +0900 | [diff] [blame] | 825 | } |
| 826 | |
| 827 | @Override |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 828 | public void event(OpenstackSecurityGroupEvent event) { |
| 829 | switch (event.type()) { |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 830 | case OPENSTACK_SECURITY_GROUP_RULE_CREATED: |
| 831 | SecurityGroupRule securityGroupRuleToAdd = event.securityGroupRule(); |
| 832 | eventExecutor.execute(() -> { |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 833 | securityGroupRuleAdded(securityGroupRuleToAdd); |
Hyunsun Moon | ae51e73 | 2017-04-25 17:46:21 +0900 | [diff] [blame] | 834 | log.info("Applied new security group rule {} to ports", |
| 835 | securityGroupRuleToAdd.getId()); |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 836 | }); |
| 837 | break; |
| 838 | |
| 839 | case OPENSTACK_SECURITY_GROUP_RULE_REMOVED: |
| 840 | SecurityGroupRule securityGroupRuleToRemove = event.securityGroupRule(); |
| 841 | eventExecutor.execute(() -> { |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 842 | securityGroupRuleRemoved(securityGroupRuleToRemove); |
Hyunsun Moon | ae51e73 | 2017-04-25 17:46:21 +0900 | [diff] [blame] | 843 | log.info("Removed security group rule {} from ports", |
| 844 | securityGroupRuleToRemove.getId()); |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 845 | }); |
| 846 | break; |
Hyunsun Moon | ae51e73 | 2017-04-25 17:46:21 +0900 | [diff] [blame] | 847 | case OPENSTACK_SECURITY_GROUP_REMOVED: |
Jian Li | 9d35bd6 | 2018-10-13 01:43:24 +0900 | [diff] [blame] | 848 | case OPENSTACK_SECURITY_GROUP_CREATED: |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 849 | default: |
Hyunsun Moon | ae51e73 | 2017-04-25 17:46:21 +0900 | [diff] [blame] | 850 | // do nothing |
| 851 | break; |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 852 | } |
| 853 | } |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 854 | } |
sangho | 1aaa788 | 2017-05-31 13:22:47 +0900 | [diff] [blame] | 855 | |
| 856 | private class InternalNodeListener implements OpenstackNodeListener { |
| 857 | |
| 858 | @Override |
| 859 | public boolean isRelevant(OpenstackNodeEvent event) { |
| 860 | // do not allow to proceed without leadership |
| 861 | NodeId leader = leadershipService.getLeader(appId.name()); |
| 862 | if (!Objects.equals(localNodeId, leader)) { |
| 863 | return false; |
| 864 | } |
| 865 | return event.subject().type() == COMPUTE; |
| 866 | } |
| 867 | |
| 868 | @Override |
| 869 | public void event(OpenstackNodeEvent event) { |
| 870 | OpenstackNode osNode = event.subject(); |
| 871 | |
| 872 | switch (event.type()) { |
| 873 | case OPENSTACK_NODE_COMPLETE: |
| 874 | eventExecutor.execute(() -> { |
| 875 | try { |
| 876 | if (useSecurityGroup) { |
| 877 | initializeConnTrackTable(osNode.intgBridge(), true); |
Jian Li | 0488c73 | 2018-09-14 20:53:07 +0900 | [diff] [blame] | 878 | log.info("SG table initialization : {} is done", |
Jian Li | bcc4228 | 2018-09-13 20:59:34 +0900 | [diff] [blame] | 879 | osNode.intgBridge()); |
sangho | 1aaa788 | 2017-05-31 13:22:47 +0900 | [diff] [blame] | 880 | } |
| 881 | } catch (IllegalArgumentException e) { |
Jian Li | bcc4228 | 2018-09-13 20:59:34 +0900 | [diff] [blame] | 882 | log.error("ACL table initialization error : {}", |
| 883 | e.getMessage()); |
sangho | 1aaa788 | 2017-05-31 13:22:47 +0900 | [diff] [blame] | 884 | } |
| 885 | }); |
| 886 | break; |
| 887 | case OPENSTACK_NODE_CREATED: |
| 888 | case OPENSTACK_NODE_REMOVED: |
| 889 | case OPENSTACK_NODE_UPDATED: |
| 890 | case OPENSTACK_NODE_INCOMPLETE: |
| 891 | default: |
| 892 | break; |
| 893 | } |
| 894 | } |
| 895 | } |
Jian Li | bcc4228 | 2018-09-13 20:59:34 +0900 | [diff] [blame] | 896 | } |