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 | |
Vinayak Tejankar | 3a409c6 | 2017-01-12 02:20:53 +0530 | [diff] [blame] | 19 | import com.google.common.collect.Lists; |
Jonathan Hart | f04b7d9 | 2016-03-29 09:39:11 -0700 | [diff] [blame] | 20 | import com.google.common.collect.Maps; |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 21 | import org.apache.felix.scr.annotations.Activate; |
| 22 | import org.apache.felix.scr.annotations.Component; |
| 23 | import org.apache.felix.scr.annotations.Deactivate; |
Charles Chan | c6d227e | 2017-02-28 15:15:17 -0800 | [diff] [blame] | 24 | import org.apache.felix.scr.annotations.Modified; |
| 25 | import org.apache.felix.scr.annotations.Property; |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 26 | import org.apache.felix.scr.annotations.Reference; |
| 27 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 28 | import org.onlab.packet.EthType; |
Vinayak Tejankar | 3a409c6 | 2017-01-12 02:20:53 +0530 | [diff] [blame] | 29 | import org.onlab.packet.Ip4Address; |
Pier Luigi | 2b1ad87 | 2017-01-31 09:35:42 -0800 | [diff] [blame] | 30 | import org.onlab.packet.Ip6Address; |
Charles Chan | d0fd5dc | 2016-02-16 23:14:49 -0800 | [diff] [blame] | 31 | import org.onlab.packet.IpPrefix; |
| 32 | import org.onlab.packet.MacAddress; |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 33 | import org.onlab.packet.VlanId; |
Charles Chan | c6d227e | 2017-02-28 15:15:17 -0800 | [diff] [blame] | 34 | import org.onlab.util.Tools; |
kishore | 786b7e4 | 2016-05-19 16:25:57 +0530 | [diff] [blame] | 35 | import org.onosproject.app.ApplicationService; |
Charles Chan | c6d227e | 2017-02-28 15:15:17 -0800 | [diff] [blame] | 36 | import org.onosproject.cfg.ComponentConfigService; |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 37 | import org.onosproject.core.ApplicationId; |
| 38 | import org.onosproject.core.CoreService; |
| 39 | import org.onosproject.incubator.net.intf.Interface; |
| 40 | import org.onosproject.incubator.net.intf.InterfaceService; |
Charles Chan | d0fd5dc | 2016-02-16 23:14:49 -0800 | [diff] [blame] | 41 | import org.onosproject.mastership.MastershipService; |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 42 | import org.onosproject.net.DeviceId; |
Charles Chan | d0fd5dc | 2016-02-16 23:14:49 -0800 | [diff] [blame] | 43 | import org.onosproject.net.Host; |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 44 | import org.onosproject.net.PortNumber; |
| 45 | import org.onosproject.net.config.NetworkConfigEvent; |
| 46 | import org.onosproject.net.config.NetworkConfigListener; |
| 47 | import org.onosproject.net.config.NetworkConfigService; |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 48 | import org.onosproject.net.device.DeviceService; |
| 49 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 50 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 51 | import org.onosproject.net.flow.TrafficSelector; |
| 52 | import org.onosproject.net.flow.TrafficTreatment; |
| 53 | import org.onosproject.net.flowobjective.DefaultForwardingObjective; |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 54 | import org.onosproject.net.flowobjective.DefaultNextObjective; |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 55 | import org.onosproject.net.flowobjective.FlowObjectiveService; |
| 56 | import org.onosproject.net.flowobjective.ForwardingObjective; |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 57 | import org.onosproject.net.flowobjective.NextObjective; |
Charles Chan | d0fd5dc | 2016-02-16 23:14:49 -0800 | [diff] [blame] | 58 | import org.onosproject.net.host.HostService; |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 59 | import org.onosproject.net.host.InterfaceIpAddress; |
Jonathan Hart | 249b4cf | 2017-02-03 18:02:58 -0800 | [diff] [blame] | 60 | import org.onosproject.routing.RouterInfo; |
| 61 | import org.onosproject.routing.InterfaceProvisionRequest; |
| 62 | import org.onosproject.routing.Router; |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 63 | import org.onosproject.routing.RoutingService; |
Jonathan Hart | 249b4cf | 2017-02-03 18:02:58 -0800 | [diff] [blame] | 64 | import org.onosproject.routing.config.RouterConfigHelper; |
| 65 | import org.onosproject.routing.config.RoutersConfig; |
| 66 | import org.onosproject.routing.config.RoutingConfigurationService; |
Charles Chan | c6d227e | 2017-02-28 15:15:17 -0800 | [diff] [blame] | 67 | import org.osgi.service.component.ComponentContext; |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 68 | import org.slf4j.Logger; |
| 69 | |
Charles Chan | c6d227e | 2017-02-28 15:15:17 -0800 | [diff] [blame] | 70 | import java.util.Dictionary; |
Jonathan Hart | f04b7d9 | 2016-03-29 09:39:11 -0700 | [diff] [blame] | 71 | import java.util.List; |
| 72 | import java.util.Map; |
Jonathan Hart | f04b7d9 | 2016-03-29 09:39:11 -0700 | [diff] [blame] | 73 | import java.util.Set; |
Jonathan Hart | 249b4cf | 2017-02-03 18:02:58 -0800 | [diff] [blame] | 74 | import java.util.concurrent.ConcurrentHashMap; |
Jonathan Hart | f04b7d9 | 2016-03-29 09:39:11 -0700 | [diff] [blame] | 75 | |
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 | |
Charles Chan | c6d227e | 2017-02-28 15:15:17 -0800 | [diff] [blame] | 130 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 131 | protected ComponentConfigService cfgService; |
| 132 | |
| 133 | @Property(name = "forceUnprovision", boolValue = false, |
| 134 | label = "Force unprovision when the device goes offline") |
| 135 | private boolean forceUnprovision = false; |
| 136 | |
Jonathan Hart | e732704 | 2017-02-02 13:11:25 -0800 | [diff] [blame] | 137 | private static final String APP_NAME = "org.onosproject.cpr"; |
Jonathan Hart | f8cd052 | 2016-10-25 07:09:55 -0700 | [diff] [blame] | 138 | private ApplicationId appId; |
| 139 | |
Jonathan Hart | f8cd052 | 2016-10-25 07:09:55 -0700 | [diff] [blame] | 140 | private Map<Host, Set<Integer>> peerNextId = Maps.newConcurrentMap(); |
| 141 | |
Jonathan Hart | 249b4cf | 2017-02-03 18:02:58 -0800 | [diff] [blame] | 142 | private Map<DeviceId, Router> routers = new ConcurrentHashMap<>(); |
Jonathan Hart | f8cd052 | 2016-10-25 07:09:55 -0700 | [diff] [blame] | 143 | |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 144 | private final InternalNetworkConfigListener networkConfigListener = |
| 145 | new InternalNetworkConfigListener(); |
| 146 | |
| 147 | @Activate |
Charles Chan | c6d227e | 2017-02-28 15:15:17 -0800 | [diff] [blame] | 148 | protected void activate(ComponentContext context) { |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 149 | this.appId = coreService.registerApplication(APP_NAME); |
| 150 | |
Charles Chan | c6d227e | 2017-02-28 15:15:17 -0800 | [diff] [blame] | 151 | cfgService.registerProperties(getClass()); |
| 152 | modified(context); |
| 153 | |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 154 | networkConfigService.addListener(networkConfigListener); |
Jonathan Hart | f8cd052 | 2016-10-25 07:09:55 -0700 | [diff] [blame] | 155 | |
Jonathan Hart | 7fc5a72 | 2017-01-26 15:54:12 -0800 | [diff] [blame] | 156 | processRouterConfig(); |
Charles Chan | 00d8b5f | 2016-12-04 17:17:39 -0800 | [diff] [blame] | 157 | |
Jonathan Hart | 249b4cf | 2017-02-03 18:02:58 -0800 | [diff] [blame] | 158 | applicationService.registerDeactivateHook(this.appId, |
| 159 | () -> routers.forEach((d, r) -> r.cleanup())); |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | @Deactivate |
Jonathan Hart | e732704 | 2017-02-02 13:11:25 -0800 | [diff] [blame] | 163 | protected void deactivate() { |
Charles Chan | c6d227e | 2017-02-28 15:15:17 -0800 | [diff] [blame] | 164 | cfgService.unregisterProperties(getClass(), false); |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 165 | networkConfigService.removeListener(networkConfigListener); |
Jonathan Hart | f8cd052 | 2016-10-25 07:09:55 -0700 | [diff] [blame] | 166 | } |
| 167 | |
Charles Chan | c6d227e | 2017-02-28 15:15:17 -0800 | [diff] [blame] | 168 | @Modified |
| 169 | protected void modified(ComponentContext context) { |
| 170 | if (context != null) { |
| 171 | readComponentConfiguration(context); |
| 172 | processRouterConfig(); |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | private void readComponentConfiguration(ComponentContext context) { |
| 177 | Dictionary<?, ?> properties = context.getProperties(); |
| 178 | Boolean flag; |
| 179 | |
| 180 | flag = Tools.isPropertyEnabled(properties, "forceUnprovision"); |
| 181 | if (flag == null) { |
| 182 | log.info("ForceUnprovision is not configured, " + |
| 183 | "using current value of {}", forceUnprovision); |
| 184 | } else { |
| 185 | forceUnprovision = flag; |
| 186 | log.info("Configured. ForceUnprovision is {}", |
| 187 | forceUnprovision ? "enabled" : "disabled"); |
| 188 | } |
| 189 | } |
| 190 | |
kishore | 786b7e4 | 2016-05-19 16:25:57 +0530 | [diff] [blame] | 191 | /** |
Jonathan Hart | f8cd052 | 2016-10-25 07:09:55 -0700 | [diff] [blame] | 192 | * Sets up the router interfaces if router config is available. |
| 193 | */ |
Jonathan Hart | 7fc5a72 | 2017-01-26 15:54:12 -0800 | [diff] [blame] | 194 | private void processRouterConfig() { |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 195 | ApplicationId routingAppId = |
| 196 | coreService.registerApplication(RoutingService.ROUTER_APP_ID); |
| 197 | |
Jonathan Hart | 249b4cf | 2017-02-03 18:02:58 -0800 | [diff] [blame] | 198 | Set<RoutersConfig.Router> routerConfigs = |
| 199 | RouterConfigHelper.getRouterConfigurations(networkConfigService, routingAppId); |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 200 | |
Jonathan Hart | 249b4cf | 2017-02-03 18:02:58 -0800 | [diff] [blame] | 201 | for (RoutersConfig.Router router : routerConfigs) { |
| 202 | DeviceId deviceId = router.controlPlaneConnectPoint().deviceId(); |
| 203 | |
| 204 | routers.compute(deviceId, (d, r) -> { |
| 205 | if (r == null) { |
| 206 | return createRouter(RouterInfo.from(router)); |
| 207 | } else { |
Charles Chan | c6d227e | 2017-02-28 15:15:17 -0800 | [diff] [blame] | 208 | r.changeConfiguration(RouterInfo.from(router), forceUnprovision); |
Jonathan Hart | 249b4cf | 2017-02-03 18:02:58 -0800 | [diff] [blame] | 209 | return r; |
| 210 | } |
| 211 | }); |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 212 | } |
| 213 | |
Jonathan Hart | 249b4cf | 2017-02-03 18:02:58 -0800 | [diff] [blame] | 214 | for (DeviceId deviceId : routers.keySet()) { |
| 215 | if (!configExists(deviceId, routerConfigs)) { |
| 216 | Router router = routers.remove(deviceId); |
| 217 | router.cleanup(); |
| 218 | } |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 219 | } |
| 220 | } |
| 221 | |
Jonathan Hart | 249b4cf | 2017-02-03 18:02:58 -0800 | [diff] [blame] | 222 | private boolean configExists(DeviceId deviceId, Set<RoutersConfig.Router> config) { |
| 223 | return config.stream() |
| 224 | .anyMatch(r -> r.controlPlaneConnectPoint().deviceId().equals(deviceId)); |
Jonathan Hart | 7fc5a72 | 2017-01-26 15:54:12 -0800 | [diff] [blame] | 225 | } |
| 226 | |
Jonathan Hart | 249b4cf | 2017-02-03 18:02:58 -0800 | [diff] [blame] | 227 | private Router createRouter(RouterInfo info) { |
| 228 | return new Router(info, |
Jonathan Hart | 7fc5a72 | 2017-01-26 15:54:12 -0800 | [diff] [blame] | 229 | interfaceService, |
Jonathan Hart | 249b4cf | 2017-02-03 18:02:58 -0800 | [diff] [blame] | 230 | deviceService, |
Jonathan Hart | 7fc5a72 | 2017-01-26 15:54:12 -0800 | [diff] [blame] | 231 | this::provisionInterface, |
Charles Chan | c6d227e | 2017-02-28 15:15:17 -0800 | [diff] [blame] | 232 | this::unprovisionInterface, |
| 233 | forceUnprovision); |
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 void provisionInterface(InterfaceProvisionRequest intf) { |
Jonathan Hart | 7fc5a72 | 2017-01-26 15:54:12 -0800 | [diff] [blame] | 237 | updateInterfaceObjectives(intf, true); |
| 238 | } |
| 239 | |
Jonathan Hart | 249b4cf | 2017-02-03 18:02:58 -0800 | [diff] [blame] | 240 | private void unprovisionInterface(InterfaceProvisionRequest intf) { |
Jonathan Hart | 7fc5a72 | 2017-01-26 15:54:12 -0800 | [diff] [blame] | 241 | updateInterfaceObjectives(intf, false); |
| 242 | } |
| 243 | |
| 244 | /** |
| 245 | * Installs or removes flow objectives relating to a give interface. |
| 246 | * |
| 247 | * @param intf interface to change objectives for |
| 248 | * @param install true to install the objectives, false to remove them |
| 249 | */ |
Jonathan Hart | 249b4cf | 2017-02-03 18:02:58 -0800 | [diff] [blame] | 250 | private void updateInterfaceObjectives(InterfaceProvisionRequest intf, boolean install) { |
Jonathan Hart | f8035d3 | 2016-06-16 16:23:26 -0700 | [diff] [blame] | 251 | updateInterfaceForwarding(intf, install); |
| 252 | updateOspfForwarding(intf, install); |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 253 | } |
| 254 | |
kishore | 71a2753 | 2016-03-16 20:23:49 +0530 | [diff] [blame] | 255 | /** |
Jonathan Hart | 7fc5a72 | 2017-01-26 15:54:12 -0800 | [diff] [blame] | 256 | * Installs or removes the basic forwarding flows for each interface. |
kishore | 71a2753 | 2016-03-16 20:23:49 +0530 | [diff] [blame] | 257 | * |
Jonathan Hart | 249b4cf | 2017-02-03 18:02:58 -0800 | [diff] [blame] | 258 | * @param request provisioning request containing router and interface |
Jonathan Hart | 7fc5a72 | 2017-01-26 15:54:12 -0800 | [diff] [blame] | 259 | * @param install true to install the objectives, false to remove them |
| 260 | */ |
Jonathan Hart | 249b4cf | 2017-02-03 18:02:58 -0800 | [diff] [blame] | 261 | private void updateInterfaceForwarding(InterfaceProvisionRequest request, boolean install) { |
| 262 | Interface intf = request.intf(); |
Jonathan Hart | 7fc5a72 | 2017-01-26 15:54:12 -0800 | [diff] [blame] | 263 | log.debug("{} interface objectives for {}", operation(install), intf); |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 264 | |
Jonathan Hart | f8cd052 | 2016-10-25 07:09:55 -0700 | [diff] [blame] | 265 | DeviceId deviceId = intf.connectPoint().deviceId(); |
Jonathan Hart | 249b4cf | 2017-02-03 18:02:58 -0800 | [diff] [blame] | 266 | |
| 267 | PortNumber controlPlanePort = request.controlPlaneConnectPoint().port(); |
Ray Milkey | 048bf9a | 2017-05-12 14:31:50 -0700 | [diff] [blame^] | 268 | for (InterfaceIpAddress ip : intf.ipAddressesList()) { |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 269 | // create nextObjectives for forwarding to this interface and the |
| 270 | // controlPlaneConnectPoint |
| 271 | int cpNextId, intfNextId; |
| 272 | if (intf.vlan() == VlanId.NONE) { |
kishore | 71a2753 | 2016-03-16 20:23:49 +0530 | [diff] [blame] | 273 | cpNextId = modifyNextObjective(deviceId, controlPlanePort, |
Jonathan Hart | f4bd048 | 2017-01-27 15:11:18 -0800 | [diff] [blame] | 274 | VlanId.vlanId(ASSIGNED_VLAN), |
kishore | 71a2753 | 2016-03-16 20:23:49 +0530 | [diff] [blame] | 275 | true, install); |
| 276 | intfNextId = modifyNextObjective(deviceId, intf.connectPoint().port(), |
Jonathan Hart | f4bd048 | 2017-01-27 15:11:18 -0800 | [diff] [blame] | 277 | VlanId.vlanId(ASSIGNED_VLAN), |
kishore | 71a2753 | 2016-03-16 20:23:49 +0530 | [diff] [blame] | 278 | true, install); |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 279 | } else { |
kishore | 71a2753 | 2016-03-16 20:23:49 +0530 | [diff] [blame] | 280 | cpNextId = modifyNextObjective(deviceId, controlPlanePort, |
| 281 | intf.vlan(), false, install); |
| 282 | intfNextId = modifyNextObjective(deviceId, intf.connectPoint().port(), |
| 283 | intf.vlan(), false, install); |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 284 | } |
Vinayak Tejankar | 3a409c6 | 2017-01-12 02:20:53 +0530 | [diff] [blame] | 285 | List<ForwardingObjective> fwdToSend = Lists.newArrayList(); |
| 286 | TrafficSelector selector; |
| 287 | // IP traffic toward the router. |
| 288 | selector = buildIPDstSelector( |
| 289 | ip.ipAddress().toIpPrefix(), |
| 290 | intf.connectPoint().port(), |
| 291 | null, |
| 292 | intf.mac(), |
| 293 | intf.vlan() |
| 294 | ); |
| 295 | fwdToSend.add(buildForwardingObjective(selector, null, cpNextId, install, ACL_PRIORITY)); |
| 296 | // IP traffic from the router. |
| 297 | selector = buildIPSrcSelector( |
| 298 | ip.ipAddress().toIpPrefix(), |
| 299 | controlPlanePort, |
| 300 | intf.mac(), |
| 301 | null, |
| 302 | intf.vlan() |
| 303 | ); |
| 304 | fwdToSend.add(buildForwardingObjective(selector, null, intfNextId, install, ACL_PRIORITY)); |
| 305 | // We build the punt treatment. |
| 306 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 307 | .punt() |
| 308 | .build(); |
Vinayak Tejankar | 3a409c6 | 2017-01-12 02:20:53 +0530 | [diff] [blame] | 309 | // Handling of neighbour discovery protocols. |
| 310 | // IPv4 traffic - we have to deal with the ARP protocol. |
| 311 | // IPv6 traffic - we have to deal with the NDP protocol. |
| 312 | if (ip.ipAddress().isIp4()) { |
| 313 | // ARP traffic towards the router. |
| 314 | selector = buildArpSelector( |
| 315 | intf.connectPoint().port(), |
| 316 | intf.vlan(), |
| 317 | null, |
| 318 | null |
| 319 | ); |
| 320 | fwdToSend.add(buildForwardingObjective(selector, treatment, cpNextId, install, ACL_PRIORITY + 1)); |
| 321 | // ARP traffic from the router. |
| 322 | selector = buildArpSelector( |
| 323 | controlPlanePort, |
| 324 | intf.vlan(), |
| 325 | ip.ipAddress().getIp4Address(), |
| 326 | intf.mac() |
| 327 | ); |
| 328 | fwdToSend.add(buildForwardingObjective(selector, treatment, intfNextId, install, ACL_PRIORITY + 1)); |
| 329 | } else { |
| 330 | // Neighbour solicitation traffic towards the router. |
Pier Luigi | 2b1ad87 | 2017-01-31 09:35:42 -0800 | [diff] [blame] | 331 | // This flow is for the global unicast address. |
Vinayak Tejankar | 3a409c6 | 2017-01-12 02:20:53 +0530 | [diff] [blame] | 332 | selector = buildNdpSelector( |
| 333 | intf.connectPoint().port(), |
| 334 | intf.vlan(), |
Pier Luigi | 2b1ad87 | 2017-01-31 09:35:42 -0800 | [diff] [blame] | 335 | null, |
Pier Luigi | 9f765dc | 2017-02-03 13:35:23 -0800 | [diff] [blame] | 336 | ip.ipAddress().toIpPrefix(), |
Pier Luigi | 2b1ad87 | 2017-01-31 09:35:42 -0800 | [diff] [blame] | 337 | NEIGHBOR_SOLICITATION, |
| 338 | null |
| 339 | ); |
| 340 | fwdToSend.add(buildForwardingObjective(selector, treatment, cpNextId, install, ACL_PRIORITY + 1)); |
| 341 | // Neighbour solicitation traffic towards the router. |
| 342 | // This flow is for the link local address. |
| 343 | selector = buildNdpSelector( |
| 344 | intf.connectPoint().port(), |
| 345 | intf.vlan(), |
Pier Luigi | 2b1ad87 | 2017-01-31 09:35:42 -0800 | [diff] [blame] | 346 | null, |
Pier Luigi | 9f765dc | 2017-02-03 13:35:23 -0800 | [diff] [blame] | 347 | Ip6Address.valueOf(getLinkLocalAddress(intf.mac().toBytes())).toIpPrefix(), |
Pier Luigi | 2b1ad87 | 2017-01-31 09:35:42 -0800 | [diff] [blame] | 348 | NEIGHBOR_SOLICITATION, |
| 349 | null |
| 350 | ); |
| 351 | fwdToSend.add(buildForwardingObjective(selector, treatment, cpNextId, install, ACL_PRIORITY + 1)); |
| 352 | // Neighbour solicitation traffic towards the router. |
| 353 | // This flow is for the solicitation node address of |
| 354 | // the global unicast address. |
| 355 | selector = buildNdpSelector( |
| 356 | intf.connectPoint().port(), |
| 357 | intf.vlan(), |
Pier Luigi | 2b1ad87 | 2017-01-31 09:35:42 -0800 | [diff] [blame] | 358 | null, |
Pier Luigi | 9f765dc | 2017-02-03 13:35:23 -0800 | [diff] [blame] | 359 | Ip6Address.valueOf(getSolicitNodeAddress(ip.ipAddress().toOctets())).toIpPrefix(), |
Pier Luigi | 2b1ad87 | 2017-01-31 09:35:42 -0800 | [diff] [blame] | 360 | NEIGHBOR_SOLICITATION, |
| 361 | null |
| 362 | ); |
| 363 | fwdToSend.add(buildForwardingObjective(selector, treatment, cpNextId, install, ACL_PRIORITY + 1)); |
| 364 | // Neighbour solicitation traffic towards the router. |
| 365 | // This flow is for the solicitation node address of |
| 366 | // the link local address. |
| 367 | selector = buildNdpSelector( |
| 368 | intf.connectPoint().port(), |
| 369 | intf.vlan(), |
Pier Luigi | 9f765dc | 2017-02-03 13:35:23 -0800 | [diff] [blame] | 370 | null, |
Pier Luigi | 2b1ad87 | 2017-01-31 09:35:42 -0800 | [diff] [blame] | 371 | Ip6Address.valueOf( |
| 372 | getSolicitNodeAddress(getLinkLocalAddress(intf.mac().toBytes())) |
| 373 | ).toIpPrefix(), |
Vinayak Tejankar | 3a409c6 | 2017-01-12 02:20:53 +0530 | [diff] [blame] | 374 | NEIGHBOR_SOLICITATION, |
| 375 | null |
| 376 | ); |
| 377 | fwdToSend.add(buildForwardingObjective(selector, treatment, cpNextId, install, ACL_PRIORITY + 1)); |
| 378 | // Neighbour solicitation traffic from the router. |
Pier Luigi | 2b1ad87 | 2017-01-31 09:35:42 -0800 | [diff] [blame] | 379 | // This flow is for the global unicast address. |
Vinayak Tejankar | 3a409c6 | 2017-01-12 02:20:53 +0530 | [diff] [blame] | 380 | selector = buildNdpSelector( |
| 381 | controlPlanePort, |
| 382 | intf.vlan(), |
| 383 | ip.ipAddress().toIpPrefix(), |
Pier Luigi | 9f765dc | 2017-02-03 13:35:23 -0800 | [diff] [blame] | 384 | null, |
Vinayak Tejankar | 3a409c6 | 2017-01-12 02:20:53 +0530 | [diff] [blame] | 385 | NEIGHBOR_SOLICITATION, |
| 386 | intf.mac() |
| 387 | ); |
| 388 | fwdToSend.add(buildForwardingObjective(selector, treatment, intfNextId, install, ACL_PRIORITY + 1)); |
Pier Luigi | 2b1ad87 | 2017-01-31 09:35:42 -0800 | [diff] [blame] | 389 | // Neighbour solicitation traffic from the router. |
| 390 | // This flow is for the link local address. |
| 391 | selector = buildNdpSelector( |
| 392 | controlPlanePort, |
| 393 | intf.vlan(), |
Pier Luigi | 2b1ad87 | 2017-01-31 09:35:42 -0800 | [diff] [blame] | 394 | Ip6Address.valueOf(getLinkLocalAddress(intf.mac().toBytes())).toIpPrefix(), |
Pier Luigi | 9f765dc | 2017-02-03 13:35:23 -0800 | [diff] [blame] | 395 | null, |
Pier Luigi | 2b1ad87 | 2017-01-31 09:35:42 -0800 | [diff] [blame] | 396 | NEIGHBOR_SOLICITATION, |
| 397 | intf.mac() |
| 398 | ); |
| 399 | fwdToSend.add(buildForwardingObjective(selector, treatment, intfNextId, install, ACL_PRIORITY + 1)); |
| 400 | // Neighbour advertisement traffic towards the router. |
| 401 | // This flow is for the global unicast address |
Vinayak Tejankar | 3a409c6 | 2017-01-12 02:20:53 +0530 | [diff] [blame] | 402 | selector = buildNdpSelector( |
| 403 | intf.connectPoint().port(), |
| 404 | intf.vlan(), |
Pier Luigi | 2b1ad87 | 2017-01-31 09:35:42 -0800 | [diff] [blame] | 405 | null, |
Pier Luigi | 9f765dc | 2017-02-03 13:35:23 -0800 | [diff] [blame] | 406 | ip.ipAddress().toIpPrefix(), |
Pier Luigi | 2b1ad87 | 2017-01-31 09:35:42 -0800 | [diff] [blame] | 407 | NEIGHBOR_ADVERTISEMENT, |
| 408 | null |
| 409 | ); |
| 410 | fwdToSend.add(buildForwardingObjective(selector, treatment, cpNextId, install, ACL_PRIORITY + 1)); |
| 411 | // Neighbour advertisement traffic towards the router. |
| 412 | // This flow is for the link local address |
| 413 | selector = buildNdpSelector( |
| 414 | intf.connectPoint().port(), |
| 415 | intf.vlan(), |
Vinayak Tejankar | 3a409c6 | 2017-01-12 02:20:53 +0530 | [diff] [blame] | 416 | null, |
Pier Luigi | 9f765dc | 2017-02-03 13:35:23 -0800 | [diff] [blame] | 417 | Ip6Address.valueOf(getLinkLocalAddress(intf.mac().toBytes())).toIpPrefix(), |
Vinayak Tejankar | 3a409c6 | 2017-01-12 02:20:53 +0530 | [diff] [blame] | 418 | NEIGHBOR_ADVERTISEMENT, |
| 419 | null |
| 420 | ); |
| 421 | fwdToSend.add(buildForwardingObjective(selector, treatment, cpNextId, install, ACL_PRIORITY + 1)); |
| 422 | // Neighbour advertisement traffic from the router. |
Pier Luigi | 2b1ad87 | 2017-01-31 09:35:42 -0800 | [diff] [blame] | 423 | // This flow is for the global unicast address |
Vinayak Tejankar | 3a409c6 | 2017-01-12 02:20:53 +0530 | [diff] [blame] | 424 | selector = buildNdpSelector( |
| 425 | controlPlanePort, |
| 426 | intf.vlan(), |
| 427 | ip.ipAddress().toIpPrefix(), |
Pier Luigi | 9f765dc | 2017-02-03 13:35:23 -0800 | [diff] [blame] | 428 | null, |
Vinayak Tejankar | 3a409c6 | 2017-01-12 02:20:53 +0530 | [diff] [blame] | 429 | NEIGHBOR_ADVERTISEMENT, |
| 430 | intf.mac() |
| 431 | ); |
| 432 | fwdToSend.add(buildForwardingObjective(selector, treatment, intfNextId, install, ACL_PRIORITY + 1)); |
Pier Luigi | 2b1ad87 | 2017-01-31 09:35:42 -0800 | [diff] [blame] | 433 | // Neighbour advertisement traffic from the router. |
| 434 | // This flow is for the link local address |
| 435 | selector = buildNdpSelector( |
| 436 | controlPlanePort, |
| 437 | intf.vlan(), |
Pier Luigi | 2b1ad87 | 2017-01-31 09:35:42 -0800 | [diff] [blame] | 438 | Ip6Address.valueOf(getLinkLocalAddress(intf.mac().toBytes())).toIpPrefix(), |
Pier Luigi | 9f765dc | 2017-02-03 13:35:23 -0800 | [diff] [blame] | 439 | null, |
Pier Luigi | 2b1ad87 | 2017-01-31 09:35:42 -0800 | [diff] [blame] | 440 | NEIGHBOR_ADVERTISEMENT, |
| 441 | intf.mac() |
| 442 | ); |
| 443 | fwdToSend.add(buildForwardingObjective(selector, treatment, intfNextId, install, ACL_PRIORITY + 1)); |
Vinayak Tejankar | 3a409c6 | 2017-01-12 02:20:53 +0530 | [diff] [blame] | 444 | } |
| 445 | // Finally we push the fwd objectives through the flow objective service. |
| 446 | fwdToSend.stream().forEach(forwardingObjective -> |
| 447 | flowObjectiveService.forward(deviceId, forwardingObjective) |
| 448 | ); |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 449 | } |
| 450 | } |
| 451 | |
kishore | 71a2753 | 2016-03-16 20:23:49 +0530 | [diff] [blame] | 452 | /** |
Jonathan Hart | f04b7d9 | 2016-03-29 09:39:11 -0700 | [diff] [blame] | 453 | * Installs or removes OSPF forwarding rules. |
kishore | 71a2753 | 2016-03-16 20:23:49 +0530 | [diff] [blame] | 454 | * |
Jonathan Hart | 249b4cf | 2017-02-03 18:02:58 -0800 | [diff] [blame] | 455 | * @param request provisioning request containing router and interface |
kishore | 71a2753 | 2016-03-16 20:23:49 +0530 | [diff] [blame] | 456 | * @param install true to create an add objective, false to create a remove |
| 457 | * objective |
Jonathan Hart | 7fc5a72 | 2017-01-26 15:54:12 -0800 | [diff] [blame] | 458 | */ |
Jonathan Hart | 249b4cf | 2017-02-03 18:02:58 -0800 | [diff] [blame] | 459 | private void updateOspfForwarding(InterfaceProvisionRequest request, boolean install) { |
Jonathan Hart | 7fc5a72 | 2017-01-26 15:54:12 -0800 | [diff] [blame] | 460 | // TODO IPv6 support has not been implemented yet |
Jonathan Hart | 249b4cf | 2017-02-03 18:02:58 -0800 | [diff] [blame] | 461 | Interface intf = request.intf(); |
Jonathan Hart | 7fc5a72 | 2017-01-26 15:54:12 -0800 | [diff] [blame] | 462 | log.debug("{} OSPF flows for {}", operation(install), intf); |
| 463 | |
Jonathan Hart | ea49238 | 2016-01-13 09:33:13 -0800 | [diff] [blame] | 464 | // OSPF to router |
| 465 | TrafficSelector toSelector = DefaultTrafficSelector.builder() |
| 466 | .matchInPort(intf.connectPoint().port()) |
| 467 | .matchEthType(EthType.EtherType.IPV4.ethType().toShort()) |
| 468 | .matchVlanId(intf.vlan()) |
| 469 | .matchIPProtocol((byte) OSPF_IP_PROTO) |
| 470 | .build(); |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 471 | |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 472 | // create nextObjectives for forwarding to the controlPlaneConnectPoint |
Jonathan Hart | f8cd052 | 2016-10-25 07:09:55 -0700 | [diff] [blame] | 473 | DeviceId deviceId = intf.connectPoint().deviceId(); |
Jonathan Hart | 249b4cf | 2017-02-03 18:02:58 -0800 | [diff] [blame] | 474 | PortNumber controlPlanePort = request.controlPlaneConnectPoint().port(); |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 475 | int cpNextId; |
| 476 | if (intf.vlan() == VlanId.NONE) { |
kishore | 71a2753 | 2016-03-16 20:23:49 +0530 | [diff] [blame] | 477 | cpNextId = modifyNextObjective(deviceId, controlPlanePort, |
Jonathan Hart | f4bd048 | 2017-01-27 15:11:18 -0800 | [diff] [blame] | 478 | VlanId.vlanId(ASSIGNED_VLAN), |
kishore | 71a2753 | 2016-03-16 20:23:49 +0530 | [diff] [blame] | 479 | true, install); |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 480 | } else { |
kishore | 71a2753 | 2016-03-16 20:23:49 +0530 | [diff] [blame] | 481 | cpNextId = modifyNextObjective(deviceId, controlPlanePort, |
| 482 | intf.vlan(), false, install); |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 483 | } |
Jonathan Hart | f8cd052 | 2016-10-25 07:09:55 -0700 | [diff] [blame] | 484 | flowObjectiveService.forward(intf.connectPoint().deviceId(), |
Jonathan Hart | 7fc5a72 | 2017-01-26 15:54:12 -0800 | [diff] [blame] | 485 | buildForwardingObjective(toSelector, null, cpNextId, |
Jonathan Hart | 249b4cf | 2017-02-03 18:02:58 -0800 | [diff] [blame] | 486 | install ? request.info().ospfEnabled() : install, ACL_PRIORITY)); |
Jonathan Hart | ea49238 | 2016-01-13 09:33:13 -0800 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | /** |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 490 | * Creates a next objective for forwarding to a port. Handles metadata for |
| 491 | * some pipelines that require vlan information for egress port. |
| 492 | * |
| 493 | * @param deviceId the device on which the next objective is being created |
| 494 | * @param portNumber the egress port |
| 495 | * @param vlanId vlan information for egress port |
| 496 | * @param popVlan if vlan tag should be popped or not |
kishore | 71a2753 | 2016-03-16 20:23:49 +0530 | [diff] [blame] | 497 | * @param install true to create an add next objective, false to create a remove |
| 498 | * next objective |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 499 | * @return nextId of the next objective created |
| 500 | */ |
kishore | 71a2753 | 2016-03-16 20:23:49 +0530 | [diff] [blame] | 501 | private int modifyNextObjective(DeviceId deviceId, PortNumber portNumber, |
| 502 | VlanId vlanId, boolean popVlan, boolean install) { |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 503 | int nextId = flowObjectiveService.allocateNextId(); |
| 504 | NextObjective.Builder nextObjBuilder = DefaultNextObjective |
| 505 | .builder().withId(nextId) |
| 506 | .withType(NextObjective.Type.SIMPLE) |
| 507 | .fromApp(appId); |
| 508 | |
| 509 | TrafficTreatment.Builder ttBuilder = DefaultTrafficTreatment.builder(); |
| 510 | if (popVlan) { |
| 511 | ttBuilder.popVlan(); |
| 512 | } |
| 513 | ttBuilder.setOutput(portNumber); |
| 514 | |
| 515 | // setup metadata to pass to nextObjective - indicate the vlan on egress |
| 516 | // if needed by the switch pipeline. |
| 517 | TrafficSelector.Builder metabuilder = DefaultTrafficSelector.builder(); |
| 518 | metabuilder.matchVlanId(vlanId); |
| 519 | |
| 520 | nextObjBuilder.withMeta(metabuilder.build()); |
| 521 | nextObjBuilder.addTreatment(ttBuilder.build()); |
Jonathan Hart | f04b7d9 | 2016-03-29 09:39:11 -0700 | [diff] [blame] | 522 | log.debug("Submitted next objective {} in device {} for port/vlan {}/{}", |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 523 | nextId, deviceId, portNumber, vlanId); |
kishore | 71a2753 | 2016-03-16 20:23:49 +0530 | [diff] [blame] | 524 | if (install) { |
| 525 | flowObjectiveService.next(deviceId, nextObjBuilder.add()); |
| 526 | } else { |
| 527 | flowObjectiveService.next(deviceId, nextObjBuilder.remove()); |
| 528 | } |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 529 | return nextId; |
| 530 | } |
Jonathan Hart | f8035d3 | 2016-06-16 16:23:26 -0700 | [diff] [blame] | 531 | |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 532 | /** |
| 533 | * Builds a forwarding objective from the given selector, treatment and nextId. |
Jonathan Hart | ea49238 | 2016-01-13 09:33:13 -0800 | [diff] [blame] | 534 | * |
| 535 | * @param selector selector |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 536 | * @param treatment treatment to apply to packet, can be null |
| 537 | * @param nextId next objective to point to for forwarding packet |
Jonathan Hart | ea49238 | 2016-01-13 09:33:13 -0800 | [diff] [blame] | 538 | * @param add true to create an add objective, false to create a remove |
| 539 | * objective |
| 540 | * @return forwarding objective |
| 541 | */ |
| 542 | private ForwardingObjective buildForwardingObjective(TrafficSelector selector, |
| 543 | TrafficTreatment treatment, |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 544 | int nextId, |
Vinayak Tejankar | 3a409c6 | 2017-01-12 02:20:53 +0530 | [diff] [blame] | 545 | boolean add, |
| 546 | int priority) { |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 547 | DefaultForwardingObjective.Builder fobBuilder = DefaultForwardingObjective.builder(); |
| 548 | fobBuilder.withSelector(selector); |
| 549 | if (treatment != null) { |
| 550 | fobBuilder.withTreatment(treatment); |
| 551 | } |
| 552 | if (nextId != -1) { |
| 553 | fobBuilder.nextStep(nextId); |
| 554 | } |
| 555 | fobBuilder.fromApp(appId) |
Vinayak Tejankar | 3a409c6 | 2017-01-12 02:20:53 +0530 | [diff] [blame] | 556 | .withPriority(priority) |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 557 | .withFlag(ForwardingObjective.Flag.VERSATILE); |
Jonathan Hart | ea49238 | 2016-01-13 09:33:13 -0800 | [diff] [blame] | 558 | |
| 559 | return add ? fobBuilder.add() : fobBuilder.remove(); |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 560 | } |
| 561 | |
Jonathan Hart | 7fc5a72 | 2017-01-26 15:54:12 -0800 | [diff] [blame] | 562 | static TrafficSelector.Builder buildBaseSelectorBuilder(PortNumber inPort, |
| 563 | MacAddress srcMac, |
| 564 | MacAddress dstMac, |
| 565 | VlanId vlanId) { |
| 566 | TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder(); |
| 567 | if (inPort != null) { |
| 568 | selectorBuilder.matchInPort(inPort); |
| 569 | } |
| 570 | if (srcMac != null) { |
| 571 | selectorBuilder.matchEthSrc(srcMac); |
| 572 | } |
| 573 | if (dstMac != null) { |
| 574 | selectorBuilder.matchEthDst(dstMac); |
| 575 | } |
| 576 | if (vlanId != null) { |
| 577 | selectorBuilder.matchVlanId(vlanId); |
| 578 | } |
| 579 | return selectorBuilder; |
| 580 | } |
| 581 | |
| 582 | static TrafficSelector buildIPDstSelector(IpPrefix dstIp, |
| 583 | PortNumber inPort, |
| 584 | MacAddress srcMac, |
| 585 | MacAddress dstMac, |
| 586 | VlanId vlanId) { |
| 587 | TrafficSelector.Builder selector = buildBaseSelectorBuilder(inPort, srcMac, dstMac, vlanId); |
| 588 | if (dstIp.isIp4()) { |
| 589 | selector.matchEthType(TYPE_IPV4); |
| 590 | selector.matchIPDst(dstIp); |
| 591 | } else { |
| 592 | selector.matchEthType(TYPE_IPV6); |
| 593 | selector.matchIPv6Dst(dstIp); |
| 594 | } |
| 595 | return selector.build(); |
| 596 | } |
| 597 | |
| 598 | static TrafficSelector buildIPSrcSelector(IpPrefix srcIp, |
| 599 | PortNumber inPort, |
| 600 | MacAddress srcMac, |
| 601 | MacAddress dstMac, |
| 602 | VlanId vlanId) { |
| 603 | TrafficSelector.Builder selector = buildBaseSelectorBuilder(inPort, srcMac, dstMac, vlanId); |
| 604 | if (srcIp.isIp4()) { |
| 605 | selector.matchEthType(TYPE_IPV4); |
| 606 | selector.matchIPSrc(srcIp); |
| 607 | } else { |
| 608 | selector.matchEthType(TYPE_IPV6); |
| 609 | selector.matchIPv6Src(srcIp); |
| 610 | } |
| 611 | return selector.build(); |
| 612 | } |
| 613 | |
| 614 | static TrafficSelector buildArpSelector(PortNumber inPort, |
| 615 | VlanId vlanId, |
| 616 | Ip4Address arpSpa, |
| 617 | MacAddress srcMac) { |
| 618 | TrafficSelector.Builder selector = buildBaseSelectorBuilder(inPort, null, null, vlanId); |
| 619 | selector.matchEthType(TYPE_ARP); |
| 620 | if (arpSpa != null) { |
| 621 | selector.matchArpSpa(arpSpa); |
| 622 | } |
| 623 | if (srcMac != null) { |
| 624 | selector.matchEthSrc(srcMac); |
| 625 | } |
| 626 | return selector.build(); |
| 627 | } |
| 628 | |
| 629 | static TrafficSelector buildNdpSelector(PortNumber inPort, |
| 630 | VlanId vlanId, |
| 631 | IpPrefix srcIp, |
Pier Luigi | 2b1ad87 | 2017-01-31 09:35:42 -0800 | [diff] [blame] | 632 | IpPrefix dstIp, |
Jonathan Hart | 7fc5a72 | 2017-01-26 15:54:12 -0800 | [diff] [blame] | 633 | byte subProto, |
| 634 | MacAddress srcMac) { |
| 635 | TrafficSelector.Builder selector = buildBaseSelectorBuilder(inPort, null, null, vlanId); |
| 636 | selector.matchEthType(TYPE_IPV6) |
| 637 | .matchIPProtocol(PROTOCOL_ICMP6) |
| 638 | .matchIcmpv6Type(subProto); |
| 639 | if (srcIp != null) { |
| 640 | selector.matchIPv6Src(srcIp); |
| 641 | } |
Pier Luigi | 2b1ad87 | 2017-01-31 09:35:42 -0800 | [diff] [blame] | 642 | if (dstIp != null) { |
Pier Luigi | 9f765dc | 2017-02-03 13:35:23 -0800 | [diff] [blame] | 643 | selector.matchIPv6Dst(dstIp); |
Pier Luigi | 2b1ad87 | 2017-01-31 09:35:42 -0800 | [diff] [blame] | 644 | } |
Jonathan Hart | 7fc5a72 | 2017-01-26 15:54:12 -0800 | [diff] [blame] | 645 | if (srcMac != null) { |
| 646 | selector.matchEthSrc(srcMac); |
| 647 | } |
| 648 | return selector.build(); |
| 649 | } |
| 650 | |
Jonathan Hart | 7fc5a72 | 2017-01-26 15:54:12 -0800 | [diff] [blame] | 651 | private String operation(boolean install) { |
| 652 | return install ? "Installing" : "Removing"; |
| 653 | } |
| 654 | |
Jonathan Hart | ea49238 | 2016-01-13 09:33:13 -0800 | [diff] [blame] | 655 | /** |
Jonathan Hart | ea49238 | 2016-01-13 09:33:13 -0800 | [diff] [blame] | 656 | * Listener for network config events. |
| 657 | */ |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 658 | private class InternalNetworkConfigListener implements NetworkConfigListener { |
| 659 | @Override |
| 660 | public void event(NetworkConfigEvent event) { |
Jonathan Hart | 249b4cf | 2017-02-03 18:02:58 -0800 | [diff] [blame] | 661 | if (event.configClass().equals(RoutingService.ROUTER_CONFIG_CLASS) || |
| 662 | event.configClass().equals(RoutersConfig.class)) { |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 663 | switch (event.type()) { |
Jonathan Hart | f8cd052 | 2016-10-25 07:09:55 -0700 | [diff] [blame] | 664 | case CONFIG_ADDED: |
| 665 | case CONFIG_UPDATED: |
Jonathan Hart | 249b4cf | 2017-02-03 18:02:58 -0800 | [diff] [blame] | 666 | case CONFIG_REMOVED: |
Jonathan Hart | 7fc5a72 | 2017-01-26 15:54:12 -0800 | [diff] [blame] | 667 | processRouterConfig(); |
Jonathan Hart | f8cd052 | 2016-10-25 07:09:55 -0700 | [diff] [blame] | 668 | break; |
| 669 | case CONFIG_REGISTERED: |
Jonathan Hart | f8cd052 | 2016-10-25 07:09:55 -0700 | [diff] [blame] | 670 | case CONFIG_UNREGISTERED: |
| 671 | break; |
Vinayak Tejankar | 3a409c6 | 2017-01-12 02:20:53 +0530 | [diff] [blame] | 672 | default: |
| 673 | break; |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 674 | } |
| 675 | } |
| 676 | } |
| 677 | } |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 678 | } |
Vinayak Tejankar | 3a409c6 | 2017-01-12 02:20:53 +0530 | [diff] [blame] | 679 | |