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