Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 1 | /* |
Jonathan Hart | f4bd048 | 2017-01-27 15:11:18 -0800 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Laboratory |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -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 | |
Jonathan Hart | f4bd048 | 2017-01-27 15:11:18 -0800 | [diff] [blame] | 17 | package org.onosproject.routing.cpr; |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 18 | |
Jonathan Hart | f04b7d9 | 2016-03-29 09:39:11 -0700 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableSortedSet; |
Vinayak Tejankar | 3a409c6 | 2017-01-12 02:20:53 +0530 | [diff] [blame] | 20 | import com.google.common.collect.Lists; |
Jonathan Hart | f04b7d9 | 2016-03-29 09:39:11 -0700 | [diff] [blame] | 21 | import com.google.common.collect.Maps; |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 22 | import org.apache.felix.scr.annotations.Activate; |
| 23 | import org.apache.felix.scr.annotations.Component; |
| 24 | import org.apache.felix.scr.annotations.Deactivate; |
| 25 | import org.apache.felix.scr.annotations.Reference; |
| 26 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 27 | import org.onlab.packet.EthType; |
Vinayak Tejankar | 3a409c6 | 2017-01-12 02:20:53 +0530 | [diff] [blame] | 28 | import org.onlab.packet.Ip4Address; |
Pier Luigi | 2b1ad87 | 2017-01-31 09:35:42 -0800 | [diff] [blame] | 29 | import org.onlab.packet.Ip6Address; |
Charles Chan | d0fd5dc | 2016-02-16 23:14:49 -0800 | [diff] [blame] | 30 | import org.onlab.packet.IpPrefix; |
| 31 | import org.onlab.packet.MacAddress; |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 32 | import org.onlab.packet.VlanId; |
kishore | 786b7e4 | 2016-05-19 16:25:57 +0530 | [diff] [blame] | 33 | import org.onosproject.app.ApplicationService; |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 34 | import org.onosproject.core.ApplicationId; |
| 35 | import org.onosproject.core.CoreService; |
| 36 | import org.onosproject.incubator.net.intf.Interface; |
| 37 | import org.onosproject.incubator.net.intf.InterfaceService; |
Charles Chan | d0fd5dc | 2016-02-16 23:14:49 -0800 | [diff] [blame] | 38 | import org.onosproject.mastership.MastershipService; |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 39 | import org.onosproject.net.DeviceId; |
Charles Chan | d0fd5dc | 2016-02-16 23:14:49 -0800 | [diff] [blame] | 40 | import org.onosproject.net.Host; |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 41 | import org.onosproject.net.PortNumber; |
| 42 | import org.onosproject.net.config.NetworkConfigEvent; |
| 43 | import org.onosproject.net.config.NetworkConfigListener; |
| 44 | import org.onosproject.net.config.NetworkConfigService; |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 45 | import org.onosproject.net.device.DeviceService; |
| 46 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 47 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 48 | import org.onosproject.net.flow.TrafficSelector; |
| 49 | import org.onosproject.net.flow.TrafficTreatment; |
| 50 | import org.onosproject.net.flowobjective.DefaultForwardingObjective; |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 51 | import org.onosproject.net.flowobjective.DefaultNextObjective; |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 52 | import org.onosproject.net.flowobjective.FlowObjectiveService; |
| 53 | import org.onosproject.net.flowobjective.ForwardingObjective; |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 54 | import org.onosproject.net.flowobjective.NextObjective; |
Charles Chan | d0fd5dc | 2016-02-16 23:14:49 -0800 | [diff] [blame] | 55 | import org.onosproject.net.host.HostEvent; |
| 56 | import org.onosproject.net.host.HostListener; |
| 57 | import org.onosproject.net.host.HostService; |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 58 | import org.onosproject.net.host.InterfaceIpAddress; |
Jonathan Hart | 249b4cf | 2017-02-03 18:02:58 -0800 | [diff] [blame] | 59 | import org.onosproject.routing.RouterInfo; |
| 60 | import org.onosproject.routing.InterfaceProvisionRequest; |
| 61 | import org.onosproject.routing.Router; |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 62 | import org.onosproject.routing.RoutingService; |
Jonathan Hart | 249b4cf | 2017-02-03 18:02:58 -0800 | [diff] [blame] | 63 | import org.onosproject.routing.config.RouterConfigHelper; |
| 64 | import org.onosproject.routing.config.RoutersConfig; |
| 65 | import org.onosproject.routing.config.RoutingConfigurationService; |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 66 | import org.slf4j.Logger; |
| 67 | |
Jonathan Hart | f04b7d9 | 2016-03-29 09:39:11 -0700 | [diff] [blame] | 68 | import java.util.Iterator; |
| 69 | import java.util.List; |
| 70 | import java.util.Map; |
| 71 | import java.util.Optional; |
| 72 | import java.util.Set; |
Jonathan Hart | 249b4cf | 2017-02-03 18:02:58 -0800 | [diff] [blame] | 73 | import java.util.concurrent.ConcurrentHashMap; |
Jonathan Hart | f04b7d9 | 2016-03-29 09:39:11 -0700 | [diff] [blame] | 74 | |
| 75 | import static com.google.common.base.Preconditions.checkState; |
Jonathan Hart | f8cd052 | 2016-10-25 07:09:55 -0700 | [diff] [blame] | 76 | import static org.onlab.packet.Ethernet.TYPE_ARP; |
| 77 | import static org.onlab.packet.Ethernet.TYPE_IPV4; |
| 78 | import static org.onlab.packet.Ethernet.TYPE_IPV6; |
| 79 | import static org.onlab.packet.ICMP6.NEIGHBOR_ADVERTISEMENT; |
| 80 | import static org.onlab.packet.ICMP6.NEIGHBOR_SOLICITATION; |
| 81 | import static org.onlab.packet.IPv6.PROTOCOL_ICMP6; |
Pier Luigi | 2b1ad87 | 2017-01-31 09:35:42 -0800 | [diff] [blame] | 82 | import static org.onlab.packet.IPv6.getLinkLocalAddress; |
| 83 | import static org.onlab.packet.IPv6.getSolicitNodeAddress; |
Jonathan Hart | f04b7d9 | 2016-03-29 09:39:11 -0700 | [diff] [blame] | 84 | import static org.slf4j.LoggerFactory.getLogger; |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 85 | |
| 86 | /** |
| 87 | * Manages connectivity between peers redirecting control traffic to a routing |
| 88 | * control plane available on the dataplane. |
| 89 | */ |
Jonathan Hart | f4bd048 | 2017-01-27 15:11:18 -0800 | [diff] [blame] | 90 | @Component(immediate = true) |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 91 | public class ControlPlaneRedirectManager { |
| 92 | |
| 93 | private final Logger log = getLogger(getClass()); |
| 94 | |
Jonathan Hart | f4bd048 | 2017-01-27 15:11:18 -0800 | [diff] [blame] | 95 | public static final short ASSIGNED_VLAN = 4094; |
| 96 | |
Charles Chan | d0fd5dc | 2016-02-16 23:14:49 -0800 | [diff] [blame] | 97 | private static final int MIN_IP_PRIORITY = 10; |
kishore | 36d1c27 | 2016-09-21 15:44:10 +0530 | [diff] [blame] | 98 | private static final int IPV4_PRIORITY = 2000; |
| 99 | private static final int IPV6_PRIORITY = 500; |
Vinayak Tejankar | 3a409c6 | 2017-01-12 02:20:53 +0530 | [diff] [blame] | 100 | static final int ACL_PRIORITY = 40001; |
Jonathan Hart | ea49238 | 2016-01-13 09:33:13 -0800 | [diff] [blame] | 101 | private static final int OSPF_IP_PROTO = 0x59; |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 102 | |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 103 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 104 | protected CoreService coreService; |
| 105 | |
| 106 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 107 | protected DeviceService deviceService; |
| 108 | |
| 109 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 110 | protected InterfaceService interfaceService; |
| 111 | |
| 112 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 113 | protected FlowObjectiveService flowObjectiveService; |
| 114 | |
| 115 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 116 | protected NetworkConfigService networkConfigService; |
| 117 | |
Charles Chan | d0fd5dc | 2016-02-16 23:14:49 -0800 | [diff] [blame] | 118 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 119 | protected MastershipService mastershipService; |
| 120 | |
| 121 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 122 | protected HostService hostService; |
| 123 | |
kishore | 786b7e4 | 2016-05-19 16:25:57 +0530 | [diff] [blame] | 124 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 125 | protected ApplicationService applicationService; |
| 126 | |
Jonathan Hart | 249b4cf | 2017-02-03 18:02:58 -0800 | [diff] [blame] | 127 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 128 | protected RoutingConfigurationService rs; |
| 129 | |
Jonathan Hart | e732704 | 2017-02-02 13:11:25 -0800 | [diff] [blame] | 130 | private static final String APP_NAME = "org.onosproject.cpr"; |
Jonathan Hart | f8cd052 | 2016-10-25 07:09:55 -0700 | [diff] [blame] | 131 | private ApplicationId appId; |
| 132 | |
Jonathan Hart | f8cd052 | 2016-10-25 07:09:55 -0700 | [diff] [blame] | 133 | private Map<Host, Set<Integer>> peerNextId = Maps.newConcurrentMap(); |
| 134 | |
Jonathan Hart | 249b4cf | 2017-02-03 18:02:58 -0800 | [diff] [blame] | 135 | private Map<DeviceId, Router> routers = new ConcurrentHashMap<>(); |
Jonathan Hart | f8cd052 | 2016-10-25 07:09:55 -0700 | [diff] [blame] | 136 | |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 137 | private final InternalNetworkConfigListener networkConfigListener = |
| 138 | new InternalNetworkConfigListener(); |
Charles Chan | d0fd5dc | 2016-02-16 23:14:49 -0800 | [diff] [blame] | 139 | private final InternalHostListener hostListener = new InternalHostListener(); |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 140 | |
| 141 | @Activate |
Jonathan Hart | e732704 | 2017-02-02 13:11:25 -0800 | [diff] [blame] | 142 | protected void activate() { |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 143 | this.appId = coreService.registerApplication(APP_NAME); |
| 144 | |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 145 | networkConfigService.addListener(networkConfigListener); |
Charles Chan | d0fd5dc | 2016-02-16 23:14:49 -0800 | [diff] [blame] | 146 | hostService.addListener(hostListener); |
Jonathan Hart | f8cd052 | 2016-10-25 07:09:55 -0700 | [diff] [blame] | 147 | |
Jonathan Hart | 7fc5a72 | 2017-01-26 15:54:12 -0800 | [diff] [blame] | 148 | processRouterConfig(); |
Charles Chan | 00d8b5f | 2016-12-04 17:17:39 -0800 | [diff] [blame] | 149 | |
Jonathan Hart | 249b4cf | 2017-02-03 18:02:58 -0800 | [diff] [blame] | 150 | applicationService.registerDeactivateHook(this.appId, |
| 151 | () -> routers.forEach((d, r) -> r.cleanup())); |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | @Deactivate |
Jonathan Hart | e732704 | 2017-02-02 13:11:25 -0800 | [diff] [blame] | 155 | protected void deactivate() { |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 156 | networkConfigService.removeListener(networkConfigListener); |
Charles Chan | d0fd5dc | 2016-02-16 23:14:49 -0800 | [diff] [blame] | 157 | hostService.removeListener(hostListener); |
Jonathan Hart | f8cd052 | 2016-10-25 07:09:55 -0700 | [diff] [blame] | 158 | } |
| 159 | |
kishore | 786b7e4 | 2016-05-19 16:25:57 +0530 | [diff] [blame] | 160 | /** |
Jonathan Hart | f8cd052 | 2016-10-25 07:09:55 -0700 | [diff] [blame] | 161 | * Sets up the router interfaces if router config is available. |
| 162 | */ |
Jonathan Hart | 7fc5a72 | 2017-01-26 15:54:12 -0800 | [diff] [blame] | 163 | private void processRouterConfig() { |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 164 | ApplicationId routingAppId = |
| 165 | coreService.registerApplication(RoutingService.ROUTER_APP_ID); |
| 166 | |
Jonathan Hart | 249b4cf | 2017-02-03 18:02:58 -0800 | [diff] [blame] | 167 | Set<RoutersConfig.Router> routerConfigs = |
| 168 | RouterConfigHelper.getRouterConfigurations(networkConfigService, routingAppId); |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 169 | |
Jonathan Hart | 249b4cf | 2017-02-03 18:02:58 -0800 | [diff] [blame] | 170 | for (RoutersConfig.Router router : routerConfigs) { |
| 171 | DeviceId deviceId = router.controlPlaneConnectPoint().deviceId(); |
| 172 | |
| 173 | routers.compute(deviceId, (d, r) -> { |
| 174 | if (r == null) { |
| 175 | return createRouter(RouterInfo.from(router)); |
| 176 | } else { |
| 177 | r.changeConfiguration(RouterInfo.from(router)); |
| 178 | return r; |
| 179 | } |
| 180 | }); |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 181 | } |
| 182 | |
Jonathan Hart | 249b4cf | 2017-02-03 18:02:58 -0800 | [diff] [blame] | 183 | for (DeviceId deviceId : routers.keySet()) { |
| 184 | if (!configExists(deviceId, routerConfigs)) { |
| 185 | Router router = routers.remove(deviceId); |
| 186 | router.cleanup(); |
| 187 | } |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 188 | } |
| 189 | } |
| 190 | |
Jonathan Hart | 249b4cf | 2017-02-03 18:02:58 -0800 | [diff] [blame] | 191 | private boolean configExists(DeviceId deviceId, Set<RoutersConfig.Router> config) { |
| 192 | return config.stream() |
| 193 | .anyMatch(r -> r.controlPlaneConnectPoint().deviceId().equals(deviceId)); |
Jonathan Hart | 7fc5a72 | 2017-01-26 15:54:12 -0800 | [diff] [blame] | 194 | } |
| 195 | |
Jonathan Hart | 249b4cf | 2017-02-03 18:02:58 -0800 | [diff] [blame] | 196 | private Router createRouter(RouterInfo info) { |
| 197 | return new Router(info, |
Jonathan Hart | 7fc5a72 | 2017-01-26 15:54:12 -0800 | [diff] [blame] | 198 | interfaceService, |
Jonathan Hart | 249b4cf | 2017-02-03 18:02:58 -0800 | [diff] [blame] | 199 | deviceService, |
Jonathan Hart | 7fc5a72 | 2017-01-26 15:54:12 -0800 | [diff] [blame] | 200 | this::provisionInterface, |
| 201 | this::unprovisionInterface); |
| 202 | } |
| 203 | |
Jonathan Hart | 249b4cf | 2017-02-03 18:02:58 -0800 | [diff] [blame] | 204 | private void provisionInterface(InterfaceProvisionRequest intf) { |
Jonathan Hart | 7fc5a72 | 2017-01-26 15:54:12 -0800 | [diff] [blame] | 205 | updateInterfaceObjectives(intf, true); |
| 206 | } |
| 207 | |
Jonathan Hart | 249b4cf | 2017-02-03 18:02:58 -0800 | [diff] [blame] | 208 | private void unprovisionInterface(InterfaceProvisionRequest intf) { |
Jonathan Hart | 7fc5a72 | 2017-01-26 15:54:12 -0800 | [diff] [blame] | 209 | updateInterfaceObjectives(intf, false); |
| 210 | } |
| 211 | |
| 212 | /** |
| 213 | * Installs or removes flow objectives relating to a give interface. |
| 214 | * |
| 215 | * @param intf interface to change objectives for |
| 216 | * @param install true to install the objectives, false to remove them |
| 217 | */ |
Jonathan Hart | 249b4cf | 2017-02-03 18:02:58 -0800 | [diff] [blame] | 218 | private void updateInterfaceObjectives(InterfaceProvisionRequest intf, boolean install) { |
Jonathan Hart | f8035d3 | 2016-06-16 16:23:26 -0700 | [diff] [blame] | 219 | updateInterfaceForwarding(intf, install); |
| 220 | updateOspfForwarding(intf, install); |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 221 | } |
| 222 | |
kishore | 71a2753 | 2016-03-16 20:23:49 +0530 | [diff] [blame] | 223 | /** |
Jonathan Hart | 7fc5a72 | 2017-01-26 15:54:12 -0800 | [diff] [blame] | 224 | * Installs or removes the basic forwarding flows for each interface. |
kishore | 71a2753 | 2016-03-16 20:23:49 +0530 | [diff] [blame] | 225 | * |
Jonathan Hart | 249b4cf | 2017-02-03 18:02:58 -0800 | [diff] [blame] | 226 | * @param request provisioning request containing router and interface |
Jonathan Hart | 7fc5a72 | 2017-01-26 15:54:12 -0800 | [diff] [blame] | 227 | * @param install true to install the objectives, false to remove them |
| 228 | */ |
Jonathan Hart | 249b4cf | 2017-02-03 18:02:58 -0800 | [diff] [blame] | 229 | private void updateInterfaceForwarding(InterfaceProvisionRequest request, boolean install) { |
| 230 | Interface intf = request.intf(); |
Jonathan Hart | 7fc5a72 | 2017-01-26 15:54:12 -0800 | [diff] [blame] | 231 | log.debug("{} interface objectives for {}", operation(install), intf); |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 232 | |
Jonathan Hart | f8cd052 | 2016-10-25 07:09:55 -0700 | [diff] [blame] | 233 | DeviceId deviceId = intf.connectPoint().deviceId(); |
Jonathan Hart | 249b4cf | 2017-02-03 18:02:58 -0800 | [diff] [blame] | 234 | |
| 235 | PortNumber controlPlanePort = request.controlPlaneConnectPoint().port(); |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 236 | for (InterfaceIpAddress ip : intf.ipAddresses()) { |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 237 | // create nextObjectives for forwarding to this interface and the |
| 238 | // controlPlaneConnectPoint |
| 239 | int cpNextId, intfNextId; |
| 240 | if (intf.vlan() == VlanId.NONE) { |
kishore | 71a2753 | 2016-03-16 20:23:49 +0530 | [diff] [blame] | 241 | cpNextId = modifyNextObjective(deviceId, controlPlanePort, |
Jonathan Hart | f4bd048 | 2017-01-27 15:11:18 -0800 | [diff] [blame] | 242 | VlanId.vlanId(ASSIGNED_VLAN), |
kishore | 71a2753 | 2016-03-16 20:23:49 +0530 | [diff] [blame] | 243 | true, install); |
| 244 | intfNextId = modifyNextObjective(deviceId, intf.connectPoint().port(), |
Jonathan Hart | f4bd048 | 2017-01-27 15:11:18 -0800 | [diff] [blame] | 245 | VlanId.vlanId(ASSIGNED_VLAN), |
kishore | 71a2753 | 2016-03-16 20:23:49 +0530 | [diff] [blame] | 246 | true, install); |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 247 | } else { |
kishore | 71a2753 | 2016-03-16 20:23:49 +0530 | [diff] [blame] | 248 | cpNextId = modifyNextObjective(deviceId, controlPlanePort, |
| 249 | intf.vlan(), false, install); |
| 250 | intfNextId = modifyNextObjective(deviceId, intf.connectPoint().port(), |
| 251 | intf.vlan(), false, install); |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 252 | } |
Vinayak Tejankar | 3a409c6 | 2017-01-12 02:20:53 +0530 | [diff] [blame] | 253 | List<ForwardingObjective> fwdToSend = Lists.newArrayList(); |
| 254 | TrafficSelector selector; |
| 255 | // IP traffic toward the router. |
| 256 | selector = buildIPDstSelector( |
| 257 | ip.ipAddress().toIpPrefix(), |
| 258 | intf.connectPoint().port(), |
| 259 | null, |
| 260 | intf.mac(), |
| 261 | intf.vlan() |
| 262 | ); |
| 263 | fwdToSend.add(buildForwardingObjective(selector, null, cpNextId, install, ACL_PRIORITY)); |
| 264 | // IP traffic from the router. |
| 265 | selector = buildIPSrcSelector( |
| 266 | ip.ipAddress().toIpPrefix(), |
| 267 | controlPlanePort, |
| 268 | intf.mac(), |
| 269 | null, |
| 270 | intf.vlan() |
| 271 | ); |
| 272 | fwdToSend.add(buildForwardingObjective(selector, null, intfNextId, install, ACL_PRIORITY)); |
| 273 | // We build the punt treatment. |
| 274 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 275 | .punt() |
| 276 | .build(); |
Vinayak Tejankar | 3a409c6 | 2017-01-12 02:20:53 +0530 | [diff] [blame] | 277 | // Handling of neighbour discovery protocols. |
| 278 | // IPv4 traffic - we have to deal with the ARP protocol. |
| 279 | // IPv6 traffic - we have to deal with the NDP protocol. |
| 280 | if (ip.ipAddress().isIp4()) { |
| 281 | // ARP traffic towards the router. |
| 282 | selector = buildArpSelector( |
| 283 | intf.connectPoint().port(), |
| 284 | intf.vlan(), |
| 285 | null, |
| 286 | null |
| 287 | ); |
| 288 | fwdToSend.add(buildForwardingObjective(selector, treatment, cpNextId, install, ACL_PRIORITY + 1)); |
| 289 | // ARP traffic from the router. |
| 290 | selector = buildArpSelector( |
| 291 | controlPlanePort, |
| 292 | intf.vlan(), |
| 293 | ip.ipAddress().getIp4Address(), |
| 294 | intf.mac() |
| 295 | ); |
| 296 | fwdToSend.add(buildForwardingObjective(selector, treatment, intfNextId, install, ACL_PRIORITY + 1)); |
| 297 | } else { |
| 298 | // Neighbour solicitation traffic towards the router. |
Pier Luigi | 2b1ad87 | 2017-01-31 09:35:42 -0800 | [diff] [blame] | 299 | // This flow is for the global unicast address. |
Vinayak Tejankar | 3a409c6 | 2017-01-12 02:20:53 +0530 | [diff] [blame] | 300 | selector = buildNdpSelector( |
| 301 | intf.connectPoint().port(), |
| 302 | intf.vlan(), |
Pier Luigi | 2b1ad87 | 2017-01-31 09:35:42 -0800 | [diff] [blame] | 303 | null, |
Pier Luigi | 9f765dc | 2017-02-03 13:35:23 -0800 | [diff] [blame] | 304 | ip.ipAddress().toIpPrefix(), |
Pier Luigi | 2b1ad87 | 2017-01-31 09:35:42 -0800 | [diff] [blame] | 305 | NEIGHBOR_SOLICITATION, |
| 306 | null |
| 307 | ); |
| 308 | fwdToSend.add(buildForwardingObjective(selector, treatment, cpNextId, install, ACL_PRIORITY + 1)); |
| 309 | // Neighbour solicitation traffic towards the router. |
| 310 | // This flow is for the link local address. |
| 311 | selector = buildNdpSelector( |
| 312 | intf.connectPoint().port(), |
| 313 | intf.vlan(), |
Pier Luigi | 2b1ad87 | 2017-01-31 09:35:42 -0800 | [diff] [blame] | 314 | null, |
Pier Luigi | 9f765dc | 2017-02-03 13:35:23 -0800 | [diff] [blame] | 315 | Ip6Address.valueOf(getLinkLocalAddress(intf.mac().toBytes())).toIpPrefix(), |
Pier Luigi | 2b1ad87 | 2017-01-31 09:35:42 -0800 | [diff] [blame] | 316 | NEIGHBOR_SOLICITATION, |
| 317 | null |
| 318 | ); |
| 319 | fwdToSend.add(buildForwardingObjective(selector, treatment, cpNextId, install, ACL_PRIORITY + 1)); |
| 320 | // Neighbour solicitation traffic towards the router. |
| 321 | // This flow is for the solicitation node address of |
| 322 | // the global unicast address. |
| 323 | selector = buildNdpSelector( |
| 324 | intf.connectPoint().port(), |
| 325 | intf.vlan(), |
Pier Luigi | 2b1ad87 | 2017-01-31 09:35:42 -0800 | [diff] [blame] | 326 | null, |
Pier Luigi | 9f765dc | 2017-02-03 13:35:23 -0800 | [diff] [blame] | 327 | Ip6Address.valueOf(getSolicitNodeAddress(ip.ipAddress().toOctets())).toIpPrefix(), |
Pier Luigi | 2b1ad87 | 2017-01-31 09:35:42 -0800 | [diff] [blame] | 328 | NEIGHBOR_SOLICITATION, |
| 329 | null |
| 330 | ); |
| 331 | fwdToSend.add(buildForwardingObjective(selector, treatment, cpNextId, install, ACL_PRIORITY + 1)); |
| 332 | // Neighbour solicitation traffic towards the router. |
| 333 | // This flow is for the solicitation node address of |
| 334 | // the link local address. |
| 335 | selector = buildNdpSelector( |
| 336 | intf.connectPoint().port(), |
| 337 | intf.vlan(), |
Pier Luigi | 9f765dc | 2017-02-03 13:35:23 -0800 | [diff] [blame] | 338 | null, |
Pier Luigi | 2b1ad87 | 2017-01-31 09:35:42 -0800 | [diff] [blame] | 339 | Ip6Address.valueOf( |
| 340 | getSolicitNodeAddress(getLinkLocalAddress(intf.mac().toBytes())) |
| 341 | ).toIpPrefix(), |
Vinayak Tejankar | 3a409c6 | 2017-01-12 02:20:53 +0530 | [diff] [blame] | 342 | NEIGHBOR_SOLICITATION, |
| 343 | null |
| 344 | ); |
| 345 | fwdToSend.add(buildForwardingObjective(selector, treatment, cpNextId, install, ACL_PRIORITY + 1)); |
| 346 | // Neighbour solicitation traffic from the router. |
Pier Luigi | 2b1ad87 | 2017-01-31 09:35:42 -0800 | [diff] [blame] | 347 | // This flow is for the global unicast address. |
Vinayak Tejankar | 3a409c6 | 2017-01-12 02:20:53 +0530 | [diff] [blame] | 348 | selector = buildNdpSelector( |
| 349 | controlPlanePort, |
| 350 | intf.vlan(), |
| 351 | ip.ipAddress().toIpPrefix(), |
Pier Luigi | 9f765dc | 2017-02-03 13:35:23 -0800 | [diff] [blame] | 352 | null, |
Vinayak Tejankar | 3a409c6 | 2017-01-12 02:20:53 +0530 | [diff] [blame] | 353 | NEIGHBOR_SOLICITATION, |
| 354 | intf.mac() |
| 355 | ); |
| 356 | fwdToSend.add(buildForwardingObjective(selector, treatment, intfNextId, install, ACL_PRIORITY + 1)); |
Pier Luigi | 2b1ad87 | 2017-01-31 09:35:42 -0800 | [diff] [blame] | 357 | // Neighbour solicitation traffic from the router. |
| 358 | // This flow is for the link local address. |
| 359 | selector = buildNdpSelector( |
| 360 | controlPlanePort, |
| 361 | intf.vlan(), |
Pier Luigi | 2b1ad87 | 2017-01-31 09:35:42 -0800 | [diff] [blame] | 362 | Ip6Address.valueOf(getLinkLocalAddress(intf.mac().toBytes())).toIpPrefix(), |
Pier Luigi | 9f765dc | 2017-02-03 13:35:23 -0800 | [diff] [blame] | 363 | null, |
Pier Luigi | 2b1ad87 | 2017-01-31 09:35:42 -0800 | [diff] [blame] | 364 | NEIGHBOR_SOLICITATION, |
| 365 | intf.mac() |
| 366 | ); |
| 367 | fwdToSend.add(buildForwardingObjective(selector, treatment, intfNextId, install, ACL_PRIORITY + 1)); |
| 368 | // Neighbour advertisement traffic towards the router. |
| 369 | // This flow is for the global unicast address |
Vinayak Tejankar | 3a409c6 | 2017-01-12 02:20:53 +0530 | [diff] [blame] | 370 | selector = buildNdpSelector( |
| 371 | intf.connectPoint().port(), |
| 372 | intf.vlan(), |
Pier Luigi | 2b1ad87 | 2017-01-31 09:35:42 -0800 | [diff] [blame] | 373 | null, |
Pier Luigi | 9f765dc | 2017-02-03 13:35:23 -0800 | [diff] [blame] | 374 | ip.ipAddress().toIpPrefix(), |
Pier Luigi | 2b1ad87 | 2017-01-31 09:35:42 -0800 | [diff] [blame] | 375 | NEIGHBOR_ADVERTISEMENT, |
| 376 | null |
| 377 | ); |
| 378 | fwdToSend.add(buildForwardingObjective(selector, treatment, cpNextId, install, ACL_PRIORITY + 1)); |
| 379 | // Neighbour advertisement traffic towards the router. |
| 380 | // This flow is for the link local address |
| 381 | selector = buildNdpSelector( |
| 382 | intf.connectPoint().port(), |
| 383 | intf.vlan(), |
Vinayak Tejankar | 3a409c6 | 2017-01-12 02:20:53 +0530 | [diff] [blame] | 384 | null, |
Pier Luigi | 9f765dc | 2017-02-03 13:35:23 -0800 | [diff] [blame] | 385 | Ip6Address.valueOf(getLinkLocalAddress(intf.mac().toBytes())).toIpPrefix(), |
Vinayak Tejankar | 3a409c6 | 2017-01-12 02:20:53 +0530 | [diff] [blame] | 386 | NEIGHBOR_ADVERTISEMENT, |
| 387 | null |
| 388 | ); |
| 389 | fwdToSend.add(buildForwardingObjective(selector, treatment, cpNextId, install, ACL_PRIORITY + 1)); |
| 390 | // Neighbour advertisement traffic from the router. |
Pier Luigi | 2b1ad87 | 2017-01-31 09:35:42 -0800 | [diff] [blame] | 391 | // This flow is for the global unicast address |
Vinayak Tejankar | 3a409c6 | 2017-01-12 02:20:53 +0530 | [diff] [blame] | 392 | selector = buildNdpSelector( |
| 393 | controlPlanePort, |
| 394 | intf.vlan(), |
| 395 | ip.ipAddress().toIpPrefix(), |
Pier Luigi | 9f765dc | 2017-02-03 13:35:23 -0800 | [diff] [blame] | 396 | null, |
Vinayak Tejankar | 3a409c6 | 2017-01-12 02:20:53 +0530 | [diff] [blame] | 397 | NEIGHBOR_ADVERTISEMENT, |
| 398 | intf.mac() |
| 399 | ); |
| 400 | fwdToSend.add(buildForwardingObjective(selector, treatment, intfNextId, install, ACL_PRIORITY + 1)); |
Pier Luigi | 2b1ad87 | 2017-01-31 09:35:42 -0800 | [diff] [blame] | 401 | // Neighbour advertisement traffic from the router. |
| 402 | // This flow is for the link local address |
| 403 | selector = buildNdpSelector( |
| 404 | controlPlanePort, |
| 405 | intf.vlan(), |
Pier Luigi | 2b1ad87 | 2017-01-31 09:35:42 -0800 | [diff] [blame] | 406 | Ip6Address.valueOf(getLinkLocalAddress(intf.mac().toBytes())).toIpPrefix(), |
Pier Luigi | 9f765dc | 2017-02-03 13:35:23 -0800 | [diff] [blame] | 407 | null, |
Pier Luigi | 2b1ad87 | 2017-01-31 09:35:42 -0800 | [diff] [blame] | 408 | NEIGHBOR_ADVERTISEMENT, |
| 409 | intf.mac() |
| 410 | ); |
| 411 | fwdToSend.add(buildForwardingObjective(selector, treatment, intfNextId, install, ACL_PRIORITY + 1)); |
Vinayak Tejankar | 3a409c6 | 2017-01-12 02:20:53 +0530 | [diff] [blame] | 412 | } |
| 413 | // Finally we push the fwd objectives through the flow objective service. |
| 414 | fwdToSend.stream().forEach(forwardingObjective -> |
| 415 | flowObjectiveService.forward(deviceId, forwardingObjective) |
| 416 | ); |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 417 | } |
| 418 | } |
| 419 | |
kishore | 71a2753 | 2016-03-16 20:23:49 +0530 | [diff] [blame] | 420 | /** |
Jonathan Hart | f04b7d9 | 2016-03-29 09:39:11 -0700 | [diff] [blame] | 421 | * Installs or removes OSPF forwarding rules. |
kishore | 71a2753 | 2016-03-16 20:23:49 +0530 | [diff] [blame] | 422 | * |
Jonathan Hart | 249b4cf | 2017-02-03 18:02:58 -0800 | [diff] [blame] | 423 | * @param request provisioning request containing router and interface |
kishore | 71a2753 | 2016-03-16 20:23:49 +0530 | [diff] [blame] | 424 | * @param install true to create an add objective, false to create a remove |
| 425 | * objective |
Jonathan Hart | 7fc5a72 | 2017-01-26 15:54:12 -0800 | [diff] [blame] | 426 | */ |
Jonathan Hart | 249b4cf | 2017-02-03 18:02:58 -0800 | [diff] [blame] | 427 | private void updateOspfForwarding(InterfaceProvisionRequest request, boolean install) { |
Jonathan Hart | 7fc5a72 | 2017-01-26 15:54:12 -0800 | [diff] [blame] | 428 | // TODO IPv6 support has not been implemented yet |
Jonathan Hart | 249b4cf | 2017-02-03 18:02:58 -0800 | [diff] [blame] | 429 | Interface intf = request.intf(); |
Jonathan Hart | 7fc5a72 | 2017-01-26 15:54:12 -0800 | [diff] [blame] | 430 | log.debug("{} OSPF flows for {}", operation(install), intf); |
| 431 | |
Jonathan Hart | ea49238 | 2016-01-13 09:33:13 -0800 | [diff] [blame] | 432 | // OSPF to router |
| 433 | TrafficSelector toSelector = DefaultTrafficSelector.builder() |
| 434 | .matchInPort(intf.connectPoint().port()) |
| 435 | .matchEthType(EthType.EtherType.IPV4.ethType().toShort()) |
| 436 | .matchVlanId(intf.vlan()) |
| 437 | .matchIPProtocol((byte) OSPF_IP_PROTO) |
| 438 | .build(); |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 439 | |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 440 | // create nextObjectives for forwarding to the controlPlaneConnectPoint |
Jonathan Hart | f8cd052 | 2016-10-25 07:09:55 -0700 | [diff] [blame] | 441 | DeviceId deviceId = intf.connectPoint().deviceId(); |
Jonathan Hart | 249b4cf | 2017-02-03 18:02:58 -0800 | [diff] [blame] | 442 | PortNumber controlPlanePort = request.controlPlaneConnectPoint().port(); |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 443 | int cpNextId; |
| 444 | if (intf.vlan() == VlanId.NONE) { |
kishore | 71a2753 | 2016-03-16 20:23:49 +0530 | [diff] [blame] | 445 | cpNextId = modifyNextObjective(deviceId, controlPlanePort, |
Jonathan Hart | f4bd048 | 2017-01-27 15:11:18 -0800 | [diff] [blame] | 446 | VlanId.vlanId(ASSIGNED_VLAN), |
kishore | 71a2753 | 2016-03-16 20:23:49 +0530 | [diff] [blame] | 447 | true, install); |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 448 | } else { |
kishore | 71a2753 | 2016-03-16 20:23:49 +0530 | [diff] [blame] | 449 | cpNextId = modifyNextObjective(deviceId, controlPlanePort, |
| 450 | intf.vlan(), false, install); |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 451 | } |
Jonathan Hart | f8cd052 | 2016-10-25 07:09:55 -0700 | [diff] [blame] | 452 | flowObjectiveService.forward(intf.connectPoint().deviceId(), |
Jonathan Hart | 7fc5a72 | 2017-01-26 15:54:12 -0800 | [diff] [blame] | 453 | buildForwardingObjective(toSelector, null, cpNextId, |
Jonathan Hart | 249b4cf | 2017-02-03 18:02:58 -0800 | [diff] [blame] | 454 | install ? request.info().ospfEnabled() : install, ACL_PRIORITY)); |
Jonathan Hart | ea49238 | 2016-01-13 09:33:13 -0800 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | /** |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 458 | * Creates a next objective for forwarding to a port. Handles metadata for |
| 459 | * some pipelines that require vlan information for egress port. |
| 460 | * |
| 461 | * @param deviceId the device on which the next objective is being created |
| 462 | * @param portNumber the egress port |
| 463 | * @param vlanId vlan information for egress port |
| 464 | * @param popVlan if vlan tag should be popped or not |
kishore | 71a2753 | 2016-03-16 20:23:49 +0530 | [diff] [blame] | 465 | * @param install true to create an add next objective, false to create a remove |
| 466 | * next objective |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 467 | * @return nextId of the next objective created |
| 468 | */ |
kishore | 71a2753 | 2016-03-16 20:23:49 +0530 | [diff] [blame] | 469 | private int modifyNextObjective(DeviceId deviceId, PortNumber portNumber, |
| 470 | VlanId vlanId, boolean popVlan, boolean install) { |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 471 | int nextId = flowObjectiveService.allocateNextId(); |
| 472 | NextObjective.Builder nextObjBuilder = DefaultNextObjective |
| 473 | .builder().withId(nextId) |
| 474 | .withType(NextObjective.Type.SIMPLE) |
| 475 | .fromApp(appId); |
| 476 | |
| 477 | TrafficTreatment.Builder ttBuilder = DefaultTrafficTreatment.builder(); |
| 478 | if (popVlan) { |
| 479 | ttBuilder.popVlan(); |
| 480 | } |
| 481 | ttBuilder.setOutput(portNumber); |
| 482 | |
| 483 | // setup metadata to pass to nextObjective - indicate the vlan on egress |
| 484 | // if needed by the switch pipeline. |
| 485 | TrafficSelector.Builder metabuilder = DefaultTrafficSelector.builder(); |
| 486 | metabuilder.matchVlanId(vlanId); |
| 487 | |
| 488 | nextObjBuilder.withMeta(metabuilder.build()); |
| 489 | nextObjBuilder.addTreatment(ttBuilder.build()); |
Jonathan Hart | f04b7d9 | 2016-03-29 09:39:11 -0700 | [diff] [blame] | 490 | log.debug("Submitted next objective {} in device {} for port/vlan {}/{}", |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 491 | nextId, deviceId, portNumber, vlanId); |
kishore | 71a2753 | 2016-03-16 20:23:49 +0530 | [diff] [blame] | 492 | if (install) { |
| 493 | flowObjectiveService.next(deviceId, nextObjBuilder.add()); |
| 494 | } else { |
| 495 | flowObjectiveService.next(deviceId, nextObjBuilder.remove()); |
| 496 | } |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 497 | return nextId; |
| 498 | } |
Jonathan Hart | f8035d3 | 2016-06-16 16:23:26 -0700 | [diff] [blame] | 499 | |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 500 | /** |
| 501 | * Builds a forwarding objective from the given selector, treatment and nextId. |
Jonathan Hart | ea49238 | 2016-01-13 09:33:13 -0800 | [diff] [blame] | 502 | * |
| 503 | * @param selector selector |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 504 | * @param treatment treatment to apply to packet, can be null |
| 505 | * @param nextId next objective to point to for forwarding packet |
Jonathan Hart | ea49238 | 2016-01-13 09:33:13 -0800 | [diff] [blame] | 506 | * @param add true to create an add objective, false to create a remove |
| 507 | * objective |
| 508 | * @return forwarding objective |
| 509 | */ |
| 510 | private ForwardingObjective buildForwardingObjective(TrafficSelector selector, |
| 511 | TrafficTreatment treatment, |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 512 | int nextId, |
Vinayak Tejankar | 3a409c6 | 2017-01-12 02:20:53 +0530 | [diff] [blame] | 513 | boolean add, |
| 514 | int priority) { |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 515 | DefaultForwardingObjective.Builder fobBuilder = DefaultForwardingObjective.builder(); |
| 516 | fobBuilder.withSelector(selector); |
| 517 | if (treatment != null) { |
| 518 | fobBuilder.withTreatment(treatment); |
| 519 | } |
| 520 | if (nextId != -1) { |
| 521 | fobBuilder.nextStep(nextId); |
| 522 | } |
| 523 | fobBuilder.fromApp(appId) |
Vinayak Tejankar | 3a409c6 | 2017-01-12 02:20:53 +0530 | [diff] [blame] | 524 | .withPriority(priority) |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 525 | .withFlag(ForwardingObjective.Flag.VERSATILE); |
Jonathan Hart | ea49238 | 2016-01-13 09:33:13 -0800 | [diff] [blame] | 526 | |
| 527 | return add ? fobBuilder.add() : fobBuilder.remove(); |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 528 | } |
| 529 | |
Jonathan Hart | 7fc5a72 | 2017-01-26 15:54:12 -0800 | [diff] [blame] | 530 | static TrafficSelector.Builder buildBaseSelectorBuilder(PortNumber inPort, |
| 531 | MacAddress srcMac, |
| 532 | MacAddress dstMac, |
| 533 | VlanId vlanId) { |
| 534 | TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder(); |
| 535 | if (inPort != null) { |
| 536 | selectorBuilder.matchInPort(inPort); |
| 537 | } |
| 538 | if (srcMac != null) { |
| 539 | selectorBuilder.matchEthSrc(srcMac); |
| 540 | } |
| 541 | if (dstMac != null) { |
| 542 | selectorBuilder.matchEthDst(dstMac); |
| 543 | } |
| 544 | if (vlanId != null) { |
| 545 | selectorBuilder.matchVlanId(vlanId); |
| 546 | } |
| 547 | return selectorBuilder; |
| 548 | } |
| 549 | |
| 550 | static TrafficSelector buildIPDstSelector(IpPrefix dstIp, |
| 551 | PortNumber inPort, |
| 552 | MacAddress srcMac, |
| 553 | MacAddress dstMac, |
| 554 | VlanId vlanId) { |
| 555 | TrafficSelector.Builder selector = buildBaseSelectorBuilder(inPort, srcMac, dstMac, vlanId); |
| 556 | if (dstIp.isIp4()) { |
| 557 | selector.matchEthType(TYPE_IPV4); |
| 558 | selector.matchIPDst(dstIp); |
| 559 | } else { |
| 560 | selector.matchEthType(TYPE_IPV6); |
| 561 | selector.matchIPv6Dst(dstIp); |
| 562 | } |
| 563 | return selector.build(); |
| 564 | } |
| 565 | |
| 566 | static TrafficSelector buildIPSrcSelector(IpPrefix srcIp, |
| 567 | PortNumber inPort, |
| 568 | MacAddress srcMac, |
| 569 | MacAddress dstMac, |
| 570 | VlanId vlanId) { |
| 571 | TrafficSelector.Builder selector = buildBaseSelectorBuilder(inPort, srcMac, dstMac, vlanId); |
| 572 | if (srcIp.isIp4()) { |
| 573 | selector.matchEthType(TYPE_IPV4); |
| 574 | selector.matchIPSrc(srcIp); |
| 575 | } else { |
| 576 | selector.matchEthType(TYPE_IPV6); |
| 577 | selector.matchIPv6Src(srcIp); |
| 578 | } |
| 579 | return selector.build(); |
| 580 | } |
| 581 | |
| 582 | static TrafficSelector buildArpSelector(PortNumber inPort, |
| 583 | VlanId vlanId, |
| 584 | Ip4Address arpSpa, |
| 585 | MacAddress srcMac) { |
| 586 | TrafficSelector.Builder selector = buildBaseSelectorBuilder(inPort, null, null, vlanId); |
| 587 | selector.matchEthType(TYPE_ARP); |
| 588 | if (arpSpa != null) { |
| 589 | selector.matchArpSpa(arpSpa); |
| 590 | } |
| 591 | if (srcMac != null) { |
| 592 | selector.matchEthSrc(srcMac); |
| 593 | } |
| 594 | return selector.build(); |
| 595 | } |
| 596 | |
| 597 | static TrafficSelector buildNdpSelector(PortNumber inPort, |
| 598 | VlanId vlanId, |
| 599 | IpPrefix srcIp, |
Pier Luigi | 2b1ad87 | 2017-01-31 09:35:42 -0800 | [diff] [blame] | 600 | IpPrefix dstIp, |
Jonathan Hart | 7fc5a72 | 2017-01-26 15:54:12 -0800 | [diff] [blame] | 601 | byte subProto, |
| 602 | MacAddress srcMac) { |
| 603 | TrafficSelector.Builder selector = buildBaseSelectorBuilder(inPort, null, null, vlanId); |
| 604 | selector.matchEthType(TYPE_IPV6) |
| 605 | .matchIPProtocol(PROTOCOL_ICMP6) |
| 606 | .matchIcmpv6Type(subProto); |
| 607 | if (srcIp != null) { |
| 608 | selector.matchIPv6Src(srcIp); |
| 609 | } |
Pier Luigi | 2b1ad87 | 2017-01-31 09:35:42 -0800 | [diff] [blame] | 610 | if (dstIp != null) { |
Pier Luigi | 9f765dc | 2017-02-03 13:35:23 -0800 | [diff] [blame] | 611 | selector.matchIPv6Dst(dstIp); |
Pier Luigi | 2b1ad87 | 2017-01-31 09:35:42 -0800 | [diff] [blame] | 612 | } |
Jonathan Hart | 7fc5a72 | 2017-01-26 15:54:12 -0800 | [diff] [blame] | 613 | if (srcMac != null) { |
| 614 | selector.matchEthSrc(srcMac); |
| 615 | } |
| 616 | return selector.build(); |
| 617 | } |
| 618 | |
| 619 | private int getPriorityFromPrefix(IpPrefix prefix) { |
| 620 | return (prefix.isIp4()) ? |
| 621 | IPV4_PRIORITY * prefix.prefixLength() + MIN_IP_PRIORITY : |
| 622 | IPV6_PRIORITY * prefix.prefixLength() + MIN_IP_PRIORITY; |
| 623 | } |
| 624 | |
| 625 | private String operation(boolean install) { |
| 626 | return install ? "Installing" : "Removing"; |
| 627 | } |
| 628 | |
| 629 | |
Jonathan Hart | ea49238 | 2016-01-13 09:33:13 -0800 | [diff] [blame] | 630 | /** |
Jonathan Hart | ea49238 | 2016-01-13 09:33:13 -0800 | [diff] [blame] | 631 | * Listener for network config events. |
| 632 | */ |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 633 | private class InternalNetworkConfigListener implements NetworkConfigListener { |
kishore | 71a2753 | 2016-03-16 20:23:49 +0530 | [diff] [blame] | 634 | |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 635 | @Override |
| 636 | public void event(NetworkConfigEvent event) { |
Jonathan Hart | 249b4cf | 2017-02-03 18:02:58 -0800 | [diff] [blame] | 637 | if (event.configClass().equals(RoutingService.ROUTER_CONFIG_CLASS) || |
| 638 | event.configClass().equals(RoutersConfig.class)) { |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 639 | switch (event.type()) { |
Jonathan Hart | f8cd052 | 2016-10-25 07:09:55 -0700 | [diff] [blame] | 640 | case CONFIG_ADDED: |
| 641 | case CONFIG_UPDATED: |
Jonathan Hart | 249b4cf | 2017-02-03 18:02:58 -0800 | [diff] [blame] | 642 | case CONFIG_REMOVED: |
Jonathan Hart | 7fc5a72 | 2017-01-26 15:54:12 -0800 | [diff] [blame] | 643 | processRouterConfig(); |
Jonathan Hart | f8cd052 | 2016-10-25 07:09:55 -0700 | [diff] [blame] | 644 | break; |
| 645 | case CONFIG_REGISTERED: |
Jonathan Hart | f8cd052 | 2016-10-25 07:09:55 -0700 | [diff] [blame] | 646 | case CONFIG_UNREGISTERED: |
| 647 | break; |
Vinayak Tejankar | 3a409c6 | 2017-01-12 02:20:53 +0530 | [diff] [blame] | 648 | default: |
| 649 | break; |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 650 | } |
| 651 | } |
| 652 | } |
| 653 | } |
Charles Chan | d0fd5dc | 2016-02-16 23:14:49 -0800 | [diff] [blame] | 654 | |
| 655 | /** |
| 656 | * Listener for host events. |
| 657 | */ |
| 658 | private class InternalHostListener implements HostListener { |
kishore | 71a2753 | 2016-03-16 20:23:49 +0530 | [diff] [blame] | 659 | |
Jonathan Hart | f8cd052 | 2016-10-25 07:09:55 -0700 | [diff] [blame] | 660 | private Optional<Interface> getPeerInterface(Host peer) { |
Jonathan Hart | 249b4cf | 2017-02-03 18:02:58 -0800 | [diff] [blame] | 661 | Router router = routers.get(peer.location().deviceId()); |
| 662 | |
Jonathan Hart | f8cd052 | 2016-10-25 07:09:55 -0700 | [diff] [blame] | 663 | return interfaceService.getInterfacesByPort(peer.location()).stream() |
Jonathan Hart | 249b4cf | 2017-02-03 18:02:58 -0800 | [diff] [blame] | 664 | .filter(intf -> router.info().interfaces().isEmpty() |
| 665 | || router.info().interfaces().contains(intf.name())) |
Charles Chan | d0fd5dc | 2016-02-16 23:14:49 -0800 | [diff] [blame] | 666 | .filter(intf -> peer.vlan().equals(intf.vlan())) |
| 667 | .findFirst(); |
Jonathan Hart | f8cd052 | 2016-10-25 07:09:55 -0700 | [diff] [blame] | 668 | } |
| 669 | |
| 670 | private void peerAdded(HostEvent event) { |
| 671 | Host peer = event.subject(); |
Jonathan Hart | 249b4cf | 2017-02-03 18:02:58 -0800 | [diff] [blame] | 672 | Router routerInfo = routers.get(peer.location().deviceId()); |
| 673 | if (routerInfo == null) { |
Jonathan Hart | f8cd052 | 2016-10-25 07:09:55 -0700 | [diff] [blame] | 674 | return; |
| 675 | } |
| 676 | |
| 677 | Optional<Interface> peerIntf = getPeerInterface(peer); |
Charles Chan | d0fd5dc | 2016-02-16 23:14:49 -0800 | [diff] [blame] | 678 | if (!peerIntf.isPresent()) { |
| 679 | log.debug("Adding peer {}/{} on {} but the interface is not configured", |
| 680 | peer.mac(), peer.vlan(), peer.location()); |
| 681 | return; |
| 682 | } |
| 683 | |
Pier Luigi | 9f765dc | 2017-02-03 13:35:23 -0800 | [diff] [blame] | 684 | // Generate L3 Unicast group for the traffic towards vRouter |
| 685 | // XXX This approach will change with the HA design |
Charles Chan | d0fd5dc | 2016-02-16 23:14:49 -0800 | [diff] [blame] | 686 | int toRouterL3Unicast = createPeerGroup(peer.mac(), peerIntf.get().mac(), |
Jonathan Hart | 249b4cf | 2017-02-03 18:02:58 -0800 | [diff] [blame] | 687 | peer.vlan(), peer.location().deviceId(), routerInfo.info().controlPlaneConnectPoint().port()); |
Pier Luigi | 9f765dc | 2017-02-03 13:35:23 -0800 | [diff] [blame] | 688 | // Generate L3 Unicast group for the traffic towards the upStream |
| 689 | // XXX This approach will change with the HA design |
Charles Chan | d0fd5dc | 2016-02-16 23:14:49 -0800 | [diff] [blame] | 690 | int toPeerL3Unicast = createPeerGroup(peerIntf.get().mac(), peer.mac(), |
| 691 | peer.vlan(), peer.location().deviceId(), peer.location().port()); |
Pier Luigi | 9f765dc | 2017-02-03 13:35:23 -0800 | [diff] [blame] | 692 | // Store the next objectives in the map |
Charles Chan | d0fd5dc | 2016-02-16 23:14:49 -0800 | [diff] [blame] | 693 | peerNextId.put(peer, ImmutableSortedSet.of(toRouterL3Unicast, toPeerL3Unicast)); |
| 694 | |
| 695 | // From peer to router |
| 696 | peerIntf.get().ipAddresses().forEach(routerIp -> { |
| 697 | flowObjectiveService.forward(peer.location().deviceId(), |
| 698 | createPeerObjBuilder(toRouterL3Unicast, routerIp.ipAddress().toIpPrefix()).add()); |
| 699 | }); |
| 700 | |
| 701 | // From router to peer |
| 702 | peer.ipAddresses().forEach(peerIp -> { |
| 703 | flowObjectiveService.forward(peer.location().deviceId(), |
| 704 | createPeerObjBuilder(toPeerL3Unicast, peerIp.toIpPrefix()).add()); |
| 705 | }); |
| 706 | } |
| 707 | |
| 708 | private void peerRemoved(HostEvent event) { |
| 709 | Host peer = event.subject(); |
Jonathan Hart | 249b4cf | 2017-02-03 18:02:58 -0800 | [diff] [blame] | 710 | if (routers.get(peer.location().deviceId()) == null) { |
| 711 | return; |
| 712 | } |
Jonathan Hart | f8cd052 | 2016-10-25 07:09:55 -0700 | [diff] [blame] | 713 | Optional<Interface> peerIntf = getPeerInterface(peer); |
Charles Chan | d0fd5dc | 2016-02-16 23:14:49 -0800 | [diff] [blame] | 714 | if (!peerIntf.isPresent()) { |
| 715 | log.debug("Removing peer {}/{} on {} but the interface is not configured", |
| 716 | peer.mac(), peer.vlan(), peer.location()); |
| 717 | return; |
| 718 | } |
| 719 | |
Charles Chan | d0fd5dc | 2016-02-16 23:14:49 -0800 | [diff] [blame] | 720 | checkState(peerNextId.get(peer) != null, |
| 721 | "Peer nextId should not be null"); |
| 722 | checkState(peerNextId.get(peer).size() == 2, |
| 723 | "Wrong nextId associated with the peer"); |
| 724 | Iterator<Integer> iter = peerNextId.get(peer).iterator(); |
| 725 | int toRouterL3Unicast = iter.next(); |
| 726 | int toPeerL3Unicast = iter.next(); |
| 727 | |
| 728 | // From peer to router |
| 729 | peerIntf.get().ipAddresses().forEach(routerIp -> { |
| 730 | flowObjectiveService.forward(peer.location().deviceId(), |
| 731 | createPeerObjBuilder(toRouterL3Unicast, routerIp.ipAddress().toIpPrefix()).remove()); |
| 732 | }); |
| 733 | |
| 734 | // From router to peer |
| 735 | peer.ipAddresses().forEach(peerIp -> { |
| 736 | flowObjectiveService.forward(peer.location().deviceId(), |
| 737 | createPeerObjBuilder(toPeerL3Unicast, peerIp.toIpPrefix()).remove()); |
| 738 | }); |
| 739 | } |
| 740 | |
| 741 | private ForwardingObjective.Builder createPeerObjBuilder( |
| 742 | int nextId, IpPrefix ipAddresses) { |
Vinayak Tejankar | 3a409c6 | 2017-01-12 02:20:53 +0530 | [diff] [blame] | 743 | TrafficSelector selector = buildIPDstSelector(ipAddresses, null, null, null, null); |
Charles Chan | d0fd5dc | 2016-02-16 23:14:49 -0800 | [diff] [blame] | 744 | DefaultForwardingObjective.Builder builder = |
| 745 | DefaultForwardingObjective.builder() |
Vinayak Tejankar | 3a409c6 | 2017-01-12 02:20:53 +0530 | [diff] [blame] | 746 | .withSelector(selector) |
Charles Chan | d0fd5dc | 2016-02-16 23:14:49 -0800 | [diff] [blame] | 747 | .fromApp(appId) |
| 748 | .withPriority(getPriorityFromPrefix(ipAddresses)) |
| 749 | .withFlag(ForwardingObjective.Flag.SPECIFIC); |
| 750 | if (nextId != -1) { |
| 751 | builder.nextStep(nextId); |
| 752 | } |
| 753 | return builder; |
| 754 | } |
| 755 | |
| 756 | private int createPeerGroup(MacAddress srcMac, MacAddress dstMac, |
| 757 | VlanId vlanId, DeviceId deviceId, PortNumber port) { |
| 758 | int nextId = flowObjectiveService.allocateNextId(); |
| 759 | NextObjective.Builder nextObjBuilder = DefaultNextObjective.builder() |
| 760 | .withId(nextId) |
| 761 | .withType(NextObjective.Type.SIMPLE) |
| 762 | .fromApp(appId); |
| 763 | |
| 764 | TrafficTreatment.Builder ttBuilder = DefaultTrafficTreatment.builder(); |
| 765 | ttBuilder.setEthSrc(srcMac); |
| 766 | ttBuilder.setEthDst(dstMac); |
| 767 | ttBuilder.setOutput(port); |
| 768 | nextObjBuilder.addTreatment(ttBuilder.build()); |
| 769 | |
| 770 | TrafficSelector.Builder metabuilder = DefaultTrafficSelector.builder(); |
| 771 | VlanId matchVlanId = (vlanId.equals(VlanId.NONE)) ? |
Jonathan Hart | f4bd048 | 2017-01-27 15:11:18 -0800 | [diff] [blame] | 772 | VlanId.vlanId(ASSIGNED_VLAN) : |
Charles Chan | d0fd5dc | 2016-02-16 23:14:49 -0800 | [diff] [blame] | 773 | vlanId; |
| 774 | metabuilder.matchVlanId(matchVlanId); |
| 775 | nextObjBuilder.withMeta(metabuilder.build()); |
| 776 | |
| 777 | flowObjectiveService.next(deviceId, nextObjBuilder.add()); |
| 778 | return nextId; |
| 779 | } |
| 780 | |
| 781 | @Override |
| 782 | public void event(HostEvent event) { |
| 783 | DeviceId deviceId = event.subject().location().deviceId(); |
| 784 | if (!mastershipService.isLocalMaster(deviceId)) { |
| 785 | return; |
| 786 | } |
| 787 | switch (event.type()) { |
| 788 | case HOST_ADDED: |
| 789 | peerAdded(event); |
| 790 | break; |
| 791 | case HOST_MOVED: |
| 792 | //TODO We assume BGP peer does not move for now |
| 793 | break; |
| 794 | case HOST_REMOVED: |
| 795 | peerRemoved(event); |
| 796 | break; |
| 797 | case HOST_UPDATED: |
Vinayak Tejankar | 3a409c6 | 2017-01-12 02:20:53 +0530 | [diff] [blame] | 798 | //FIXME We assume BGP peer does not change IP for now |
| 799 | // but we can discover new address. |
Charles Chan | d0fd5dc | 2016-02-16 23:14:49 -0800 | [diff] [blame] | 800 | break; |
| 801 | default: |
| 802 | break; |
| 803 | } |
| 804 | } |
| 805 | } |
| 806 | |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 807 | } |
Vinayak Tejankar | 3a409c6 | 2017-01-12 02:20:53 +0530 | [diff] [blame] | 808 | |