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