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.Sets; |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 21 | import org.onlab.packet.Ethernet; |
| 22 | import org.onlab.packet.IPv4; |
| 23 | import org.onlab.packet.Ip4Address; |
| 24 | import org.onlab.packet.Ip4Prefix; |
| 25 | import org.onlab.packet.IpPrefix; |
| 26 | import org.onlab.packet.TpPort; |
Jian Li | e8b28db | 2018-10-17 14:04:09 +0900 | [diff] [blame] | 27 | import org.onlab.packet.VlanId; |
Jian Li | ac30e27 | 2018-10-18 23:08:03 +0900 | [diff] [blame] | 28 | import org.onlab.util.KryoNamespace; |
sangho | 0248ca2 | 2017-05-31 13:22:47 +0900 | [diff] [blame] | 29 | import org.onlab.util.Tools; |
| 30 | import org.onosproject.cfg.ComponentConfigService; |
Jian Li | 627e016 | 2020-03-12 17:50:36 +0900 | [diff] [blame] | 31 | import org.onosproject.cfg.ConfigProperty; |
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; |
Jian Li | 857f1b0 | 2019-09-20 16:58:19 +0900 | [diff] [blame] | 39 | import org.onosproject.net.device.DeviceService; |
sangho | 1aaa788 | 2017-05-31 13:22:47 +0900 | [diff] [blame] | 40 | import org.onosproject.net.driver.DriverService; |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 41 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 42 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 43 | import org.onosproject.net.flow.TrafficSelector; |
sangho | 1aaa788 | 2017-05-31 13:22:47 +0900 | [diff] [blame] | 44 | import org.onosproject.net.flow.TrafficTreatment; |
| 45 | import org.onosproject.net.flow.criteria.ExtensionSelector; |
Jian Li | 28ec77f | 2018-10-31 07:07:25 +0900 | [diff] [blame] | 46 | import org.onosproject.net.flow.instructions.ExtensionTreatment; |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 47 | import org.onosproject.openstacknetworking.api.InstancePort; |
Jian Li | 9d35bd6 | 2018-10-13 01:43:24 +0900 | [diff] [blame] | 48 | import org.onosproject.openstacknetworking.api.InstancePortAdminService; |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 49 | import org.onosproject.openstacknetworking.api.InstancePortEvent; |
| 50 | import org.onosproject.openstacknetworking.api.InstancePortListener; |
sangho | dc37537 | 2017-06-08 10:41:30 +0900 | [diff] [blame] | 51 | import org.onosproject.openstacknetworking.api.OpenstackFlowRuleService; |
SONA Project | 6bc5c4a | 2018-12-14 23:49:52 +0900 | [diff] [blame] | 52 | import org.onosproject.openstacknetworking.api.OpenstackNetwork.Type; |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 53 | import org.onosproject.openstacknetworking.api.OpenstackNetworkEvent; |
| 54 | import org.onosproject.openstacknetworking.api.OpenstackNetworkListener; |
| 55 | import org.onosproject.openstacknetworking.api.OpenstackNetworkService; |
| 56 | import org.onosproject.openstacknetworking.api.OpenstackSecurityGroupEvent; |
| 57 | import org.onosproject.openstacknetworking.api.OpenstackSecurityGroupListener; |
| 58 | import org.onosproject.openstacknetworking.api.OpenstackSecurityGroupService; |
Jian Li | 2694976 | 2018-03-30 15:46:37 +0900 | [diff] [blame] | 59 | import org.onosproject.openstacknetworking.util.RulePopulatorUtil; |
Jian Li | 627e016 | 2020-03-12 17:50:36 +0900 | [diff] [blame] | 60 | import org.onosproject.openstacknode.api.OpenstackNode; |
sangho | 1aaa788 | 2017-05-31 13:22:47 +0900 | [diff] [blame] | 61 | import org.onosproject.openstacknode.api.OpenstackNodeEvent; |
| 62 | import org.onosproject.openstacknode.api.OpenstackNodeListener; |
sangho | 3dd2a8b | 2017-07-19 15:54:31 +0900 | [diff] [blame] | 63 | import org.onosproject.openstacknode.api.OpenstackNodeService; |
Jian Li | ac30e27 | 2018-10-18 23:08:03 +0900 | [diff] [blame] | 64 | import org.onosproject.store.serializers.KryoNamespaces; |
| 65 | import org.onosproject.store.service.ConsistentMap; |
| 66 | import org.onosproject.store.service.Serializer; |
| 67 | import org.onosproject.store.service.StorageService; |
Jian Li | 1e9cb73 | 2018-11-25 23:17:21 +0900 | [diff] [blame] | 68 | import org.openstack4j.model.network.Network; |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 69 | import org.openstack4j.model.network.Port; |
| 70 | import org.openstack4j.model.network.SecurityGroup; |
| 71 | import org.openstack4j.model.network.SecurityGroupRule; |
Jian Li | ac30e27 | 2018-10-18 23:08:03 +0900 | [diff] [blame] | 72 | import org.openstack4j.model.network.State; |
| 73 | import org.openstack4j.openstack.networking.domain.NeutronAllowedAddressPair; |
| 74 | import org.openstack4j.openstack.networking.domain.NeutronExtraDhcpOptCreate; |
| 75 | import org.openstack4j.openstack.networking.domain.NeutronIP; |
| 76 | import org.openstack4j.openstack.networking.domain.NeutronPort; |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 77 | import org.openstack4j.openstack.networking.domain.NeutronSecurityGroupRule; |
sangho | 0248ca2 | 2017-05-31 13:22:47 +0900 | [diff] [blame] | 78 | import org.osgi.service.component.ComponentContext; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 79 | import org.osgi.service.component.annotations.Activate; |
| 80 | import org.osgi.service.component.annotations.Component; |
| 81 | import org.osgi.service.component.annotations.Deactivate; |
| 82 | import org.osgi.service.component.annotations.Modified; |
| 83 | import org.osgi.service.component.annotations.Reference; |
| 84 | import org.osgi.service.component.annotations.ReferenceCardinality; |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 85 | import org.slf4j.Logger; |
| 86 | |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 87 | import java.util.Collections; |
sangho | 0248ca2 | 2017-05-31 13:22:47 +0900 | [diff] [blame] | 88 | import java.util.Dictionary; |
Jian Li | ac30e27 | 2018-10-18 23:08:03 +0900 | [diff] [blame] | 89 | import java.util.HashSet; |
| 90 | import java.util.LinkedHashMap; |
Jian Li | 362f9fd | 2018-11-28 11:14:40 +0900 | [diff] [blame] | 91 | import java.util.List; |
sangho | 2e97be0 | 2017-07-03 18:18:27 +0900 | [diff] [blame] | 92 | import java.util.Map; |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 93 | import java.util.Objects; |
| 94 | import java.util.Set; |
| 95 | import java.util.concurrent.ExecutorService; |
| 96 | import java.util.stream.Collectors; |
| 97 | |
| 98 | import static java.util.concurrent.Executors.newSingleThreadExecutor; |
| 99 | import static org.onlab.util.Tools.groupedThreads; |
Jian Li | 1e9cb73 | 2018-11-25 23:17:21 +0900 | [diff] [blame] | 100 | import static org.onosproject.openstacknetworking.api.Constants.ACL_EGRESS_TABLE; |
| 101 | import static org.onosproject.openstacknetworking.api.Constants.ACL_INGRESS_TABLE; |
| 102 | import static org.onosproject.openstacknetworking.api.Constants.ACL_RECIRC_TABLE; |
sangho | 1aaa788 | 2017-05-31 13:22:47 +0900 | [diff] [blame] | 103 | import static org.onosproject.openstacknetworking.api.Constants.CT_TABLE; |
| 104 | import static org.onosproject.openstacknetworking.api.Constants.ERROR_TABLE; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 105 | import static org.onosproject.openstacknetworking.api.Constants.JUMP_TABLE; |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 106 | import static org.onosproject.openstacknetworking.api.Constants.OPENSTACK_NETWORKING_APP_ID; |
Jian Li | 1e9cb73 | 2018-11-25 23:17:21 +0900 | [diff] [blame] | 107 | import static org.onosproject.openstacknetworking.api.Constants.PRIORITY_ACL_INGRESS_RULE; |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 108 | import static org.onosproject.openstacknetworking.api.Constants.PRIORITY_ACL_RULE; |
sangho | 1aaa788 | 2017-05-31 13:22:47 +0900 | [diff] [blame] | 109 | import static org.onosproject.openstacknetworking.api.Constants.PRIORITY_CT_DROP_RULE; |
| 110 | import static org.onosproject.openstacknetworking.api.Constants.PRIORITY_CT_HOOK_RULE; |
| 111 | import static org.onosproject.openstacknetworking.api.Constants.PRIORITY_CT_RULE; |
Jian Li | 621f73c | 2018-12-15 01:49:22 +0900 | [diff] [blame] | 112 | import static org.onosproject.openstacknetworking.api.OpenstackNetwork.Type.GENEVE; |
SONA Project | 6bc5c4a | 2018-12-14 23:49:52 +0900 | [diff] [blame] | 113 | import static org.onosproject.openstacknetworking.api.OpenstackNetwork.Type.GRE; |
| 114 | import static org.onosproject.openstacknetworking.api.OpenstackNetwork.Type.VLAN; |
| 115 | import static org.onosproject.openstacknetworking.api.OpenstackNetwork.Type.VXLAN; |
Jian Li | 4d13870 | 2018-11-27 17:25:28 +0900 | [diff] [blame] | 116 | import static org.onosproject.openstacknetworking.api.OpenstackNetworkEvent.Type.OPENSTACK_PORT_PRE_REMOVE; |
Ray Milkey | 8e40651 | 2018-10-24 15:56:50 -0700 | [diff] [blame] | 117 | import static org.onosproject.openstacknetworking.impl.OsgiPropertyConstants.USE_SECURITY_GROUP; |
| 118 | import static org.onosproject.openstacknetworking.impl.OsgiPropertyConstants.USE_SECURITY_GROUP_DEFAULT; |
Jian Li | 0f6ef9c | 2020-07-08 21:37:26 +0900 | [diff] [blame] | 119 | import static org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil.getPropertyValueAsBoolean; |
Jian Li | b8cdcc1 | 2018-10-23 01:53:10 +0900 | [diff] [blame] | 120 | import static org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil.swapStaleLocation; |
Jian Li | 6d2ffbf | 2020-11-04 15:58:18 +0900 | [diff] [blame^] | 121 | import static org.onosproject.openstacknetworking.util.RulePopulatorUtil.buildPortRangeMatches; |
Jian Li | bcc4228 | 2018-09-13 20:59:34 +0900 | [diff] [blame] | 122 | import static org.onosproject.openstacknetworking.util.RulePopulatorUtil.computeCtMaskFlag; |
| 123 | import static org.onosproject.openstacknetworking.util.RulePopulatorUtil.computeCtStateFlag; |
| 124 | import static org.onosproject.openstacknetworking.util.RulePopulatorUtil.niciraConnTrackTreatmentBuilder; |
sangho | 1aaa788 | 2017-05-31 13:22:47 +0900 | [diff] [blame] | 125 | import static org.onosproject.openstacknode.api.OpenstackNode.NodeType.COMPUTE; |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 126 | import static org.slf4j.LoggerFactory.getLogger; |
| 127 | |
| 128 | /** |
| 129 | * Populates flow rules to handle OpenStack SecurityGroups. |
| 130 | */ |
Ray Milkey | 8e40651 | 2018-10-24 15:56:50 -0700 | [diff] [blame] | 131 | @Component( |
| 132 | immediate = true, |
| 133 | property = { |
| 134 | USE_SECURITY_GROUP + ":Boolean=" + USE_SECURITY_GROUP_DEFAULT |
| 135 | } |
| 136 | ) |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 137 | public class OpenstackSecurityGroupHandler { |
| 138 | |
| 139 | private final Logger log = getLogger(getClass()); |
| 140 | |
Jian Li | 2b9838c | 2018-10-28 17:09:42 +0900 | [diff] [blame] | 141 | private static final int VM_IP_PREFIX = 32; |
| 142 | |
Jian Li | 4d13870 | 2018-11-27 17:25:28 +0900 | [diff] [blame] | 143 | private static final String STR_NULL = "null"; |
Jian Li | 4d13870 | 2018-11-27 17:25:28 +0900 | [diff] [blame] | 144 | |
Jian Li | 900b723 | 2018-12-14 14:04:51 +0900 | [diff] [blame] | 145 | /** Apply OpenStack security group rule for VM traffic. */ |
Ray Milkey | 8e40651 | 2018-10-24 15:56:50 -0700 | [diff] [blame] | 146 | private boolean useSecurityGroup = USE_SECURITY_GROUP_DEFAULT; |
sangho | 0248ca2 | 2017-05-31 13:22:47 +0900 | [diff] [blame] | 147 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 148 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 149 | protected CoreService coreService; |
| 150 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 151 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Jian Li | 9d35bd6 | 2018-10-13 01:43:24 +0900 | [diff] [blame] | 152 | protected InstancePortAdminService instancePortService; |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 153 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 154 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 155 | protected MastershipService mastershipService; |
| 156 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 157 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Hyunsun Moon | ae51e73 | 2017-04-25 17:46:21 +0900 | [diff] [blame] | 158 | protected OpenstackNetworkService osNetService; |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 159 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 160 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 161 | protected OpenstackSecurityGroupService securityGroupService; |
| 162 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 163 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
sangho | dc37537 | 2017-06-08 10:41:30 +0900 | [diff] [blame] | 164 | protected OpenstackFlowRuleService osFlowRuleService; |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 165 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 166 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
sangho | 0248ca2 | 2017-05-31 13:22:47 +0900 | [diff] [blame] | 167 | protected ComponentConfigService configService; |
| 168 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 169 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
sangho | 3dd2a8b | 2017-07-19 15:54:31 +0900 | [diff] [blame] | 170 | protected OpenstackNodeService osNodeService; |
| 171 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 172 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
sangho | 1aaa788 | 2017-05-31 13:22:47 +0900 | [diff] [blame] | 173 | protected DriverService driverService; |
| 174 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 175 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Jian Li | 857f1b0 | 2019-09-20 16:58:19 +0900 | [diff] [blame] | 176 | protected DeviceService deviceService; |
| 177 | |
| 178 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
sangho | 1aaa788 | 2017-05-31 13:22:47 +0900 | [diff] [blame] | 179 | protected LeadershipService leadershipService; |
| 180 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 181 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
sangho | 1aaa788 | 2017-05-31 13:22:47 +0900 | [diff] [blame] | 182 | protected ClusterService clusterService; |
| 183 | |
Ray Milkey | 0b18b72 | 2018-10-16 13:19:15 -0700 | [diff] [blame] | 184 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Jian Li | ac30e27 | 2018-10-18 23:08:03 +0900 | [diff] [blame] | 185 | protected StorageService storageService; |
| 186 | |
| 187 | private static final KryoNamespace SERIALIZER_PORT = KryoNamespace.newBuilder() |
| 188 | .register(KryoNamespaces.API) |
| 189 | .register(Port.class) |
| 190 | .register(NeutronPort.class) |
| 191 | .register(NeutronIP.class) |
| 192 | .register(State.class) |
| 193 | .register(NeutronAllowedAddressPair.class) |
| 194 | .register(NeutronExtraDhcpOptCreate.class) |
| 195 | .register(LinkedHashMap.class) |
| 196 | .build(); |
Jian Li | 9d35bd6 | 2018-10-13 01:43:24 +0900 | [diff] [blame] | 197 | |
Jian Li | bcc4228 | 2018-09-13 20:59:34 +0900 | [diff] [blame] | 198 | private final InstancePortListener instancePortListener = |
Jian Li | a70fdb60 | 2018-11-05 01:32:22 +0900 | [diff] [blame] | 199 | new InternalInstancePortListener(); |
Jian Li | bcc4228 | 2018-09-13 20:59:34 +0900 | [diff] [blame] | 200 | private final OpenstackNetworkListener osNetworkListener = |
Jian Li | a70fdb60 | 2018-11-05 01:32:22 +0900 | [diff] [blame] | 201 | new InternalOpenstackNetworkListener(); |
Jian Li | bcc4228 | 2018-09-13 20:59:34 +0900 | [diff] [blame] | 202 | private final OpenstackNetworkListener osPortListener = |
Jian Li | a70fdb60 | 2018-11-05 01:32:22 +0900 | [diff] [blame] | 203 | new InternalOpenstackPortListener(); |
Jian Li | bcc4228 | 2018-09-13 20:59:34 +0900 | [diff] [blame] | 204 | private final OpenstackSecurityGroupListener securityGroupListener = |
Jian Li | a70fdb60 | 2018-11-05 01:32:22 +0900 | [diff] [blame] | 205 | new InternalSecurityGroupListener(); |
sangho | 1aaa788 | 2017-05-31 13:22:47 +0900 | [diff] [blame] | 206 | private final OpenstackNodeListener osNodeListener = new InternalNodeListener(); |
Jian Li | ac30e27 | 2018-10-18 23:08:03 +0900 | [diff] [blame] | 207 | |
| 208 | private ConsistentMap<String, Port> removedOsPortStore; |
| 209 | |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 210 | private ApplicationId appId; |
sangho | 1aaa788 | 2017-05-31 13:22:47 +0900 | [diff] [blame] | 211 | private NodeId localNodeId; |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 212 | |
| 213 | private final ExecutorService eventExecutor = newSingleThreadExecutor( |
| 214 | groupedThreads(this.getClass().getSimpleName(), "event-handler")); |
| 215 | |
| 216 | private static final String PROTO_ICMP = "ICMP"; |
Jian Li | 0f6ef9c | 2020-07-08 21:37:26 +0900 | [diff] [blame] | 217 | private static final String PROTO_ICMP_NUM = "1"; |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 218 | private static final String PROTO_TCP = "TCP"; |
Jian Li | 0f6ef9c | 2020-07-08 21:37:26 +0900 | [diff] [blame] | 219 | private static final String PROTO_TCP_NUM = "6"; |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 220 | private static final String PROTO_UDP = "UDP"; |
Jian Li | 0f6ef9c | 2020-07-08 21:37:26 +0900 | [diff] [blame] | 221 | private static final String PROTO_UDP_NUM = "17"; |
| 222 | private static final String PROTO_SCTP = "SCTP"; |
| 223 | private static final String PROTO_SCTP_NUM = "132"; |
| 224 | private static final byte PROTOCOL_SCTP = (byte) 0x84; |
| 225 | private static final String PROTO_ANY = "ANY"; |
| 226 | private static final String PROTO_ANY_NUM = "0"; |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 227 | private static final String ETHTYPE_IPV4 = "IPV4"; |
| 228 | private static final String EGRESS = "EGRESS"; |
| 229 | private static final String INGRESS = "INGRESS"; |
| 230 | private static final IpPrefix IP_PREFIX_ANY = Ip4Prefix.valueOf("0.0.0.0/0"); |
| 231 | |
sangho | 1aaa788 | 2017-05-31 13:22:47 +0900 | [diff] [blame] | 232 | // We expose pipeline structure to SONA application considering removing pipeline soon. |
sanghoshin | bbeb31a | 2018-09-11 17:01:01 +0800 | [diff] [blame] | 233 | private static final int GOTO_CONNTRACK_TABLE = CT_TABLE; |
| 234 | private static final int GOTO_JUMP_TABLE = JUMP_TABLE; |
sangho | 1aaa788 | 2017-05-31 13:22:47 +0900 | [diff] [blame] | 235 | |
| 236 | private static final int CT_COMMIT = 0; |
| 237 | private static final int CT_NO_COMMIT = 1; |
| 238 | private static final short CT_NO_RECIRC = -1; |
| 239 | |
| 240 | private static final int ACTION_NONE = 0; |
| 241 | private static final int ACTION_DROP = -1; |
| 242 | |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 243 | @Activate |
| 244 | protected void activate() { |
| 245 | appId = coreService.registerApplication(OPENSTACK_NETWORKING_APP_ID); |
sangho | 1aaa788 | 2017-05-31 13:22:47 +0900 | [diff] [blame] | 246 | localNodeId = clusterService.getLocalNode().id(); |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 247 | instancePortService.addListener(instancePortListener); |
| 248 | securityGroupService.addListener(securityGroupListener); |
Jian Li | bcc4228 | 2018-09-13 20:59:34 +0900 | [diff] [blame] | 249 | osNetService.addListener(osPortListener); |
| 250 | osNetService.addListener(osNetworkListener); |
sangho | 0248ca2 | 2017-05-31 13:22:47 +0900 | [diff] [blame] | 251 | configService.registerProperties(getClass()); |
sangho | 1aaa788 | 2017-05-31 13:22:47 +0900 | [diff] [blame] | 252 | osNodeService.addListener(osNodeListener); |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 253 | |
Jian Li | ac30e27 | 2018-10-18 23:08:03 +0900 | [diff] [blame] | 254 | removedOsPortStore = storageService.<String, Port>consistentMapBuilder() |
| 255 | .withSerializer(Serializer.using(SERIALIZER_PORT)) |
| 256 | .withName("openstack-removed-portstore") |
| 257 | .withApplicationId(appId) |
| 258 | .build(); |
| 259 | |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 260 | log.info("Started"); |
| 261 | } |
| 262 | |
| 263 | @Deactivate |
| 264 | protected void deactivate() { |
| 265 | instancePortService.removeListener(instancePortListener); |
| 266 | securityGroupService.removeListener(securityGroupListener); |
Jian Li | bcc4228 | 2018-09-13 20:59:34 +0900 | [diff] [blame] | 267 | osNetService.removeListener(osNetworkListener); |
| 268 | osNetService.removeListener(osPortListener); |
sangho | 0248ca2 | 2017-05-31 13:22:47 +0900 | [diff] [blame] | 269 | configService.unregisterProperties(getClass(), false); |
sangho | 1aaa788 | 2017-05-31 13:22:47 +0900 | [diff] [blame] | 270 | osNodeService.removeListener(osNodeListener); |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 271 | eventExecutor.shutdown(); |
| 272 | |
| 273 | log.info("Stopped"); |
| 274 | } |
| 275 | |
sangho | 0248ca2 | 2017-05-31 13:22:47 +0900 | [diff] [blame] | 276 | @Modified |
| 277 | protected void modified(ComponentContext context) { |
| 278 | Dictionary<?, ?> properties = context.getProperties(); |
| 279 | Boolean flag; |
| 280 | |
Ray Milkey | 8e40651 | 2018-10-24 15:56:50 -0700 | [diff] [blame] | 281 | flag = Tools.isPropertyEnabled(properties, USE_SECURITY_GROUP); |
sangho | 0248ca2 | 2017-05-31 13:22:47 +0900 | [diff] [blame] | 282 | if (flag == null) { |
| 283 | log.info("useSecurityGroup is not configured, " + |
| 284 | "using current value of {}", useSecurityGroup); |
| 285 | } else { |
| 286 | useSecurityGroup = flag; |
| 287 | log.info("Configured. useSecurityGroup is {}", |
| 288 | useSecurityGroup ? "enabled" : "disabled"); |
| 289 | } |
| 290 | |
sangho | e6457a3 | 2017-08-24 14:31:19 +0900 | [diff] [blame] | 291 | securityGroupService.setSecurityGroupEnabled(useSecurityGroup); |
sangho | 0248ca2 | 2017-05-31 13:22:47 +0900 | [diff] [blame] | 292 | resetSecurityGroupRules(); |
| 293 | } |
| 294 | |
Jian Li | 627e016 | 2020-03-12 17:50:36 +0900 | [diff] [blame] | 295 | private boolean getUseSecurityGroupFlag() { |
| 296 | Set<ConfigProperty> properties = |
| 297 | configService.getProperties(getClass().getName()); |
| 298 | return getPropertyValueAsBoolean(properties, USE_SECURITY_GROUP); |
| 299 | } |
| 300 | |
sangho | 1aaa788 | 2017-05-31 13:22:47 +0900 | [diff] [blame] | 301 | private void initializeConnTrackTable(DeviceId deviceId, boolean install) { |
| 302 | |
| 303 | //table=1,ip,ct_state=-trk, actions=ct(table:2) |
Jian Li | bcc4228 | 2018-09-13 20:59:34 +0900 | [diff] [blame] | 304 | long ctState = computeCtStateFlag(false, false, false); |
| 305 | long ctMask = computeCtMaskFlag(true, false, false); |
sangho | 1aaa788 | 2017-05-31 13:22:47 +0900 | [diff] [blame] | 306 | setConnTrackRule(deviceId, ctState, ctMask, CT_NO_COMMIT, (short) GOTO_CONNTRACK_TABLE, |
| 307 | ACTION_NONE, PRIORITY_CT_HOOK_RULE, install); |
| 308 | |
| 309 | //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] | 310 | ctState = computeCtStateFlag(true, false, true); |
| 311 | ctMask = computeCtMaskFlag(true, false, true); |
sangho | 1aaa788 | 2017-05-31 13:22:47 +0900 | [diff] [blame] | 312 | setConnTrackRule(deviceId, ctState, ctMask, CT_NO_COMMIT, CT_NO_RECIRC, |
| 313 | GOTO_JUMP_TABLE, PRIORITY_CT_RULE, install); |
| 314 | |
| 315 | //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] | 316 | ctState = computeCtStateFlag(true, true, false); |
| 317 | ctMask = computeCtMaskFlag(true, true, false); |
sangho | 1aaa788 | 2017-05-31 13:22:47 +0900 | [diff] [blame] | 318 | setConnTrackRule(deviceId, ctState, ctMask, CT_NO_COMMIT, CT_NO_RECIRC, |
| 319 | ACTION_DROP, PRIORITY_CT_DROP_RULE, install); |
| 320 | } |
| 321 | |
Jian Li | 1e9cb73 | 2018-11-25 23:17:21 +0900 | [diff] [blame] | 322 | private void initializeAclTable(DeviceId deviceId, boolean install) { |
| 323 | |
| 324 | ExtensionTreatment ctTreatment = |
| 325 | niciraConnTrackTreatmentBuilder(driverService, deviceId) |
| 326 | .commit(true) |
| 327 | .build(); |
| 328 | |
| 329 | TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder(); |
| 330 | sBuilder.matchEthType(Ethernet.TYPE_IPV4); |
| 331 | |
| 332 | TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder(); |
| 333 | tBuilder.extension(ctTreatment, deviceId) |
| 334 | .transition(JUMP_TABLE); |
| 335 | |
| 336 | osFlowRuleService.setRule(appId, |
| 337 | deviceId, |
| 338 | sBuilder.build(), |
| 339 | tBuilder.build(), |
| 340 | PRIORITY_ACL_INGRESS_RULE, |
| 341 | ACL_RECIRC_TABLE, |
| 342 | install); |
| 343 | } |
| 344 | |
| 345 | private void initializeIngressTable(DeviceId deviceId, boolean install) { |
| 346 | if (install) { |
| 347 | osFlowRuleService.setUpTableMissEntry(deviceId, ACL_INGRESS_TABLE); |
| 348 | } else { |
| 349 | osFlowRuleService.connectTables(deviceId, ACL_INGRESS_TABLE, JUMP_TABLE); |
| 350 | } |
| 351 | } |
| 352 | |
Jian Li | bcc4228 | 2018-09-13 20:59:34 +0900 | [diff] [blame] | 353 | private void updateSecurityGroupRule(InstancePort instPort, Port port, |
| 354 | SecurityGroupRule sgRule, boolean install) { |
sangho | 2e97be0 | 2017-07-03 18:18:27 +0900 | [diff] [blame] | 355 | |
Daniel Park | 3a14059 | 2018-06-28 18:33:10 +0900 | [diff] [blame] | 356 | if (instPort == null || port == null || sgRule == null) { |
| 357 | return; |
| 358 | } |
| 359 | |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 360 | if (sgRule.getRemoteGroupId() != null && !sgRule.getRemoteGroupId().isEmpty()) { |
Jian Li | a70fdb60 | 2018-11-05 01:32:22 +0900 | [diff] [blame] | 361 | getRemoteInstPorts(port, sgRule.getRemoteGroupId(), install) |
Jian Li | bcc4228 | 2018-09-13 20:59:34 +0900 | [diff] [blame] | 362 | .forEach(rInstPort -> { |
Jian Li | 1e9cb73 | 2018-11-25 23:17:21 +0900 | [diff] [blame] | 363 | populateSecurityGroupRule(sgRule, instPort, |
Jian Li | bcc4228 | 2018-09-13 20:59:34 +0900 | [diff] [blame] | 364 | rInstPort.ipAddress().toIpPrefix(), install); |
Jian Li | 1e9cb73 | 2018-11-25 23:17:21 +0900 | [diff] [blame] | 365 | populateSecurityGroupRule(sgRule, rInstPort, |
Jian Li | bcc4228 | 2018-09-13 20:59:34 +0900 | [diff] [blame] | 366 | instPort.ipAddress().toIpPrefix(), install); |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 367 | |
Jian Li | bcc4228 | 2018-09-13 20:59:34 +0900 | [diff] [blame] | 368 | SecurityGroupRule rSgRule = |
| 369 | new NeutronSecurityGroupRule |
| 370 | .SecurityGroupRuleConcreteBuilder() |
Jian Li | 28ec77f | 2018-10-31 07:07:25 +0900 | [diff] [blame] | 371 | .from(sgRule) |
Jian Li | 4d13870 | 2018-11-27 17:25:28 +0900 | [diff] [blame] | 372 | .direction(sgRule.getDirection() |
| 373 | .equalsIgnoreCase(EGRESS) ? INGRESS : EGRESS) |
Jian Li | 28ec77f | 2018-10-31 07:07:25 +0900 | [diff] [blame] | 374 | .build(); |
Jian Li | 1e9cb73 | 2018-11-25 23:17:21 +0900 | [diff] [blame] | 375 | populateSecurityGroupRule(rSgRule, instPort, |
Jian Li | bcc4228 | 2018-09-13 20:59:34 +0900 | [diff] [blame] | 376 | rInstPort.ipAddress().toIpPrefix(), install); |
Jian Li | 1e9cb73 | 2018-11-25 23:17:21 +0900 | [diff] [blame] | 377 | populateSecurityGroupRule(rSgRule, rInstPort, |
Jian Li | bcc4228 | 2018-09-13 20:59:34 +0900 | [diff] [blame] | 378 | instPort.ipAddress().toIpPrefix(), install); |
| 379 | }); |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 380 | } else { |
Jian Li | 1e9cb73 | 2018-11-25 23:17:21 +0900 | [diff] [blame] | 381 | populateSecurityGroupRule(sgRule, instPort, |
Jian Li | bcc4228 | 2018-09-13 20:59:34 +0900 | [diff] [blame] | 382 | sgRule.getRemoteIpPrefix() == null ? IP_PREFIX_ANY : |
Jian Li | 28ec77f | 2018-10-31 07:07:25 +0900 | [diff] [blame] | 383 | IpPrefix.valueOf(sgRule.getRemoteIpPrefix()), install); |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 384 | } |
| 385 | } |
| 386 | |
Jian Li | 0f6ef9c | 2020-07-08 21:37:26 +0900 | [diff] [blame] | 387 | private boolean checkProtocol(String protocol) { |
| 388 | if (protocol == null) { |
| 389 | log.debug("No protocol was specified, use default IP(v4/v6) protocol."); |
| 390 | return true; |
| 391 | } else { |
| 392 | String protocolUpper = protocol.toUpperCase(); |
| 393 | if (protocolUpper.equals(PROTO_TCP) || |
| 394 | protocolUpper.equals(PROTO_UDP) || |
| 395 | protocolUpper.equals(PROTO_ICMP) || |
| 396 | protocolUpper.equals(PROTO_SCTP) || |
| 397 | protocolUpper.equals(PROTO_ANY) || |
| 398 | protocol.equals(PROTO_TCP_NUM) || |
| 399 | protocol.equals(PROTO_UDP_NUM) || |
| 400 | protocol.equals(PROTO_ICMP_NUM) || |
| 401 | protocol.equals(PROTO_SCTP_NUM) || |
| 402 | protocol.equals(PROTO_ANY_NUM)) { |
| 403 | return true; |
| 404 | } else { |
| 405 | log.error("Unsupported protocol {}, we only support " + |
| 406 | "TCP/UDP/ICMP/SCTP protocols.", protocol); |
| 407 | return false; |
| 408 | } |
| 409 | } |
| 410 | } |
| 411 | |
Jian Li | bcc4228 | 2018-09-13 20:59:34 +0900 | [diff] [blame] | 412 | private void populateSecurityGroupRule(SecurityGroupRule sgRule, |
| 413 | InstancePort instPort, |
Jian Li | e8b28db | 2018-10-17 14:04:09 +0900 | [diff] [blame] | 414 | IpPrefix remoteIp, |
| 415 | boolean install) { |
Jian Li | 0f6ef9c | 2020-07-08 21:37:26 +0900 | [diff] [blame] | 416 | if (!checkProtocol(sgRule.getProtocol())) { |
| 417 | return; |
| 418 | } |
| 419 | |
sangho | 2e97be0 | 2017-07-03 18:18:27 +0900 | [diff] [blame] | 420 | Set<TrafficSelector> selectors = buildSelectors(sgRule, |
Jian Li | 1e9cb73 | 2018-11-25 23:17:21 +0900 | [diff] [blame] | 421 | Ip4Address.valueOf(instPort.ipAddress().toInetAddress()), |
| 422 | remoteIp, instPort.networkId()); |
sangho | 2e97be0 | 2017-07-03 18:18:27 +0900 | [diff] [blame] | 423 | if (selectors == null || selectors.isEmpty()) { |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 424 | return; |
| 425 | } |
| 426 | |
Jian Li | 857f1b0 | 2019-09-20 16:58:19 +0900 | [diff] [blame] | 427 | // if the device is not available we do not perform any action |
| 428 | if (instPort.deviceId() == null || !deviceService.isAvailable(instPort.deviceId())) { |
| 429 | return; |
| 430 | } |
| 431 | |
Jian Li | 362f9fd | 2018-11-28 11:14:40 +0900 | [diff] [blame] | 432 | // in case a port is bound to multiple security groups, we do NOT remove |
| 433 | // egress rules unless all security groups bound to the port to be removed |
| 434 | Port osPort = osNetService.port(instPort.portId()); |
| 435 | if (!install && osPort != null && sgRule.getDirection().equalsIgnoreCase(EGRESS)) { |
| 436 | List<String> sgIds = osPort.getSecurityGroups(); |
| 437 | if (!sgIds.contains(sgRule.getSecurityGroupId()) && !sgIds.isEmpty()) { |
| 438 | return; |
| 439 | } |
| 440 | } |
| 441 | |
Jian Li | 28ec77f | 2018-10-31 07:07:25 +0900 | [diff] [blame] | 442 | // XXX All egress traffic needs to go through connection tracking module, |
| 443 | // which might hurt its performance. |
| 444 | ExtensionTreatment ctTreatment = |
| 445 | niciraConnTrackTreatmentBuilder(driverService, instPort.deviceId()) |
| 446 | .commit(true) |
| 447 | .build(); |
| 448 | |
Jian Li | 1e9cb73 | 2018-11-25 23:17:21 +0900 | [diff] [blame] | 449 | TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder(); |
Jian Li | 28ec77f | 2018-10-31 07:07:25 +0900 | [diff] [blame] | 450 | |
Jian Li | 1e9cb73 | 2018-11-25 23:17:21 +0900 | [diff] [blame] | 451 | int aclTable; |
Jian Li | 4d13870 | 2018-11-27 17:25:28 +0900 | [diff] [blame] | 452 | if (sgRule.getDirection().equalsIgnoreCase(EGRESS)) { |
Jian Li | 1e9cb73 | 2018-11-25 23:17:21 +0900 | [diff] [blame] | 453 | aclTable = ACL_EGRESS_TABLE; |
| 454 | tBuilder.transition(ACL_RECIRC_TABLE); |
| 455 | } else { |
| 456 | aclTable = ACL_INGRESS_TABLE; |
| 457 | tBuilder.extension(ctTreatment, instPort.deviceId()) |
| 458 | .transition(JUMP_TABLE); |
| 459 | } |
| 460 | |
| 461 | int finalAclTable = aclTable; |
| 462 | selectors.forEach(selector -> { |
| 463 | osFlowRuleService.setRule(appId, |
| 464 | instPort.deviceId(), |
| 465 | selector, tBuilder.build(), |
| 466 | PRIORITY_ACL_RULE, |
| 467 | finalAclTable, |
| 468 | install); |
| 469 | }); |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 470 | } |
| 471 | |
| 472 | /** |
sangho | 1aaa788 | 2017-05-31 13:22:47 +0900 | [diff] [blame] | 473 | * Sets connection tracking rule using OVS extension commands. |
Jian Li | 2b9838c | 2018-10-28 17:09:42 +0900 | [diff] [blame] | 474 | * It is not so graceful, but I don't want to make it more general because |
| 475 | * it is going to be used only here. |
| 476 | * The following is the usage of the function. |
sangho | 1aaa788 | 2017-05-31 13:22:47 +0900 | [diff] [blame] | 477 | * |
| 478 | * @param deviceId Device ID |
Jian Li | 2b9838c | 2018-10-28 17:09:42 +0900 | [diff] [blame] | 479 | * @param ctState ctState: please use RulePopulatorUtil.computeCtStateFlag() |
| 480 | * to build the value |
| 481 | * @param ctMask crMask: please use RulePopulatorUtil.computeCtMaskFlag() |
| 482 | * to build the value |
sangho | 1aaa788 | 2017-05-31 13:22:47 +0900 | [diff] [blame] | 483 | * @param commit CT_COMMIT for commit action, CT_NO_COMMIT otherwise |
Jian Li | 2b9838c | 2018-10-28 17:09:42 +0900 | [diff] [blame] | 484 | * @param recircTable table number for recirculation after CT actions. |
| 485 | * CT_NO_RECIRC with no recirculation |
| 486 | * @param action Additional actions. ACTION_DROP, ACTION_NONE, |
| 487 | * GOTO_XXX_TABLE are supported. |
sangho | 1aaa788 | 2017-05-31 13:22:47 +0900 | [diff] [blame] | 488 | * @param priority priority value for the rule |
| 489 | * @param install true for insertion, false for removal |
| 490 | */ |
| 491 | private void setConnTrackRule(DeviceId deviceId, long ctState, long ctMask, |
| 492 | int commit, short recircTable, |
| 493 | int action, int priority, boolean install) { |
| 494 | |
Jian Li | bcc4228 | 2018-09-13 20:59:34 +0900 | [diff] [blame] | 495 | ExtensionSelector esCtSate = RulePopulatorUtil |
| 496 | .buildCtExtensionSelector(driverService, deviceId, ctState, ctMask); |
sangho | 1aaa788 | 2017-05-31 13:22:47 +0900 | [diff] [blame] | 497 | TrafficSelector selector = DefaultTrafficSelector.builder() |
| 498 | .extension(esCtSate, deviceId) |
| 499 | .matchEthType(Ethernet.TYPE_IPV4) |
| 500 | .build(); |
| 501 | |
| 502 | TrafficTreatment.Builder tb = DefaultTrafficTreatment.builder(); |
| 503 | |
| 504 | if (commit == CT_COMMIT || recircTable > 0) { |
Jian Li | 5a26ab3 | 2019-03-21 15:20:01 +0900 | [diff] [blame] | 505 | RulePopulatorUtil.NiciraConnTrackTreatmentBuilder natTreatmentBuilder = |
Jian Li | bcc4228 | 2018-09-13 20:59:34 +0900 | [diff] [blame] | 506 | niciraConnTrackTreatmentBuilder(driverService, deviceId); |
sangho | 1aaa788 | 2017-05-31 13:22:47 +0900 | [diff] [blame] | 507 | natTreatmentBuilder.natAction(false); |
| 508 | if (commit == CT_COMMIT) { |
| 509 | natTreatmentBuilder.commit(true); |
| 510 | } else { |
| 511 | natTreatmentBuilder.commit(false); |
| 512 | } |
| 513 | if (recircTable > 0) { |
| 514 | natTreatmentBuilder.table(recircTable); |
| 515 | } |
| 516 | tb.extension(natTreatmentBuilder.build(), deviceId); |
| 517 | } else if (action == ACTION_DROP) { |
| 518 | tb.drop(); |
| 519 | } |
| 520 | |
sanghoshin | bbeb31a | 2018-09-11 17:01:01 +0800 | [diff] [blame] | 521 | if (action != ACTION_NONE && action != ACTION_DROP) { |
sangho | 1aaa788 | 2017-05-31 13:22:47 +0900 | [diff] [blame] | 522 | tb.transition(action); |
| 523 | } |
| 524 | |
| 525 | int tableType = ERROR_TABLE; |
| 526 | if (priority == PRIORITY_CT_RULE || priority == PRIORITY_CT_DROP_RULE) { |
| 527 | tableType = CT_TABLE; |
| 528 | } else if (priority == PRIORITY_CT_HOOK_RULE) { |
Jian Li | 1e9cb73 | 2018-11-25 23:17:21 +0900 | [diff] [blame] | 529 | tableType = ACL_INGRESS_TABLE; |
sangho | 1aaa788 | 2017-05-31 13:22:47 +0900 | [diff] [blame] | 530 | } else { |
| 531 | log.error("Cannot an appropriate table for the conn track rule."); |
| 532 | } |
| 533 | |
| 534 | osFlowRuleService.setRule( |
| 535 | appId, |
| 536 | deviceId, |
| 537 | selector, |
| 538 | tb.build(), |
| 539 | priority, |
| 540 | tableType, |
| 541 | install); |
| 542 | } |
| 543 | |
| 544 | /** |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 545 | * Returns a set of host IP addresses engaged with supplied security group ID. |
| 546 | * It only searches a VM in the same tenant boundary. |
| 547 | * |
Jian Li | a70fdb60 | 2018-11-05 01:32:22 +0900 | [diff] [blame] | 548 | * @param srcPort openstack port |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 549 | * @param sgId security group id |
| 550 | * @return set of ip addresses |
| 551 | */ |
Jian Li | a70fdb60 | 2018-11-05 01:32:22 +0900 | [diff] [blame] | 552 | private Set<InstancePort> getRemoteInstPorts(Port srcPort, |
Jian Li | 2b9838c | 2018-10-28 17:09:42 +0900 | [diff] [blame] | 553 | String sgId, boolean install) { |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 554 | Set<InstancePort> remoteInstPorts; |
| 555 | |
Jian Li | ac30e27 | 2018-10-18 23:08:03 +0900 | [diff] [blame] | 556 | Set<Port> removedPorts = Sets.newConcurrentHashSet(); |
| 557 | |
| 558 | if (!install) { |
| 559 | removedPorts = new HashSet<>(removedOsPortStore.asJavaMap().values()); |
| 560 | } |
| 561 | |
| 562 | remoteInstPorts = Sets.union(osNetService.ports(), removedPorts).stream() |
Jian Li | a70fdb60 | 2018-11-05 01:32:22 +0900 | [diff] [blame] | 563 | .filter(port -> !port.getId().equals(srcPort.getId())) |
| 564 | .filter(port -> port.getTenantId().equals(srcPort.getTenantId())) |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 565 | .filter(port -> port.getSecurityGroups().contains(sgId)) |
Jian Li | a70fdb60 | 2018-11-05 01:32:22 +0900 | [diff] [blame] | 566 | .filter(port -> port.getNetworkId().equals(srcPort.getNetworkId())) |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 567 | .map(port -> instancePortService.instancePort(port.getId())) |
| 568 | .filter(instPort -> instPort != null && instPort.ipAddress() != null) |
| 569 | .collect(Collectors.toSet()); |
| 570 | |
| 571 | return Collections.unmodifiableSet(remoteInstPorts); |
| 572 | } |
| 573 | |
sangho | 2e97be0 | 2017-07-03 18:18:27 +0900 | [diff] [blame] | 574 | private Set<TrafficSelector> buildSelectors(SecurityGroupRule sgRule, |
| 575 | Ip4Address vmIp, |
Jian Li | e8b28db | 2018-10-17 14:04:09 +0900 | [diff] [blame] | 576 | IpPrefix remoteIp, |
Jian Li | 1e9cb73 | 2018-11-25 23:17:21 +0900 | [diff] [blame] | 577 | String netId) { |
Jian Li | 2b9838c | 2018-10-28 17:09:42 +0900 | [diff] [blame] | 578 | if (remoteIp != null && remoteIp.equals(IpPrefix.valueOf(vmIp, VM_IP_PREFIX))) { |
sangho | 2e97be0 | 2017-07-03 18:18:27 +0900 | [diff] [blame] | 579 | // do nothing if the remote IP is my IP |
| 580 | return null; |
| 581 | } |
| 582 | |
| 583 | Set<TrafficSelector> selectorSet = Sets.newHashSet(); |
| 584 | |
sangho | 2e97be0 | 2017-07-03 18:18:27 +0900 | [diff] [blame] | 585 | if (sgRule.getPortRangeMax() != null && sgRule.getPortRangeMin() != null && |
| 586 | sgRule.getPortRangeMin() < sgRule.getPortRangeMax()) { |
Jian Li | bcc4228 | 2018-09-13 20:59:34 +0900 | [diff] [blame] | 587 | Map<TpPort, TpPort> portRangeMatchMap = |
| 588 | buildPortRangeMatches(sgRule.getPortRangeMin(), |
Jian Li | 28ec77f | 2018-10-31 07:07:25 +0900 | [diff] [blame] | 589 | sgRule.getPortRangeMax()); |
Jian Li | 2b9838c | 2018-10-28 17:09:42 +0900 | [diff] [blame] | 590 | portRangeMatchMap.forEach((key, value) -> { |
sangho | 2e97be0 | 2017-07-03 18:18:27 +0900 | [diff] [blame] | 591 | |
Jian Li | 22ce467 | 2020-05-26 00:31:27 +0900 | [diff] [blame] | 592 | TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder(); |
| 593 | buildMatches(sBuilder, sgRule, vmIp, remoteIp, netId); |
| 594 | |
Jian Li | 0f6ef9c | 2020-07-08 21:37:26 +0900 | [diff] [blame] | 595 | if (sgRule.getProtocol().equalsIgnoreCase(PROTO_TCP) || |
| 596 | sgRule.getProtocol().equals(PROTO_TCP_NUM)) { |
Jian Li | 4d13870 | 2018-11-27 17:25:28 +0900 | [diff] [blame] | 597 | if (sgRule.getDirection().equalsIgnoreCase(EGRESS)) { |
Jian Li | 22ce467 | 2020-05-26 00:31:27 +0900 | [diff] [blame] | 598 | if (value.toInt() == TpPort.MAX_PORT) { |
| 599 | sBuilder.matchTcpSrc(key); |
| 600 | } else { |
| 601 | sBuilder.matchTcpSrcMasked(key, value); |
| 602 | } |
Jian Li | 2b9838c | 2018-10-28 17:09:42 +0900 | [diff] [blame] | 603 | } else { |
Jian Li | 22ce467 | 2020-05-26 00:31:27 +0900 | [diff] [blame] | 604 | if (value.toInt() == TpPort.MAX_PORT) { |
| 605 | sBuilder.matchTcpDst(key); |
| 606 | } else { |
| 607 | sBuilder.matchTcpDstMasked(key, value); |
| 608 | } |
Jian Li | bcc4228 | 2018-09-13 20:59:34 +0900 | [diff] [blame] | 609 | } |
Jian Li | 0f6ef9c | 2020-07-08 21:37:26 +0900 | [diff] [blame] | 610 | } else if (sgRule.getProtocol().equalsIgnoreCase(PROTO_UDP) || |
| 611 | sgRule.getProtocol().equals(PROTO_UDP_NUM)) { |
Jian Li | 4d13870 | 2018-11-27 17:25:28 +0900 | [diff] [blame] | 612 | if (sgRule.getDirection().equalsIgnoreCase(EGRESS)) { |
Jian Li | 22ce467 | 2020-05-26 00:31:27 +0900 | [diff] [blame] | 613 | if (value.toInt() == TpPort.MAX_PORT) { |
| 614 | sBuilder.matchUdpSrc(key); |
| 615 | } else { |
| 616 | sBuilder.matchUdpSrcMasked(key, value); |
| 617 | } |
Jian Li | 2b9838c | 2018-10-28 17:09:42 +0900 | [diff] [blame] | 618 | } else { |
Jian Li | 22ce467 | 2020-05-26 00:31:27 +0900 | [diff] [blame] | 619 | if (value.toInt() == TpPort.MAX_PORT) { |
| 620 | sBuilder.matchUdpDst(key); |
| 621 | } else { |
| 622 | sBuilder.matchUdpDstMasked(key, value); |
| 623 | } |
Jian Li | 2b9838c | 2018-10-28 17:09:42 +0900 | [diff] [blame] | 624 | } |
Jian Li | 0f6ef9c | 2020-07-08 21:37:26 +0900 | [diff] [blame] | 625 | } else if (sgRule.getProtocol().equalsIgnoreCase(PROTO_SCTP) || |
| 626 | sgRule.getProtocol().equals(PROTO_SCTP_NUM)) { |
| 627 | if (sgRule.getDirection().equalsIgnoreCase(EGRESS)) { |
| 628 | if (value.toInt() == TpPort.MAX_PORT) { |
| 629 | sBuilder.matchSctpSrc(key); |
| 630 | } else { |
| 631 | sBuilder.matchSctpSrcMasked(key, value); |
| 632 | } |
| 633 | } else { |
| 634 | if (value.toInt() == TpPort.MAX_PORT) { |
| 635 | sBuilder.matchSctpDst(key); |
| 636 | } else { |
| 637 | sBuilder.matchSctpDstMasked(key, value); |
| 638 | } |
| 639 | } |
Jian Li | 2b9838c | 2018-10-28 17:09:42 +0900 | [diff] [blame] | 640 | } |
| 641 | |
| 642 | selectorSet.add(sBuilder.build()); |
| 643 | }); |
sangho | 2e97be0 | 2017-07-03 18:18:27 +0900 | [diff] [blame] | 644 | } else { |
Jian Li | 22ce467 | 2020-05-26 00:31:27 +0900 | [diff] [blame] | 645 | |
| 646 | TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder(); |
| 647 | buildMatches(sBuilder, sgRule, vmIp, remoteIp, netId); |
| 648 | |
sangho | 2e97be0 | 2017-07-03 18:18:27 +0900 | [diff] [blame] | 649 | selectorSet.add(sBuilder.build()); |
| 650 | } |
| 651 | |
| 652 | return selectorSet; |
| 653 | } |
| 654 | |
Jian Li | bcc4228 | 2018-09-13 20:59:34 +0900 | [diff] [blame] | 655 | private void buildMatches(TrafficSelector.Builder sBuilder, |
Jian Li | 1e9cb73 | 2018-11-25 23:17:21 +0900 | [diff] [blame] | 656 | SecurityGroupRule sgRule, Ip4Address vmIp, |
| 657 | IpPrefix remoteIp, String netId) { |
| 658 | buildTunnelId(sBuilder, netId); |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 659 | buildMatchEthType(sBuilder, sgRule.getEtherType()); |
| 660 | buildMatchDirection(sBuilder, sgRule.getDirection(), vmIp); |
| 661 | buildMatchProto(sBuilder, sgRule.getProtocol()); |
| 662 | buildMatchPort(sBuilder, sgRule.getProtocol(), sgRule.getDirection(), |
sangho | 2e97be0 | 2017-07-03 18:18:27 +0900 | [diff] [blame] | 663 | sgRule.getPortRangeMin() == null ? 0 : sgRule.getPortRangeMin(), |
| 664 | sgRule.getPortRangeMax() == null ? 0 : sgRule.getPortRangeMax()); |
Jian Li | 0f6ef9c | 2020-07-08 21:37:26 +0900 | [diff] [blame] | 665 | buildMatchIcmp(sBuilder, sgRule.getProtocol(), |
| 666 | sgRule.getPortRangeMin(), sgRule.getPortRangeMax()); |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 667 | buildMatchRemoteIp(sBuilder, remoteIp, sgRule.getDirection()); |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 668 | } |
| 669 | |
Jian Li | 1e9cb73 | 2018-11-25 23:17:21 +0900 | [diff] [blame] | 670 | private void buildTunnelId(TrafficSelector.Builder sBuilder, String netId) { |
| 671 | String segId = osNetService.segmentId(netId); |
SONA Project | 6bc5c4a | 2018-12-14 23:49:52 +0900 | [diff] [blame] | 672 | Type netType = osNetService.networkType(netId); |
Jian Li | e8b28db | 2018-10-17 14:04:09 +0900 | [diff] [blame] | 673 | |
SONA Project | 6bc5c4a | 2018-12-14 23:49:52 +0900 | [diff] [blame] | 674 | if (netType == VLAN) { |
Jian Li | e8b28db | 2018-10-17 14:04:09 +0900 | [diff] [blame] | 675 | sBuilder.matchVlanId(VlanId.vlanId(segId)); |
Jian Li | 621f73c | 2018-12-15 01:49:22 +0900 | [diff] [blame] | 676 | } else if (netType == VXLAN || netType == GRE || netType == GENEVE) { |
Jian Li | e8b28db | 2018-10-17 14:04:09 +0900 | [diff] [blame] | 677 | sBuilder.matchTunnelId(Long.valueOf(segId)); |
Jian Li | 28ec77f | 2018-10-31 07:07:25 +0900 | [diff] [blame] | 678 | } else { |
Jian Li | 2dc3759 | 2019-03-14 17:32:15 +0900 | [diff] [blame] | 679 | log.debug("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] | 680 | } |
| 681 | } |
| 682 | |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 683 | private void buildMatchDirection(TrafficSelector.Builder sBuilder, |
| 684 | String direction, |
| 685 | Ip4Address vmIp) { |
Jian Li | 4d13870 | 2018-11-27 17:25:28 +0900 | [diff] [blame] | 686 | if (direction.equalsIgnoreCase(EGRESS)) { |
Jian Li | 2b9838c | 2018-10-28 17:09:42 +0900 | [diff] [blame] | 687 | sBuilder.matchIPSrc(IpPrefix.valueOf(vmIp, VM_IP_PREFIX)); |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 688 | } else { |
Jian Li | 2b9838c | 2018-10-28 17:09:42 +0900 | [diff] [blame] | 689 | sBuilder.matchIPDst(IpPrefix.valueOf(vmIp, VM_IP_PREFIX)); |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 690 | } |
| 691 | } |
| 692 | |
| 693 | private void buildMatchEthType(TrafficSelector.Builder sBuilder, String etherType) { |
| 694 | // Either IpSrc or IpDst (or both) is set by default, and we need to set EthType as IPv4. |
| 695 | sBuilder.matchEthType(Ethernet.TYPE_IPV4); |
Jian Li | 4d13870 | 2018-11-27 17:25:28 +0900 | [diff] [blame] | 696 | if (etherType != null && !Objects.equals(etherType, STR_NULL) && |
| 697 | !etherType.equalsIgnoreCase(ETHTYPE_IPV4)) { |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 698 | log.debug("EthType {} is not supported yet in Security Group", etherType); |
| 699 | } |
| 700 | } |
| 701 | |
Jian Li | bcc4228 | 2018-09-13 20:59:34 +0900 | [diff] [blame] | 702 | private void buildMatchRemoteIp(TrafficSelector.Builder sBuilder, |
| 703 | IpPrefix remoteIpPrefix, String direction) { |
| 704 | if (remoteIpPrefix != null && |
| 705 | !remoteIpPrefix.getIp4Prefix().equals(IP_PREFIX_ANY)) { |
Jian Li | 4d13870 | 2018-11-27 17:25:28 +0900 | [diff] [blame] | 706 | if (direction.equalsIgnoreCase(EGRESS)) { |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 707 | sBuilder.matchIPDst(remoteIpPrefix); |
| 708 | } else { |
| 709 | sBuilder.matchIPSrc(remoteIpPrefix); |
| 710 | } |
| 711 | } |
| 712 | } |
| 713 | |
| 714 | private void buildMatchProto(TrafficSelector.Builder sBuilder, String protocol) { |
| 715 | if (protocol != null) { |
| 716 | switch (protocol.toUpperCase()) { |
| 717 | case PROTO_ICMP: |
Jian Li | 0f6ef9c | 2020-07-08 21:37:26 +0900 | [diff] [blame] | 718 | case PROTO_ICMP_NUM: |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 719 | sBuilder.matchIPProtocol(IPv4.PROTOCOL_ICMP); |
| 720 | break; |
| 721 | case PROTO_TCP: |
Jian Li | 0f6ef9c | 2020-07-08 21:37:26 +0900 | [diff] [blame] | 722 | case PROTO_TCP_NUM: |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 723 | sBuilder.matchIPProtocol(IPv4.PROTOCOL_TCP); |
| 724 | break; |
| 725 | case PROTO_UDP: |
Jian Li | 0f6ef9c | 2020-07-08 21:37:26 +0900 | [diff] [blame] | 726 | case PROTO_UDP_NUM: |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 727 | sBuilder.matchIPProtocol(IPv4.PROTOCOL_UDP); |
| 728 | break; |
Jian Li | 0f6ef9c | 2020-07-08 21:37:26 +0900 | [diff] [blame] | 729 | case PROTO_SCTP: |
| 730 | case PROTO_SCTP_NUM: |
| 731 | sBuilder.matchIPProtocol(PROTOCOL_SCTP); |
| 732 | break; |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 733 | default: |
Jian Li | 0f6ef9c | 2020-07-08 21:37:26 +0900 | [diff] [blame] | 734 | break; |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 735 | } |
| 736 | } |
| 737 | } |
| 738 | |
Jian Li | bcc4228 | 2018-09-13 20:59:34 +0900 | [diff] [blame] | 739 | private void buildMatchPort(TrafficSelector.Builder sBuilder, |
| 740 | String protocol, String direction, |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 741 | int portMin, int portMax) { |
Jian Li | 0f6ef9c | 2020-07-08 21:37:26 +0900 | [diff] [blame] | 742 | if (portMax > 0 && portMin == portMax) { |
| 743 | if (protocol.equalsIgnoreCase(PROTO_TCP) || |
| 744 | protocol.equals(PROTO_TCP_NUM)) { |
Jian Li | 4d13870 | 2018-11-27 17:25:28 +0900 | [diff] [blame] | 745 | if (direction.equalsIgnoreCase(EGRESS)) { |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 746 | sBuilder.matchTcpSrc(TpPort.tpPort(portMax)); |
| 747 | } else { |
| 748 | sBuilder.matchTcpDst(TpPort.tpPort(portMax)); |
| 749 | } |
Jian Li | 0f6ef9c | 2020-07-08 21:37:26 +0900 | [diff] [blame] | 750 | } else if (protocol.equalsIgnoreCase(PROTO_UDP) || |
| 751 | protocol.equals(PROTO_UDP_NUM)) { |
Jian Li | 4d13870 | 2018-11-27 17:25:28 +0900 | [diff] [blame] | 752 | if (direction.equalsIgnoreCase(EGRESS)) { |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 753 | sBuilder.matchUdpSrc(TpPort.tpPort(portMax)); |
| 754 | } else { |
| 755 | sBuilder.matchUdpDst(TpPort.tpPort(portMax)); |
| 756 | } |
Jian Li | 0f6ef9c | 2020-07-08 21:37:26 +0900 | [diff] [blame] | 757 | } else if (protocol.equalsIgnoreCase(PROTO_SCTP) || |
| 758 | protocol.equals(PROTO_SCTP_NUM)) { |
| 759 | if (direction.equalsIgnoreCase(EGRESS)) { |
| 760 | sBuilder.matchSctpSrc(TpPort.tpPort(portMax)); |
| 761 | } else { |
| 762 | sBuilder.matchSctpDst(TpPort.tpPort(portMax)); |
| 763 | } |
| 764 | } |
| 765 | } |
| 766 | |
| 767 | |
| 768 | } |
| 769 | |
| 770 | private void buildMatchIcmp(TrafficSelector.Builder sBuilder, |
| 771 | String protocol, Integer icmpCode, Integer icmpType) { |
| 772 | if (protocol != null) { |
| 773 | if (protocol.equalsIgnoreCase(PROTO_ICMP) || |
| 774 | protocol.equals(PROTO_ICMP_NUM)) { |
| 775 | if (icmpCode != null && icmpCode >= 0 && icmpCode <= 255) { |
| 776 | sBuilder.matchIcmpCode(icmpCode.byteValue()); |
| 777 | } |
| 778 | if (icmpType != null && icmpType >= 0 && icmpType <= 255) { |
| 779 | sBuilder.matchIcmpType(icmpType.byteValue()); |
| 780 | } |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 781 | } |
| 782 | } |
| 783 | } |
| 784 | |
sangho | 0248ca2 | 2017-05-31 13:22:47 +0900 | [diff] [blame] | 785 | private void resetSecurityGroupRules() { |
| 786 | |
Jian Li | 627e016 | 2020-03-12 17:50:36 +0900 | [diff] [blame] | 787 | if (getUseSecurityGroupFlag()) { |
Jian Li | b696950 | 2018-10-30 20:38:07 +0900 | [diff] [blame] | 788 | osNodeService.completeNodes(COMPUTE).forEach(node -> { |
Jian Li | 1e9cb73 | 2018-11-25 23:17:21 +0900 | [diff] [blame] | 789 | osFlowRuleService.setUpTableMissEntry(node.intgBridge(), ACL_EGRESS_TABLE); |
Jian Li | b696950 | 2018-10-30 20:38:07 +0900 | [diff] [blame] | 790 | initializeConnTrackTable(node.intgBridge(), true); |
Jian Li | 1e9cb73 | 2018-11-25 23:17:21 +0900 | [diff] [blame] | 791 | initializeAclTable(node.intgBridge(), true); |
| 792 | initializeIngressTable(node.intgBridge(), true); |
Jian Li | b696950 | 2018-10-30 20:38:07 +0900 | [diff] [blame] | 793 | }); |
| 794 | |
sangho | 0248ca2 | 2017-05-31 13:22:47 +0900 | [diff] [blame] | 795 | securityGroupService.securityGroups().forEach(securityGroup -> |
| 796 | securityGroup.getRules().forEach(this::securityGroupRuleAdded)); |
| 797 | } else { |
Jian Li | b696950 | 2018-10-30 20:38:07 +0900 | [diff] [blame] | 798 | osNodeService.completeNodes(COMPUTE).forEach(node -> { |
Jian Li | 1e9cb73 | 2018-11-25 23:17:21 +0900 | [diff] [blame] | 799 | osFlowRuleService.connectTables(node.intgBridge(), ACL_EGRESS_TABLE, JUMP_TABLE); |
Jian Li | b696950 | 2018-10-30 20:38:07 +0900 | [diff] [blame] | 800 | initializeConnTrackTable(node.intgBridge(), false); |
Jian Li | 1e9cb73 | 2018-11-25 23:17:21 +0900 | [diff] [blame] | 801 | initializeAclTable(node.intgBridge(), false); |
| 802 | initializeIngressTable(node.intgBridge(), false); |
Jian Li | b696950 | 2018-10-30 20:38:07 +0900 | [diff] [blame] | 803 | }); |
| 804 | |
sangho | 0248ca2 | 2017-05-31 13:22:47 +0900 | [diff] [blame] | 805 | securityGroupService.securityGroups().forEach(securityGroup -> |
| 806 | securityGroup.getRules().forEach(this::securityGroupRuleRemoved)); |
| 807 | } |
| 808 | |
Jian Li | bcc4228 | 2018-09-13 20:59:34 +0900 | [diff] [blame] | 809 | log.info("Reset security group info " + |
Jian Li | 38c82f7 | 2020-04-02 11:15:38 +0900 | [diff] [blame] | 810 | (getUseSecurityGroupFlag() ? " with " : " without") + " Security Group"); |
sangho | 0248ca2 | 2017-05-31 13:22:47 +0900 | [diff] [blame] | 811 | } |
| 812 | |
| 813 | private void securityGroupRuleAdded(SecurityGroupRule sgRule) { |
| 814 | osNetService.ports().stream() |
Jian Li | bcc4228 | 2018-09-13 20:59:34 +0900 | [diff] [blame] | 815 | .filter(port -> port.getSecurityGroups() |
Jian Li | 28ec77f | 2018-10-31 07:07:25 +0900 | [diff] [blame] | 816 | .contains(sgRule.getSecurityGroupId())) |
sangho | 0248ca2 | 2017-05-31 13:22:47 +0900 | [diff] [blame] | 817 | .forEach(port -> { |
| 818 | updateSecurityGroupRule( |
| 819 | instancePortService.instancePort(port.getId()), |
| 820 | port, sgRule, true); |
| 821 | log.debug("Applied security group rule {} to port {}", |
| 822 | sgRule.getId(), port.getId()); |
| 823 | }); |
| 824 | } |
| 825 | |
| 826 | private void securityGroupRuleRemoved(SecurityGroupRule sgRule) { |
Jian Li | ac30e27 | 2018-10-18 23:08:03 +0900 | [diff] [blame] | 827 | Set<Port> removedPorts = new HashSet<>(removedOsPortStore.asJavaMap().values()); |
| 828 | |
| 829 | Sets.union(osNetService.ports(), removedPorts).stream() |
Jian Li | bcc4228 | 2018-09-13 20:59:34 +0900 | [diff] [blame] | 830 | .filter(port -> port.getSecurityGroups() |
Jian Li | 28ec77f | 2018-10-31 07:07:25 +0900 | [diff] [blame] | 831 | .contains(sgRule.getSecurityGroupId())) |
sangho | 0248ca2 | 2017-05-31 13:22:47 +0900 | [diff] [blame] | 832 | .forEach(port -> { |
| 833 | updateSecurityGroupRule( |
| 834 | instancePortService.instancePort(port.getId()), |
| 835 | port, sgRule, false); |
| 836 | log.debug("Removed security group rule {} from port {}", |
| 837 | sgRule.getId(), port.getId()); |
| 838 | }); |
| 839 | } |
| 840 | |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 841 | private class InternalInstancePortListener implements InstancePortListener { |
| 842 | |
| 843 | @Override |
| 844 | public boolean isRelevant(InstancePortEvent event) { |
Jian Li | 38c82f7 | 2020-04-02 11:15:38 +0900 | [diff] [blame] | 845 | return getUseSecurityGroupFlag(); |
Jian Li | 34220ea | 2018-11-14 01:30:24 +0900 | [diff] [blame] | 846 | } |
| 847 | |
| 848 | private boolean isRelevantHelper(InstancePortEvent event) { |
| 849 | return mastershipService.isLocalMaster(event.subject().deviceId()); |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 850 | } |
| 851 | |
| 852 | @Override |
| 853 | public void event(InstancePortEvent event) { |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 854 | switch (event.type()) { |
Jian Li | ec5c32b | 2018-07-13 14:28:58 +0900 | [diff] [blame] | 855 | case OPENSTACK_INSTANCE_PORT_UPDATED: |
Jian Li | e8b28db | 2018-10-17 14:04:09 +0900 | [diff] [blame] | 856 | case OPENSTACK_INSTANCE_PORT_DETECTED: |
Jian Li | ac30e27 | 2018-10-18 23:08:03 +0900 | [diff] [blame] | 857 | case OPENSTACK_INSTANCE_MIGRATION_STARTED: |
Jian Li | 4d13870 | 2018-11-27 17:25:28 +0900 | [diff] [blame] | 858 | eventExecutor.execute(() -> processInstanceMigrationStart(event)); |
Jian Li | ac30e27 | 2018-10-18 23:08:03 +0900 | [diff] [blame] | 859 | break; |
| 860 | case OPENSTACK_INSTANCE_PORT_VANISHED: |
Jian Li | 4d13870 | 2018-11-27 17:25:28 +0900 | [diff] [blame] | 861 | eventExecutor.execute(() -> processInstancePortVanish(event)); |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 862 | break; |
Jian Li | b8cdcc1 | 2018-10-23 01:53:10 +0900 | [diff] [blame] | 863 | case OPENSTACK_INSTANCE_MIGRATION_ENDED: |
Jian Li | 4d13870 | 2018-11-27 17:25:28 +0900 | [diff] [blame] | 864 | eventExecutor.execute(() -> processInstanceMigrationEnd(event)); |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 865 | break; |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 866 | default: |
| 867 | break; |
| 868 | } |
| 869 | } |
Jian Li | ac30e27 | 2018-10-18 23:08:03 +0900 | [diff] [blame] | 870 | |
Jian Li | 4d13870 | 2018-11-27 17:25:28 +0900 | [diff] [blame] | 871 | private void processInstanceMigrationStart(InstancePortEvent event) { |
| 872 | if (!isRelevantHelper(event)) { |
| 873 | return; |
| 874 | } |
| 875 | |
| 876 | InstancePort instPort = event.subject(); |
| 877 | installSecurityGroupRules(event, instPort); |
| 878 | setAclRecircRules(instPort, true); |
| 879 | } |
| 880 | |
| 881 | private void processInstancePortVanish(InstancePortEvent event) { |
| 882 | if (!isRelevantHelper(event)) { |
| 883 | return; |
| 884 | } |
| 885 | |
| 886 | InstancePort instPort = event.subject(); |
| 887 | Port osPort = removedOsPortStore.asJavaMap().get(instPort.portId()); |
| 888 | setSecurityGroupRules(instPort, osPort, false); |
| 889 | removedOsPortStore.remove(instPort.portId()); |
| 890 | setAclRecircRules(instPort, false); |
| 891 | } |
| 892 | |
| 893 | private void processInstanceMigrationEnd(InstancePortEvent event) { |
| 894 | if (!isRelevantHelper(event)) { |
| 895 | return; |
| 896 | } |
| 897 | |
| 898 | InstancePort instPort = event.subject(); |
| 899 | InstancePort revisedInstPort = swapStaleLocation(instPort); |
| 900 | Port port = osNetService.port(instPort.portId()); |
| 901 | setSecurityGroupRules(revisedInstPort, port, false); |
| 902 | setAclRecircRules(revisedInstPort, false); |
| 903 | } |
| 904 | |
Jian Li | ac30e27 | 2018-10-18 23:08:03 +0900 | [diff] [blame] | 905 | private void installSecurityGroupRules(InstancePortEvent event, |
| 906 | InstancePort instPort) { |
| 907 | log.debug("Instance port detected/updated MAC:{} IP:{}", |
| 908 | instPort.macAddress(), |
| 909 | instPort.ipAddress()); |
Jian Li | 2b9838c | 2018-10-28 17:09:42 +0900 | [diff] [blame] | 910 | eventExecutor.execute(() -> |
| 911 | setSecurityGroupRules(instPort, |
| 912 | osNetService.port(event.subject().portId()), true)); |
Jian Li | ac30e27 | 2018-10-18 23:08:03 +0900 | [diff] [blame] | 913 | } |
Jian Li | 1e9cb73 | 2018-11-25 23:17:21 +0900 | [diff] [blame] | 914 | |
Jian Li | 4d13870 | 2018-11-27 17:25:28 +0900 | [diff] [blame] | 915 | private void setSecurityGroupRules(InstancePort instPort, |
| 916 | Port port, boolean install) { |
| 917 | Port osPort = port; |
| 918 | |
| 919 | if (!install) { |
| 920 | Port rmvPort = removedOsPortStore.asJavaMap().get(instPort.portId()); |
| 921 | if (osPort == null && rmvPort == null) { |
| 922 | return; |
| 923 | } |
| 924 | |
| 925 | if (port == null) { |
| 926 | osPort = rmvPort; |
| 927 | } |
| 928 | } |
| 929 | |
| 930 | final Port finalPort = osPort; |
| 931 | |
| 932 | osPort.getSecurityGroups().forEach(sgId -> { |
| 933 | SecurityGroup sg = securityGroupService.securityGroup(sgId); |
| 934 | if (sg == null) { |
| 935 | log.error("Security Group {} not found", sgId); |
| 936 | return; |
| 937 | } |
| 938 | sg.getRules().forEach(sgRule -> |
| 939 | updateSecurityGroupRule(instPort, finalPort, sgRule, install)); |
| 940 | final String action = install ? "Installed " : "Removed "; |
| 941 | log.debug(action + "Security Group Rule ID : " + sgId); |
| 942 | }); |
| 943 | } |
| 944 | |
Jian Li | 1e9cb73 | 2018-11-25 23:17:21 +0900 | [diff] [blame] | 945 | private void setAclRecircRules(InstancePort instPort, boolean install) { |
| 946 | TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder(); |
| 947 | |
| 948 | Network net = osNetService.network(instPort.networkId()); |
SONA Project | 6bc5c4a | 2018-12-14 23:49:52 +0900 | [diff] [blame] | 949 | Type netType = osNetService.networkType(instPort.networkId()); |
Jian Li | 1e9cb73 | 2018-11-25 23:17:21 +0900 | [diff] [blame] | 950 | String segId = net.getProviderSegID(); |
| 951 | |
| 952 | switch (netType) { |
| 953 | case VXLAN: |
Jian Li | 2d68c19 | 2018-12-13 15:52:59 +0900 | [diff] [blame] | 954 | case GRE: |
Jian Li | 621f73c | 2018-12-15 01:49:22 +0900 | [diff] [blame] | 955 | case GENEVE: |
Jian Li | 1e9cb73 | 2018-11-25 23:17:21 +0900 | [diff] [blame] | 956 | sBuilder.matchTunnelId(Long.valueOf(segId)); |
| 957 | break; |
| 958 | case VLAN: |
| 959 | sBuilder.matchVlanId(VlanId.vlanId(segId)); |
| 960 | break; |
| 961 | default: |
| 962 | break; |
| 963 | } |
| 964 | |
| 965 | sBuilder.matchEthType(Ethernet.TYPE_IPV4); |
| 966 | sBuilder.matchIPDst(IpPrefix.valueOf(instPort.ipAddress(), VM_IP_PREFIX)); |
| 967 | |
| 968 | TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder(); |
| 969 | tBuilder.transition(ACL_INGRESS_TABLE); |
| 970 | |
| 971 | osFlowRuleService.setRule( |
| 972 | appId, |
| 973 | instPort.deviceId(), |
| 974 | sBuilder.build(), |
| 975 | tBuilder.build(), |
| 976 | PRIORITY_ACL_RULE, |
| 977 | ACL_RECIRC_TABLE, |
| 978 | install); |
| 979 | } |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 980 | } |
| 981 | |
| 982 | private class InternalOpenstackPortListener implements OpenstackNetworkListener { |
| 983 | |
| 984 | @Override |
| 985 | public boolean isRelevant(OpenstackNetworkEvent event) { |
sanghoshin | bbeb31a | 2018-09-11 17:01:01 +0800 | [diff] [blame] | 986 | if (event.port() == null || Strings.isNullOrEmpty(event.port().getId())) { |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 987 | return false; |
| 988 | } |
Jian Li | bcc4228 | 2018-09-13 20:59:34 +0900 | [diff] [blame] | 989 | |
Jian Li | 38c82f7 | 2020-04-02 11:15:38 +0900 | [diff] [blame] | 990 | return getUseSecurityGroupFlag(); |
Jian Li | 34220ea | 2018-11-14 01:30:24 +0900 | [diff] [blame] | 991 | } |
| 992 | |
| 993 | private boolean isRelevantHelper(OpenstackNetworkEvent event) { |
Jian Li | bcc4228 | 2018-09-13 20:59:34 +0900 | [diff] [blame] | 994 | InstancePort instPort = instancePortService.instancePort(event.port().getId()); |
| 995 | |
| 996 | if (instPort == null) { |
| 997 | return false; |
| 998 | } |
| 999 | |
Jian Li | 34220ea | 2018-11-14 01:30:24 +0900 | [diff] [blame] | 1000 | return mastershipService.isLocalMaster(instPort.deviceId()); |
Jian Li | bcc4228 | 2018-09-13 20:59:34 +0900 | [diff] [blame] | 1001 | } |
| 1002 | |
| 1003 | @Override |
| 1004 | public void event(OpenstackNetworkEvent event) { |
| 1005 | log.debug("openstack port event received {}", event); |
Jian Li | bcc4228 | 2018-09-13 20:59:34 +0900 | [diff] [blame] | 1006 | |
Jian Li | 4d13870 | 2018-11-27 17:25:28 +0900 | [diff] [blame] | 1007 | if (event.type() == OPENSTACK_PORT_PRE_REMOVE) { |
| 1008 | eventExecutor.execute(() -> processPortPreRemove(event)); |
Jian Li | bcc4228 | 2018-09-13 20:59:34 +0900 | [diff] [blame] | 1009 | } |
| 1010 | } |
Jian Li | 4d13870 | 2018-11-27 17:25:28 +0900 | [diff] [blame] | 1011 | |
| 1012 | private void processPortPreRemove(OpenstackNetworkEvent event) { |
| 1013 | if (!isRelevantHelper(event)) { |
| 1014 | return; |
| 1015 | } |
| 1016 | |
| 1017 | Port osPort = event.port(); |
| 1018 | removedOsPortStore.put(osPort.getId(), osPort); |
| 1019 | } |
Jian Li | bcc4228 | 2018-09-13 20:59:34 +0900 | [diff] [blame] | 1020 | } |
| 1021 | |
Jian Li | 34220ea | 2018-11-14 01:30:24 +0900 | [diff] [blame] | 1022 | private class InternalOpenstackNetworkListener implements OpenstackNetworkListener { |
Jian Li | bcc4228 | 2018-09-13 20:59:34 +0900 | [diff] [blame] | 1023 | |
| 1024 | @Override |
| 1025 | public boolean isRelevant(OpenstackNetworkEvent event) { |
| 1026 | if (event.port() == null || Strings.isNullOrEmpty(event.port().getId())) { |
| 1027 | return false; |
| 1028 | } |
Jian Li | 2b9838c | 2018-10-28 17:09:42 +0900 | [diff] [blame] | 1029 | |
Jian Li | 38c82f7 | 2020-04-02 11:15:38 +0900 | [diff] [blame] | 1030 | return getUseSecurityGroupFlag(); |
Jian Li | 34220ea | 2018-11-14 01:30:24 +0900 | [diff] [blame] | 1031 | } |
| 1032 | |
| 1033 | private boolean isRelevantHelper(OpenstackNetworkEvent event) { |
Hyunsun Moon | ae51e73 | 2017-04-25 17:46:21 +0900 | [diff] [blame] | 1034 | if (event.securityGroupId() == null || |
| 1035 | securityGroupService.securityGroup(event.securityGroupId()) == null) { |
| 1036 | return false; |
| 1037 | } |
Jian Li | 2b9838c | 2018-10-28 17:09:42 +0900 | [diff] [blame] | 1038 | |
| 1039 | InstancePort instPort = instancePortService.instancePort(event.port().getId()); |
| 1040 | |
| 1041 | if (instPort == null) { |
Hyunsun Moon | ae51e73 | 2017-04-25 17:46:21 +0900 | [diff] [blame] | 1042 | return false; |
| 1043 | } |
Jian Li | 2b9838c | 2018-10-28 17:09:42 +0900 | [diff] [blame] | 1044 | |
Jian Li | 34220ea | 2018-11-14 01:30:24 +0900 | [diff] [blame] | 1045 | return mastershipService.isLocalMaster(instPort.deviceId()); |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 1046 | } |
| 1047 | |
| 1048 | @Override |
| 1049 | public void event(OpenstackNetworkEvent event) { |
sanghoshin | bbeb31a | 2018-09-11 17:01:01 +0800 | [diff] [blame] | 1050 | log.debug("security group event received {}", event); |
Hyunsun Moon | ae51e73 | 2017-04-25 17:46:21 +0900 | [diff] [blame] | 1051 | |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 1052 | switch (event.type()) { |
Hyunsun Moon | ae51e73 | 2017-04-25 17:46:21 +0900 | [diff] [blame] | 1053 | case OPENSTACK_PORT_SECURITY_GROUP_ADDED: |
Jian Li | 4d13870 | 2018-11-27 17:25:28 +0900 | [diff] [blame] | 1054 | eventExecutor.execute(() -> processPortSgAdd(event)); |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 1055 | break; |
Hyunsun Moon | ae51e73 | 2017-04-25 17:46:21 +0900 | [diff] [blame] | 1056 | case OPENSTACK_PORT_SECURITY_GROUP_REMOVED: |
Jian Li | 4d13870 | 2018-11-27 17:25:28 +0900 | [diff] [blame] | 1057 | eventExecutor.execute(() -> processPortSgRemove(event)); |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 1058 | break; |
| 1059 | default: |
Hyunsun Moon | ae51e73 | 2017-04-25 17:46:21 +0900 | [diff] [blame] | 1060 | // do nothing for the other events |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 1061 | break; |
| 1062 | } |
| 1063 | } |
Jian Li | 4d13870 | 2018-11-27 17:25:28 +0900 | [diff] [blame] | 1064 | |
| 1065 | private void processPortSgAdd(OpenstackNetworkEvent event) { |
| 1066 | if (!isRelevantHelper(event)) { |
| 1067 | return; |
| 1068 | } |
| 1069 | |
| 1070 | InstancePort instPort = instancePortService.instancePort(event.port().getId()); |
| 1071 | SecurityGroup osSg = securityGroupService.securityGroup(event.securityGroupId()); |
| 1072 | |
| 1073 | osSg.getRules().forEach(sgRule -> { |
| 1074 | updateSecurityGroupRule(instPort, event.port(), sgRule, true); |
| 1075 | }); |
| 1076 | log.info("Added security group {} to port {}", |
| 1077 | event.securityGroupId(), event.port().getId()); |
| 1078 | } |
| 1079 | |
| 1080 | private void processPortSgRemove(OpenstackNetworkEvent event) { |
| 1081 | if (!isRelevantHelper(event)) { |
| 1082 | return; |
| 1083 | } |
| 1084 | |
| 1085 | InstancePort instPort = instancePortService.instancePort(event.port().getId()); |
| 1086 | SecurityGroup osSg = securityGroupService.securityGroup(event.securityGroupId()); |
| 1087 | |
| 1088 | osSg.getRules().forEach(sgRule -> { |
| 1089 | updateSecurityGroupRule(instPort, event.port(), sgRule, false); |
| 1090 | }); |
| 1091 | log.info("Removed security group {} from port {}", |
| 1092 | event.securityGroupId(), event.port().getId()); |
| 1093 | } |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 1094 | } |
| 1095 | |
Jian Li | 34220ea | 2018-11-14 01:30:24 +0900 | [diff] [blame] | 1096 | private class InternalSecurityGroupListener implements OpenstackSecurityGroupListener { |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 1097 | |
| 1098 | @Override |
sangho | 0248ca2 | 2017-05-31 13:22:47 +0900 | [diff] [blame] | 1099 | public boolean isRelevant(OpenstackSecurityGroupEvent event) { |
Jian Li | 38c82f7 | 2020-04-02 11:15:38 +0900 | [diff] [blame] | 1100 | return getUseSecurityGroupFlag(); |
sangho | 0248ca2 | 2017-05-31 13:22:47 +0900 | [diff] [blame] | 1101 | } |
| 1102 | |
Jian Li | 34220ea | 2018-11-14 01:30:24 +0900 | [diff] [blame] | 1103 | private boolean isRelevantHelper() { |
| 1104 | return Objects.equals(localNodeId, leadershipService.getLeader(appId.name())); |
| 1105 | } |
| 1106 | |
sangho | 0248ca2 | 2017-05-31 13:22:47 +0900 | [diff] [blame] | 1107 | @Override |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 1108 | public void event(OpenstackSecurityGroupEvent event) { |
| 1109 | switch (event.type()) { |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 1110 | case OPENSTACK_SECURITY_GROUP_RULE_CREATED: |
Jian Li | 4d13870 | 2018-11-27 17:25:28 +0900 | [diff] [blame] | 1111 | eventExecutor.execute(() -> processSgRuleCreate(event)); |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 1112 | break; |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 1113 | case OPENSTACK_SECURITY_GROUP_RULE_REMOVED: |
Jian Li | 4d13870 | 2018-11-27 17:25:28 +0900 | [diff] [blame] | 1114 | eventExecutor.execute(() -> processSgRuleRemove(event)); |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 1115 | break; |
Hyunsun Moon | ae51e73 | 2017-04-25 17:46:21 +0900 | [diff] [blame] | 1116 | case OPENSTACK_SECURITY_GROUP_REMOVED: |
Jian Li | 9d35bd6 | 2018-10-13 01:43:24 +0900 | [diff] [blame] | 1117 | case OPENSTACK_SECURITY_GROUP_CREATED: |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 1118 | default: |
Hyunsun Moon | ae51e73 | 2017-04-25 17:46:21 +0900 | [diff] [blame] | 1119 | // do nothing |
| 1120 | break; |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 1121 | } |
| 1122 | } |
Jian Li | 4d13870 | 2018-11-27 17:25:28 +0900 | [diff] [blame] | 1123 | |
| 1124 | private void processSgRuleCreate(OpenstackSecurityGroupEvent event) { |
| 1125 | if (!isRelevantHelper()) { |
| 1126 | return; |
| 1127 | } |
| 1128 | |
| 1129 | SecurityGroupRule sgRuleToAdd = event.securityGroupRule(); |
| 1130 | securityGroupRuleAdded(sgRuleToAdd); |
| 1131 | log.info("Applied new security group rule {} to ports", sgRuleToAdd.getId()); |
| 1132 | } |
| 1133 | |
| 1134 | private void processSgRuleRemove(OpenstackSecurityGroupEvent event) { |
| 1135 | if (!isRelevantHelper()) { |
| 1136 | return; |
| 1137 | } |
| 1138 | |
| 1139 | SecurityGroupRule sgRuleToRemove = event.securityGroupRule(); |
| 1140 | securityGroupRuleRemoved(sgRuleToRemove); |
| 1141 | log.info("Removed security group rule {} from ports", sgRuleToRemove.getId()); |
| 1142 | } |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 1143 | } |
sangho | 1aaa788 | 2017-05-31 13:22:47 +0900 | [diff] [blame] | 1144 | |
| 1145 | private class InternalNodeListener implements OpenstackNodeListener { |
| 1146 | |
| 1147 | @Override |
| 1148 | public boolean isRelevant(OpenstackNodeEvent event) { |
Jian Li | 2dc3759 | 2019-03-14 17:32:15 +0900 | [diff] [blame] | 1149 | return event.subject().type() == COMPUTE; |
sangho | 1aaa788 | 2017-05-31 13:22:47 +0900 | [diff] [blame] | 1150 | } |
| 1151 | |
Jian Li | 34220ea | 2018-11-14 01:30:24 +0900 | [diff] [blame] | 1152 | private boolean isRelevantHelper() { |
| 1153 | return Objects.equals(localNodeId, leadershipService.getLeader(appId.name())); |
| 1154 | } |
| 1155 | |
sangho | 1aaa788 | 2017-05-31 13:22:47 +0900 | [diff] [blame] | 1156 | @Override |
| 1157 | public void event(OpenstackNodeEvent event) { |
sangho | 1aaa788 | 2017-05-31 13:22:47 +0900 | [diff] [blame] | 1158 | switch (event.type()) { |
| 1159 | case OPENSTACK_NODE_COMPLETE: |
Jian Li | 627e016 | 2020-03-12 17:50:36 +0900 | [diff] [blame] | 1160 | eventExecutor.execute(() -> processNodeComplete(event.subject())); |
sangho | 1aaa788 | 2017-05-31 13:22:47 +0900 | [diff] [blame] | 1161 | break; |
| 1162 | case OPENSTACK_NODE_CREATED: |
| 1163 | case OPENSTACK_NODE_REMOVED: |
| 1164 | case OPENSTACK_NODE_UPDATED: |
| 1165 | case OPENSTACK_NODE_INCOMPLETE: |
| 1166 | default: |
| 1167 | break; |
| 1168 | } |
| 1169 | } |
Jian Li | 4d13870 | 2018-11-27 17:25:28 +0900 | [diff] [blame] | 1170 | |
Jian Li | 627e016 | 2020-03-12 17:50:36 +0900 | [diff] [blame] | 1171 | private void processNodeComplete(OpenstackNode node) { |
Jian Li | 4d13870 | 2018-11-27 17:25:28 +0900 | [diff] [blame] | 1172 | if (!isRelevantHelper()) { |
| 1173 | return; |
| 1174 | } |
| 1175 | |
Jian Li | 627e016 | 2020-03-12 17:50:36 +0900 | [diff] [blame] | 1176 | resetSecurityGroupRulesByNode(node); |
| 1177 | } |
| 1178 | |
| 1179 | private void resetSecurityGroupRulesByNode(OpenstackNode node) { |
| 1180 | if (getUseSecurityGroupFlag()) { |
| 1181 | osFlowRuleService.setUpTableMissEntry(node.intgBridge(), ACL_EGRESS_TABLE); |
| 1182 | initializeConnTrackTable(node.intgBridge(), true); |
| 1183 | initializeAclTable(node.intgBridge(), true); |
| 1184 | initializeIngressTable(node.intgBridge(), true); |
| 1185 | |
| 1186 | securityGroupService.securityGroups().forEach(securityGroup -> |
| 1187 | securityGroup.getRules().forEach( |
| 1188 | OpenstackSecurityGroupHandler.this::securityGroupRuleAdded)); |
| 1189 | } else { |
| 1190 | osFlowRuleService.connectTables(node.intgBridge(), ACL_EGRESS_TABLE, JUMP_TABLE); |
| 1191 | initializeConnTrackTable(node.intgBridge(), false); |
| 1192 | initializeAclTable(node.intgBridge(), false); |
| 1193 | initializeIngressTable(node.intgBridge(), false); |
| 1194 | |
| 1195 | securityGroupService.securityGroups().forEach(securityGroup -> |
| 1196 | securityGroup.getRules().forEach( |
| 1197 | OpenstackSecurityGroupHandler.this::securityGroupRuleRemoved)); |
| 1198 | } |
| 1199 | |
| 1200 | log.info("Reset security group info " + |
Jian Li | 38c82f7 | 2020-04-02 11:15:38 +0900 | [diff] [blame] | 1201 | (getUseSecurityGroupFlag() ? " with " : " without") + " Security Group"); |
Jian Li | 4d13870 | 2018-11-27 17:25:28 +0900 | [diff] [blame] | 1202 | } |
sangho | 1aaa788 | 2017-05-31 13:22:47 +0900 | [diff] [blame] | 1203 | } |
Jian Li | 627e016 | 2020-03-12 17:50:36 +0900 | [diff] [blame] | 1204 | } |