Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 Open Networking Laboratory |
| 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 | |
| 17 | package org.onosproject.routing.impl; |
| 18 | |
kishore | 71a2753 | 2016-03-16 20:23:49 +0530 | [diff] [blame] | 19 | import static com.google.common.base.Preconditions.checkState; |
| 20 | import static org.slf4j.LoggerFactory.getLogger; |
| 21 | |
| 22 | import java.util.Collections; |
| 23 | import java.util.Iterator; |
| 24 | import java.util.List; |
| 25 | import java.util.Map; |
| 26 | import java.util.Optional; |
| 27 | import java.util.Set; |
| 28 | |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 29 | import org.apache.felix.scr.annotations.Activate; |
| 30 | import org.apache.felix.scr.annotations.Component; |
| 31 | import org.apache.felix.scr.annotations.Deactivate; |
| 32 | import org.apache.felix.scr.annotations.Reference; |
| 33 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 34 | import org.onlab.packet.EthType; |
Charles Chan | d0fd5dc | 2016-02-16 23:14:49 -0800 | [diff] [blame] | 35 | import org.onlab.packet.IpPrefix; |
| 36 | import org.onlab.packet.MacAddress; |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 37 | import org.onlab.packet.VlanId; |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 38 | import org.onosproject.core.ApplicationId; |
| 39 | import org.onosproject.core.CoreService; |
| 40 | import org.onosproject.incubator.net.intf.Interface; |
kishore | 71a2753 | 2016-03-16 20:23:49 +0530 | [diff] [blame] | 41 | import org.onosproject.incubator.net.intf.InterfaceEvent; |
| 42 | import org.onosproject.incubator.net.intf.InterfaceListener; |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 43 | import org.onosproject.incubator.net.intf.InterfaceService; |
Charles Chan | d0fd5dc | 2016-02-16 23:14:49 -0800 | [diff] [blame] | 44 | import org.onosproject.mastership.MastershipService; |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 45 | import org.onosproject.net.ConnectPoint; |
| 46 | import org.onosproject.net.DeviceId; |
Charles Chan | d0fd5dc | 2016-02-16 23:14:49 -0800 | [diff] [blame] | 47 | import org.onosproject.net.Host; |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 48 | import org.onosproject.net.PortNumber; |
| 49 | import org.onosproject.net.config.NetworkConfigEvent; |
| 50 | import org.onosproject.net.config.NetworkConfigListener; |
| 51 | import org.onosproject.net.config.NetworkConfigService; |
| 52 | import org.onosproject.net.device.DeviceEvent; |
| 53 | import org.onosproject.net.device.DeviceListener; |
| 54 | import org.onosproject.net.device.DeviceService; |
| 55 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 56 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 57 | import org.onosproject.net.flow.TrafficSelector; |
| 58 | import org.onosproject.net.flow.TrafficTreatment; |
| 59 | import org.onosproject.net.flowobjective.DefaultForwardingObjective; |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 60 | import org.onosproject.net.flowobjective.DefaultNextObjective; |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 61 | import org.onosproject.net.flowobjective.FlowObjectiveService; |
| 62 | import org.onosproject.net.flowobjective.ForwardingObjective; |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 63 | import org.onosproject.net.flowobjective.NextObjective; |
Charles Chan | d0fd5dc | 2016-02-16 23:14:49 -0800 | [diff] [blame] | 64 | import org.onosproject.net.host.HostEvent; |
| 65 | import org.onosproject.net.host.HostListener; |
| 66 | import org.onosproject.net.host.HostService; |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 67 | import org.onosproject.net.host.InterfaceIpAddress; |
| 68 | import org.onosproject.routing.RoutingService; |
| 69 | import org.onosproject.routing.config.RouterConfig; |
| 70 | import org.slf4j.Logger; |
| 71 | |
kishore | 71a2753 | 2016-03-16 20:23:49 +0530 | [diff] [blame] | 72 | import com.google.common.collect.ImmutableSortedSet; |
| 73 | import com.google.common.collect.Maps; |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 74 | |
| 75 | /** |
| 76 | * Manages connectivity between peers redirecting control traffic to a routing |
| 77 | * control plane available on the dataplane. |
| 78 | */ |
| 79 | @Component(immediate = true, enabled = false) |
| 80 | public class ControlPlaneRedirectManager { |
| 81 | |
| 82 | private final Logger log = getLogger(getClass()); |
| 83 | |
Charles Chan | d0fd5dc | 2016-02-16 23:14:49 -0800 | [diff] [blame] | 84 | private static final int MIN_IP_PRIORITY = 10; |
| 85 | private static final int ACL_PRIORITY = 40001; |
Jonathan Hart | ea49238 | 2016-01-13 09:33:13 -0800 | [diff] [blame] | 86 | private static final int OSPF_IP_PROTO = 0x59; |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 87 | |
| 88 | private static final String APP_NAME = "org.onosproject.cpredirect"; |
| 89 | private ApplicationId appId; |
| 90 | |
| 91 | private ConnectPoint controlPlaneConnectPoint; |
Jonathan Hart | ea49238 | 2016-01-13 09:33:13 -0800 | [diff] [blame] | 92 | private boolean ospfEnabled = false; |
Jonathan Hart | 883fd37 | 2016-02-10 14:36:15 -0800 | [diff] [blame] | 93 | private List<String> interfaces = Collections.emptyList(); |
Charles Chan | d0fd5dc | 2016-02-16 23:14:49 -0800 | [diff] [blame] | 94 | private Map<Host, Set<Integer>> peerNextId = Maps.newConcurrentMap(); |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 95 | |
| 96 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 97 | protected CoreService coreService; |
| 98 | |
| 99 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 100 | protected DeviceService deviceService; |
| 101 | |
| 102 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 103 | protected InterfaceService interfaceService; |
| 104 | |
| 105 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 106 | protected FlowObjectiveService flowObjectiveService; |
| 107 | |
| 108 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 109 | protected NetworkConfigService networkConfigService; |
| 110 | |
Charles Chan | d0fd5dc | 2016-02-16 23:14:49 -0800 | [diff] [blame] | 111 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 112 | protected MastershipService mastershipService; |
| 113 | |
| 114 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 115 | protected HostService hostService; |
| 116 | |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 117 | private final InternalDeviceListener deviceListener = new InternalDeviceListener(); |
| 118 | private final InternalNetworkConfigListener networkConfigListener = |
| 119 | new InternalNetworkConfigListener(); |
Charles Chan | d0fd5dc | 2016-02-16 23:14:49 -0800 | [diff] [blame] | 120 | private final InternalHostListener hostListener = new InternalHostListener(); |
kishore | 71a2753 | 2016-03-16 20:23:49 +0530 | [diff] [blame] | 121 | private final InternalInterfaceListener interfaceListener = new InternalInterfaceListener(); |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 122 | |
| 123 | @Activate |
| 124 | public void activate() { |
| 125 | this.appId = coreService.registerApplication(APP_NAME); |
| 126 | |
| 127 | deviceService.addListener(deviceListener); |
| 128 | networkConfigService.addListener(networkConfigListener); |
Charles Chan | d0fd5dc | 2016-02-16 23:14:49 -0800 | [diff] [blame] | 129 | hostService.addListener(hostListener); |
kishore | 71a2753 | 2016-03-16 20:23:49 +0530 | [diff] [blame] | 130 | interfaceService.addListener(interfaceListener); |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 131 | |
| 132 | updateConfig(); |
| 133 | } |
| 134 | |
| 135 | @Deactivate |
| 136 | public void deactivate() { |
| 137 | deviceService.removeListener(deviceListener); |
| 138 | networkConfigService.removeListener(networkConfigListener); |
Charles Chan | d0fd5dc | 2016-02-16 23:14:49 -0800 | [diff] [blame] | 139 | hostService.removeListener(hostListener); |
kishore | 71a2753 | 2016-03-16 20:23:49 +0530 | [diff] [blame] | 140 | interfaceService.removeListener(interfaceListener); |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | private void updateConfig() { |
| 144 | ApplicationId routingAppId = |
| 145 | coreService.registerApplication(RoutingService.ROUTER_APP_ID); |
| 146 | |
| 147 | RouterConfig config = networkConfigService.getConfig( |
| 148 | routingAppId, RoutingService.ROUTER_CONFIG_CLASS); |
| 149 | |
| 150 | if (config == null) { |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 151 | log.warn("Router config not available"); |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 152 | return; |
| 153 | } |
| 154 | |
Jonathan Hart | ea49238 | 2016-01-13 09:33:13 -0800 | [diff] [blame] | 155 | controlPlaneConnectPoint = config.getControlPlaneConnectPoint(); |
| 156 | ospfEnabled = config.getOspfEnabled(); |
Jonathan Hart | 883fd37 | 2016-02-10 14:36:15 -0800 | [diff] [blame] | 157 | interfaces = config.getInterfaces(); |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 158 | |
Jonathan Hart | ea49238 | 2016-01-13 09:33:13 -0800 | [diff] [blame] | 159 | updateDevice(); |
| 160 | } |
| 161 | |
| 162 | private void updateDevice() { |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 163 | if (controlPlaneConnectPoint != null && |
| 164 | deviceService.isAvailable(controlPlaneConnectPoint.deviceId())) { |
Jonathan Hart | ea49238 | 2016-01-13 09:33:13 -0800 | [diff] [blame] | 165 | DeviceId deviceId = controlPlaneConnectPoint.deviceId(); |
| 166 | |
| 167 | interfaceService.getInterfaces().stream() |
| 168 | .filter(intf -> intf.connectPoint().deviceId().equals(deviceId)) |
Jonathan Hart | 883fd37 | 2016-02-10 14:36:15 -0800 | [diff] [blame] | 169 | .filter(intf -> interfaces.isEmpty() || interfaces.contains(intf.name())) |
Jonathan Hart | ea49238 | 2016-01-13 09:33:13 -0800 | [diff] [blame] | 170 | .forEach(this::provisionInterface); |
| 171 | |
| 172 | log.info("Set up interfaces on {}", controlPlaneConnectPoint.deviceId()); |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 173 | } |
| 174 | } |
| 175 | |
kishore | 71a2753 | 2016-03-16 20:23:49 +0530 | [diff] [blame] | 176 | private void removeInterface(Interface intf) { |
| 177 | modifyBasicInterfaceForwarding(intf, false); |
| 178 | updateOspfForwarding(intf, false); |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 179 | } |
| 180 | |
kishore | 71a2753 | 2016-03-16 20:23:49 +0530 | [diff] [blame] | 181 | private void provisionInterface(Interface intf) { |
| 182 | modifyBasicInterfaceForwarding(intf, true); |
| 183 | updateOspfForwarding(intf, true); |
| 184 | } |
| 185 | /** |
| 186 | * Install or removes the basic forwarding flows for each interface |
| 187 | * based on the flag used. |
| 188 | * |
| 189 | * @param intf the Interface on which event is received |
| 190 | * @param install true to create an add objective, false to create a remove |
| 191 | * objective |
| 192 | **/ |
| 193 | private void modifyBasicInterfaceForwarding(Interface intf, boolean install) { |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 194 | log.debug("Adding interface objectives for {}", intf); |
| 195 | |
| 196 | DeviceId deviceId = controlPlaneConnectPoint.deviceId(); |
| 197 | PortNumber controlPlanePort = controlPlaneConnectPoint.port(); |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 198 | for (InterfaceIpAddress ip : intf.ipAddresses()) { |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 199 | // create nextObjectives for forwarding to this interface and the |
| 200 | // controlPlaneConnectPoint |
| 201 | int cpNextId, intfNextId; |
| 202 | if (intf.vlan() == VlanId.NONE) { |
kishore | 71a2753 | 2016-03-16 20:23:49 +0530 | [diff] [blame] | 203 | cpNextId = modifyNextObjective(deviceId, controlPlanePort, |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 204 | VlanId.vlanId(SingleSwitchFibInstaller.ASSIGNED_VLAN), |
kishore | 71a2753 | 2016-03-16 20:23:49 +0530 | [diff] [blame] | 205 | true, install); |
| 206 | intfNextId = modifyNextObjective(deviceId, intf.connectPoint().port(), |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 207 | VlanId.vlanId(SingleSwitchFibInstaller.ASSIGNED_VLAN), |
kishore | 71a2753 | 2016-03-16 20:23:49 +0530 | [diff] [blame] | 208 | true, install); |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 209 | } else { |
kishore | 71a2753 | 2016-03-16 20:23:49 +0530 | [diff] [blame] | 210 | cpNextId = modifyNextObjective(deviceId, controlPlanePort, |
| 211 | intf.vlan(), false, install); |
| 212 | intfNextId = modifyNextObjective(deviceId, intf.connectPoint().port(), |
| 213 | intf.vlan(), false, install); |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 214 | } |
| 215 | |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 216 | // IPv4 to router |
| 217 | TrafficSelector toSelector = DefaultTrafficSelector.builder() |
| 218 | .matchInPort(intf.connectPoint().port()) |
| 219 | .matchEthDst(intf.mac()) |
| 220 | .matchEthType(EthType.EtherType.IPV4.ethType().toShort()) |
| 221 | .matchVlanId(intf.vlan()) |
| 222 | .matchIPDst(ip.ipAddress().toIpPrefix()) |
| 223 | .build(); |
| 224 | |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 225 | flowObjectiveService.forward(deviceId, |
kishore | 71a2753 | 2016-03-16 20:23:49 +0530 | [diff] [blame] | 226 | buildForwardingObjective(toSelector, null, cpNextId, install)); |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 227 | |
| 228 | // IPv4 from router |
| 229 | TrafficSelector fromSelector = DefaultTrafficSelector.builder() |
| 230 | .matchInPort(controlPlanePort) |
| 231 | .matchEthSrc(intf.mac()) |
| 232 | .matchVlanId(intf.vlan()) |
| 233 | .matchEthType(EthType.EtherType.IPV4.ethType().toShort()) |
| 234 | .matchIPSrc(ip.ipAddress().toIpPrefix()) |
| 235 | .build(); |
| 236 | |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 237 | flowObjectiveService.forward(deviceId, |
kishore | 71a2753 | 2016-03-16 20:23:49 +0530 | [diff] [blame] | 238 | buildForwardingObjective(fromSelector, null, intfNextId, install)); |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 239 | |
| 240 | // ARP to router |
| 241 | toSelector = DefaultTrafficSelector.builder() |
| 242 | .matchInPort(intf.connectPoint().port()) |
| 243 | .matchEthType(EthType.EtherType.ARP.ethType().toShort()) |
| 244 | .matchVlanId(intf.vlan()) |
| 245 | .build(); |
| 246 | |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 247 | TrafficTreatment puntTreatment = DefaultTrafficTreatment.builder() |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 248 | .punt() |
| 249 | .build(); |
| 250 | |
| 251 | flowObjectiveService.forward(deviceId, |
kishore | 71a2753 | 2016-03-16 20:23:49 +0530 | [diff] [blame] | 252 | buildForwardingObjective(toSelector, puntTreatment, cpNextId, install)); |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 253 | |
| 254 | // ARP from router |
| 255 | fromSelector = DefaultTrafficSelector.builder() |
| 256 | .matchInPort(controlPlanePort) |
| 257 | .matchEthSrc(intf.mac()) |
| 258 | .matchVlanId(intf.vlan()) |
| 259 | .matchEthType(EthType.EtherType.ARP.ethType().toShort()) |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 260 | .matchArpSpa(ip.ipAddress().getIp4Address()) |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 261 | .build(); |
| 262 | |
| 263 | flowObjectiveService.forward(deviceId, |
kishore | 71a2753 | 2016-03-16 20:23:49 +0530 | [diff] [blame] | 264 | buildForwardingObjective(fromSelector, puntTreatment, intfNextId, install)); |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 265 | } |
| 266 | } |
| 267 | |
kishore | 71a2753 | 2016-03-16 20:23:49 +0530 | [diff] [blame] | 268 | /** |
| 269 | * Install or removes ospf forwarding rules. |
| 270 | * |
| 271 | * @param intf the Interface on which event is received |
| 272 | * @param install true to create an add objective, false to create a remove |
| 273 | * objective |
| 274 | **/ |
| 275 | private void updateOspfForwarding(Interface intf, boolean install) { |
Jonathan Hart | ea49238 | 2016-01-13 09:33:13 -0800 | [diff] [blame] | 276 | // OSPF to router |
| 277 | TrafficSelector toSelector = DefaultTrafficSelector.builder() |
| 278 | .matchInPort(intf.connectPoint().port()) |
| 279 | .matchEthType(EthType.EtherType.IPV4.ethType().toShort()) |
| 280 | .matchVlanId(intf.vlan()) |
| 281 | .matchIPProtocol((byte) OSPF_IP_PROTO) |
| 282 | .build(); |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 283 | |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 284 | // create nextObjectives for forwarding to the controlPlaneConnectPoint |
| 285 | DeviceId deviceId = controlPlaneConnectPoint.deviceId(); |
| 286 | PortNumber controlPlanePort = controlPlaneConnectPoint.port(); |
| 287 | int cpNextId; |
| 288 | if (intf.vlan() == VlanId.NONE) { |
kishore | 71a2753 | 2016-03-16 20:23:49 +0530 | [diff] [blame] | 289 | cpNextId = modifyNextObjective(deviceId, controlPlanePort, |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 290 | VlanId.vlanId(SingleSwitchFibInstaller.ASSIGNED_VLAN), |
kishore | 71a2753 | 2016-03-16 20:23:49 +0530 | [diff] [blame] | 291 | true, install); |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 292 | } else { |
kishore | 71a2753 | 2016-03-16 20:23:49 +0530 | [diff] [blame] | 293 | cpNextId = modifyNextObjective(deviceId, controlPlanePort, |
| 294 | intf.vlan(), false, install); |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 295 | } |
| 296 | log.debug("ospf flows intf:{} nextid:{}", intf, cpNextId); |
Jonathan Hart | ea49238 | 2016-01-13 09:33:13 -0800 | [diff] [blame] | 297 | flowObjectiveService.forward(controlPlaneConnectPoint.deviceId(), |
kishore | 71a2753 | 2016-03-16 20:23:49 +0530 | [diff] [blame] | 298 | buildForwardingObjective(toSelector, null, cpNextId, install ? ospfEnabled : install)); |
Jonathan Hart | ea49238 | 2016-01-13 09:33:13 -0800 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | /** |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 302 | * Creates a next objective for forwarding to a port. Handles metadata for |
| 303 | * some pipelines that require vlan information for egress port. |
| 304 | * |
| 305 | * @param deviceId the device on which the next objective is being created |
| 306 | * @param portNumber the egress port |
| 307 | * @param vlanId vlan information for egress port |
| 308 | * @param popVlan if vlan tag should be popped or not |
kishore | 71a2753 | 2016-03-16 20:23:49 +0530 | [diff] [blame] | 309 | * @param install true to create an add next objective, false to create a remove |
| 310 | * next objective |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 311 | * @return nextId of the next objective created |
| 312 | */ |
kishore | 71a2753 | 2016-03-16 20:23:49 +0530 | [diff] [blame] | 313 | private int modifyNextObjective(DeviceId deviceId, PortNumber portNumber, |
| 314 | VlanId vlanId, boolean popVlan, boolean install) { |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 315 | int nextId = flowObjectiveService.allocateNextId(); |
| 316 | NextObjective.Builder nextObjBuilder = DefaultNextObjective |
| 317 | .builder().withId(nextId) |
| 318 | .withType(NextObjective.Type.SIMPLE) |
| 319 | .fromApp(appId); |
| 320 | |
| 321 | TrafficTreatment.Builder ttBuilder = DefaultTrafficTreatment.builder(); |
| 322 | if (popVlan) { |
| 323 | ttBuilder.popVlan(); |
| 324 | } |
| 325 | ttBuilder.setOutput(portNumber); |
| 326 | |
| 327 | // setup metadata to pass to nextObjective - indicate the vlan on egress |
| 328 | // if needed by the switch pipeline. |
| 329 | TrafficSelector.Builder metabuilder = DefaultTrafficSelector.builder(); |
| 330 | metabuilder.matchVlanId(vlanId); |
| 331 | |
| 332 | nextObjBuilder.withMeta(metabuilder.build()); |
| 333 | nextObjBuilder.addTreatment(ttBuilder.build()); |
| 334 | log.debug("Submited next objective {} in device {} for port/vlan {}/{}", |
| 335 | nextId, deviceId, portNumber, vlanId); |
kishore | 71a2753 | 2016-03-16 20:23:49 +0530 | [diff] [blame] | 336 | if (install) { |
| 337 | flowObjectiveService.next(deviceId, nextObjBuilder.add()); |
| 338 | } else { |
| 339 | flowObjectiveService.next(deviceId, nextObjBuilder.remove()); |
| 340 | } |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 341 | return nextId; |
| 342 | } |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 343 | /** |
| 344 | * Builds a forwarding objective from the given selector, treatment and nextId. |
Jonathan Hart | ea49238 | 2016-01-13 09:33:13 -0800 | [diff] [blame] | 345 | * |
| 346 | * @param selector selector |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 347 | * @param treatment treatment to apply to packet, can be null |
| 348 | * @param nextId next objective to point to for forwarding packet |
Jonathan Hart | ea49238 | 2016-01-13 09:33:13 -0800 | [diff] [blame] | 349 | * @param add true to create an add objective, false to create a remove |
| 350 | * objective |
| 351 | * @return forwarding objective |
| 352 | */ |
| 353 | private ForwardingObjective buildForwardingObjective(TrafficSelector selector, |
| 354 | TrafficTreatment treatment, |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 355 | int nextId, |
Jonathan Hart | ea49238 | 2016-01-13 09:33:13 -0800 | [diff] [blame] | 356 | boolean add) { |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 357 | DefaultForwardingObjective.Builder fobBuilder = DefaultForwardingObjective.builder(); |
| 358 | fobBuilder.withSelector(selector); |
| 359 | if (treatment != null) { |
| 360 | fobBuilder.withTreatment(treatment); |
| 361 | } |
| 362 | if (nextId != -1) { |
| 363 | fobBuilder.nextStep(nextId); |
| 364 | } |
| 365 | fobBuilder.fromApp(appId) |
Charles Chan | d0fd5dc | 2016-02-16 23:14:49 -0800 | [diff] [blame] | 366 | .withPriority(ACL_PRIORITY) |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 367 | .withFlag(ForwardingObjective.Flag.VERSATILE); |
Jonathan Hart | ea49238 | 2016-01-13 09:33:13 -0800 | [diff] [blame] | 368 | |
| 369 | return add ? fobBuilder.add() : fobBuilder.remove(); |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 370 | } |
| 371 | |
Jonathan Hart | ea49238 | 2016-01-13 09:33:13 -0800 | [diff] [blame] | 372 | /** |
| 373 | * Listener for device events. |
| 374 | */ |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 375 | private class InternalDeviceListener implements DeviceListener { |
kishore | 71a2753 | 2016-03-16 20:23:49 +0530 | [diff] [blame] | 376 | |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 377 | @Override |
| 378 | public void event(DeviceEvent event) { |
| 379 | if (controlPlaneConnectPoint != null && |
| 380 | event.subject().id().equals(controlPlaneConnectPoint.deviceId())) { |
| 381 | switch (event.type()) { |
| 382 | case DEVICE_ADDED: |
| 383 | case DEVICE_AVAILABILITY_CHANGED: |
| 384 | if (deviceService.isAvailable(event.subject().id())) { |
| 385 | log.info("Device connected {}", event.subject().id()); |
Jonathan Hart | ea49238 | 2016-01-13 09:33:13 -0800 | [diff] [blame] | 386 | updateDevice(); |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | break; |
| 390 | case DEVICE_UPDATED: |
| 391 | case DEVICE_REMOVED: |
| 392 | case DEVICE_SUSPENDED: |
| 393 | case PORT_ADDED: |
| 394 | case PORT_UPDATED: |
| 395 | case PORT_REMOVED: |
| 396 | default: |
| 397 | break; |
| 398 | } |
| 399 | } |
| 400 | } |
| 401 | } |
| 402 | |
Jonathan Hart | ea49238 | 2016-01-13 09:33:13 -0800 | [diff] [blame] | 403 | /** |
| 404 | * Listener for network config events. |
| 405 | */ |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 406 | private class InternalNetworkConfigListener implements NetworkConfigListener { |
kishore | 71a2753 | 2016-03-16 20:23:49 +0530 | [diff] [blame] | 407 | |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 408 | @Override |
| 409 | public void event(NetworkConfigEvent event) { |
Jonathan Hart | ea49238 | 2016-01-13 09:33:13 -0800 | [diff] [blame] | 410 | if (event.configClass().equals(RoutingService.ROUTER_CONFIG_CLASS)) { |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 411 | switch (event.type()) { |
| 412 | case CONFIG_ADDED: |
| 413 | case CONFIG_UPDATED: |
| 414 | updateConfig(); |
| 415 | break; |
| 416 | case CONFIG_REGISTERED: |
| 417 | case CONFIG_UNREGISTERED: |
| 418 | case CONFIG_REMOVED: |
| 419 | default: |
| 420 | break; |
| 421 | } |
| 422 | } |
| 423 | } |
| 424 | } |
Charles Chan | d0fd5dc | 2016-02-16 23:14:49 -0800 | [diff] [blame] | 425 | |
| 426 | /** |
| 427 | * Listener for host events. |
| 428 | */ |
| 429 | private class InternalHostListener implements HostListener { |
kishore | 71a2753 | 2016-03-16 20:23:49 +0530 | [diff] [blame] | 430 | |
Charles Chan | d0fd5dc | 2016-02-16 23:14:49 -0800 | [diff] [blame] | 431 | private void peerAdded(HostEvent event) { |
| 432 | Host peer = event.subject(); |
| 433 | Optional<Interface> peerIntf = |
| 434 | interfaceService.getInterfacesByPort(peer.location()).stream() |
| 435 | .filter(intf -> interfaces.isEmpty() || interfaces.contains(intf.name())) |
| 436 | .filter(intf -> peer.vlan().equals(intf.vlan())) |
| 437 | .findFirst(); |
| 438 | if (!peerIntf.isPresent()) { |
| 439 | log.debug("Adding peer {}/{} on {} but the interface is not configured", |
| 440 | peer.mac(), peer.vlan(), peer.location()); |
| 441 | return; |
| 442 | } |
| 443 | |
| 444 | // Generate L3 Unicast groups and store it in the map |
| 445 | int toRouterL3Unicast = createPeerGroup(peer.mac(), peerIntf.get().mac(), |
| 446 | peer.vlan(), peer.location().deviceId(), controlPlaneConnectPoint.port()); |
| 447 | int toPeerL3Unicast = createPeerGroup(peerIntf.get().mac(), peer.mac(), |
| 448 | peer.vlan(), peer.location().deviceId(), peer.location().port()); |
| 449 | peerNextId.put(peer, ImmutableSortedSet.of(toRouterL3Unicast, toPeerL3Unicast)); |
| 450 | |
| 451 | // From peer to router |
| 452 | peerIntf.get().ipAddresses().forEach(routerIp -> { |
| 453 | flowObjectiveService.forward(peer.location().deviceId(), |
| 454 | createPeerObjBuilder(toRouterL3Unicast, routerIp.ipAddress().toIpPrefix()).add()); |
| 455 | }); |
| 456 | |
| 457 | // From router to peer |
| 458 | peer.ipAddresses().forEach(peerIp -> { |
| 459 | flowObjectiveService.forward(peer.location().deviceId(), |
| 460 | createPeerObjBuilder(toPeerL3Unicast, peerIp.toIpPrefix()).add()); |
| 461 | }); |
| 462 | } |
| 463 | |
| 464 | private void peerRemoved(HostEvent event) { |
| 465 | Host peer = event.subject(); |
| 466 | Optional<Interface> peerIntf = |
| 467 | interfaceService.getInterfacesByPort(peer.location()).stream() |
| 468 | .filter(intf -> interfaces.isEmpty() || interfaces.contains(intf.name())) |
| 469 | .filter(intf -> peer.vlan().equals(intf.vlan())) |
| 470 | .findFirst(); |
| 471 | if (!peerIntf.isPresent()) { |
| 472 | log.debug("Removing peer {}/{} on {} but the interface is not configured", |
| 473 | peer.mac(), peer.vlan(), peer.location()); |
| 474 | return; |
| 475 | } |
| 476 | |
| 477 | Set<Integer> nextIds = peerNextId.get(peer); |
| 478 | checkState(peerNextId.get(peer) != null, |
| 479 | "Peer nextId should not be null"); |
| 480 | checkState(peerNextId.get(peer).size() == 2, |
| 481 | "Wrong nextId associated with the peer"); |
| 482 | Iterator<Integer> iter = peerNextId.get(peer).iterator(); |
| 483 | int toRouterL3Unicast = iter.next(); |
| 484 | int toPeerL3Unicast = iter.next(); |
| 485 | |
| 486 | // From peer to router |
| 487 | peerIntf.get().ipAddresses().forEach(routerIp -> { |
| 488 | flowObjectiveService.forward(peer.location().deviceId(), |
| 489 | createPeerObjBuilder(toRouterL3Unicast, routerIp.ipAddress().toIpPrefix()).remove()); |
| 490 | }); |
| 491 | |
| 492 | // From router to peer |
| 493 | peer.ipAddresses().forEach(peerIp -> { |
| 494 | flowObjectiveService.forward(peer.location().deviceId(), |
| 495 | createPeerObjBuilder(toPeerL3Unicast, peerIp.toIpPrefix()).remove()); |
| 496 | }); |
| 497 | } |
| 498 | |
| 499 | private ForwardingObjective.Builder createPeerObjBuilder( |
| 500 | int nextId, IpPrefix ipAddresses) { |
| 501 | TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder(); |
| 502 | sbuilder.matchEthType(EthType.EtherType.IPV4.ethType().toShort()); |
| 503 | sbuilder.matchIPDst(ipAddresses); |
| 504 | DefaultForwardingObjective.Builder builder = |
| 505 | DefaultForwardingObjective.builder() |
| 506 | .withSelector(sbuilder.build()) |
| 507 | .fromApp(appId) |
| 508 | .withPriority(getPriorityFromPrefix(ipAddresses)) |
| 509 | .withFlag(ForwardingObjective.Flag.SPECIFIC); |
| 510 | if (nextId != -1) { |
| 511 | builder.nextStep(nextId); |
| 512 | } |
| 513 | return builder; |
| 514 | } |
| 515 | |
| 516 | private int createPeerGroup(MacAddress srcMac, MacAddress dstMac, |
| 517 | VlanId vlanId, DeviceId deviceId, PortNumber port) { |
| 518 | int nextId = flowObjectiveService.allocateNextId(); |
| 519 | NextObjective.Builder nextObjBuilder = DefaultNextObjective.builder() |
| 520 | .withId(nextId) |
| 521 | .withType(NextObjective.Type.SIMPLE) |
| 522 | .fromApp(appId); |
| 523 | |
| 524 | TrafficTreatment.Builder ttBuilder = DefaultTrafficTreatment.builder(); |
| 525 | ttBuilder.setEthSrc(srcMac); |
| 526 | ttBuilder.setEthDst(dstMac); |
| 527 | ttBuilder.setOutput(port); |
| 528 | nextObjBuilder.addTreatment(ttBuilder.build()); |
| 529 | |
| 530 | TrafficSelector.Builder metabuilder = DefaultTrafficSelector.builder(); |
| 531 | VlanId matchVlanId = (vlanId.equals(VlanId.NONE)) ? |
| 532 | VlanId.vlanId(SingleSwitchFibInstaller.ASSIGNED_VLAN) : |
| 533 | vlanId; |
| 534 | metabuilder.matchVlanId(matchVlanId); |
| 535 | nextObjBuilder.withMeta(metabuilder.build()); |
| 536 | |
| 537 | flowObjectiveService.next(deviceId, nextObjBuilder.add()); |
| 538 | return nextId; |
| 539 | } |
| 540 | |
| 541 | @Override |
| 542 | public void event(HostEvent event) { |
| 543 | DeviceId deviceId = event.subject().location().deviceId(); |
| 544 | if (!mastershipService.isLocalMaster(deviceId)) { |
| 545 | return; |
| 546 | } |
| 547 | switch (event.type()) { |
| 548 | case HOST_ADDED: |
| 549 | peerAdded(event); |
| 550 | break; |
| 551 | case HOST_MOVED: |
| 552 | //TODO We assume BGP peer does not move for now |
| 553 | break; |
| 554 | case HOST_REMOVED: |
| 555 | peerRemoved(event); |
| 556 | break; |
| 557 | case HOST_UPDATED: |
| 558 | //TODO We assume BGP peer does not change IP for now |
| 559 | break; |
| 560 | default: |
| 561 | break; |
| 562 | } |
| 563 | } |
| 564 | } |
| 565 | |
| 566 | private int getPriorityFromPrefix(IpPrefix prefix) { |
| 567 | return (prefix.isIp4()) ? |
| 568 | 2000 * prefix.prefixLength() + MIN_IP_PRIORITY : |
| 569 | 500 * prefix.prefixLength() + MIN_IP_PRIORITY; |
| 570 | } |
kishore | 71a2753 | 2016-03-16 20:23:49 +0530 | [diff] [blame] | 571 | private class InternalInterfaceListener implements InterfaceListener { |
| 572 | |
| 573 | @Override |
| 574 | public void event(InterfaceEvent event) { |
| 575 | Interface intf = event.subject(); |
| 576 | switch (event.type()) { |
| 577 | case INTERFACE_ADDED: |
| 578 | if (intf != null && !intf.connectPoint().equals(controlPlaneConnectPoint)) { |
| 579 | provisionInterface(intf); |
| 580 | } |
| 581 | break; |
| 582 | case INTERFACE_UPDATED: |
| 583 | break; |
| 584 | case INTERFACE_REMOVED: |
| 585 | if (intf != null && !intf.connectPoint().equals(controlPlaneConnectPoint)) { |
| 586 | removeInterface(intf); |
| 587 | } |
| 588 | break; |
| 589 | default: |
| 590 | break; |
| 591 | } |
| 592 | } |
| 593 | } |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 594 | } |