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