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