lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.onosproject.vtn.manager.impl; |
| 17 | |
jiangrui | c69a7fd | 2015-11-19 15:40:01 +0800 | [diff] [blame] | 18 | import static org.onosproject.net.flow.instructions.ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_SET_TUNNEL_DST; |
lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [diff] [blame] | 19 | import static org.slf4j.LoggerFactory.getLogger; |
| 20 | |
Bob zhou | 59a2106 | 2016-05-12 19:40:05 +0800 | [diff] [blame] | 21 | import java.nio.ByteBuffer; |
lishuai | 858efd3 | 2015-12-04 14:30:36 +0800 | [diff] [blame] | 22 | import java.util.ArrayList; |
lishuai | b43dbf7 | 2016-01-06 11:11:35 +0800 | [diff] [blame] | 23 | import java.util.Arrays; |
lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [diff] [blame] | 24 | import java.util.Collection; |
| 25 | import java.util.HashMap; |
| 26 | import java.util.HashSet; |
| 27 | import java.util.Iterator; |
jiangrui | c69a7fd | 2015-11-19 15:40:01 +0800 | [diff] [blame] | 28 | import java.util.List; |
lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [diff] [blame] | 29 | import java.util.Map; |
| 30 | import java.util.Set; |
Bob zhou | 59a2106 | 2016-05-12 19:40:05 +0800 | [diff] [blame] | 31 | import java.util.UUID; |
lishuai | 74f2d53 | 2015-12-10 17:03:34 +0800 | [diff] [blame] | 32 | import java.util.concurrent.ConcurrentHashMap; |
lishuai | 858efd3 | 2015-12-04 14:30:36 +0800 | [diff] [blame] | 33 | import java.util.stream.Collectors; |
lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [diff] [blame] | 34 | |
| 35 | import org.apache.felix.scr.annotations.Activate; |
| 36 | import org.apache.felix.scr.annotations.Component; |
| 37 | import org.apache.felix.scr.annotations.Deactivate; |
| 38 | import org.apache.felix.scr.annotations.Reference; |
| 39 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 40 | import org.apache.felix.scr.annotations.Service; |
Bob zhou | 59a2106 | 2016-05-12 19:40:05 +0800 | [diff] [blame] | 41 | import org.onlab.packet.ARP; |
| 42 | import org.onlab.packet.Ethernet; |
| 43 | import org.onlab.packet.IPv4; |
jiangrui | c69a7fd | 2015-11-19 15:40:01 +0800 | [diff] [blame] | 44 | import org.onlab.packet.Ip4Address; |
lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [diff] [blame] | 45 | import org.onlab.packet.IpAddress; |
Bob zhou | 59a2106 | 2016-05-12 19:40:05 +0800 | [diff] [blame] | 46 | import org.onlab.packet.IpPrefix; |
lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [diff] [blame] | 47 | import org.onlab.packet.MacAddress; |
| 48 | import org.onlab.util.KryoNamespace; |
| 49 | import org.onosproject.core.ApplicationId; |
| 50 | import org.onosproject.core.CoreService; |
| 51 | import org.onosproject.mastership.MastershipService; |
lishuai | 858efd3 | 2015-12-04 14:30:36 +0800 | [diff] [blame] | 52 | import org.onosproject.net.AnnotationKeys; |
Bob zhou | 59a2106 | 2016-05-12 19:40:05 +0800 | [diff] [blame] | 53 | import org.onosproject.net.ConnectPoint; |
lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [diff] [blame] | 54 | import org.onosproject.net.Device; |
| 55 | import org.onosproject.net.DeviceId; |
| 56 | import org.onosproject.net.Host; |
lishuai | 858efd3 | 2015-12-04 14:30:36 +0800 | [diff] [blame] | 57 | import org.onosproject.net.HostId; |
lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [diff] [blame] | 58 | import org.onosproject.net.Port; |
| 59 | import org.onosproject.net.PortNumber; |
| 60 | import org.onosproject.net.behaviour.BridgeConfig; |
| 61 | import org.onosproject.net.behaviour.BridgeDescription; |
jiangrui | c69a7fd | 2015-11-19 15:40:01 +0800 | [diff] [blame] | 62 | import org.onosproject.net.behaviour.ExtensionTreatmentResolver; |
lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [diff] [blame] | 63 | import org.onosproject.net.config.NetworkConfigService; |
| 64 | import org.onosproject.net.config.basics.BasicDeviceConfig; |
Phaneendra Manda | b212bc9 | 2016-07-08 16:50:11 +0530 | [diff] [blame] | 65 | import org.onosproject.net.config.basics.BasicHostConfig; |
lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [diff] [blame] | 66 | import org.onosproject.net.device.DeviceEvent; |
| 67 | import org.onosproject.net.device.DeviceListener; |
| 68 | import org.onosproject.net.device.DeviceService; |
| 69 | import org.onosproject.net.driver.DriverHandler; |
| 70 | import org.onosproject.net.driver.DriverService; |
jiangrui | c69a7fd | 2015-11-19 15:40:01 +0800 | [diff] [blame] | 71 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
Bob zhou | 59a2106 | 2016-05-12 19:40:05 +0800 | [diff] [blame] | 72 | import org.onosproject.net.flow.FlowEntry; |
| 73 | import org.onosproject.net.flow.FlowRuleService; |
| 74 | import org.onosproject.net.flow.TrafficSelector; |
| 75 | import org.onosproject.net.flow.TrafficTreatment; |
jiangrui | c69a7fd | 2015-11-19 15:40:01 +0800 | [diff] [blame] | 76 | import org.onosproject.net.flow.TrafficTreatment.Builder; |
Bob zhou | 59a2106 | 2016-05-12 19:40:05 +0800 | [diff] [blame] | 77 | import org.onosproject.net.flow.criteria.Criterion; |
jiangrui | c69a7fd | 2015-11-19 15:40:01 +0800 | [diff] [blame] | 78 | import org.onosproject.net.flow.instructions.ExtensionTreatment; |
lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [diff] [blame] | 79 | import org.onosproject.net.flowobjective.Objective; |
jiangrui | c69a7fd | 2015-11-19 15:40:01 +0800 | [diff] [blame] | 80 | import org.onosproject.net.group.DefaultGroupBucket; |
| 81 | import org.onosproject.net.group.DefaultGroupDescription; |
| 82 | import org.onosproject.net.group.DefaultGroupKey; |
| 83 | import org.onosproject.net.group.GroupBucket; |
| 84 | import org.onosproject.net.group.GroupBuckets; |
| 85 | import org.onosproject.net.group.GroupDescription; |
| 86 | import org.onosproject.net.group.GroupKey; |
| 87 | import org.onosproject.net.group.GroupService; |
lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [diff] [blame] | 88 | import org.onosproject.net.host.HostEvent; |
| 89 | import org.onosproject.net.host.HostListener; |
| 90 | import org.onosproject.net.host.HostService; |
Bob zhou | 59a2106 | 2016-05-12 19:40:05 +0800 | [diff] [blame] | 91 | import org.onosproject.net.packet.DefaultOutboundPacket; |
| 92 | import org.onosproject.net.packet.InboundPacket; |
| 93 | import org.onosproject.net.packet.OutboundPacket; |
| 94 | import org.onosproject.net.packet.PacketContext; |
| 95 | import org.onosproject.net.packet.PacketProcessor; |
| 96 | import org.onosproject.net.packet.PacketService; |
lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [diff] [blame] | 97 | import org.onosproject.store.serializers.KryoNamespaces; |
lishuai | b43dbf7 | 2016-01-06 11:11:35 +0800 | [diff] [blame] | 98 | import org.onosproject.store.service.ConsistentMap; |
lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [diff] [blame] | 99 | import org.onosproject.store.service.EventuallyConsistentMap; |
| 100 | import org.onosproject.store.service.LogicalClockService; |
lishuai | b43dbf7 | 2016-01-06 11:11:35 +0800 | [diff] [blame] | 101 | import org.onosproject.store.service.Serializer; |
lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [diff] [blame] | 102 | import org.onosproject.store.service.StorageService; |
lishuai | b43dbf7 | 2016-01-06 11:11:35 +0800 | [diff] [blame] | 103 | import org.onosproject.store.service.Versioned; |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 104 | import org.onosproject.vtn.manager.VtnService; |
lishuai | 858efd3 | 2015-12-04 14:30:36 +0800 | [diff] [blame] | 105 | import org.onosproject.vtn.table.ArpService; |
lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [diff] [blame] | 106 | import org.onosproject.vtn.table.ClassifierService; |
lishuai | 858efd3 | 2015-12-04 14:30:36 +0800 | [diff] [blame] | 107 | import org.onosproject.vtn.table.DnatService; |
lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [diff] [blame] | 108 | import org.onosproject.vtn.table.L2ForwardService; |
lishuai | 858efd3 | 2015-12-04 14:30:36 +0800 | [diff] [blame] | 109 | import org.onosproject.vtn.table.L3ForwardService; |
| 110 | import org.onosproject.vtn.table.SnatService; |
lishuai | 74f2d53 | 2015-12-10 17:03:34 +0800 | [diff] [blame] | 111 | import org.onosproject.vtn.table.impl.ArpServiceImpl; |
lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [diff] [blame] | 112 | import org.onosproject.vtn.table.impl.ClassifierServiceImpl; |
lishuai | 74f2d53 | 2015-12-10 17:03:34 +0800 | [diff] [blame] | 113 | import org.onosproject.vtn.table.impl.DnatServiceImpl; |
lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [diff] [blame] | 114 | import org.onosproject.vtn.table.impl.L2ForwardServiceImpl; |
lishuai | 74f2d53 | 2015-12-10 17:03:34 +0800 | [diff] [blame] | 115 | import org.onosproject.vtn.table.impl.L3ForwardServiceImpl; |
| 116 | import org.onosproject.vtn.table.impl.SnatServiceImpl; |
lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [diff] [blame] | 117 | import org.onosproject.vtn.util.DataPathIdGenerator; |
Bob zhou | 59a2106 | 2016-05-12 19:40:05 +0800 | [diff] [blame] | 118 | import org.onosproject.vtn.util.IpUtil; |
lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [diff] [blame] | 119 | import org.onosproject.vtn.util.VtnConfig; |
| 120 | import org.onosproject.vtn.util.VtnData; |
lishuai | 7dc63d9 | 2015-11-27 17:15:25 +0800 | [diff] [blame] | 121 | import org.onosproject.vtnrsc.AllowedAddressPair; |
| 122 | import org.onosproject.vtnrsc.BindingHostId; |
Bob zhou | 59a2106 | 2016-05-12 19:40:05 +0800 | [diff] [blame] | 123 | import org.onosproject.vtnrsc.DefaultFloatingIp; |
lishuai | 7dc63d9 | 2015-11-27 17:15:25 +0800 | [diff] [blame] | 124 | import org.onosproject.vtnrsc.DefaultVirtualPort; |
| 125 | import org.onosproject.vtnrsc.FixedIp; |
lishuai | 858efd3 | 2015-12-04 14:30:36 +0800 | [diff] [blame] | 126 | import org.onosproject.vtnrsc.FloatingIp; |
Bob zhou | 59a2106 | 2016-05-12 19:40:05 +0800 | [diff] [blame] | 127 | import org.onosproject.vtnrsc.FloatingIpId; |
lishuai | bee5eca | 2016-05-10 19:17:37 +0800 | [diff] [blame] | 128 | import org.onosproject.vtnrsc.RouterId; |
lishuai | 858efd3 | 2015-12-04 14:30:36 +0800 | [diff] [blame] | 129 | import org.onosproject.vtnrsc.RouterInterface; |
lishuai | 7dc63d9 | 2015-11-27 17:15:25 +0800 | [diff] [blame] | 130 | import org.onosproject.vtnrsc.SecurityGroup; |
lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [diff] [blame] | 131 | import org.onosproject.vtnrsc.SegmentationId; |
Wu wenbin | acc10ea | 2016-05-06 16:48:56 +0800 | [diff] [blame] | 132 | import org.onosproject.vtnrsc.Subnet; |
lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [diff] [blame] | 133 | import org.onosproject.vtnrsc.SubnetId; |
| 134 | import org.onosproject.vtnrsc.TenantId; |
| 135 | import org.onosproject.vtnrsc.TenantNetwork; |
| 136 | import org.onosproject.vtnrsc.TenantNetworkId; |
lishuai | 8798bbe | 2016-05-05 16:02:03 +0800 | [diff] [blame] | 137 | import org.onosproject.vtnrsc.TenantRouter; |
lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [diff] [blame] | 138 | import org.onosproject.vtnrsc.VirtualPort; |
| 139 | import org.onosproject.vtnrsc.VirtualPortId; |
lishuai | 858efd3 | 2015-12-04 14:30:36 +0800 | [diff] [blame] | 140 | import org.onosproject.vtnrsc.event.VtnRscEvent; |
| 141 | import org.onosproject.vtnrsc.event.VtnRscEventFeedback; |
| 142 | import org.onosproject.vtnrsc.event.VtnRscListener; |
| 143 | import org.onosproject.vtnrsc.floatingip.FloatingIpService; |
lishuai | 858efd3 | 2015-12-04 14:30:36 +0800 | [diff] [blame] | 144 | import org.onosproject.vtnrsc.routerinterface.RouterInterfaceService; |
| 145 | import org.onosproject.vtnrsc.service.VtnRscService; |
| 146 | import org.onosproject.vtnrsc.subnet.SubnetService; |
lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [diff] [blame] | 147 | import org.onosproject.vtnrsc.tenantnetwork.TenantNetworkService; |
| 148 | import org.onosproject.vtnrsc.virtualport.VirtualPortService; |
| 149 | import org.slf4j.Logger; |
| 150 | |
jiangrui | c69a7fd | 2015-11-19 15:40:01 +0800 | [diff] [blame] | 151 | import com.google.common.collect.Lists; |
lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [diff] [blame] | 152 | import com.google.common.collect.Sets; |
| 153 | |
| 154 | /** |
| 155 | * Provides implementation of VTNService. |
| 156 | */ |
| 157 | @Component(immediate = true) |
| 158 | @Service |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 159 | public class VtnManager implements VtnService { |
lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [diff] [blame] | 160 | private final Logger log = getLogger(getClass()); |
| 161 | private static final String APP_ID = "org.onosproject.app.vtn"; |
| 162 | |
| 163 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 164 | protected NetworkConfigService configService; |
| 165 | |
| 166 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 167 | protected DeviceService deviceService; |
| 168 | |
| 169 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 170 | protected HostService hostService; |
| 171 | |
| 172 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 173 | protected CoreService coreService; |
| 174 | |
| 175 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 176 | protected StorageService storageService; |
| 177 | |
| 178 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 179 | protected TenantNetworkService tenantNetworkService; |
| 180 | |
| 181 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 182 | protected VirtualPortService virtualPortService; |
| 183 | |
| 184 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 185 | protected DriverService driverService; |
| 186 | |
| 187 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 188 | protected LogicalClockService clockService; |
| 189 | |
| 190 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 191 | protected MastershipService mastershipService; |
| 192 | |
jiangrui | c69a7fd | 2015-11-19 15:40:01 +0800 | [diff] [blame] | 193 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 194 | protected GroupService groupService; |
| 195 | |
lishuai | 858efd3 | 2015-12-04 14:30:36 +0800 | [diff] [blame] | 196 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 197 | protected SubnetService subnetService; |
| 198 | |
| 199 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 200 | protected VtnRscService vtnRscService; |
| 201 | |
| 202 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 203 | protected FloatingIpService floatingIpService; |
| 204 | |
| 205 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
lishuai | 858efd3 | 2015-12-04 14:30:36 +0800 | [diff] [blame] | 206 | protected RouterInterfaceService routerInterfaceService; |
| 207 | |
Bob zhou | 59a2106 | 2016-05-12 19:40:05 +0800 | [diff] [blame] | 208 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 209 | protected FlowRuleService flowRuleService; |
| 210 | |
Phaneendra Manda | b212bc9 | 2016-07-08 16:50:11 +0530 | [diff] [blame] | 211 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 212 | protected NetworkConfigService networkConfigService; |
| 213 | |
lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [diff] [blame] | 214 | private ApplicationId appId; |
| 215 | private ClassifierService classifierService; |
| 216 | private L2ForwardService l2ForwardService; |
lishuai | 858efd3 | 2015-12-04 14:30:36 +0800 | [diff] [blame] | 217 | private ArpService arpService; |
| 218 | private L3ForwardService l3ForwardService; |
| 219 | private SnatService snatService; |
| 220 | private DnatService dnatService; |
lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [diff] [blame] | 221 | |
| 222 | private final HostListener hostListener = new InnerHostListener(); |
| 223 | private final DeviceListener deviceListener = new InnerDeviceListener(); |
lishuai | 858efd3 | 2015-12-04 14:30:36 +0800 | [diff] [blame] | 224 | private final VtnRscListener l3EventListener = new VtnL3EventListener(); |
lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [diff] [blame] | 225 | |
lishuai | b43dbf7 | 2016-01-06 11:11:35 +0800 | [diff] [blame] | 226 | private static final String EX_PORT_KEY = "exPortKey"; |
lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [diff] [blame] | 227 | private static final String IFACEID = "ifaceid"; |
| 228 | private static final String CONTROLLER_IP_KEY = "ipaddress"; |
| 229 | public static final String DRIVER_NAME = "onosfw"; |
lishuai | 7dc63d9 | 2015-11-27 17:15:25 +0800 | [diff] [blame] | 230 | private static final String VIRTUALPORT = "vtn-virtual-port"; |
lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [diff] [blame] | 231 | private static final String SWITCHES_OF_CONTROLLER = "switchesOfController"; |
| 232 | private static final String SWITCH_OF_LOCAL_HOST_PORTS = "switchOfLocalHostPorts"; |
lishuai | 8798bbe | 2016-05-05 16:02:03 +0800 | [diff] [blame] | 233 | private static final String ROUTERINF_FLAG_OF_TENANTROUTER = "routerInfFlagOfTenantRouter"; |
lishuai | 858efd3 | 2015-12-04 14:30:36 +0800 | [diff] [blame] | 234 | private static final String HOSTS_OF_SUBNET = "hostsOfSubnet"; |
| 235 | private static final String EX_PORT_OF_DEVICE = "exPortOfDevice"; |
lishuai | b43dbf7 | 2016-01-06 11:11:35 +0800 | [diff] [blame] | 236 | private static final String EX_PORT_MAP = "exPortMap"; |
jiangrui | c69a7fd | 2015-11-19 15:40:01 +0800 | [diff] [blame] | 237 | private static final String DEFAULT_IP = "0.0.0.0"; |
Bob zhou | 59a2106 | 2016-05-12 19:40:05 +0800 | [diff] [blame] | 238 | private static final String FLOATINGSTORE = "vtn-floatingIp"; |
Wu wenbin | acc10ea | 2016-05-06 16:48:56 +0800 | [diff] [blame] | 239 | private static final String USERDATA_IP = "169.254.169.254"; |
lishuai | 858efd3 | 2015-12-04 14:30:36 +0800 | [diff] [blame] | 240 | private static final int SUBNET_NUM = 2; |
Bob zhou | 59a2106 | 2016-05-12 19:40:05 +0800 | [diff] [blame] | 241 | private static final int SNAT_TABLE = 40; |
| 242 | private static final int SNAT_DEFAULT_RULE_PRIORITY = 0; |
| 243 | private static final byte[] ZERO_MAC_ADDRESS = MacAddress.ZERO.toBytes(); |
lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [diff] [blame] | 244 | |
lishuai | 7dc63d9 | 2015-11-27 17:15:25 +0800 | [diff] [blame] | 245 | private EventuallyConsistentMap<VirtualPortId, VirtualPort> vPortStore; |
lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [diff] [blame] | 246 | private EventuallyConsistentMap<IpAddress, Boolean> switchesOfController; |
jiangrui | c69a7fd | 2015-11-19 15:40:01 +0800 | [diff] [blame] | 247 | private EventuallyConsistentMap<DeviceId, NetworkOfLocalHostPorts> switchOfLocalHostPorts; |
lishuai | 858efd3 | 2015-12-04 14:30:36 +0800 | [diff] [blame] | 248 | private EventuallyConsistentMap<SubnetId, Map<HostId, Host>> hostsOfSubnet; |
lishuai | 8798bbe | 2016-05-05 16:02:03 +0800 | [diff] [blame] | 249 | private EventuallyConsistentMap<TenantRouter, Boolean> routerInfFlagOfTenantRouter; |
lishuai | 858efd3 | 2015-12-04 14:30:36 +0800 | [diff] [blame] | 250 | private EventuallyConsistentMap<DeviceId, Port> exPortOfDevice; |
Bob zhou | 59a2106 | 2016-05-12 19:40:05 +0800 | [diff] [blame] | 251 | private EventuallyConsistentMap<IpAddress, FloatingIp> floatingIpStore; |
lishuai | b43dbf7 | 2016-01-06 11:11:35 +0800 | [diff] [blame] | 252 | private static ConsistentMap<String, String> exPortMap; |
lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [diff] [blame] | 253 | |
Bob zhou | 59a2106 | 2016-05-12 19:40:05 +0800 | [diff] [blame] | 254 | private VtnL3PacketProcessor l3PacketProcessor = new VtnL3PacketProcessor(); |
| 255 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 256 | protected PacketService packetService; |
| 257 | |
lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [diff] [blame] | 258 | @Activate |
| 259 | public void activate() { |
| 260 | appId = coreService.registerApplication(APP_ID); |
| 261 | classifierService = new ClassifierServiceImpl(appId); |
| 262 | l2ForwardService = new L2ForwardServiceImpl(appId); |
lishuai | 74f2d53 | 2015-12-10 17:03:34 +0800 | [diff] [blame] | 263 | arpService = new ArpServiceImpl(appId); |
| 264 | l3ForwardService = new L3ForwardServiceImpl(appId); |
| 265 | snatService = new SnatServiceImpl(appId); |
| 266 | dnatService = new DnatServiceImpl(appId); |
lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [diff] [blame] | 267 | |
| 268 | deviceService.addListener(deviceListener); |
| 269 | hostService.addListener(hostListener); |
lishuai | 74f2d53 | 2015-12-10 17:03:34 +0800 | [diff] [blame] | 270 | vtnRscService.addListener(l3EventListener); |
lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [diff] [blame] | 271 | |
| 272 | KryoNamespace.Builder serializer = KryoNamespace.newBuilder() |
| 273 | .register(KryoNamespaces.API) |
| 274 | .register(NetworkOfLocalHostPorts.class) |
| 275 | .register(TenantNetworkId.class) |
| 276 | .register(Host.class) |
| 277 | .register(TenantNetwork.class) |
Bob zhou | 59a2106 | 2016-05-12 19:40:05 +0800 | [diff] [blame] | 278 | .register(TenantNetworkId.class) |
lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [diff] [blame] | 279 | .register(TenantId.class) |
lishuai | 7dc63d9 | 2015-11-27 17:15:25 +0800 | [diff] [blame] | 280 | .register(SubnetId.class) |
| 281 | .register(VirtualPortId.class) |
| 282 | .register(VirtualPort.State.class) |
| 283 | .register(AllowedAddressPair.class) |
| 284 | .register(FixedIp.class) |
Bob zhou | 59a2106 | 2016-05-12 19:40:05 +0800 | [diff] [blame] | 285 | .register(FloatingIp.class) |
| 286 | .register(FloatingIpId.class) |
| 287 | .register(FloatingIp.Status.class) |
| 288 | .register(UUID.class) |
| 289 | .register(DefaultFloatingIp.class) |
lishuai | 7dc63d9 | 2015-11-27 17:15:25 +0800 | [diff] [blame] | 290 | .register(BindingHostId.class) |
| 291 | .register(SecurityGroup.class) |
| 292 | .register(IpAddress.class) |
lishuai | bee5eca | 2016-05-10 19:17:37 +0800 | [diff] [blame] | 293 | .register(DefaultVirtualPort.class) |
| 294 | .register(RouterId.class) |
| 295 | .register(TenantRouter.class); |
Bob zhou | 59a2106 | 2016-05-12 19:40:05 +0800 | [diff] [blame] | 296 | floatingIpStore = storageService |
| 297 | .<IpAddress, FloatingIp>eventuallyConsistentMapBuilder() |
| 298 | .withName(FLOATINGSTORE).withSerializer(serializer) |
| 299 | .withTimestampProvider((k, v) -> clockService.getTimestamp()) |
| 300 | .build(); |
lishuai | 7dc63d9 | 2015-11-27 17:15:25 +0800 | [diff] [blame] | 301 | |
| 302 | vPortStore = storageService |
| 303 | .<VirtualPortId, VirtualPort>eventuallyConsistentMapBuilder() |
| 304 | .withName(VIRTUALPORT).withSerializer(serializer) |
| 305 | .withTimestampProvider((k, v) -> clockService.getTimestamp()) |
| 306 | .build(); |
lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [diff] [blame] | 307 | |
| 308 | switchesOfController = storageService |
| 309 | .<IpAddress, Boolean>eventuallyConsistentMapBuilder() |
| 310 | .withName(SWITCHES_OF_CONTROLLER).withSerializer(serializer) |
| 311 | .withTimestampProvider((k, v) -> clockService.getTimestamp()) |
| 312 | .build(); |
| 313 | |
| 314 | switchOfLocalHostPorts = storageService |
jiangrui | c69a7fd | 2015-11-19 15:40:01 +0800 | [diff] [blame] | 315 | .<DeviceId, NetworkOfLocalHostPorts>eventuallyConsistentMapBuilder() |
| 316 | .withName(SWITCH_OF_LOCAL_HOST_PORTS).withSerializer(serializer) |
| 317 | .withTimestampProvider((k, v) -> clockService.getTimestamp()) |
lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [diff] [blame] | 318 | .build(); |
| 319 | |
lishuai | 858efd3 | 2015-12-04 14:30:36 +0800 | [diff] [blame] | 320 | hostsOfSubnet = storageService |
| 321 | .<SubnetId, Map<HostId, Host>>eventuallyConsistentMapBuilder() |
| 322 | .withName(HOSTS_OF_SUBNET).withSerializer(serializer) |
| 323 | .withTimestampProvider((k, v) -> clockService.getTimestamp()) |
| 324 | .build(); |
| 325 | |
lishuai | 8798bbe | 2016-05-05 16:02:03 +0800 | [diff] [blame] | 326 | routerInfFlagOfTenantRouter = storageService |
| 327 | .<TenantRouter, Boolean>eventuallyConsistentMapBuilder() |
| 328 | .withName(ROUTERINF_FLAG_OF_TENANTROUTER).withSerializer(serializer) |
lishuai | 858efd3 | 2015-12-04 14:30:36 +0800 | [diff] [blame] | 329 | .withTimestampProvider((k, v) -> clockService.getTimestamp()) |
| 330 | .build(); |
| 331 | |
| 332 | exPortOfDevice = storageService |
| 333 | .<DeviceId, Port>eventuallyConsistentMapBuilder() |
| 334 | .withName(EX_PORT_OF_DEVICE).withSerializer(serializer) |
| 335 | .withTimestampProvider((k, v) -> clockService.getTimestamp()) |
| 336 | .build(); |
| 337 | |
lishuai | b43dbf7 | 2016-01-06 11:11:35 +0800 | [diff] [blame] | 338 | exPortMap = storageService |
| 339 | .<String, String>consistentMapBuilder() |
| 340 | .withName(EX_PORT_MAP) |
| 341 | .withApplicationId(appId) |
| 342 | .withPurgeOnUninstall() |
| 343 | .withSerializer(Serializer.using(Arrays.asList(KryoNamespaces.API))) |
| 344 | .build(); |
| 345 | |
Bob zhou | 59a2106 | 2016-05-12 19:40:05 +0800 | [diff] [blame] | 346 | packetService.addProcessor(l3PacketProcessor, PacketProcessor.director(0)); |
lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [diff] [blame] | 347 | log.info("Started"); |
| 348 | } |
| 349 | |
| 350 | @Deactivate |
| 351 | public void deactivate() { |
| 352 | deviceService.removeListener(deviceListener); |
| 353 | hostService.removeListener(hostListener); |
lishuai | 858efd3 | 2015-12-04 14:30:36 +0800 | [diff] [blame] | 354 | vtnRscService.removeListener(l3EventListener); |
lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [diff] [blame] | 355 | log.info("Stopped"); |
| 356 | } |
| 357 | |
| 358 | @Override |
| 359 | public void onControllerDetected(Device controllerDevice) { |
| 360 | if (controllerDevice == null) { |
| 361 | log.error("The controller device is null"); |
| 362 | return; |
| 363 | } |
| 364 | String localIpAddress = controllerDevice.annotations() |
| 365 | .value(CONTROLLER_IP_KEY); |
| 366 | IpAddress localIp = IpAddress.valueOf(localIpAddress); |
| 367 | DeviceId controllerDeviceId = controllerDevice.id(); |
| 368 | DriverHandler handler = driverService.createHandler(controllerDeviceId); |
| 369 | if (mastershipService.isLocalMaster(controllerDeviceId)) { |
| 370 | // Get DataPathIdGenerator |
| 371 | String ipaddress = controllerDevice.annotations().value("ipaddress"); |
| 372 | DataPathIdGenerator dpidGenerator = DataPathIdGenerator.builder() |
| 373 | .addIpAddress(ipaddress).build(); |
| 374 | DeviceId deviceId = dpidGenerator.getDeviceId(); |
| 375 | String dpid = dpidGenerator.getDpId(); |
| 376 | // Inject pipeline driver name |
| 377 | BasicDeviceConfig config = configService.addConfig(deviceId, |
| 378 | BasicDeviceConfig.class); |
| 379 | config.driver(DRIVER_NAME); |
| 380 | configService.applyConfig(deviceId, BasicDeviceConfig.class, config.node()); |
| 381 | // Add Bridge |
lishuai | b43dbf7 | 2016-01-06 11:11:35 +0800 | [diff] [blame] | 382 | Versioned<String> exPortVersioned = exPortMap.get(EX_PORT_KEY); |
| 383 | if (exPortVersioned != null) { |
| 384 | VtnConfig.applyBridgeConfig(handler, dpid, exPortVersioned.value()); |
| 385 | log.info("A new ovs is created in node {}", localIp.toString()); |
| 386 | } |
lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [diff] [blame] | 387 | switchesOfController.put(localIp, true); |
| 388 | } |
| 389 | // Create tunnel in br-int on all controllers |
| 390 | programTunnelConfig(controllerDeviceId, localIp, handler); |
| 391 | } |
| 392 | |
| 393 | @Override |
| 394 | public void onControllerVanished(Device controllerDevice) { |
| 395 | if (controllerDevice == null) { |
| 396 | log.error("The device is null"); |
| 397 | return; |
| 398 | } |
| 399 | String dstIp = controllerDevice.annotations().value(CONTROLLER_IP_KEY); |
| 400 | IpAddress dstIpAddress = IpAddress.valueOf(dstIp); |
| 401 | DeviceId controllerDeviceId = controllerDevice.id(); |
| 402 | if (mastershipService.isLocalMaster(controllerDeviceId)) { |
| 403 | switchesOfController.remove(dstIpAddress); |
| 404 | } |
lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | @Override |
| 408 | public void onOvsDetected(Device device) { |
lishuai | bee5eca | 2016-05-10 19:17:37 +0800 | [diff] [blame] | 409 | if (device == null) { |
| 410 | log.error("The device is null"); |
| 411 | return; |
| 412 | } |
| 413 | if (!mastershipService.isLocalMaster(device.id())) { |
| 414 | return; |
| 415 | } |
lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [diff] [blame] | 416 | // Create tunnel out flow rules |
| 417 | applyTunnelOut(device, Objective.Operation.ADD); |
lishuai | bee5eca | 2016-05-10 19:17:37 +0800 | [diff] [blame] | 418 | // apply L3 arp flows |
| 419 | Iterable<RouterInterface> interfaces = routerInterfaceService |
| 420 | .getRouterInterfaces(); |
| 421 | interfaces.forEach(routerInf -> { |
| 422 | VirtualPort gwPort = virtualPortService.getPort(routerInf.portId()); |
| 423 | if (gwPort == null) { |
| 424 | gwPort = VtnData.getPort(vPortStore, routerInf.portId()); |
| 425 | } |
| 426 | applyL3ArpFlows(device.id(), gwPort, Objective.Operation.ADD); |
| 427 | }); |
lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | @Override |
| 431 | public void onOvsVanished(Device device) { |
lishuai | bee5eca | 2016-05-10 19:17:37 +0800 | [diff] [blame] | 432 | if (device == null) { |
| 433 | log.error("The device is null"); |
| 434 | return; |
| 435 | } |
| 436 | if (!mastershipService.isLocalMaster(device.id())) { |
| 437 | return; |
| 438 | } |
lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [diff] [blame] | 439 | // Remove Tunnel out flow rules |
| 440 | applyTunnelOut(device, Objective.Operation.REMOVE); |
lishuai | bee5eca | 2016-05-10 19:17:37 +0800 | [diff] [blame] | 441 | // apply L3 arp flows |
| 442 | Iterable<RouterInterface> interfaces = routerInterfaceService |
| 443 | .getRouterInterfaces(); |
| 444 | interfaces.forEach(routerInf -> { |
| 445 | VirtualPort gwPort = virtualPortService.getPort(routerInf.portId()); |
| 446 | if (gwPort == null) { |
| 447 | gwPort = VtnData.getPort(vPortStore, routerInf.portId()); |
| 448 | } |
| 449 | applyL3ArpFlows(device.id(), gwPort, Objective.Operation.REMOVE); |
| 450 | }); |
lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | @Override |
| 454 | public void onHostDetected(Host host) { |
lishuai | 858efd3 | 2015-12-04 14:30:36 +0800 | [diff] [blame] | 455 | DeviceId deviceId = host.location().deviceId(); |
| 456 | if (!mastershipService.isLocalMaster(deviceId)) { |
| 457 | return; |
| 458 | } |
| 459 | String ifaceId = host.annotations().value(IFACEID); |
| 460 | if (ifaceId == null) { |
| 461 | log.error("The ifaceId of Host is null"); |
| 462 | return; |
| 463 | } |
lishuai | b43dbf7 | 2016-01-06 11:11:35 +0800 | [diff] [blame] | 464 | programSffAndClassifierHost(host, Objective.Operation.ADD); |
lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [diff] [blame] | 465 | // apply L2 openflow rules |
| 466 | applyHostMonitoredL2Rules(host, Objective.Operation.ADD); |
lishuai | 858efd3 | 2015-12-04 14:30:36 +0800 | [diff] [blame] | 467 | // apply L3 openflow rules |
| 468 | applyHostMonitoredL3Rules(host, Objective.Operation.ADD); |
lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | @Override |
| 472 | public void onHostVanished(Host host) { |
lishuai | 858efd3 | 2015-12-04 14:30:36 +0800 | [diff] [blame] | 473 | DeviceId deviceId = host.location().deviceId(); |
| 474 | if (!mastershipService.isLocalMaster(deviceId)) { |
| 475 | return; |
| 476 | } |
| 477 | String ifaceId = host.annotations().value(IFACEID); |
| 478 | if (ifaceId == null) { |
| 479 | log.error("The ifaceId of Host is null"); |
| 480 | return; |
| 481 | } |
lishuai | b43dbf7 | 2016-01-06 11:11:35 +0800 | [diff] [blame] | 482 | programSffAndClassifierHost(host, Objective.Operation.REMOVE); |
lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [diff] [blame] | 483 | // apply L2 openflow rules |
| 484 | applyHostMonitoredL2Rules(host, Objective.Operation.REMOVE); |
lishuai | 858efd3 | 2015-12-04 14:30:36 +0800 | [diff] [blame] | 485 | // apply L3 openflow rules |
| 486 | applyHostMonitoredL3Rules(host, Objective.Operation.REMOVE); |
lishuai | 590d93a | 2015-12-11 13:05:14 +0800 | [diff] [blame] | 487 | VirtualPortId virtualPortId = VirtualPortId.portId(ifaceId); |
| 488 | vPortStore.remove(virtualPortId); |
lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [diff] [blame] | 489 | } |
| 490 | |
| 491 | private void programTunnelConfig(DeviceId localDeviceId, IpAddress localIp, |
| 492 | DriverHandler localHandler) { |
jiangrui | c69a7fd | 2015-11-19 15:40:01 +0800 | [diff] [blame] | 493 | if (mastershipService.isLocalMaster(localDeviceId)) { |
Hyunsun Moon | dd14e8e | 2016-06-09 16:17:32 -0700 | [diff] [blame] | 494 | VtnConfig.applyTunnelConfig(localHandler, localIp); |
jiangrui | c69a7fd | 2015-11-19 15:40:01 +0800 | [diff] [blame] | 495 | log.info("Add tunnel on {}", localIp); |
| 496 | } |
lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [diff] [blame] | 497 | } |
| 498 | |
| 499 | private void applyTunnelOut(Device device, Objective.Operation type) { |
lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [diff] [blame] | 500 | String controllerIp = VtnData.getControllerIpOfSwitch(device); |
| 501 | if (controllerIp == null) { |
| 502 | log.error("Can't find controller of device: {}", |
| 503 | device.id().toString()); |
| 504 | return; |
| 505 | } |
| 506 | IpAddress ipAddress = IpAddress.valueOf(controllerIp); |
| 507 | if (!switchesOfController.containsKey(ipAddress)) { |
| 508 | log.error("Can't find controller of device: {}", |
| 509 | device.id().toString()); |
| 510 | return; |
| 511 | } |
| 512 | if (type == Objective.Operation.ADD) { |
lishuai | a3e3234 | 2015-12-07 10:59:17 +0800 | [diff] [blame] | 513 | // Save external port |
| 514 | Port export = getExPort(device.id()); |
lishuai | d6f0c9e | 2015-12-16 11:40:01 +0800 | [diff] [blame] | 515 | if (export != null) { |
Bob zhou | 59a2106 | 2016-05-12 19:40:05 +0800 | [diff] [blame] | 516 | classifierService.programExportPortArpClassifierRules(export, |
| 517 | device.id(), |
| 518 | type); |
lishuai | d6f0c9e | 2015-12-16 11:40:01 +0800 | [diff] [blame] | 519 | exPortOfDevice.put(device.id(), export); |
| 520 | } |
lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [diff] [blame] | 521 | switchOfLocalHostPorts.put(device.id(), new NetworkOfLocalHostPorts()); |
| 522 | } else if (type == Objective.Operation.REMOVE) { |
lishuai | a3e3234 | 2015-12-07 10:59:17 +0800 | [diff] [blame] | 523 | exPortOfDevice.remove(device.id()); |
lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [diff] [blame] | 524 | switchOfLocalHostPorts.remove(device.id()); |
| 525 | } |
| 526 | Iterable<Device> devices = deviceService.getAvailableDevices(); |
| 527 | DeviceId localControllerId = VtnData.getControllerId(device, devices); |
| 528 | DriverHandler handler = driverService.createHandler(localControllerId); |
| 529 | Set<PortNumber> ports = VtnConfig.getPortNumbers(handler); |
| 530 | Iterable<Host> allHosts = hostService.getHosts(); |
jiangrui | c69a7fd | 2015-11-19 15:40:01 +0800 | [diff] [blame] | 531 | String tunnelName = "vxlan-" + DEFAULT_IP; |
lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [diff] [blame] | 532 | if (allHosts != null) { |
| 533 | Sets.newHashSet(allHosts).stream().forEach(host -> { |
| 534 | MacAddress hostMac = host.mac(); |
| 535 | String ifaceId = host.annotations().value(IFACEID); |
| 536 | if (ifaceId == null) { |
| 537 | log.error("The ifaceId of Host is null"); |
| 538 | return; |
| 539 | } |
| 540 | VirtualPortId virtualPortId = VirtualPortId.portId(ifaceId); |
| 541 | VirtualPort virtualPort = virtualPortService |
| 542 | .getPort(virtualPortId); |
| 543 | TenantNetwork network = tenantNetworkService |
| 544 | .getNetwork(virtualPort.networkId()); |
| 545 | SegmentationId segmentationId = network.segmentationId(); |
| 546 | DeviceId remoteDeviceId = host.location().deviceId(); |
| 547 | Device remoteDevice = deviceService.getDevice(remoteDeviceId); |
| 548 | String remoteControllerIp = VtnData |
| 549 | .getControllerIpOfSwitch(remoteDevice); |
| 550 | if (remoteControllerIp == null) { |
| 551 | log.error("Can't find remote controller of device: {}", |
| 552 | remoteDeviceId.toString()); |
| 553 | return; |
| 554 | } |
| 555 | IpAddress remoteIpAddress = IpAddress |
| 556 | .valueOf(remoteControllerIp); |
lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [diff] [blame] | 557 | ports.stream() |
| 558 | .filter(p -> p.name().equalsIgnoreCase(tunnelName)) |
| 559 | .forEach(p -> { |
| 560 | l2ForwardService |
| 561 | .programTunnelOut(device.id(), segmentationId, p, |
jiangrui | c69a7fd | 2015-11-19 15:40:01 +0800 | [diff] [blame] | 562 | hostMac, type, remoteIpAddress); |
lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [diff] [blame] | 563 | }); |
| 564 | }); |
| 565 | } |
| 566 | } |
| 567 | |
lishuai | b43dbf7 | 2016-01-06 11:11:35 +0800 | [diff] [blame] | 568 | private void programSffAndClassifierHost(Host host, Objective.Operation type) { |
| 569 | DeviceId deviceId = host.location().deviceId(); |
| 570 | String ifaceId = host.annotations().value(IFACEID); |
| 571 | VirtualPortId virtualPortId = VirtualPortId.portId(ifaceId); |
| 572 | VirtualPort virtualPort = virtualPortService.getPort(virtualPortId); |
| 573 | if (virtualPort == null) { |
| 574 | virtualPort = VtnData.getPort(vPortStore, virtualPortId); |
| 575 | } |
| 576 | TenantId tenantId = virtualPort.tenantId(); |
| 577 | if (Objective.Operation.ADD == type) { |
| 578 | vtnRscService.addDeviceIdOfOvsMap(virtualPortId, tenantId, deviceId); |
| 579 | } else if (Objective.Operation.REMOVE == type) { |
| 580 | vtnRscService.removeDeviceIdOfOvsMap(host, tenantId, deviceId); |
| 581 | } |
| 582 | } |
| 583 | |
lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [diff] [blame] | 584 | private void applyHostMonitoredL2Rules(Host host, Objective.Operation type) { |
| 585 | DeviceId deviceId = host.location().deviceId(); |
| 586 | if (!mastershipService.isLocalMaster(deviceId)) { |
| 587 | return; |
| 588 | } |
| 589 | String ifaceId = host.annotations().value(IFACEID); |
| 590 | if (ifaceId == null) { |
| 591 | log.error("The ifaceId of Host is null"); |
| 592 | return; |
| 593 | } |
| 594 | VirtualPortId virtualPortId = VirtualPortId.portId(ifaceId); |
| 595 | VirtualPort virtualPort = virtualPortService.getPort(virtualPortId); |
| 596 | if (virtualPort == null) { |
lishuai | 858efd3 | 2015-12-04 14:30:36 +0800 | [diff] [blame] | 597 | virtualPort = VtnData.getPort(vPortStore, virtualPortId); |
lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [diff] [blame] | 598 | } |
lishuai | 74f2d53 | 2015-12-10 17:03:34 +0800 | [diff] [blame] | 599 | Iterator<FixedIp> fixip = virtualPort.fixedIps().iterator(); |
| 600 | SubnetId subnetId = null; |
| 601 | if (fixip.hasNext()) { |
| 602 | subnetId = fixip.next().subnetId(); |
| 603 | } |
| 604 | if (subnetId != null) { |
| 605 | Map<HostId, Host> hosts = new ConcurrentHashMap(); |
| 606 | if (hostsOfSubnet.get(subnetId) != null) { |
| 607 | hosts = hostsOfSubnet.get(subnetId); |
| 608 | } |
| 609 | if (type == Objective.Operation.ADD) { |
| 610 | hosts.put(host.id(), host); |
| 611 | hostsOfSubnet.put(subnetId, hosts); |
| 612 | } else if (type == Objective.Operation.REMOVE) { |
| 613 | hosts.remove(host.id()); |
| 614 | if (hosts.size() != 0) { |
| 615 | hostsOfSubnet.put(subnetId, hosts); |
| 616 | } else { |
| 617 | hostsOfSubnet.remove(subnetId); |
| 618 | } |
| 619 | } |
| 620 | } |
lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [diff] [blame] | 621 | |
| 622 | Iterable<Device> devices = deviceService.getAvailableDevices(); |
| 623 | PortNumber inPort = host.location().port(); |
| 624 | MacAddress mac = host.mac(); |
| 625 | Device device = deviceService.getDevice(deviceId); |
| 626 | String controllerIp = VtnData.getControllerIpOfSwitch(device); |
| 627 | IpAddress ipAddress = IpAddress.valueOf(controllerIp); |
| 628 | TenantNetwork network = tenantNetworkService.getNetwork(virtualPort.networkId()); |
| 629 | if (network == null) { |
| 630 | log.error("Can't find network of the host"); |
| 631 | return; |
| 632 | } |
| 633 | SegmentationId segmentationId = network.segmentationId(); |
| 634 | // Get all the tunnel PortNumber in the current node |
| 635 | Iterable<Port> ports = deviceService.getPorts(deviceId); |
| 636 | Collection<PortNumber> localTunnelPorts = VtnData.getLocalTunnelPorts(ports); |
| 637 | // Get all the local vm's PortNumber in the current node |
| 638 | Map<TenantNetworkId, Set<PortNumber>> localHostPorts = switchOfLocalHostPorts |
jiangrui | c69a7fd | 2015-11-19 15:40:01 +0800 | [diff] [blame] | 639 | .get(deviceId).getNetworkOfLocalHostPorts(); |
lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [diff] [blame] | 640 | Set<PortNumber> networkOflocalHostPorts = localHostPorts.get(network.id()); |
jiangrui | c69a7fd | 2015-11-19 15:40:01 +0800 | [diff] [blame] | 641 | for (PortNumber p : localTunnelPorts) { |
| 642 | programGroupTable(deviceId, appId, p, devices, type); |
| 643 | } |
Wu wenbin | acc10ea | 2016-05-06 16:48:56 +0800 | [diff] [blame] | 644 | Subnet subnet = subnetService.getSubnet(subnetId); |
| 645 | String deviceOwner = virtualPort.deviceOwner(); |
| 646 | if (deviceOwner != null) { |
| 647 | if (deviceOwner.equalsIgnoreCase("network:dhcp")) { |
| 648 | Sets.newHashSet(devices).stream() |
| 649 | .filter(d -> d.type() == Device.Type.SWITCH) |
| 650 | .forEach(d -> { |
| 651 | if (subnet != null) { |
| 652 | IpAddress dstIp = IpAddress |
| 653 | .valueOf(USERDATA_IP); |
| 654 | classifierService |
| 655 | .programUserdataClassifierRules(d.id(), |
| 656 | subnet.cidr(), |
| 657 | dstIp, |
| 658 | mac, |
| 659 | segmentationId, |
| 660 | type); |
| 661 | } |
| 662 | }); |
| 663 | } |
| 664 | } |
lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [diff] [blame] | 665 | if (type == Objective.Operation.ADD) { |
lishuai | 7dc63d9 | 2015-11-27 17:15:25 +0800 | [diff] [blame] | 666 | vPortStore.put(virtualPortId, virtualPort); |
lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [diff] [blame] | 667 | if (networkOflocalHostPorts == null) { |
| 668 | networkOflocalHostPorts = new HashSet<PortNumber>(); |
| 669 | localHostPorts.putIfAbsent(network.id(), networkOflocalHostPorts); |
| 670 | } |
| 671 | networkOflocalHostPorts.add(inPort); |
jiangrui | c69a7fd | 2015-11-19 15:40:01 +0800 | [diff] [blame] | 672 | l2ForwardService.programLocalBcastRules(deviceId, segmentationId, |
| 673 | inPort, networkOflocalHostPorts, |
| 674 | localTunnelPorts, |
| 675 | type); |
lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [diff] [blame] | 676 | classifierService.programTunnelIn(deviceId, segmentationId, |
| 677 | localTunnelPorts, |
| 678 | type); |
| 679 | } else if (type == Objective.Operation.REMOVE) { |
jiangrui | c69a7fd | 2015-11-19 15:40:01 +0800 | [diff] [blame] | 680 | if (networkOflocalHostPorts != null) { |
| 681 | l2ForwardService.programLocalBcastRules(deviceId, segmentationId, |
| 682 | inPort, networkOflocalHostPorts, |
| 683 | localTunnelPorts, |
| 684 | type); |
| 685 | networkOflocalHostPorts.remove(inPort); |
| 686 | if (networkOflocalHostPorts.isEmpty()) { |
| 687 | classifierService.programTunnelIn(deviceId, segmentationId, |
| 688 | localTunnelPorts, |
| 689 | type); |
| 690 | switchOfLocalHostPorts.get(deviceId).getNetworkOfLocalHostPorts() |
| 691 | .remove(virtualPort.networkId()); |
| 692 | } |
lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [diff] [blame] | 693 | } |
| 694 | } |
| 695 | |
jiangrui | c69a7fd | 2015-11-19 15:40:01 +0800 | [diff] [blame] | 696 | l2ForwardService.programLocalOut(deviceId, segmentationId, inPort, mac, |
| 697 | type); |
| 698 | |
lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [diff] [blame] | 699 | l2ForwardService.programTunnelBcastRules(deviceId, segmentationId, |
| 700 | networkOflocalHostPorts, |
| 701 | localTunnelPorts, |
| 702 | type); |
| 703 | |
| 704 | programTunnelOuts(devices, ipAddress, segmentationId, mac, |
| 705 | type); |
| 706 | |
| 707 | classifierService.programLocalIn(deviceId, segmentationId, inPort, mac, |
| 708 | appId, type); |
| 709 | } |
| 710 | |
| 711 | private void programTunnelOuts(Iterable<Device> devices, |
| 712 | IpAddress ipAddress, |
| 713 | SegmentationId segmentationId, |
| 714 | MacAddress dstMac, |
| 715 | Objective.Operation type) { |
jiangrui | c69a7fd | 2015-11-19 15:40:01 +0800 | [diff] [blame] | 716 | String tunnelName = "vxlan-" + DEFAULT_IP; |
lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [diff] [blame] | 717 | Sets.newHashSet(devices).stream() |
jiangrui | c69a7fd | 2015-11-19 15:40:01 +0800 | [diff] [blame] | 718 | .filter(d -> d.type() == Device.Type.CONTROLLER) |
| 719 | .filter(d -> !("ovsdb:" + ipAddress).equals(d.id().toString())) |
| 720 | .forEach(d -> { |
lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [diff] [blame] | 721 | DriverHandler handler = driverService.createHandler(d.id()); |
Hyunsun Moon | 1251e19 | 2016-06-07 16:57:05 -0700 | [diff] [blame] | 722 | BridgeConfig bridgeConfig = handler.behaviour(BridgeConfig.class); |
| 723 | Collection<BridgeDescription> bridgeDescriptions = bridgeConfig.getBridges(); |
| 724 | for (BridgeDescription sw : bridgeDescriptions) { |
| 725 | if (sw.name().equals(VtnConfig.DEFAULT_BRIDGE_NAME) && |
| 726 | sw.deviceId().isPresent()) { |
| 727 | List<Port> ports = deviceService.getPorts(sw.deviceId().get()); |
| 728 | ports.stream().filter(p -> p.annotations().value(AnnotationKeys.PORT_NAME) |
| 729 | .equalsIgnoreCase(tunnelName)) |
| 730 | .forEach(p -> l2ForwardService.programTunnelOut( |
| 731 | sw.deviceId().get(), segmentationId, p.number(), |
| 732 | dstMac, type, ipAddress)); |
lishuai | 70304d1 | 2015-12-14 17:15:26 +0800 | [diff] [blame] | 733 | break; |
| 734 | } |
lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [diff] [blame] | 735 | } |
| 736 | }); |
| 737 | } |
| 738 | |
| 739 | private class InnerDeviceListener implements DeviceListener { |
| 740 | |
| 741 | @Override |
| 742 | public void event(DeviceEvent event) { |
| 743 | Device device = event.subject(); |
| 744 | if (Device.Type.CONTROLLER == device.type()) { |
| 745 | if (DeviceEvent.Type.DEVICE_ADDED == event.type()) { |
| 746 | onControllerDetected(device); |
| 747 | } |
| 748 | if (DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED == event.type()) { |
| 749 | if (deviceService.isAvailable(device.id())) { |
| 750 | onControllerDetected(device); |
| 751 | } else { |
| 752 | onControllerVanished(device); |
| 753 | } |
| 754 | } |
| 755 | } else if (Device.Type.SWITCH == device.type()) { |
| 756 | if (DeviceEvent.Type.DEVICE_ADDED == event.type()) { |
| 757 | onOvsDetected(device); |
| 758 | } |
| 759 | if (DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED == event.type()) { |
| 760 | if (deviceService.isAvailable(device.id())) { |
| 761 | onOvsDetected(device); |
| 762 | } else { |
| 763 | onOvsVanished(device); |
| 764 | } |
| 765 | } |
| 766 | } else { |
| 767 | log.info("Do nothing for this device type"); |
| 768 | } |
| 769 | } |
| 770 | } |
| 771 | |
| 772 | private class InnerHostListener implements HostListener { |
| 773 | |
| 774 | @Override |
| 775 | public void event(HostEvent event) { |
| 776 | Host host = event.subject(); |
| 777 | if (HostEvent.Type.HOST_ADDED == event.type()) { |
| 778 | onHostDetected(host); |
| 779 | } else if (HostEvent.Type.HOST_REMOVED == event.type()) { |
| 780 | onHostVanished(host); |
| 781 | } else if (HostEvent.Type.HOST_UPDATED == event.type()) { |
| 782 | onHostVanished(host); |
| 783 | onHostDetected(host); |
| 784 | } |
| 785 | } |
| 786 | |
| 787 | } |
| 788 | |
| 789 | // Local Host Ports of Network. |
| 790 | private class NetworkOfLocalHostPorts { |
| 791 | private final Map<TenantNetworkId, Set<PortNumber>> networkOfLocalHostPorts = |
| 792 | new HashMap<TenantNetworkId, Set<PortNumber>>(); |
| 793 | |
| 794 | public Map<TenantNetworkId, Set<PortNumber>> getNetworkOfLocalHostPorts() { |
| 795 | return networkOfLocalHostPorts; |
| 796 | } |
| 797 | } |
| 798 | |
jiangrui | c69a7fd | 2015-11-19 15:40:01 +0800 | [diff] [blame] | 799 | private void programGroupTable(DeviceId deviceId, ApplicationId appid, |
| 800 | PortNumber portNumber, Iterable<Device> devices, Objective.Operation type) { |
| 801 | if (type.equals(Objective.Operation.REMOVE)) { |
| 802 | return; |
| 803 | } |
| 804 | |
| 805 | List<GroupBucket> buckets = Lists.newArrayList(); |
| 806 | Sets.newHashSet(devices) |
| 807 | .stream() |
| 808 | .filter(d -> d.type() == Device.Type.CONTROLLER) |
| 809 | .filter(d -> !deviceId.equals(d.id())) |
| 810 | .forEach(d -> { |
| 811 | String ipAddress = d.annotations() |
| 812 | .value(CONTROLLER_IP_KEY); |
| 813 | Ip4Address dst = Ip4Address.valueOf(ipAddress); |
| 814 | Builder builder = DefaultTrafficTreatment.builder(); |
| 815 | |
| 816 | DriverHandler handler = driverService.createHandler(deviceId); |
| 817 | ExtensionTreatmentResolver resolver = handler.behaviour(ExtensionTreatmentResolver.class); |
| 818 | ExtensionTreatment treatment = resolver.getExtensionInstruction(NICIRA_SET_TUNNEL_DST.type()); |
| 819 | try { |
| 820 | treatment.setPropertyValue("tunnelDst", dst); |
| 821 | } catch (Exception e) { |
| 822 | log.error("Failed to get extension instruction to set tunnel dst {}", deviceId); |
| 823 | } |
| 824 | |
| 825 | builder.extension(treatment, deviceId); |
| 826 | builder.setOutput(portNumber); |
| 827 | GroupBucket bucket = DefaultGroupBucket |
| 828 | .createAllGroupBucket(builder.build()); |
| 829 | buckets.add(bucket); |
| 830 | }); |
| 831 | final GroupKey key = new DefaultGroupKey(APP_ID.getBytes()); |
| 832 | GroupDescription groupDescription = new DefaultGroupDescription(deviceId, |
| 833 | GroupDescription.Type.ALL, |
| 834 | new GroupBuckets(buckets), |
| 835 | key, |
| 836 | L2ForwardServiceImpl.GROUP_ID, |
| 837 | appid); |
| 838 | groupService.addGroup(groupDescription); |
| 839 | } |
lishuai | 858efd3 | 2015-12-04 14:30:36 +0800 | [diff] [blame] | 840 | |
| 841 | private class VtnL3EventListener implements VtnRscListener { |
| 842 | @Override |
| 843 | public void event(VtnRscEvent event) { |
| 844 | VtnRscEventFeedback l3Feedback = event.subject(); |
| 845 | if (VtnRscEvent.Type.ROUTER_INTERFACE_PUT == event.type()) { |
| 846 | onRouterInterfaceDetected(l3Feedback); |
Phaneendra Manda | b212bc9 | 2016-07-08 16:50:11 +0530 | [diff] [blame] | 847 | } else if (VtnRscEvent.Type.ROUTER_INTERFACE_DELETE == event.type()) { |
lishuai | 858efd3 | 2015-12-04 14:30:36 +0800 | [diff] [blame] | 848 | onRouterInterfaceVanished(l3Feedback); |
lishuai | 762df81 | 2016-01-08 11:51:15 +0800 | [diff] [blame] | 849 | } else if (VtnRscEvent.Type.FLOATINGIP_BIND == event.type()) { |
lishuai | 858efd3 | 2015-12-04 14:30:36 +0800 | [diff] [blame] | 850 | onFloatingIpDetected(l3Feedback); |
lishuai | 762df81 | 2016-01-08 11:51:15 +0800 | [diff] [blame] | 851 | } else if (VtnRscEvent.Type.FLOATINGIP_UNBIND == event.type()) { |
lishuai | 858efd3 | 2015-12-04 14:30:36 +0800 | [diff] [blame] | 852 | onFloatingIpVanished(l3Feedback); |
Phaneendra Manda | b212bc9 | 2016-07-08 16:50:11 +0530 | [diff] [blame] | 853 | } else if (VtnRscEvent.Type.VIRTUAL_PORT_PUT == event.type()) { |
| 854 | onVirtualPortCreated(l3Feedback); |
| 855 | } else if (VtnRscEvent.Type.VIRTUAL_PORT_DELETE == event.type()) { |
| 856 | onVirtualPortDeleted(l3Feedback); |
lishuai | 858efd3 | 2015-12-04 14:30:36 +0800 | [diff] [blame] | 857 | } |
| 858 | } |
| 859 | |
| 860 | } |
| 861 | |
| 862 | @Override |
| 863 | public void onRouterInterfaceDetected(VtnRscEventFeedback l3Feedback) { |
| 864 | Objective.Operation operation = Objective.Operation.ADD; |
| 865 | RouterInterface routerInf = l3Feedback.routerInterface(); |
lishuai | 762df81 | 2016-01-08 11:51:15 +0800 | [diff] [blame] | 866 | VirtualPort gwPort = virtualPortService.getPort(routerInf.portId()); |
| 867 | vPortStore.put(gwPort.portId(), gwPort); |
lishuai | 858efd3 | 2015-12-04 14:30:36 +0800 | [diff] [blame] | 868 | Iterable<RouterInterface> interfaces = routerInterfaceService |
| 869 | .getRouterInterfaces(); |
lishuai | 8798bbe | 2016-05-05 16:02:03 +0800 | [diff] [blame] | 870 | Set<RouterInterface> interfacesSet = Sets.newHashSet(interfaces).stream() |
| 871 | .filter(r -> r.tenantId().equals(routerInf.tenantId())) |
| 872 | .filter(r -> r.routerId().equals(routerInf.routerId())) |
lishuai | 858efd3 | 2015-12-04 14:30:36 +0800 | [diff] [blame] | 873 | .collect(Collectors.toSet()); |
lishuai | 8798bbe | 2016-05-05 16:02:03 +0800 | [diff] [blame] | 874 | TenantRouter tenantRouter = TenantRouter |
| 875 | .tenantRouter(routerInf.tenantId(), routerInf.routerId()); |
| 876 | if (routerInfFlagOfTenantRouter.get(tenantRouter) != null) { |
lishuai | 858efd3 | 2015-12-04 14:30:36 +0800 | [diff] [blame] | 877 | programRouterInterface(routerInf, operation); |
| 878 | } else { |
| 879 | if (interfacesSet.size() >= SUBNET_NUM) { |
| 880 | programInterfacesSet(interfacesSet, operation); |
| 881 | } |
| 882 | } |
lishuai | bee5eca | 2016-05-10 19:17:37 +0800 | [diff] [blame] | 883 | // apply L3 arp flows |
| 884 | applyL3ArpFlows(null, gwPort, operation); |
lishuai | 858efd3 | 2015-12-04 14:30:36 +0800 | [diff] [blame] | 885 | } |
| 886 | |
| 887 | @Override |
| 888 | public void onRouterInterfaceVanished(VtnRscEventFeedback l3Feedback) { |
| 889 | Objective.Operation operation = Objective.Operation.REMOVE; |
| 890 | RouterInterface routerInf = l3Feedback.routerInterface(); |
| 891 | Iterable<RouterInterface> interfaces = routerInterfaceService |
| 892 | .getRouterInterfaces(); |
| 893 | Set<RouterInterface> interfacesSet = Sets.newHashSet(interfaces) |
| 894 | .stream().filter(r -> r.tenantId().equals(routerInf.tenantId())) |
| 895 | .collect(Collectors.toSet()); |
lishuai | 8798bbe | 2016-05-05 16:02:03 +0800 | [diff] [blame] | 896 | TenantRouter tenantRouter = TenantRouter |
| 897 | .tenantRouter(routerInf.tenantId(), routerInf.routerId()); |
| 898 | if (routerInfFlagOfTenantRouter.get(tenantRouter) != null) { |
lishuai | 858efd3 | 2015-12-04 14:30:36 +0800 | [diff] [blame] | 899 | programRouterInterface(routerInf, operation); |
| 900 | if (interfacesSet.size() == 1) { |
lishuai | 8798bbe | 2016-05-05 16:02:03 +0800 | [diff] [blame] | 901 | routerInfFlagOfTenantRouter.remove(tenantRouter); |
lishuai | 858efd3 | 2015-12-04 14:30:36 +0800 | [diff] [blame] | 902 | interfacesSet.stream().forEach(r -> { |
| 903 | programRouterInterface(r, operation); |
| 904 | }); |
| 905 | } |
| 906 | } |
lishuai | 762df81 | 2016-01-08 11:51:15 +0800 | [diff] [blame] | 907 | VirtualPort gwPort = virtualPortService.getPort(routerInf.portId()); |
| 908 | if (gwPort == null) { |
| 909 | gwPort = VtnData.getPort(vPortStore, routerInf.portId()); |
| 910 | } |
| 911 | vPortStore.remove(gwPort.portId()); |
lishuai | bee5eca | 2016-05-10 19:17:37 +0800 | [diff] [blame] | 912 | // apply L3 arp flows |
| 913 | applyL3ArpFlows(null, gwPort, operation); |
lishuai | 858efd3 | 2015-12-04 14:30:36 +0800 | [diff] [blame] | 914 | } |
| 915 | |
| 916 | @Override |
| 917 | public void onFloatingIpDetected(VtnRscEventFeedback l3Feedback) { |
Bob zhou | 59a2106 | 2016-05-12 19:40:05 +0800 | [diff] [blame] | 918 | floatingIpStore.put(l3Feedback.floatingIp().floatingIp(), |
| 919 | l3Feedback.floatingIp()); |
lishuai | 762df81 | 2016-01-08 11:51:15 +0800 | [diff] [blame] | 920 | programFloatingIpEvent(l3Feedback, VtnRscEvent.Type.FLOATINGIP_BIND); |
lishuai | 858efd3 | 2015-12-04 14:30:36 +0800 | [diff] [blame] | 921 | } |
| 922 | |
| 923 | @Override |
| 924 | public void onFloatingIpVanished(VtnRscEventFeedback l3Feedback) { |
Bob zhou | 59a2106 | 2016-05-12 19:40:05 +0800 | [diff] [blame] | 925 | floatingIpStore.remove(l3Feedback.floatingIp().floatingIp()); |
lishuai | 762df81 | 2016-01-08 11:51:15 +0800 | [diff] [blame] | 926 | programFloatingIpEvent(l3Feedback, VtnRscEvent.Type.FLOATINGIP_UNBIND); |
lishuai | 858efd3 | 2015-12-04 14:30:36 +0800 | [diff] [blame] | 927 | } |
| 928 | |
Phaneendra Manda | b212bc9 | 2016-07-08 16:50:11 +0530 | [diff] [blame] | 929 | public void onVirtualPortCreated(VtnRscEventFeedback l3Feedback) { |
| 930 | VirtualPort vPort = l3Feedback.virtualPort(); |
| 931 | BasicHostConfig basicHostConfig = networkConfigService.addConfig(HostId.hostId(vPort.macAddress()), |
| 932 | BasicHostConfig.class); |
| 933 | Set<IpAddress> ips = new HashSet<>(); |
| 934 | for (FixedIp fixedIp : vPort.fixedIps()) { |
| 935 | ips.add(fixedIp.ip()); |
| 936 | } |
| 937 | basicHostConfig.setIps(ips).apply(); |
| 938 | } |
| 939 | |
| 940 | public void onVirtualPortDeleted(VtnRscEventFeedback l3Feedback) { |
| 941 | VirtualPort vPort = l3Feedback.virtualPort(); |
| 942 | HostId hostId = HostId.hostId(vPort.macAddress()); |
| 943 | BasicHostConfig basicHostConfig = networkConfigService.addConfig(hostId, |
| 944 | BasicHostConfig.class); |
| 945 | Set<IpAddress> ips = hostService.getHost(hostId).ipAddresses(); |
| 946 | for (FixedIp fixedIp : vPort.fixedIps()) { |
| 947 | ips.remove(fixedIp.ip()); |
| 948 | } |
| 949 | basicHostConfig.setIps(ips).apply(); |
| 950 | } |
| 951 | |
lishuai | 858efd3 | 2015-12-04 14:30:36 +0800 | [diff] [blame] | 952 | private void programInterfacesSet(Set<RouterInterface> interfacesSet, |
| 953 | Objective.Operation operation) { |
| 954 | int subnetVmNum = 0; |
| 955 | for (RouterInterface r : interfacesSet) { |
| 956 | // Get all the host of the subnet |
| 957 | Map<HostId, Host> hosts = hostsOfSubnet.get(r.subnetId()); |
lishuai | d6f0c9e | 2015-12-16 11:40:01 +0800 | [diff] [blame] | 958 | if (hosts != null && hosts.size() > 0) { |
lishuai | 858efd3 | 2015-12-04 14:30:36 +0800 | [diff] [blame] | 959 | subnetVmNum++; |
| 960 | if (subnetVmNum >= SUBNET_NUM) { |
lishuai | 8798bbe | 2016-05-05 16:02:03 +0800 | [diff] [blame] | 961 | TenantRouter tenantRouter = TenantRouter |
| 962 | .tenantRouter(r.tenantId(), r.routerId()); |
| 963 | routerInfFlagOfTenantRouter.put(tenantRouter, true); |
lishuai | 858efd3 | 2015-12-04 14:30:36 +0800 | [diff] [blame] | 964 | interfacesSet.stream().forEach(f -> { |
| 965 | programRouterInterface(f, operation); |
| 966 | }); |
| 967 | break; |
| 968 | } |
| 969 | } |
| 970 | } |
| 971 | } |
| 972 | |
| 973 | private void programRouterInterface(RouterInterface routerInf, |
| 974 | Objective.Operation operation) { |
lishuai | 8798bbe | 2016-05-05 16:02:03 +0800 | [diff] [blame] | 975 | TenantRouter tenantRouter = TenantRouter |
| 976 | .tenantRouter(routerInf.tenantId(), routerInf.routerId()); |
| 977 | SegmentationId l3vni = vtnRscService.getL3vni(tenantRouter); |
lishuai | 858efd3 | 2015-12-04 14:30:36 +0800 | [diff] [blame] | 978 | // Get all the host of the subnet |
| 979 | Map<HostId, Host> hosts = hostsOfSubnet.get(routerInf.subnetId()); |
| 980 | hosts.values().stream().forEach(h -> { |
| 981 | applyEastWestL3Flows(h, l3vni, operation); |
| 982 | }); |
| 983 | } |
| 984 | |
lishuai | bee5eca | 2016-05-10 19:17:37 +0800 | [diff] [blame] | 985 | private void applyL3ArpFlows(DeviceId deviceId, VirtualPort gwPort, |
| 986 | Objective.Operation operation) { |
| 987 | IpAddress ip = null; |
| 988 | Iterator<FixedIp> gwIps = gwPort.fixedIps().iterator(); |
| 989 | if (gwIps.hasNext()) { |
| 990 | ip = gwIps.next().ip(); |
| 991 | } |
| 992 | IpAddress gwIp = ip; |
| 993 | MacAddress gwMac = gwPort.macAddress(); |
| 994 | TenantNetwork network = tenantNetworkService |
| 995 | .getNetwork(gwPort.networkId()); |
| 996 | if (deviceId != null) { |
| 997 | // Arp rules |
| 998 | DriverHandler handler = driverService.createHandler(deviceId); |
| 999 | arpService.programArpRules(handler, deviceId, gwIp, |
| 1000 | network.segmentationId(), gwMac, |
| 1001 | operation); |
| 1002 | } else { |
| 1003 | Iterable<Device> devices = deviceService.getAvailableDevices(); |
| 1004 | Sets.newHashSet(devices).stream() |
| 1005 | .filter(d -> Device.Type.SWITCH == d.type()).forEach(d -> { |
| 1006 | // Arp rules |
| 1007 | DriverHandler handler = driverService.createHandler(d.id()); |
| 1008 | arpService.programArpRules(handler, d.id(), gwIp, |
| 1009 | network.segmentationId(), gwMac, |
| 1010 | operation); |
| 1011 | }); |
| 1012 | } |
| 1013 | } |
| 1014 | |
lishuai | 858efd3 | 2015-12-04 14:30:36 +0800 | [diff] [blame] | 1015 | private void applyEastWestL3Flows(Host h, SegmentationId l3vni, |
| 1016 | Objective.Operation operation) { |
| 1017 | if (!mastershipService.isLocalMaster(h.location().deviceId())) { |
| 1018 | log.debug("not master device:{}", h.location().deviceId()); |
| 1019 | return; |
| 1020 | } |
| 1021 | String ifaceId = h.annotations().value(IFACEID); |
| 1022 | VirtualPort hPort = virtualPortService |
| 1023 | .getPort(VirtualPortId.portId(ifaceId)); |
| 1024 | if (hPort == null) { |
| 1025 | hPort = VtnData.getPort(vPortStore, VirtualPortId.portId(ifaceId)); |
| 1026 | } |
| 1027 | IpAddress srcIp = null; |
| 1028 | IpAddress srcGwIp = null; |
| 1029 | MacAddress srcVmGwMac = null; |
| 1030 | SubnetId srcSubnetId = null; |
| 1031 | Iterator<FixedIp> srcIps = hPort.fixedIps().iterator(); |
| 1032 | if (srcIps.hasNext()) { |
| 1033 | FixedIp fixedIp = srcIps.next(); |
| 1034 | srcIp = fixedIp.ip(); |
| 1035 | srcSubnetId = fixedIp.subnetId(); |
| 1036 | srcGwIp = subnetService.getSubnet(srcSubnetId).gatewayIp(); |
| 1037 | FixedIp fixedGwIp = FixedIp.fixedIp(srcSubnetId, srcGwIp); |
| 1038 | VirtualPort gwPort = virtualPortService.getPort(fixedGwIp); |
| 1039 | if (gwPort == null) { |
| 1040 | gwPort = VtnData.getPort(vPortStore, fixedGwIp); |
| 1041 | } |
| 1042 | srcVmGwMac = gwPort.macAddress(); |
| 1043 | } |
| 1044 | TenantNetwork network = tenantNetworkService |
| 1045 | .getNetwork(hPort.networkId()); |
lishuai | bee5eca | 2016-05-10 19:17:37 +0800 | [diff] [blame] | 1046 | IpAddress dstVmIP = srcIp; |
| 1047 | MacAddress dstVmGwMac = srcVmGwMac; |
| 1048 | TenantId tenantId = hPort.tenantId(); |
lishuai | 858efd3 | 2015-12-04 14:30:36 +0800 | [diff] [blame] | 1049 | // Classifier rules |
lishuai | bee5eca | 2016-05-10 19:17:37 +0800 | [diff] [blame] | 1050 | if (operation == Objective.Operation.ADD) { |
| 1051 | sendEastWestL3Flows(h, srcVmGwMac, l3vni, srcGwIp, network, |
| 1052 | dstVmIP, dstVmGwMac, operation); |
| 1053 | } else if (operation == Objective.Operation.REMOVE) { |
| 1054 | FloatingIp floatingIp = null; |
| 1055 | Iterable<FloatingIp> floatingIps = floatingIpService.getFloatingIps(); |
| 1056 | Set<FloatingIp> floatingIpSet = Sets.newHashSet(floatingIps).stream() |
| 1057 | .filter(f -> f.tenantId().equals(tenantId)) |
| 1058 | .collect(Collectors.toSet()); |
| 1059 | for (FloatingIp f : floatingIpSet) { |
| 1060 | IpAddress fixedIp = f.fixedIp(); |
| 1061 | if (fixedIp != null && fixedIp.equals(srcIp)) { |
| 1062 | floatingIp = f; |
| 1063 | break; |
| 1064 | } |
| 1065 | } |
| 1066 | if (floatingIp == null) { |
| 1067 | sendEastWestL3Flows(h, srcVmGwMac, l3vni, srcGwIp, network, |
| 1068 | dstVmIP, dstVmGwMac, operation); |
| 1069 | } |
| 1070 | } |
| 1071 | } |
| 1072 | |
| 1073 | private void sendEastWestL3Flows(Host h, MacAddress srcVmGwMac, |
| 1074 | SegmentationId l3vni, IpAddress srcGwIp, |
| 1075 | TenantNetwork network, IpAddress dstVmIP, |
| 1076 | MacAddress dstVmGwMac, |
| 1077 | Objective.Operation operation) { |
lishuai | 858efd3 | 2015-12-04 14:30:36 +0800 | [diff] [blame] | 1078 | classifierService |
| 1079 | .programL3InPortClassifierRules(h.location().deviceId(), |
| 1080 | h.location().port(), h.mac(), |
| 1081 | srcVmGwMac, l3vni, operation); |
lishuai | bee5eca | 2016-05-10 19:17:37 +0800 | [diff] [blame] | 1082 | classifierService |
| 1083 | .programArpClassifierRules(h.location().deviceId(), |
| 1084 | h.location().port(), srcGwIp, |
| 1085 | network.segmentationId(), operation); |
lishuai | 858efd3 | 2015-12-04 14:30:36 +0800 | [diff] [blame] | 1086 | Iterable<Device> devices = deviceService.getAvailableDevices(); |
lishuai | 858efd3 | 2015-12-04 14:30:36 +0800 | [diff] [blame] | 1087 | Sets.newHashSet(devices).stream() |
| 1088 | .filter(d -> Device.Type.SWITCH == d.type()).forEach(d -> { |
| 1089 | // L3FWD rules |
lishuai | bee5eca | 2016-05-10 19:17:37 +0800 | [diff] [blame] | 1090 | l3ForwardService.programRouteRules(d.id(), l3vni, dstVmIP, |
lishuai | 858efd3 | 2015-12-04 14:30:36 +0800 | [diff] [blame] | 1091 | network.segmentationId(), |
lishuai | bee5eca | 2016-05-10 19:17:37 +0800 | [diff] [blame] | 1092 | dstVmGwMac, h.mac(), |
lishuai | 858efd3 | 2015-12-04 14:30:36 +0800 | [diff] [blame] | 1093 | operation); |
| 1094 | }); |
| 1095 | } |
| 1096 | |
| 1097 | private void programFloatingIpEvent(VtnRscEventFeedback l3Feedback, |
| 1098 | VtnRscEvent.Type type) { |
| 1099 | FloatingIp floaingIp = l3Feedback.floatingIp(); |
| 1100 | if (floaingIp != null) { |
| 1101 | VirtualPortId vmPortId = floaingIp.portId(); |
| 1102 | VirtualPort vmPort = virtualPortService.getPort(vmPortId); |
| 1103 | VirtualPort fipPort = virtualPortService |
| 1104 | .getPort(floaingIp.networkId(), floaingIp.floatingIp()); |
| 1105 | if (vmPort == null) { |
| 1106 | vmPort = VtnData.getPort(vPortStore, vmPortId); |
| 1107 | } |
| 1108 | if (fipPort == null) { |
| 1109 | fipPort = VtnData.getPort(vPortStore, floaingIp.networkId(), |
| 1110 | floaingIp.floatingIp()); |
| 1111 | } |
| 1112 | Set<Host> hostSet = hostService.getHostsByMac(vmPort.macAddress()); |
| 1113 | Host host = null; |
| 1114 | for (Host h : hostSet) { |
| 1115 | String ifaceid = h.annotations().value(IFACEID); |
| 1116 | if (ifaceid != null && ifaceid.equals(vmPortId.portId())) { |
| 1117 | host = h; |
| 1118 | break; |
| 1119 | } |
| 1120 | } |
| 1121 | if (host != null && vmPort != null && fipPort != null) { |
| 1122 | DeviceId deviceId = host.location().deviceId(); |
| 1123 | Port exPort = exPortOfDevice.get(deviceId); |
lishuai | 8798bbe | 2016-05-05 16:02:03 +0800 | [diff] [blame] | 1124 | TenantRouter tenantRouter = TenantRouter |
| 1125 | .tenantRouter(floaingIp.tenantId(), floaingIp.routerId()); |
| 1126 | SegmentationId l3vni = vtnRscService.getL3vni(tenantRouter); |
lishuai | 858efd3 | 2015-12-04 14:30:36 +0800 | [diff] [blame] | 1127 | // Floating ip BIND |
lishuai | 762df81 | 2016-01-08 11:51:15 +0800 | [diff] [blame] | 1128 | if (type == VtnRscEvent.Type.FLOATINGIP_BIND) { |
lishuai | 16c05af | 2016-01-21 18:39:05 +0800 | [diff] [blame] | 1129 | vPortStore.put(fipPort.portId(), fipPort); |
lishuai | bee5eca | 2016-05-10 19:17:37 +0800 | [diff] [blame] | 1130 | applyNorthSouthL3Flows(deviceId, false, tenantRouter, host, |
| 1131 | vmPort, fipPort, floaingIp, l3vni, |
| 1132 | exPort, Objective.Operation.ADD); |
lishuai | 762df81 | 2016-01-08 11:51:15 +0800 | [diff] [blame] | 1133 | } else if (type == VtnRscEvent.Type.FLOATINGIP_UNBIND) { |
lishuai | 858efd3 | 2015-12-04 14:30:36 +0800 | [diff] [blame] | 1134 | // Floating ip UNBIND |
lishuai | bee5eca | 2016-05-10 19:17:37 +0800 | [diff] [blame] | 1135 | applyNorthSouthL3Flows(deviceId, false, tenantRouter, host, |
| 1136 | vmPort, fipPort, floaingIp, l3vni, |
| 1137 | exPort, |
lishuai | 858efd3 | 2015-12-04 14:30:36 +0800 | [diff] [blame] | 1138 | Objective.Operation.REMOVE); |
lishuai | 16c05af | 2016-01-21 18:39:05 +0800 | [diff] [blame] | 1139 | vPortStore.remove(fipPort.portId()); |
lishuai | 858efd3 | 2015-12-04 14:30:36 +0800 | [diff] [blame] | 1140 | } |
| 1141 | } |
| 1142 | } |
| 1143 | } |
| 1144 | |
lishuai | bee5eca | 2016-05-10 19:17:37 +0800 | [diff] [blame] | 1145 | private void sendNorthSouthL3Flows(DeviceId deviceId, FloatingIp floatingIp, |
| 1146 | IpAddress dstVmGwIp, |
| 1147 | MacAddress dstVmGwMac, |
| 1148 | SegmentationId l3vni, |
| 1149 | TenantNetwork vmNetwork, |
| 1150 | VirtualPort vmPort, Host host, |
| 1151 | Objective.Operation operation) { |
| 1152 | l3ForwardService |
| 1153 | .programRouteRules(deviceId, l3vni, floatingIp.fixedIp(), |
| 1154 | vmNetwork.segmentationId(), dstVmGwMac, |
| 1155 | vmPort.macAddress(), operation); |
| 1156 | classifierService.programL3InPortClassifierRules(deviceId, |
| 1157 | host.location().port(), |
| 1158 | host.mac(), dstVmGwMac, |
| 1159 | l3vni, operation); |
| 1160 | classifierService.programArpClassifierRules(deviceId, host.location() |
| 1161 | .port(), dstVmGwIp, vmNetwork.segmentationId(), operation); |
| 1162 | } |
| 1163 | |
| 1164 | private void applyNorthSouthL3Flows(DeviceId deviceId, boolean hostFlag, |
| 1165 | TenantRouter tenantRouter, Host host, |
lishuai | 858efd3 | 2015-12-04 14:30:36 +0800 | [diff] [blame] | 1166 | VirtualPort vmPort, VirtualPort fipPort, |
| 1167 | FloatingIp floatingIp, |
lishuai | 8798bbe | 2016-05-05 16:02:03 +0800 | [diff] [blame] | 1168 | SegmentationId l3vni, Port exPort, |
lishuai | 858efd3 | 2015-12-04 14:30:36 +0800 | [diff] [blame] | 1169 | Objective.Operation operation) { |
| 1170 | if (!mastershipService.isLocalMaster(deviceId)) { |
| 1171 | log.debug("not master device:{}", deviceId); |
| 1172 | return; |
| 1173 | } |
| 1174 | List gwIpMac = getGwIpAndMac(vmPort); |
| 1175 | IpAddress dstVmGwIp = (IpAddress) gwIpMac.get(0); |
| 1176 | MacAddress dstVmGwMac = (MacAddress) gwIpMac.get(1); |
lishuai | 858efd3 | 2015-12-04 14:30:36 +0800 | [diff] [blame] | 1177 | TenantNetwork vmNetwork = tenantNetworkService |
| 1178 | .getNetwork(vmPort.networkId()); |
| 1179 | TenantNetwork fipNetwork = tenantNetworkService |
| 1180 | .getNetwork(fipPort.networkId()); |
| 1181 | // L3 downlink traffic flow |
lishuai | a3e3234 | 2015-12-07 10:59:17 +0800 | [diff] [blame] | 1182 | MacAddress exPortMac = MacAddress.valueOf(exPort.annotations() |
| 1183 | .value(AnnotationKeys.PORT_MAC)); |
lishuai | 858efd3 | 2015-12-04 14:30:36 +0800 | [diff] [blame] | 1184 | classifierService.programL3ExPortClassifierRules(deviceId, exPort.number(), |
| 1185 | floatingIp.floatingIp(), operation); |
lishuai | 858efd3 | 2015-12-04 14:30:36 +0800 | [diff] [blame] | 1186 | dnatService.programRules(deviceId, floatingIp.floatingIp(), |
Bob zhou | 59a2106 | 2016-05-12 19:40:05 +0800 | [diff] [blame] | 1187 | exPortMac, floatingIp.fixedIp(), |
lishuai | 8798bbe | 2016-05-05 16:02:03 +0800 | [diff] [blame] | 1188 | l3vni, operation); |
lishuai | 858efd3 | 2015-12-04 14:30:36 +0800 | [diff] [blame] | 1189 | |
Bob zhou | 59a2106 | 2016-05-12 19:40:05 +0800 | [diff] [blame] | 1190 | Subnet subnet = getSubnetOfFloatingIP(floatingIp); |
| 1191 | IpPrefix ipPrefix = subnet.cidr(); |
| 1192 | snatService.programSnatSameSegmentUploadControllerRules(deviceId, l3vni, |
| 1193 | floatingIp.fixedIp(), |
| 1194 | floatingIp.floatingIp(), |
| 1195 | ipPrefix, |
| 1196 | operation); |
lishuai | 858efd3 | 2015-12-04 14:30:36 +0800 | [diff] [blame] | 1197 | // L3 uplink traffic flow |
lishuai | bee5eca | 2016-05-10 19:17:37 +0800 | [diff] [blame] | 1198 | if (operation == Objective.Operation.ADD) { |
| 1199 | sendNorthSouthL3Flows(deviceId, floatingIp, dstVmGwIp, dstVmGwMac, |
| 1200 | l3vni, vmNetwork, vmPort, host, operation); |
Bob zhou | 59a2106 | 2016-05-12 19:40:05 +0800 | [diff] [blame] | 1201 | l2ForwardService |
| 1202 | .programExternalOut(deviceId, fipNetwork.segmentationId(), |
| 1203 | exPort.number(), exPortMac, operation); |
lishuai | bee5eca | 2016-05-10 19:17:37 +0800 | [diff] [blame] | 1204 | } else if (operation == Objective.Operation.REMOVE) { |
| 1205 | if (hostFlag || (!hostFlag |
| 1206 | && routerInfFlagOfTenantRouter.get(tenantRouter) == null)) { |
| 1207 | sendNorthSouthL3Flows(deviceId, floatingIp, dstVmGwIp, dstVmGwMac, |
| 1208 | l3vni, vmNetwork, vmPort, host, operation); |
| 1209 | } |
| 1210 | Iterable<FloatingIp> floatingIps = floatingIpService.getFloatingIps(); |
| 1211 | boolean exPortFlag = true; |
| 1212 | if (floatingIps != null) { |
| 1213 | Set<FloatingIp> floatingIpSet = Sets.newHashSet(floatingIps); |
| 1214 | for (FloatingIp fip : floatingIpSet) { |
| 1215 | if (fip.fixedIp() != null) { |
| 1216 | exPortFlag = false; |
| 1217 | break; |
| 1218 | } |
| 1219 | } |
| 1220 | } |
| 1221 | if (exPortFlag) { |
Bob zhou | 59a2106 | 2016-05-12 19:40:05 +0800 | [diff] [blame] | 1222 | l2ForwardService.programExternalOut(deviceId, |
| 1223 | fipNetwork.segmentationId(), |
| 1224 | exPort.number(), exPortMac, |
| 1225 | operation); |
lishuai | bee5eca | 2016-05-10 19:17:37 +0800 | [diff] [blame] | 1226 | } |
Bob zhou | 59a2106 | 2016-05-12 19:40:05 +0800 | [diff] [blame] | 1227 | removeRulesInSnat(deviceId, floatingIp.fixedIp()); |
lishuai | bee5eca | 2016-05-10 19:17:37 +0800 | [diff] [blame] | 1228 | } |
lishuai | 858efd3 | 2015-12-04 14:30:36 +0800 | [diff] [blame] | 1229 | } |
| 1230 | |
| 1231 | private Port getExPort(DeviceId deviceId) { |
| 1232 | List<Port> ports = deviceService.getPorts(deviceId); |
| 1233 | Port exPort = null; |
| 1234 | for (Port port : ports) { |
| 1235 | String portName = port.annotations().value(AnnotationKeys.PORT_NAME); |
lishuai | b43dbf7 | 2016-01-06 11:11:35 +0800 | [diff] [blame] | 1236 | Versioned<String> exPortVersioned = exPortMap.get(EX_PORT_KEY); |
| 1237 | if (portName != null && exPortVersioned != null && portName. |
| 1238 | equals(exPortVersioned.value())) { |
lishuai | 858efd3 | 2015-12-04 14:30:36 +0800 | [diff] [blame] | 1239 | exPort = port; |
| 1240 | break; |
| 1241 | } |
| 1242 | } |
| 1243 | return exPort; |
| 1244 | } |
| 1245 | |
| 1246 | private List getGwIpAndMac(VirtualPort port) { |
| 1247 | List list = new ArrayList(); |
| 1248 | MacAddress gwMac = null; |
| 1249 | SubnetId subnetId = null; |
| 1250 | IpAddress gwIp = null; |
| 1251 | Iterator<FixedIp> fixips = port.fixedIps().iterator(); |
| 1252 | if (fixips.hasNext()) { |
| 1253 | FixedIp fixip = fixips.next(); |
| 1254 | subnetId = fixip.subnetId(); |
| 1255 | gwIp = subnetService.getSubnet(subnetId).gatewayIp(); |
| 1256 | FixedIp fixedGwIp = FixedIp.fixedIp(fixip.subnetId(), gwIp); |
| 1257 | VirtualPort gwPort = virtualPortService.getPort(fixedGwIp); |
| 1258 | if (gwPort == null) { |
| 1259 | gwPort = VtnData.getPort(vPortStore, fixedGwIp); |
| 1260 | } |
| 1261 | gwMac = gwPort.macAddress(); |
| 1262 | } |
| 1263 | list.add(gwIp); |
| 1264 | list.add(gwMac); |
| 1265 | return list; |
| 1266 | } |
| 1267 | |
lishuai | 858efd3 | 2015-12-04 14:30:36 +0800 | [diff] [blame] | 1268 | private void applyHostMonitoredL3Rules(Host host, |
| 1269 | Objective.Operation operation) { |
| 1270 | String ifaceId = host.annotations().value(IFACEID); |
| 1271 | DeviceId deviceId = host.location().deviceId(); |
| 1272 | VirtualPortId portId = VirtualPortId.portId(ifaceId); |
| 1273 | VirtualPort port = virtualPortService.getPort(portId); |
| 1274 | if (port == null) { |
| 1275 | port = VtnData.getPort(vPortStore, portId); |
| 1276 | } |
| 1277 | TenantId tenantId = port.tenantId(); |
| 1278 | Port exPort = exPortOfDevice.get(deviceId); |
lishuai | 858efd3 | 2015-12-04 14:30:36 +0800 | [diff] [blame] | 1279 | Iterator<FixedIp> fixips = port.fixedIps().iterator(); |
| 1280 | SubnetId sid = null; |
| 1281 | IpAddress hostIp = null; |
| 1282 | if (fixips.hasNext()) { |
| 1283 | FixedIp fixip = fixips.next(); |
| 1284 | sid = fixip.subnetId(); |
| 1285 | hostIp = fixip.ip(); |
| 1286 | } |
| 1287 | final SubnetId subnetId = sid; |
| 1288 | // L3 internal network access to each other |
| 1289 | Iterable<RouterInterface> interfaces = routerInterfaceService |
| 1290 | .getRouterInterfaces(); |
lishuai | 8798bbe | 2016-05-05 16:02:03 +0800 | [diff] [blame] | 1291 | Set<RouterInterface> hostInterfaces = Sets.newHashSet(interfaces) |
lishuai | 858efd3 | 2015-12-04 14:30:36 +0800 | [diff] [blame] | 1292 | .stream().filter(r -> r.tenantId().equals(tenantId)) |
lishuai | 8798bbe | 2016-05-05 16:02:03 +0800 | [diff] [blame] | 1293 | .filter(r -> r.subnetId().equals(subnetId)) |
lishuai | 858efd3 | 2015-12-04 14:30:36 +0800 | [diff] [blame] | 1294 | .collect(Collectors.toSet()); |
lishuai | 8798bbe | 2016-05-05 16:02:03 +0800 | [diff] [blame] | 1295 | hostInterfaces.stream().forEach(routerInf -> { |
| 1296 | Set<RouterInterface> interfacesSet = Sets.newHashSet(interfaces) |
| 1297 | .stream().filter(r -> r.tenantId().equals(tenantId)) |
| 1298 | .filter(r -> r.routerId().equals(routerInf.routerId())) |
| 1299 | .collect(Collectors.toSet()); |
| 1300 | long count = interfacesSet.stream() |
| 1301 | .filter(r -> !r.subnetId().equals(subnetId)).count(); |
| 1302 | if (count > 0) { |
| 1303 | TenantRouter tenantRouter = TenantRouter |
| 1304 | .tenantRouter(routerInf.tenantId(), routerInf.routerId()); |
| 1305 | SegmentationId l3vni = vtnRscService.getL3vni(tenantRouter); |
| 1306 | if (operation == Objective.Operation.ADD) { |
| 1307 | if (routerInfFlagOfTenantRouter.get(tenantRouter) != null) { |
| 1308 | applyEastWestL3Flows(host, l3vni, operation); |
| 1309 | } else { |
| 1310 | if (interfacesSet.size() > 1) { |
| 1311 | programInterfacesSet(interfacesSet, operation); |
| 1312 | } |
| 1313 | } |
| 1314 | } else if (operation == Objective.Operation.REMOVE) { |
| 1315 | if (routerInfFlagOfTenantRouter.get(tenantRouter) != null) { |
| 1316 | applyEastWestL3Flows(host, l3vni, operation); |
lishuai | 858efd3 | 2015-12-04 14:30:36 +0800 | [diff] [blame] | 1317 | } |
| 1318 | } |
lishuai | 858efd3 | 2015-12-04 14:30:36 +0800 | [diff] [blame] | 1319 | } |
lishuai | 8798bbe | 2016-05-05 16:02:03 +0800 | [diff] [blame] | 1320 | }); |
lishuai | 858efd3 | 2015-12-04 14:30:36 +0800 | [diff] [blame] | 1321 | // L3 external and internal network access to each other |
| 1322 | FloatingIp floatingIp = null; |
| 1323 | Iterable<FloatingIp> floatingIps = floatingIpService.getFloatingIps(); |
| 1324 | Set<FloatingIp> floatingIpSet = Sets.newHashSet(floatingIps).stream() |
| 1325 | .filter(f -> f.tenantId().equals(tenantId)) |
| 1326 | .collect(Collectors.toSet()); |
| 1327 | for (FloatingIp f : floatingIpSet) { |
| 1328 | IpAddress fixedIp = f.fixedIp(); |
lishuai | bee5eca | 2016-05-10 19:17:37 +0800 | [diff] [blame] | 1329 | if (fixedIp != null && fixedIp.equals(hostIp)) { |
lishuai | 858efd3 | 2015-12-04 14:30:36 +0800 | [diff] [blame] | 1330 | floatingIp = f; |
| 1331 | break; |
| 1332 | } |
| 1333 | } |
| 1334 | if (floatingIp != null) { |
lishuai | 8798bbe | 2016-05-05 16:02:03 +0800 | [diff] [blame] | 1335 | TenantRouter tenantRouter = TenantRouter |
| 1336 | .tenantRouter(floatingIp.tenantId(), floatingIp.routerId()); |
| 1337 | SegmentationId l3vni = vtnRscService.getL3vni(tenantRouter); |
lishuai | 858efd3 | 2015-12-04 14:30:36 +0800 | [diff] [blame] | 1338 | VirtualPort fipPort = virtualPortService |
| 1339 | .getPort(floatingIp.networkId(), floatingIp.floatingIp()); |
| 1340 | if (fipPort == null) { |
| 1341 | fipPort = VtnData.getPort(vPortStore, floatingIp.networkId(), |
| 1342 | floatingIp.floatingIp()); |
| 1343 | } |
lishuai | bee5eca | 2016-05-10 19:17:37 +0800 | [diff] [blame] | 1344 | applyNorthSouthL3Flows(deviceId, true, tenantRouter, host, port, |
| 1345 | fipPort, floatingIp, l3vni, exPort, |
| 1346 | operation); |
lishuai | 858efd3 | 2015-12-04 14:30:36 +0800 | [diff] [blame] | 1347 | } |
| 1348 | } |
lishuai | 74f2d53 | 2015-12-10 17:03:34 +0800 | [diff] [blame] | 1349 | |
| 1350 | public static void setExPortName(String name) { |
lishuai | b43dbf7 | 2016-01-06 11:11:35 +0800 | [diff] [blame] | 1351 | exPortMap.put(EX_PORT_KEY, name); |
lishuai | 74f2d53 | 2015-12-10 17:03:34 +0800 | [diff] [blame] | 1352 | } |
Bob zhou | 59a2106 | 2016-05-12 19:40:05 +0800 | [diff] [blame] | 1353 | |
| 1354 | /** |
| 1355 | * Packet processor responsible for forwarding packets along their paths. |
| 1356 | */ |
| 1357 | private class VtnL3PacketProcessor implements PacketProcessor { |
| 1358 | |
| 1359 | @Override |
| 1360 | public void process(PacketContext context) { |
| 1361 | InboundPacket pkt = context.inPacket(); |
| 1362 | ConnectPoint connectPoint = pkt.receivedFrom(); |
| 1363 | DeviceId deviceId = connectPoint.deviceId(); |
| 1364 | Ethernet ethPkt = pkt.parsed(); |
| 1365 | if (ethPkt == null) { |
| 1366 | return; |
| 1367 | } |
| 1368 | if (ethPkt.getEtherType() == Ethernet.TYPE_ARP) { |
| 1369 | ARP arpPacket = (ARP) ethPkt.getPayload(); |
| 1370 | if ((arpPacket.getOpCode() == ARP.OP_REQUEST)) { |
| 1371 | arprequestProcess(arpPacket, deviceId); |
| 1372 | } else if (arpPacket.getOpCode() == ARP.OP_REPLY) { |
| 1373 | arpresponceProcess(arpPacket, deviceId); |
| 1374 | } |
| 1375 | } else if (ethPkt.getEtherType() == Ethernet.TYPE_IPV4) { |
| 1376 | if (ethPkt.getDestinationMAC().isMulticast()) { |
| 1377 | return; |
| 1378 | } |
| 1379 | IPv4 ip = (IPv4) ethPkt.getPayload(); |
| 1380 | upStreamPacketProcessor(ip, deviceId); |
| 1381 | |
| 1382 | } else { |
| 1383 | return; |
| 1384 | } |
| 1385 | } |
| 1386 | |
| 1387 | private void arprequestProcess(ARP arpPacket, DeviceId deviceId) { |
| 1388 | MacAddress dstMac = MacAddress |
| 1389 | .valueOf(arpPacket.getSenderHardwareAddress()); |
| 1390 | IpAddress srcIp = IpAddress.valueOf(IPv4 |
| 1391 | .toIPv4Address(arpPacket.getTargetProtocolAddress())); |
| 1392 | IpAddress dstIp = IpAddress.valueOf(IPv4 |
| 1393 | .toIPv4Address(arpPacket.getSenderProtocolAddress())); |
| 1394 | FloatingIp floatingIp = floatingIpStore.get(srcIp); |
| 1395 | if (floatingIp == null) { |
| 1396 | return; |
| 1397 | } |
| 1398 | DeviceId deviceIdOfFloatingIp = getDeviceIdOfFloatingIP(floatingIp); |
| 1399 | if (!deviceId.equals(deviceIdOfFloatingIp)) { |
| 1400 | return; |
| 1401 | } |
| 1402 | Port exPort = exPortOfDevice.get(deviceId); |
| 1403 | MacAddress srcMac = MacAddress.valueOf(exPort.annotations() |
| 1404 | .value(AnnotationKeys.PORT_MAC)); |
| 1405 | if (!downloadSnatRules(deviceId, srcMac, srcIp, dstMac, dstIp, |
| 1406 | floatingIp)) { |
| 1407 | return; |
| 1408 | } |
| 1409 | Ethernet ethernet = buildArpResponse(dstIp, dstMac, srcIp, srcMac); |
| 1410 | if (ethernet != null) { |
| 1411 | sendPacketOut(deviceId, exPort.number(), ethernet); |
| 1412 | } |
| 1413 | } |
| 1414 | |
| 1415 | private void arpresponceProcess(ARP arpPacket, DeviceId deviceId) { |
| 1416 | MacAddress srcMac = MacAddress |
| 1417 | .valueOf(arpPacket.getTargetHardwareAddress()); |
| 1418 | MacAddress dstMac = MacAddress |
| 1419 | .valueOf(arpPacket.getSenderHardwareAddress()); |
| 1420 | IpAddress srcIp = IpAddress.valueOf(IPv4 |
| 1421 | .toIPv4Address(arpPacket.getTargetProtocolAddress())); |
| 1422 | IpAddress dstIp = IpAddress.valueOf(IPv4 |
| 1423 | .toIPv4Address(arpPacket.getSenderProtocolAddress())); |
| 1424 | FloatingIp floatingIp = floatingIpStore.get(srcIp); |
| 1425 | if (floatingIp == null) { |
| 1426 | return; |
| 1427 | } |
| 1428 | DeviceId deviceIdOfFloatingIp = getDeviceIdOfFloatingIP(floatingIp); |
| 1429 | if (!deviceId.equals(deviceIdOfFloatingIp)) { |
| 1430 | return; |
| 1431 | } |
| 1432 | if (!downloadSnatRules(deviceId, srcMac, srcIp, dstMac, dstIp, |
| 1433 | floatingIp)) { |
| 1434 | return; |
| 1435 | } |
| 1436 | } |
| 1437 | |
| 1438 | private void upStreamPacketProcessor(IPv4 ipPacket, DeviceId deviceId) { |
| 1439 | IpAddress srcIp = IpAddress.valueOf(ipPacket.getSourceAddress()); |
| 1440 | IpAddress dstIp = IpAddress.valueOf(ipPacket.getDestinationAddress()); |
| 1441 | FloatingIp floatingIp = null; |
| 1442 | Collection<FloatingIp> floatingIps = floatingIpService |
| 1443 | .getFloatingIps(); |
| 1444 | Set<FloatingIp> floatingIpSet = Sets.newHashSet(floatingIps) |
| 1445 | .stream().collect(Collectors.toSet()); |
| 1446 | for (FloatingIp f : floatingIpSet) { |
| 1447 | IpAddress fixIp = f.fixedIp(); |
| 1448 | if (fixIp != null && fixIp.equals(srcIp)) { |
| 1449 | floatingIp = f; |
| 1450 | break; |
| 1451 | } |
| 1452 | } |
| 1453 | if (floatingIp == null) { |
| 1454 | return; |
| 1455 | } |
| 1456 | Subnet subnet = getSubnetOfFloatingIP(floatingIp); |
| 1457 | IpAddress gwIp = subnet.gatewayIp(); |
| 1458 | Port exportPort = exPortOfDevice.get(deviceId); |
| 1459 | MacAddress exPortMac = MacAddress.valueOf(exportPort.annotations() |
| 1460 | .value(AnnotationKeys.PORT_MAC)); |
| 1461 | IpPrefix ipPrefix = subnet.cidr(); |
| 1462 | if (ipPrefix == null) { |
| 1463 | return; |
| 1464 | } |
| 1465 | int mask = ipPrefix.prefixLength(); |
| 1466 | if (mask <= 0) { |
| 1467 | return; |
| 1468 | } |
| 1469 | Ethernet ethernet = null; |
| 1470 | // if the same ip segment |
| 1471 | if (IpUtil.checkSameSegment(floatingIp.floatingIp(), dstIp, mask)) { |
| 1472 | ethernet = buildArpRequest(dstIp, floatingIp.floatingIp(), |
| 1473 | exPortMac); |
| 1474 | } else { |
| 1475 | ethernet = buildArpRequest(gwIp, floatingIp.floatingIp(), |
| 1476 | exPortMac); |
| 1477 | } |
| 1478 | if (ethernet != null) { |
| 1479 | sendPacketOut(deviceId, exportPort.number(), ethernet); |
| 1480 | } |
| 1481 | } |
| 1482 | } |
| 1483 | |
| 1484 | private Ethernet buildArpRequest(IpAddress targetIp, IpAddress sourceIp, |
| 1485 | MacAddress sourceMac) { |
| 1486 | ARP arp = new ARP(); |
| 1487 | arp.setHardwareType(ARP.HW_TYPE_ETHERNET) |
| 1488 | .setHardwareAddressLength((byte) Ethernet.DATALAYER_ADDRESS_LENGTH) |
| 1489 | .setProtocolType(ARP.PROTO_TYPE_IP) |
| 1490 | .setProtocolAddressLength((byte) Ip4Address.BYTE_LENGTH) |
| 1491 | .setOpCode(ARP.OP_REQUEST); |
| 1492 | |
| 1493 | arp.setSenderHardwareAddress(sourceMac.toBytes()) |
| 1494 | .setSenderProtocolAddress(sourceIp.getIp4Address().toInt()) |
| 1495 | .setTargetHardwareAddress(ZERO_MAC_ADDRESS) |
| 1496 | .setTargetProtocolAddress(targetIp.getIp4Address().toInt()); |
| 1497 | |
| 1498 | Ethernet ethernet = new Ethernet(); |
| 1499 | ethernet.setEtherType(Ethernet.TYPE_ARP) |
| 1500 | .setDestinationMACAddress(MacAddress.BROADCAST) |
| 1501 | .setSourceMACAddress(sourceMac) |
| 1502 | .setPayload(arp); |
| 1503 | |
| 1504 | ethernet.setPad(true); |
| 1505 | return ethernet; |
| 1506 | } |
| 1507 | |
| 1508 | private Ethernet buildArpResponse(IpAddress targetIp, MacAddress targetMac, |
| 1509 | IpAddress sourceIp, MacAddress sourceMac) { |
| 1510 | ARP arp = new ARP(); |
| 1511 | arp.setHardwareType(ARP.HW_TYPE_ETHERNET) |
| 1512 | .setHardwareAddressLength((byte) Ethernet.DATALAYER_ADDRESS_LENGTH) |
| 1513 | .setProtocolType(ARP.PROTO_TYPE_IP) |
| 1514 | .setProtocolAddressLength((byte) Ip4Address.BYTE_LENGTH) |
| 1515 | .setOpCode(ARP.OP_REPLY); |
| 1516 | |
| 1517 | arp.setSenderHardwareAddress(sourceMac.toBytes()) |
| 1518 | .setSenderProtocolAddress(sourceIp.getIp4Address().toInt()) |
| 1519 | .setTargetHardwareAddress(targetMac.toBytes()) |
| 1520 | .setTargetProtocolAddress(targetIp.getIp4Address().toInt()); |
| 1521 | |
| 1522 | Ethernet ethernet = new Ethernet(); |
| 1523 | ethernet.setEtherType(Ethernet.TYPE_ARP) |
| 1524 | .setDestinationMACAddress(targetMac) |
| 1525 | .setSourceMACAddress(sourceMac) |
| 1526 | .setPayload(arp); |
| 1527 | |
| 1528 | ethernet.setPad(true); |
| 1529 | |
| 1530 | return ethernet; |
| 1531 | } |
| 1532 | |
| 1533 | private void sendPacketOut(DeviceId deviceId, PortNumber portNumber, |
| 1534 | Ethernet payload) { |
| 1535 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 1536 | .setOutput(portNumber).build(); |
| 1537 | OutboundPacket packet = new DefaultOutboundPacket(deviceId, treatment, |
| 1538 | ByteBuffer |
| 1539 | .wrap(payload |
| 1540 | .serialize())); |
| 1541 | packetService.emit(packet); |
| 1542 | } |
| 1543 | |
| 1544 | private Subnet getSubnetOfFloatingIP(FloatingIp floatingIp) { |
| 1545 | DeviceId exVmPortId = DeviceId |
| 1546 | .deviceId(floatingIp.id().floatingIpId().toString()); |
| 1547 | Collection<VirtualPort> exVmPortList = virtualPortService |
| 1548 | .getPorts(exVmPortId); |
| 1549 | VirtualPort exVmPort = null; |
| 1550 | if (exVmPortList != null) { |
| 1551 | exVmPort = exVmPortList.iterator().next(); |
| 1552 | } |
| 1553 | if (exVmPort == null) { |
| 1554 | return null; |
| 1555 | } |
| 1556 | Set<FixedIp> fixedIps = exVmPort.fixedIps(); |
| 1557 | SubnetId subnetId = null; |
| 1558 | for (FixedIp f : fixedIps) { |
| 1559 | IpAddress fp = f.ip(); |
| 1560 | if (fp.equals(floatingIp.floatingIp())) { |
| 1561 | subnetId = f.subnetId(); |
| 1562 | break; |
| 1563 | } |
| 1564 | } |
| 1565 | if (subnetId == null) { |
| 1566 | return null; |
| 1567 | } |
| 1568 | Subnet subnet = subnetService.getSubnet(subnetId); |
| 1569 | return subnet; |
| 1570 | } |
| 1571 | |
| 1572 | private DeviceId getDeviceIdOfFloatingIP(FloatingIp floatingIp) { |
| 1573 | VirtualPortId vmPortId = floatingIp.portId(); |
| 1574 | VirtualPort vmPort = virtualPortService.getPort(vmPortId); |
| 1575 | if (vmPort == null) { |
| 1576 | vmPort = VtnData.getPort(vPortStore, vmPortId); |
| 1577 | } |
| 1578 | Set<Host> hostSet = hostService.getHostsByMac(vmPort.macAddress()); |
| 1579 | Host host = null; |
| 1580 | for (Host h : hostSet) { |
| 1581 | String ifaceid = h.annotations().value(IFACEID); |
| 1582 | if (ifaceid != null && ifaceid.equals(vmPortId.portId())) { |
| 1583 | host = h; |
| 1584 | break; |
| 1585 | } |
| 1586 | } |
| 1587 | if (host == null) { |
| 1588 | return null; |
| 1589 | } else { |
| 1590 | return host.location().deviceId(); |
| 1591 | } |
| 1592 | } |
| 1593 | |
| 1594 | private boolean downloadSnatRules(DeviceId deviceId, MacAddress srcMac, |
| 1595 | IpAddress srcIp, MacAddress dstMac, |
| 1596 | IpAddress dstIp, FloatingIp floatingIp) { |
| 1597 | TenantNetwork exNetwork = tenantNetworkService |
| 1598 | .getNetwork(floatingIp.networkId()); |
| 1599 | IpAddress fixedIp = floatingIp.fixedIp(); |
| 1600 | VirtualPortId vmPortId = floatingIp.portId(); |
| 1601 | VirtualPort vmPort = virtualPortService.getPort(vmPortId); |
| 1602 | if (vmPort == null) { |
| 1603 | vmPort = VtnData.getPort(vPortStore, vmPortId); |
| 1604 | } |
| 1605 | Subnet subnet = getSubnetOfFloatingIP(floatingIp); |
| 1606 | IpPrefix ipPrefix = subnet.cidr(); |
| 1607 | IpAddress gwIp = subnet.gatewayIp(); |
| 1608 | if (ipPrefix == null) { |
| 1609 | return false; |
| 1610 | } |
| 1611 | int mask = ipPrefix.prefixLength(); |
| 1612 | if (mask <= 0) { |
| 1613 | return false; |
| 1614 | } |
| 1615 | TenantRouter tenantRouter = TenantRouter |
| 1616 | .tenantRouter(floatingIp.tenantId(), floatingIp.routerId()); |
| 1617 | SegmentationId l3vni = vtnRscService.getL3vni(tenantRouter); |
| 1618 | // if the same ip segment |
| 1619 | if (IpUtil.checkSameSegment(srcIp, dstIp, mask)) { |
| 1620 | snatService.programSnatSameSegmentRules(deviceId, l3vni, fixedIp, |
| 1621 | dstIp, dstMac, srcMac, |
| 1622 | srcIp, |
| 1623 | exNetwork.segmentationId(), |
| 1624 | Objective.Operation.ADD); |
| 1625 | if (dstIp.equals(gwIp)) { |
| 1626 | snatService |
| 1627 | .programSnatDiffSegmentRules(deviceId, l3vni, fixedIp, |
| 1628 | dstMac, srcMac, srcIp, |
| 1629 | exNetwork.segmentationId(), |
| 1630 | Objective.Operation.ADD); |
| 1631 | } |
| 1632 | } |
| 1633 | return true; |
| 1634 | } |
| 1635 | |
| 1636 | private void removeRulesInSnat(DeviceId deviceId, IpAddress fixedIp) { |
| 1637 | for (FlowEntry f : flowRuleService.getFlowEntries(deviceId)) { |
| 1638 | if (f.tableId() == SNAT_TABLE |
| 1639 | && f.priority() > SNAT_DEFAULT_RULE_PRIORITY) { |
| 1640 | String srcIp = f.selector() |
| 1641 | .getCriterion(Criterion.Type.IPV4_SRC).toString(); |
| 1642 | int priority = f.priority(); |
| 1643 | if (srcIp != null && srcIp.contains(fixedIp.toString())) { |
| 1644 | log.info("Match snat rules bob"); |
| 1645 | TrafficSelector selector = f.selector(); |
| 1646 | TrafficTreatment treatment = f.treatment(); |
| 1647 | snatService.removeSnatRules(deviceId, selector, treatment, |
| 1648 | priority, |
| 1649 | Objective.Operation.REMOVE); |
| 1650 | |
| 1651 | } |
| 1652 | } |
| 1653 | } |
| 1654 | } |
lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [diff] [blame] | 1655 | } |