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