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