Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 1 | /* |
Jian Li | 8df54a9 | 2018-08-23 17:01:31 +0900 | [diff] [blame] | 2 | * Copyright 2018-present Open Networking Foundation |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [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 | */ |
Jian Li | 8df54a9 | 2018-08-23 17:01:31 +0900 | [diff] [blame] | 16 | package org.onosproject.simplefabric.impl; |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 17 | |
| 18 | import com.google.common.collect.ImmutableList; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 19 | import org.osgi.service.component.annotations.Activate; |
| 20 | import org.osgi.service.component.annotations.Component; |
| 21 | import org.osgi.service.component.annotations.Deactivate; |
| 22 | import org.osgi.service.component.annotations.Reference; |
| 23 | import org.osgi.service.component.annotations.ReferenceCardinality; |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 24 | import org.onlab.packet.EthType; |
| 25 | import org.onlab.packet.Ethernet; |
| 26 | import org.onlab.packet.ICMP; |
| 27 | import org.onlab.packet.ICMP6; |
| 28 | import org.onlab.packet.IPv4; |
| 29 | import org.onlab.packet.IPv6; |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 30 | import org.onlab.packet.Ip4Address; |
| 31 | import org.onlab.packet.Ip4Prefix; |
| 32 | import org.onlab.packet.Ip6Address; |
| 33 | import org.onlab.packet.Ip6Prefix; |
Jian Li | 8df54a9 | 2018-08-23 17:01:31 +0900 | [diff] [blame] | 34 | import org.onlab.packet.IpAddress; |
| 35 | import org.onlab.packet.IpPrefix; |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 36 | import org.onlab.packet.MacAddress; |
| 37 | import org.onlab.packet.VlanId; |
| 38 | import org.onosproject.core.ApplicationId; |
| 39 | import org.onosproject.core.CoreService; |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 40 | import org.onosproject.net.ConnectPoint; |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 41 | import org.onosproject.net.Device; |
| 42 | import org.onosproject.net.DeviceId; |
Jian Li | 8df54a9 | 2018-08-23 17:01:31 +0900 | [diff] [blame] | 43 | import org.onosproject.net.EncapsulationType; |
| 44 | import org.onosproject.net.FilteredConnectPoint; |
| 45 | import org.onosproject.net.Host; |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 46 | import org.onosproject.net.device.DeviceService; |
| 47 | import org.onosproject.net.flow.DefaultFlowRule; |
| 48 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 49 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 50 | import org.onosproject.net.flow.FlowRule; |
| 51 | import org.onosproject.net.flow.FlowRuleService; |
Jian Li | 8df54a9 | 2018-08-23 17:01:31 +0900 | [diff] [blame] | 52 | import org.onosproject.net.flow.TrafficSelector; |
| 53 | import org.onosproject.net.flow.TrafficTreatment; |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 54 | import org.onosproject.net.host.HostService; |
| 55 | import org.onosproject.net.intent.Constraint; |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 56 | import org.onosproject.net.intent.Intent; |
| 57 | import org.onosproject.net.intent.IntentService; |
| 58 | import org.onosproject.net.intent.Key; |
| 59 | import org.onosproject.net.intent.MultiPointToSinglePointIntent; |
Jian Li | 8df54a9 | 2018-08-23 17:01:31 +0900 | [diff] [blame] | 60 | import org.onosproject.net.intent.constraint.EncapsulationConstraint; |
| 61 | import org.onosproject.net.intent.constraint.HashedPathSelectionConstraint; |
| 62 | import org.onosproject.net.intent.constraint.PartialFailureConstraint; |
Lee Yongjae | 6dc7e4f | 2017-12-06 16:17:51 +0900 | [diff] [blame] | 63 | import org.onosproject.net.intf.Interface; |
Jian Li | 8df54a9 | 2018-08-23 17:01:31 +0900 | [diff] [blame] | 64 | import org.onosproject.net.intf.InterfaceService; |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 65 | import org.onosproject.net.link.LinkService; |
| 66 | import org.onosproject.net.packet.DefaultOutboundPacket; |
| 67 | import org.onosproject.net.packet.InboundPacket; |
| 68 | import org.onosproject.net.packet.OutboundPacket; |
| 69 | import org.onosproject.net.packet.PacketContext; |
| 70 | import org.onosproject.net.packet.PacketPriority; |
| 71 | import org.onosproject.net.packet.PacketProcessor; |
| 72 | import org.onosproject.net.packet.PacketService; |
Jian Li | e2d8751 | 2018-08-23 17:33:05 +0900 | [diff] [blame] | 73 | import org.onosproject.simplefabric.api.FabricNetwork; |
| 74 | import org.onosproject.simplefabric.api.FabricSubnet; |
| 75 | import org.onosproject.simplefabric.api.FabricRoute; |
Jian Li | c7efc1d | 2018-08-23 16:37:34 +0900 | [diff] [blame] | 76 | import org.onosproject.simplefabric.api.SimpleFabricEvent; |
| 77 | import org.onosproject.simplefabric.api.SimpleFabricListener; |
| 78 | import org.onosproject.simplefabric.api.SimpleFabricService; |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 79 | import org.slf4j.Logger; |
| 80 | import org.slf4j.LoggerFactory; |
| 81 | |
| 82 | import java.io.PrintStream; |
| 83 | import java.nio.ByteBuffer; |
| 84 | import java.util.ArrayList; |
| 85 | import java.util.Collections; |
| 86 | import java.util.Comparator; |
| 87 | import java.util.HashSet; |
| 88 | import java.util.List; |
| 89 | import java.util.Set; |
| 90 | |
Jian Li | 8df54a9 | 2018-08-23 17:01:31 +0900 | [diff] [blame] | 91 | import static org.onosproject.simplefabric.api.Constants.ALLOW_ETH_ADDRESS_SELECTOR; |
| 92 | import static org.onosproject.simplefabric.api.Constants.ALLOW_IPV6; |
| 93 | import static org.onosproject.simplefabric.api.Constants.PRI_REACTIVE_BORDER_BASE; |
| 94 | import static org.onosproject.simplefabric.api.Constants.PRI_REACTIVE_BORDER_FORWARD; |
| 95 | import static org.onosproject.simplefabric.api.Constants.PRI_REACTIVE_BORDER_INTERCEPT; |
| 96 | import static org.onosproject.simplefabric.api.Constants.PRI_REACTIVE_BORDER_STEP; |
| 97 | import static org.onosproject.simplefabric.api.Constants.PRI_REACTIVE_LOCAL_FORWARD; |
| 98 | import static org.onosproject.simplefabric.api.Constants.PRI_REACTIVE_LOCAL_INTERCEPT; |
| 99 | import static org.onosproject.simplefabric.api.Constants.REACTIVE_ALLOW_LINK_CP; |
Jian Li | e2d8751 | 2018-08-23 17:33:05 +0900 | [diff] [blame] | 100 | import static org.onosproject.simplefabric.api.Constants.ROUTING_APP_ID; |
Jian Li | 8df54a9 | 2018-08-23 17:01:31 +0900 | [diff] [blame] | 101 | import static org.onosproject.simplefabric.api.Constants.REACTIVE_HASHED_PATH_SELECTION; |
| 102 | import static org.onosproject.simplefabric.api.Constants.REACTIVE_MATCH_IP_PROTO; |
| 103 | import static org.onosproject.simplefabric.api.Constants.REACTIVE_SINGLE_TO_SINGLE; |
| 104 | |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 105 | |
| 106 | /** |
Jian Li | e2d8751 | 2018-08-23 17:33:05 +0900 | [diff] [blame] | 107 | * SimpleFabricRouting handles Routing. |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 108 | */ |
| 109 | @Component(immediate = true, enabled = false) |
Jian Li | e2d8751 | 2018-08-23 17:33:05 +0900 | [diff] [blame] | 110 | public class SimpleFabricRouting { |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 111 | |
| 112 | private final Logger log = LoggerFactory.getLogger(getClass()); |
Jian Li | e2d8751 | 2018-08-23 17:33:05 +0900 | [diff] [blame] | 113 | private ApplicationId appId; |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 114 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 115 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 116 | protected CoreService coreService; |
| 117 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 118 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 119 | protected PacketService packetService; |
| 120 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 121 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 122 | protected InterfaceService interfaceService; |
| 123 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 124 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 125 | protected DeviceService deviceService; |
| 126 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 127 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 128 | protected LinkService linkService; |
| 129 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 130 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 131 | protected HostService hostService; |
| 132 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 133 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 134 | protected IntentService intentService; |
| 135 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 136 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 137 | protected FlowRuleService flowRuleService; |
| 138 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 139 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 140 | protected SimpleFabricService simpleFabric; |
| 141 | |
| 142 | private ImmutableList<Constraint> reactiveConstraints |
| 143 | = ImmutableList.of(new PartialFailureConstraint()); |
| 144 | //= ImmutableList.of(); |
| 145 | // NOTE: SHOULD NOT use HashedPathSelectionConstraint |
| 146 | // for unpredictable srcCp of Link appears as reactive packet traffic |
| 147 | |
| 148 | private Set<FlowRule> interceptFlowRules = new HashSet<>(); |
| 149 | private Set<Key> toBePurgedIntentKeys = new HashSet<>(); |
| 150 | // NOTE: manage purged intents by key for intentService.getIntent() supports key only |
| 151 | |
| 152 | private final InternalSimpleFabricListener simpleFabricListener = new InternalSimpleFabricListener(); |
Jian Li | e2d8751 | 2018-08-23 17:33:05 +0900 | [diff] [blame] | 153 | private InternalRoutingProcessor processor = new InternalRoutingProcessor(); |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 154 | |
| 155 | @Activate |
| 156 | public void activate() { |
Jian Li | e2d8751 | 2018-08-23 17:33:05 +0900 | [diff] [blame] | 157 | appId = coreService.registerApplication(ROUTING_APP_ID); |
| 158 | log.info("simple fabric routing starting with app id {}", appId.toString()); |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 159 | |
| 160 | // NOTE: may not clear at init for MIGHT generate pending_remove garbages |
| 161 | // use flush event from simple fabric cli command |
| 162 | |
Jian Li | 8df54a9 | 2018-08-23 17:01:31 +0900 | [diff] [blame] | 163 | if (REACTIVE_HASHED_PATH_SELECTION) { |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 164 | reactiveConstraints = ImmutableList.of(new PartialFailureConstraint(), |
| 165 | new HashedPathSelectionConstraint()); |
| 166 | } else { |
| 167 | reactiveConstraints = ImmutableList.of(new PartialFailureConstraint()); |
| 168 | } |
| 169 | |
Jian Li | e2d8751 | 2018-08-23 17:33:05 +0900 | [diff] [blame] | 170 | processor = new InternalRoutingProcessor(); |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 171 | packetService.addProcessor(processor, PacketProcessor.director(2)); |
| 172 | simpleFabric.addListener(simpleFabricListener); |
| 173 | |
| 174 | registerIntercepts(); |
| 175 | refreshIntercepts(); |
| 176 | |
Jian Li | e2d8751 | 2018-08-23 17:33:05 +0900 | [diff] [blame] | 177 | log.info("simple fabric routing started"); |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | @Deactivate |
| 181 | public void deactivate() { |
Jian Li | e2d8751 | 2018-08-23 17:33:05 +0900 | [diff] [blame] | 182 | log.info("simple fabric routing stopping"); |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 183 | |
| 184 | packetService.removeProcessor(processor); |
| 185 | simpleFabric.removeListener(simpleFabricListener); |
| 186 | |
| 187 | withdrawIntercepts(); |
| 188 | |
| 189 | // NOTE: may not clear at init for MIGHT generate pending_remove garbages |
| 190 | // use flush event from simple fabric cli command |
| 191 | |
| 192 | toBePurgedIntentKeys.clear(); |
| 193 | |
Jian Li | e2d8751 | 2018-08-23 17:33:05 +0900 | [diff] [blame] | 194 | flowRuleService.removeFlowRulesById(appId); |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 195 | |
| 196 | processor = null; |
| 197 | |
Jian Li | e2d8751 | 2018-08-23 17:33:05 +0900 | [diff] [blame] | 198 | log.info("simple fabric routing stopped"); |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | /** |
| 202 | * Request packet in via the PacketService. |
| 203 | */ |
| 204 | private void registerIntercepts() { |
| 205 | // register default intercepts on packetService for broder routing intercepts |
| 206 | |
| 207 | packetService.requestPackets( |
| 208 | DefaultTrafficSelector.builder().matchEthType(Ethernet.TYPE_IPV4).build(), |
Jian Li | e2d8751 | 2018-08-23 17:33:05 +0900 | [diff] [blame] | 209 | PacketPriority.REACTIVE, appId); |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 210 | |
Jian Li | 8df54a9 | 2018-08-23 17:01:31 +0900 | [diff] [blame] | 211 | if (ALLOW_IPV6) { |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 212 | packetService.requestPackets( |
| 213 | DefaultTrafficSelector.builder().matchEthType(Ethernet.TYPE_IPV6).build(), |
Jian Li | e2d8751 | 2018-08-23 17:33:05 +0900 | [diff] [blame] | 214 | PacketPriority.REACTIVE, appId); |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 215 | } |
| 216 | |
Jian Li | e2d8751 | 2018-08-23 17:33:05 +0900 | [diff] [blame] | 217 | log.info("simple fabric routing ip packet intercepts started"); |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | /** |
| 221 | * Cancel request for packet in via PacketService. |
| 222 | */ |
| 223 | private void withdrawIntercepts() { |
| 224 | // unregister default intercepts on packetService |
| 225 | |
| 226 | packetService.cancelPackets( |
| 227 | DefaultTrafficSelector.builder().matchEthType(Ethernet.TYPE_IPV4).build(), |
Jian Li | e2d8751 | 2018-08-23 17:33:05 +0900 | [diff] [blame] | 228 | PacketPriority.REACTIVE, appId); |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 229 | |
Jian Li | 8df54a9 | 2018-08-23 17:01:31 +0900 | [diff] [blame] | 230 | if (ALLOW_IPV6) { |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 231 | packetService.cancelPackets( |
| 232 | DefaultTrafficSelector.builder().matchEthType(Ethernet.TYPE_IPV6).build(), |
Jian Li | e2d8751 | 2018-08-23 17:33:05 +0900 | [diff] [blame] | 233 | PacketPriority.REACTIVE, appId); |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 234 | } |
| 235 | |
Jian Li | e2d8751 | 2018-08-23 17:33:05 +0900 | [diff] [blame] | 236 | log.info("simple fabric routing ip packet intercepts stopped"); |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | /** |
Jian Li | e2d8751 | 2018-08-23 17:33:05 +0900 | [diff] [blame] | 240 | * Refresh device flow rules for intercepts on local fabricSubnets. |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 241 | */ |
| 242 | private void refreshIntercepts() { |
| 243 | Set<FlowRule> newInterceptFlowRules = new HashSet<>(); |
| 244 | for (Device device : deviceService.getAvailableDevices()) { |
Jian Li | e2d8751 | 2018-08-23 17:33:05 +0900 | [diff] [blame] | 245 | for (FabricSubnet subnet : simpleFabric.defaultFabricSubnets()) { |
| 246 | newInterceptFlowRules.add(generateInterceptFlowRule(true, device.id(), subnet.prefix())); |
| 247 | // check if this devices has the fabricSubnet, then add ip broadcast flue rule |
Jian Li | da0b485 | 2018-08-29 20:40:44 +0900 | [diff] [blame] | 248 | FabricNetwork fabricNetwork = simpleFabric.fabricNetwork(subnet.networkName()); |
Jian Li | e2d8751 | 2018-08-23 17:33:05 +0900 | [diff] [blame] | 249 | if (fabricNetwork != null && fabricNetwork.contains(device.id())) { |
| 250 | newInterceptFlowRules.add(generateLocalSubnetIpBctFlowRule(device.id(), subnet.prefix(), |
| 251 | fabricNetwork)); |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 252 | } |
| 253 | // JUST FOR FLOW RULE TEST ONLY |
| 254 | //newInterceptFlowRules.add(generateTestFlowRule(device.id(), subnet.ipPrefix())); |
| 255 | } |
Jian Li | e2d8751 | 2018-08-23 17:33:05 +0900 | [diff] [blame] | 256 | for (FabricRoute route : simpleFabric.fabricRoutes()) { |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 257 | newInterceptFlowRules.add(generateInterceptFlowRule(false, device.id(), route.prefix())); |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | if (!newInterceptFlowRules.equals(interceptFlowRules)) { |
| 262 | // NOTE: DO NOT REMOVE INTERCEPT FLOW RULES FOR FAILED DEVICE FLOW UPDATE MIGHT BE BLOCKED |
| 263 | /* |
| 264 | interceptFlowRules.stream() |
| 265 | .filter(rule -> !newInterceptFlowRules.contains(rule)) |
| 266 | .forEach(rule -> { |
| 267 | flowRuleService.removeFlowRules(rule); |
| 268 | log.info("simple fabric reactive routing remove intercept flow rule: {}", rule); |
| 269 | }); |
| 270 | */ |
| 271 | newInterceptFlowRules.stream() |
| 272 | .filter(rule -> !interceptFlowRules.contains(rule)) |
| 273 | .forEach(rule -> { |
| 274 | flowRuleService.applyFlowRules(rule); |
Jian Li | e2d8751 | 2018-08-23 17:33:05 +0900 | [diff] [blame] | 275 | log.info("simple fabric routing apply intercept flow rule: {}", rule); |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 276 | }); |
| 277 | interceptFlowRules = newInterceptFlowRules; |
| 278 | } |
| 279 | } |
| 280 | |
Lee Yongjae | 6dc7e4f | 2017-12-06 16:17:51 +0900 | [diff] [blame] | 281 | private FlowRule generateInterceptFlowRule(boolean isDstLocalSubnet, DeviceId deviceId, IpPrefix prefix) { |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 282 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
| 283 | if (prefix.isIp4()) { |
| 284 | selector.matchEthType(Ethernet.TYPE_IPV4); |
| 285 | if (prefix.prefixLength() > 0) { |
| 286 | selector.matchIPDst(prefix); |
| 287 | } |
| 288 | } else { |
| 289 | selector.matchEthType(Ethernet.TYPE_IPV6); |
| 290 | if (prefix.prefixLength() > 0) { |
| 291 | selector.matchIPv6Dst(prefix); |
| 292 | } |
| 293 | } |
| 294 | FlowRule rule = DefaultFlowRule.builder() |
| 295 | .forDevice(deviceId) |
Lee Yongjae | 6dc7e4f | 2017-12-06 16:17:51 +0900 | [diff] [blame] | 296 | .withPriority(reactivePriority(false, isDstLocalSubnet, prefix.prefixLength())) |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 297 | .withSelector(selector.build()) |
| 298 | .withTreatment(DefaultTrafficTreatment.builder().punt().build()) |
Jian Li | e2d8751 | 2018-08-23 17:33:05 +0900 | [diff] [blame] | 299 | .fromApp(appId) |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 300 | .makePermanent() |
| 301 | .forTable(0).build(); |
| 302 | return rule; |
| 303 | } |
| 304 | |
Jian Li | e2d8751 | 2018-08-23 17:33:05 +0900 | [diff] [blame] | 305 | private FlowRule generateLocalSubnetIpBctFlowRule(DeviceId deviceId, IpPrefix prefix, FabricNetwork fabricNetwork) { |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 306 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
| 307 | IpPrefix bctPrefix; |
| 308 | if (prefix.isIp4()) { |
| 309 | bctPrefix = Ip4Prefix.valueOf(prefix.getIp4Prefix().address().toInt() | |
| 310 | ~Ip4Address.makeMaskPrefix(prefix.prefixLength()).toInt(), |
| 311 | Ip4Address.BIT_LENGTH); |
| 312 | selector.matchEthType(Ethernet.TYPE_IPV4); |
| 313 | selector.matchIPDst(bctPrefix); |
| 314 | } else { |
| 315 | byte[] p = prefix.getIp6Prefix().address().toOctets(); |
| 316 | byte[] m = Ip6Address.makeMaskPrefix(prefix.prefixLength()).toOctets(); |
| 317 | for (int i = 0; i < p.length; i++) { |
| 318 | p[i] |= ~m[i]; |
| 319 | } |
| 320 | bctPrefix = Ip6Prefix.valueOf(p, Ip6Address.BIT_LENGTH); |
| 321 | selector.matchEthType(Ethernet.TYPE_IPV6); |
| 322 | selector.matchIPv6Dst(bctPrefix); |
| 323 | } |
| 324 | TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder(); |
| 325 | Set<ConnectPoint> newEgressPoints = new HashSet<>(); |
Jian Li | e2d8751 | 2018-08-23 17:33:05 +0900 | [diff] [blame] | 326 | for (Interface iface : fabricNetwork.interfaces()) { |
Lee Yongjae | 6dc7e4f | 2017-12-06 16:17:51 +0900 | [diff] [blame] | 327 | if (iface.connectPoint().deviceId().equals(deviceId)) { |
| 328 | treatment.setOutput(iface.connectPoint().port()); |
| 329 | } |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 330 | } |
| 331 | FlowRule rule = DefaultFlowRule.builder() |
| 332 | .forDevice(deviceId) |
| 333 | .withPriority(reactivePriority(true, true, bctPrefix.prefixLength())) |
| 334 | .withSelector(selector.build()) |
| 335 | .withTreatment(treatment.build()) |
Jian Li | e2d8751 | 2018-08-23 17:33:05 +0900 | [diff] [blame] | 336 | .fromApp(appId) |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 337 | .makePermanent() |
| 338 | .forTable(0).build(); |
| 339 | return rule; |
| 340 | } |
| 341 | |
| 342 | /** |
| 343 | * Refresh routes by examining network resource status. |
| 344 | */ |
| 345 | private void refreshRouteIntents() { |
| 346 | for (Intent entry : intentService.getIntents()) { |
Jian Li | e2d8751 | 2018-08-23 17:33:05 +0900 | [diff] [blame] | 347 | if (!appId.equals(entry.appId())) { |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 348 | continue; |
| 349 | } |
| 350 | |
| 351 | MultiPointToSinglePointIntent intent = (MultiPointToSinglePointIntent) entry; |
| 352 | |
| 353 | if (!intentService.isLocal(intent.key())) { |
| 354 | if (toBePurgedIntentKeys.contains(intent.key())) { |
| 355 | toBePurgedIntentKeys.remove(intent.key()); // clear non local intent |
| 356 | } |
| 357 | continue; |
| 358 | } |
| 359 | |
| 360 | try { |
| 361 | switch (intentService.getIntentState(intent.key())) { |
| 362 | //case FAILED: // failed intent is not auto removed |
| 363 | case WITHDRAWN: |
| 364 | log.warn("intent found failed or withdrawn; " |
| 365 | + "remove and try to purge intent: key={}", intent.key()); |
| 366 | // purge intents here without withdraw |
| 367 | intentService.purge(intentService.getIntent(intent.key())); |
| 368 | toBePurgedIntentKeys.add(intent.key()); |
| 369 | continue; |
| 370 | default: // no action |
| 371 | break; |
| 372 | } |
| 373 | } catch (Exception e) { |
| 374 | log.warn("intent status lookup failed: error={}", e); |
| 375 | continue; // this intent seems invalid; no action |
| 376 | } |
| 377 | |
| 378 | // dummy loop to break on remove cases |
| 379 | if (!deviceService.isAvailable(intent.egressPoint().deviceId())) { |
| 380 | log.info("refresh route intents; remove intent for no device: key={}", intent.key()); |
| 381 | intentService.withdraw(intentService.getIntent(intent.key())); |
| 382 | toBePurgedIntentKeys.add(intent.key()); |
| 383 | continue; |
| 384 | } |
Jian Li | e2d8751 | 2018-08-23 17:33:05 +0900 | [diff] [blame] | 385 | if (!(simpleFabric.fabricNetwork(intent.egressPoint(), VlanId.NONE) != null || |
Jian Li | 8df54a9 | 2018-08-23 17:01:31 +0900 | [diff] [blame] | 386 | (REACTIVE_ALLOW_LINK_CP && |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 387 | !linkService.getEgressLinks(intent.egressPoint()).isEmpty()))) { |
| 388 | log.info("refresh route intents; remove intent for egress point not available: key={}", intent.key()); |
| 389 | intentService.withdraw(intentService.getIntent(intent.key())); |
| 390 | toBePurgedIntentKeys.add(intent.key()); |
| 391 | continue; |
| 392 | } |
| 393 | |
| 394 | // MAY NEED TO CHECK: intent.egressPoint and intent.treatment's dstMac is valid against hosts |
Jian Li | 8df54a9 | 2018-08-23 17:01:31 +0900 | [diff] [blame] | 395 | if (REACTIVE_SINGLE_TO_SINGLE && !REACTIVE_ALLOW_LINK_CP) { |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 396 | // single path intent only; no need to check ingress points |
| 397 | continue; |
| 398 | } |
| 399 | |
Ray Milkey | 27cff8c | 2018-03-05 14:58:26 -0800 | [diff] [blame] | 400 | Set<FilteredConnectPoint> newIngressPoints = new HashSet<>(); |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 401 | boolean ingressPointChanged = false; |
Ray Milkey | 27cff8c | 2018-03-05 14:58:26 -0800 | [diff] [blame] | 402 | for (FilteredConnectPoint cp : intent.filteredIngressPoints()) { |
| 403 | if (deviceService.isAvailable(cp.connectPoint().deviceId()) && |
Jian Li | e2d8751 | 2018-08-23 17:33:05 +0900 | [diff] [blame] | 404 | (simpleFabric.fabricNetwork(cp.connectPoint(), VlanId.NONE) != null || |
Jian Li | 8df54a9 | 2018-08-23 17:01:31 +0900 | [diff] [blame] | 405 | (REACTIVE_ALLOW_LINK_CP && |
Ray Milkey | 27cff8c | 2018-03-05 14:58:26 -0800 | [diff] [blame] | 406 | !linkService.getIngressLinks(cp.connectPoint()).isEmpty()))) { |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 407 | newIngressPoints.add(cp); |
| 408 | } else { |
| 409 | log.info("refresh route ingress cp of " |
| 410 | + "not in 2Networks nor links: {}", cp); |
| 411 | ingressPointChanged = true; |
| 412 | } |
| 413 | } |
| 414 | if (newIngressPoints.isEmpty()) { |
| 415 | log.info("refresh route intents; " |
| 416 | + "remove intent for no ingress nor egress point available: key={}", intent.key()); |
| 417 | intentService.withdraw(intentService.getIntent(intent.key())); |
| 418 | toBePurgedIntentKeys.add(intent.key()); |
| 419 | continue; |
| 420 | } |
| 421 | // update ingress points |
| 422 | if (ingressPointChanged) { |
| 423 | MultiPointToSinglePointIntent updatedIntent = |
| 424 | MultiPointToSinglePointIntent.builder() |
Jian Li | e2d8751 | 2018-08-23 17:33:05 +0900 | [diff] [blame] | 425 | .appId(appId) |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 426 | .key(intent.key()) |
| 427 | .selector(intent.selector()) |
| 428 | .treatment(intent.treatment()) |
Ray Milkey | 27cff8c | 2018-03-05 14:58:26 -0800 | [diff] [blame] | 429 | .filteredIngressPoints(newIngressPoints) |
| 430 | .filteredEgressPoint(intent.filteredEgressPoint()) |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 431 | .priority(intent.priority()) |
| 432 | .constraints(intent.constraints()) |
| 433 | .build(); |
| 434 | log.info("refresh route update intent: key={} updatedIntent={}", |
| 435 | intent.key(), updatedIntent); |
| 436 | toBePurgedIntentKeys.remove(intent.key()); // may remove from old purged entry |
| 437 | intentService.submit(updatedIntent); |
| 438 | } |
| 439 | } |
| 440 | } |
| 441 | |
| 442 | private void checkIntentsPurge() { |
| 443 | // check intents to be purge |
| 444 | if (!toBePurgedIntentKeys.isEmpty()) { |
| 445 | Set<Key> removeKeys = new HashSet<>(); |
| 446 | for (Key key : toBePurgedIntentKeys) { |
| 447 | if (!intentService.isLocal(key)) { |
| 448 | removeKeys.add(key); |
| 449 | continue; |
| 450 | } |
| 451 | Intent intentToPurge = intentService.getIntent(key); |
| 452 | if (intentToPurge == null) { |
| 453 | log.info("purged intent: key={}", key); |
| 454 | removeKeys.add(key); |
| 455 | } else { |
| 456 | switch (intentService.getIntentState(key)) { |
| 457 | // case FAILED: // not auto removed |
| 458 | case WITHDRAWN: |
| 459 | log.info("try to purge intent: key={}", key); |
| 460 | intentService.purge(intentToPurge); |
| 461 | break; |
| 462 | case INSTALL_REQ: |
| 463 | case INSTALLED: |
| 464 | case INSTALLING: |
| 465 | case RECOMPILING: |
| 466 | case COMPILING: |
| 467 | log.warn("not to purge for active intent: key={}", key); |
| 468 | removeKeys.add(key); |
| 469 | break; |
| 470 | case WITHDRAW_REQ: |
| 471 | case WITHDRAWING: |
| 472 | case PURGE_REQ: |
| 473 | case CORRUPT: |
| 474 | default: |
| 475 | // no action |
| 476 | break; |
| 477 | } |
| 478 | } |
| 479 | } |
| 480 | toBePurgedIntentKeys.removeAll(removeKeys); |
| 481 | } |
| 482 | } |
| 483 | |
| 484 | public void withdrawAllReactiveIntents() { |
| 485 | // check all intents of this app |
| 486 | // NOTE: cli calls are handling within the cli called node only; so should not user inents.isLocal() |
| 487 | Set<Intent> myIntents = new HashSet<>(); |
| 488 | for (Intent intent : intentService.getIntents()) { |
Jian Li | e2d8751 | 2018-08-23 17:33:05 +0900 | [diff] [blame] | 489 | if (appId.equals(intent.appId())) { |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 490 | myIntents.add(intent); |
| 491 | } |
| 492 | } |
| 493 | // withdraw all my intents |
| 494 | for (Intent intent : myIntents) { |
| 495 | switch (intentService.getIntentState(intent.key())) { |
| 496 | case FAILED: |
| 497 | intentService.purge(intent); |
| 498 | toBePurgedIntentKeys.add(intent.key()); |
| 499 | break; |
| 500 | case WITHDRAWN: |
| 501 | intentService.purge(intent); |
| 502 | toBePurgedIntentKeys.add(intent.key()); |
| 503 | break; |
| 504 | case INSTALL_REQ: |
| 505 | case INSTALLED: |
| 506 | case INSTALLING: |
| 507 | case RECOMPILING: |
| 508 | case COMPILING: |
| 509 | intentService.withdraw(intent); |
| 510 | toBePurgedIntentKeys.add(intent.key()); |
| 511 | break; |
| 512 | case WITHDRAW_REQ: |
| 513 | case WITHDRAWING: |
| 514 | toBePurgedIntentKeys.add(intent.key()); |
| 515 | break; |
| 516 | case PURGE_REQ: |
| 517 | case CORRUPT: |
| 518 | default: |
| 519 | // no action |
| 520 | break; |
| 521 | } |
| 522 | } |
| 523 | } |
| 524 | |
| 525 | /** |
Jian Li | e2d8751 | 2018-08-23 17:33:05 +0900 | [diff] [blame] | 526 | * Internal Packet Handling. |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 527 | */ |
Jian Li | e2d8751 | 2018-08-23 17:33:05 +0900 | [diff] [blame] | 528 | private class InternalRoutingProcessor implements PacketProcessor { |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 529 | @Override |
| 530 | public void process(PacketContext context) { |
| 531 | InboundPacket pkt = context.inPacket(); |
| 532 | Ethernet ethPkt = pkt.parsed(); |
| 533 | if (ethPkt == null) { |
| 534 | return; |
| 535 | } |
| 536 | ConnectPoint srcCp = pkt.receivedFrom(); |
| 537 | IpAddress srcIp; |
| 538 | IpAddress dstIp; |
| 539 | byte ipProto = 0; /* 0 or tcp, udp */ |
| 540 | |
| 541 | switch (EthType.EtherType.lookup(ethPkt.getEtherType())) { |
| 542 | case IPV4: |
| 543 | IPv4 ipv4Packet = (IPv4) ethPkt.getPayload(); |
| 544 | srcIp = IpAddress.valueOf(ipv4Packet.getSourceAddress()); |
| 545 | dstIp = IpAddress.valueOf(ipv4Packet.getDestinationAddress()); |
| 546 | ipProto = ipv4Packet.getProtocol(); |
| 547 | break; |
| 548 | case IPV6: |
| 549 | IPv6 ipv6Packet = (IPv6) ethPkt.getPayload(); |
| 550 | srcIp = IpAddress.valueOf(IpAddress.Version.INET6, ipv6Packet.getSourceAddress()); |
| 551 | dstIp = IpAddress.valueOf(IpAddress.Version.INET6, ipv6Packet.getDestinationAddress()); |
| 552 | ipProto = ipv6Packet.getNextHeader(); |
| 553 | break; |
| 554 | default: |
| 555 | return; // ignore unknow ether type packets |
| 556 | } |
| 557 | if (ipProto != 6 && ipProto != 17) { |
| 558 | ipProto = 0; /* handle special for TCP and UDP only */ |
| 559 | } |
| 560 | |
| 561 | if (!checkVirtualGatewayIpPacket(pkt, srcIp, dstIp)) { |
| 562 | ipPacketReactiveProcessor(context, ethPkt, srcCp, srcIp, dstIp, ipProto); |
| 563 | // TODO: add ReactiveRouting for dstIp to srcIp with discovered egressCp as srcCp |
| 564 | } |
| 565 | } |
| 566 | } |
| 567 | |
| 568 | /** |
| 569 | * handle Packet with dstIp=virtualGatewayIpAddresses. |
| 570 | * returns true(handled) or false(not for virtual gateway) |
| 571 | */ |
| 572 | private boolean checkVirtualGatewayIpPacket(InboundPacket pkt, IpAddress srcIp, IpAddress dstIp) { |
| 573 | Ethernet ethPkt = pkt.parsed(); // assume valid |
| 574 | |
Jian Li | e2d8751 | 2018-08-23 17:33:05 +0900 | [diff] [blame] | 575 | MacAddress mac = simpleFabric.vMacForIp(dstIp); |
| 576 | if (mac == null || !simpleFabric.isVirtualGatewayMac(ethPkt.getDestinationMAC())) { |
Lee Yongjae | 6dc7e4f | 2017-12-06 16:17:51 +0900 | [diff] [blame] | 577 | /* Destination MAC should be any of virtual gateway macs */ |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 578 | return false; |
| 579 | } else if (dstIp.isIp4()) { |
| 580 | IPv4 ipv4Packet = (IPv4) ethPkt.getPayload(); |
| 581 | if (ipv4Packet.getProtocol() == IPv4.PROTOCOL_ICMP) { |
| 582 | ICMP icmpPacket = (ICMP) ipv4Packet.getPayload(); |
| 583 | |
| 584 | if (icmpPacket.getIcmpType() == ICMP.TYPE_ECHO_REQUEST) { |
| 585 | log.info("IPV4 ICMP ECHO request to virtual gateway: " |
| 586 | + "srcIp={} dstIp={} proto={}", srcIp, dstIp, ipv4Packet.getProtocol()); |
| 587 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 588 | .setOutput(pkt.receivedFrom().port()).build(); |
| 589 | OutboundPacket packet = |
| 590 | new DefaultOutboundPacket(pkt.receivedFrom().deviceId(), treatment, |
| 591 | ByteBuffer.wrap(icmpPacket.buildIcmpReply(pkt.parsed()).serialize())); |
| 592 | packetService.emit(packet); |
| 593 | return true; |
| 594 | } |
| 595 | } |
| 596 | log.warn("IPV4 packet to virtual gateway dropped: " |
| 597 | + "srcIp={} dstIp={} proto={}", srcIp, dstIp, ipv4Packet.getProtocol()); |
| 598 | return true; |
| 599 | |
| 600 | } else if (dstIp.isIp6()) { |
| 601 | // TODO: not tested yet (2017-07-20) |
| 602 | IPv6 ipv6Packet = (IPv6) ethPkt.getPayload(); |
| 603 | if (ipv6Packet.getNextHeader() == IPv6.PROTOCOL_ICMP6) { |
| 604 | ICMP6 icmp6Packet = (ICMP6) ipv6Packet.getPayload(); |
| 605 | |
| 606 | if (icmp6Packet.getIcmpType() == ICMP6.ECHO_REQUEST) { |
| 607 | log.info("IPV6 ICMP6 ECHO request to virtual gateway: srcIp={} dstIp={} nextHeader={}", |
| 608 | srcIp, dstIp, ipv6Packet.getNextHeader()); |
| 609 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 610 | .setOutput(pkt.receivedFrom().port()).build(); |
| 611 | OutboundPacket packet = |
| 612 | new DefaultOutboundPacket(pkt.receivedFrom().deviceId(), treatment, |
| 613 | ByteBuffer.wrap(icmp6Packet.buildIcmp6Reply(pkt.parsed()).serialize())); |
| 614 | packetService.emit(packet); |
| 615 | return true; |
| 616 | } |
| 617 | } |
| 618 | log.warn("IPV6 packet to virtual gateway dropped: srcIp={} dstIp={} nextHeader={}", |
| 619 | srcIp, dstIp, ipv6Packet.getNextHeader()); |
| 620 | return true; |
| 621 | |
| 622 | } |
| 623 | return false; // unknown traffic |
| 624 | } |
| 625 | |
| 626 | /** |
| 627 | * Routes packet reactively. |
| 628 | */ |
| 629 | private void ipPacketReactiveProcessor(PacketContext context, Ethernet ethPkt, ConnectPoint srcCp, |
| 630 | IpAddress srcIp, IpAddress dstIp, byte ipProto) { |
| 631 | /* check reactive handling and forward packet */ |
| 632 | log.trace("ip packet: srcCp={} srcIp={} dstIp={} ipProto={}", |
| 633 | srcCp, srcIp, dstIp, ipProto); |
| 634 | |
| 635 | EncapsulationType encap = EncapsulationType.NONE; |
| 636 | |
| 637 | // prefix and nextHop for local Subnet |
| 638 | IpPrefix srcPrefix = srcIp.toIpPrefix(); |
| 639 | IpPrefix dstPrefix = dstIp.toIpPrefix(); |
| 640 | IpAddress srcNextHop = srcIp; |
| 641 | IpAddress dstNextHop = dstIp; |
| 642 | MacAddress treatmentSrcMac = ethPkt.getDestinationMAC(); |
| 643 | int borderRoutePrefixLength = 0; |
Jian Li | e2d8751 | 2018-08-23 17:33:05 +0900 | [diff] [blame] | 644 | boolean updateMac = simpleFabric.isVirtualGatewayMac(ethPkt.getDestinationMAC()); |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 645 | |
| 646 | // check subnet local or route |
Jian Li | e2d8751 | 2018-08-23 17:33:05 +0900 | [diff] [blame] | 647 | FabricSubnet srcSubnet = simpleFabric.fabricSubnet(srcIp); |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 648 | if (srcSubnet == null) { |
Jian Li | e2d8751 | 2018-08-23 17:33:05 +0900 | [diff] [blame] | 649 | FabricRoute route = simpleFabric.fabricRoute(srcIp); |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 650 | if (route == null) { |
Lee Yongjae | 6dc7e4f | 2017-12-06 16:17:51 +0900 | [diff] [blame] | 651 | log.warn("unknown srcIp; drop: srcCp={} srcIp={} dstIp={} ipProto={}", |
| 652 | srcCp, srcIp, dstIp, ipProto); |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 653 | return; |
| 654 | } |
| 655 | srcPrefix = route.prefix(); |
| 656 | srcNextHop = route.nextHop(); |
| 657 | borderRoutePrefixLength = route.prefix().prefixLength(); |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 658 | } |
Jian Li | e2d8751 | 2018-08-23 17:33:05 +0900 | [diff] [blame] | 659 | FabricSubnet dstSubnet = simpleFabric.fabricSubnet(dstIp); |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 660 | if (dstSubnet == null) { |
Jian Li | e2d8751 | 2018-08-23 17:33:05 +0900 | [diff] [blame] | 661 | FabricRoute route = simpleFabric.fabricRoute(dstIp); |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 662 | if (route == null) { |
Lee Yongjae | 6dc7e4f | 2017-12-06 16:17:51 +0900 | [diff] [blame] | 663 | log.warn("unknown dstIp; drop: srcCp={} srcIp={} dstIp={} ipProto={}", |
| 664 | srcCp, srcIp, dstIp, ipProto); |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 665 | return; |
| 666 | } |
| 667 | dstPrefix = route.prefix(); |
| 668 | dstNextHop = route.nextHop(); |
| 669 | borderRoutePrefixLength = route.prefix().prefixLength(); |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 670 | } |
| 671 | |
| 672 | if (dstSubnet != null) { |
| 673 | // destination is local subnet ip |
Jian Li | 8df54a9 | 2018-08-23 17:01:31 +0900 | [diff] [blame] | 674 | if (ALLOW_ETH_ADDRESS_SELECTOR && dstSubnet.equals(srcSubnet)) { |
Jian Li | e2d8751 | 2018-08-23 17:33:05 +0900 | [diff] [blame] | 675 | // NOTE: if ALLOW_ETH_ADDRESS_SELECTOR=false; isForward is always false |
Jian Li | da0b485 | 2018-08-29 20:40:44 +0900 | [diff] [blame] | 676 | FabricNetwork fabricNetwork = simpleFabric.fabricNetwork(dstSubnet.networkName()); |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 677 | treatmentSrcMac = ethPkt.getSourceMAC(); |
Jian Li | e2d8751 | 2018-08-23 17:33:05 +0900 | [diff] [blame] | 678 | if (fabricNetwork != null && fabricNetwork.isForward()) { |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 679 | // NOTE: no reactive route action but do forward packet for L2Forward do not handle packet |
| 680 | // update mac only if dstMac is virtualGatewayMac, else assume valid mac already for the l2 network |
| 681 | log.info("LOCAL FORWARD ONLY: " |
| 682 | + "srcCp={} srcIp={} dstIp={} srcMac={} dstMac={} vlanId={} ipProto={} updateMac={}", |
| 683 | context.inPacket().receivedFrom(), |
| 684 | srcIp, dstIp, ethPkt.getSourceMAC(), ethPkt.getDestinationMAC(), |
| 685 | ethPkt.getVlanID(), ipProto, updateMac); |
| 686 | forwardPacketToDstIp(context, dstIp, treatmentSrcMac, updateMac); |
| 687 | return; |
| 688 | } |
| 689 | } |
| 690 | encap = dstSubnet.encapsulation(); |
| 691 | if (encap == EncapsulationType.NONE && srcSubnet != null) { |
| 692 | encap = srcSubnet.encapsulation(); |
| 693 | } |
| 694 | } else { |
| 695 | // destination is external network |
| 696 | if (srcSubnet == null) { |
| 697 | // both are externel network |
| 698 | log.warn("INVALID PACKET: srcIp and dstIp are both NON-LOCAL: " |
| 699 | + "srcCP={} srcIp={} dstIp={} srcMac={} dstMac={} vlanId={} ipProto={} updateMac={}", |
| 700 | context.inPacket().receivedFrom(), |
| 701 | srcIp, dstIp, ethPkt.getSourceMAC(), ethPkt.getDestinationMAC(), |
| 702 | ethPkt.getVlanID(), ipProto, updateMac); |
| 703 | return; |
| 704 | } |
| 705 | encap = srcSubnet.encapsulation(); |
| 706 | } |
| 707 | |
| 708 | log.info("REGI AND FORWARD: " |
| 709 | + "srcCP={} srcIp={} dstIp={} srcMac={} dstMac={} vlanId={} ipProto={} updateMac={}", |
| 710 | context.inPacket().receivedFrom(), |
| 711 | srcIp, dstIp, ethPkt.getSourceMAC(), ethPkt.getDestinationMAC(), |
| 712 | ethPkt.getVlanID(), ipProto, updateMac); |
| 713 | setUpConnectivity(srcCp, ipProto, srcPrefix, dstPrefix, dstNextHop, treatmentSrcMac, encap, updateMac, |
Lee Yongjae | 6dc7e4f | 2017-12-06 16:17:51 +0900 | [diff] [blame] | 714 | dstSubnet != null, borderRoutePrefixLength); |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 715 | forwardPacketToDstIp(context, dstNextHop, treatmentSrcMac, updateMac); |
| 716 | } |
| 717 | |
| 718 | /** |
| 719 | * Emits the specified packet onto the network. |
| 720 | */ |
| 721 | private void forwardPacketToDstIp(PacketContext context, IpAddress nextHopIp, |
| 722 | MacAddress srcMac, boolean updateMac) { |
| 723 | Set<Host> hosts = hostService.getHostsByIp(nextHopIp); |
| 724 | Host dstHost; |
| 725 | if (!hosts.isEmpty()) { |
| 726 | dstHost = hosts.iterator().next(); |
| 727 | } else { |
| 728 | // NOTE: hostService.requestMac(nextHopIp); NOT IMPLEMENTED in ONOS HostManager.java; do it myself |
| 729 | log.warn("forward packet nextHopIp host_mac unknown: nextHopIp={}", nextHopIp); |
| 730 | hostService.startMonitoringIp(nextHopIp); |
| 731 | simpleFabric.requestMac(nextHopIp); |
Jian Li | e2d8751 | 2018-08-23 17:33:05 +0900 | [diff] [blame] | 732 | // CONSIDER: make flood on all port of the dstHost's DefaultFabricNetwork |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 733 | return; |
| 734 | } |
| 735 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 736 | .setOutput(dstHost.location().port()).build(); |
| 737 | OutboundPacket outPacket; |
| 738 | if (updateMac) { |
| 739 | // NOTE: eth address update by treatment is NOT applied, so update mac myself |
| 740 | outPacket = new DefaultOutboundPacket(dstHost.location().deviceId(), treatment, |
| 741 | ByteBuffer.wrap(context.inPacket().parsed() |
| 742 | .setSourceMACAddress(srcMac) |
| 743 | .setDestinationMACAddress(dstHost.mac()).serialize())); |
| 744 | } else { |
| 745 | outPacket = new DefaultOutboundPacket(dstHost.location().deviceId(), treatment, |
| 746 | context.inPacket().unparsed()); |
| 747 | } |
| 748 | // be quiet on normal situation |
| 749 | log.info("forward packet: nextHopIP={} srcCP={} dstCP={}", |
| 750 | nextHopIp, context.inPacket().receivedFrom(), dstHost.location()); |
| 751 | packetService.emit(outPacket); |
| 752 | } |
| 753 | |
| 754 | /** |
| 755 | * Update intents for connectivity. |
| 756 | * |
| 757 | * ToHost: dstPrefix = dstHostIp.toIpPrefix(), nextHopIp = destHostIp |
| 758 | * ToInternet: dstPrefix = route.prefix(), nextHopIp = route.nextHopIp |
Jian Li | e2d8751 | 2018-08-23 17:33:05 +0900 | [diff] [blame] | 759 | * returns intent submitted or not |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 760 | */ |
| 761 | private boolean setUpConnectivity(ConnectPoint srcCp, byte ipProto, IpPrefix srcPrefix, IpPrefix dstPrefix, |
| 762 | IpAddress nextHopIp, MacAddress treatmentSrcMac, |
| 763 | EncapsulationType encap, boolean updateMac, |
Lee Yongjae | 6dc7e4f | 2017-12-06 16:17:51 +0900 | [diff] [blame] | 764 | boolean isDstLocalSubnet, int borderRoutePrefixLength) { |
Jian Li | e2d8751 | 2018-08-23 17:33:05 +0900 | [diff] [blame] | 765 | if (!(simpleFabric.fabricNetwork(srcCp, VlanId.NONE) != null || |
Jian Li | 8df54a9 | 2018-08-23 17:01:31 +0900 | [diff] [blame] | 766 | (REACTIVE_ALLOW_LINK_CP && !linkService.getIngressLinks(srcCp).isEmpty()))) { |
Jian Li | e2d8751 | 2018-08-23 17:33:05 +0900 | [diff] [blame] | 767 | log.warn("NO REGI for srcCp not in DefaultFabricNetwork; srcCp={} srcPrefix={} dstPrefix={} nextHopIp={}", |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 768 | srcCp, srcPrefix, dstPrefix, nextHopIp); |
| 769 | return false; |
| 770 | } |
| 771 | |
| 772 | MacAddress nextHopMac = null; |
| 773 | ConnectPoint egressPoint = null; |
| 774 | for (Host host : hostService.getHostsByIp(nextHopIp)) { |
| 775 | if (host.mac() != null) { |
| 776 | nextHopMac = host.mac(); |
| 777 | egressPoint = host.location(); |
| 778 | break; |
| 779 | } |
| 780 | } |
| 781 | if (nextHopMac == null || egressPoint == null) { |
| 782 | log.info("NO REGI for unknown nextHop Cp and Mac: srcPrefix={} dstPrefix={} nextHopIp={}", |
| 783 | srcPrefix, dstPrefix, nextHopIp); |
| 784 | hostService.startMonitoringIp(nextHopIp); |
| 785 | simpleFabric.requestMac(nextHopIp); |
| 786 | return false; |
| 787 | } |
| 788 | TrafficTreatment treatment; |
Jian Li | 8df54a9 | 2018-08-23 17:01:31 +0900 | [diff] [blame] | 789 | if (updateMac && ALLOW_ETH_ADDRESS_SELECTOR) { |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 790 | treatment = generateSetMacTreatment(nextHopMac, treatmentSrcMac); |
| 791 | } else { |
| 792 | treatment = DefaultTrafficTreatment.builder().build(); |
| 793 | } |
| 794 | |
| 795 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
| 796 | if (dstPrefix.isIp4()) { |
| 797 | selector.matchEthType(Ethernet.TYPE_IPV4); |
Jian Li | 8df54a9 | 2018-08-23 17:01:31 +0900 | [diff] [blame] | 798 | if (REACTIVE_SINGLE_TO_SINGLE && srcPrefix.prefixLength() > 0) { |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 799 | selector.matchIPSrc(srcPrefix); |
| 800 | } |
| 801 | if (dstPrefix.prefixLength() > 0) { |
| 802 | selector.matchIPDst(dstPrefix); |
| 803 | } |
Jian Li | 8df54a9 | 2018-08-23 17:01:31 +0900 | [diff] [blame] | 804 | if (ipProto != 0 && REACTIVE_MATCH_IP_PROTO) { |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 805 | selector.matchIPProtocol(ipProto); |
| 806 | } |
| 807 | } else { |
| 808 | selector.matchEthType(Ethernet.TYPE_IPV6); |
Jian Li | 8df54a9 | 2018-08-23 17:01:31 +0900 | [diff] [blame] | 809 | if (REACTIVE_SINGLE_TO_SINGLE && srcPrefix.prefixLength() > 0) { |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 810 | selector.matchIPv6Src(srcPrefix); |
| 811 | } |
| 812 | if (dstPrefix.prefixLength() > 0) { |
| 813 | selector.matchIPv6Dst(dstPrefix); |
| 814 | } |
Jian Li | 8df54a9 | 2018-08-23 17:01:31 +0900 | [diff] [blame] | 815 | if (ipProto != 0 && REACTIVE_MATCH_IP_PROTO) { |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 816 | selector.matchIPProtocol(ipProto); |
| 817 | } |
| 818 | } |
| 819 | |
Lee Yongjae | 6dc7e4f | 2017-12-06 16:17:51 +0900 | [diff] [blame] | 820 | Key key; |
| 821 | String keyProtoTag = ""; |
Jian Li | 8df54a9 | 2018-08-23 17:01:31 +0900 | [diff] [blame] | 822 | if (REACTIVE_MATCH_IP_PROTO) { |
Lee Yongjae | 6dc7e4f | 2017-12-06 16:17:51 +0900 | [diff] [blame] | 823 | keyProtoTag = "-p" + ipProto; |
| 824 | } |
Jian Li | 8df54a9 | 2018-08-23 17:01:31 +0900 | [diff] [blame] | 825 | if (REACTIVE_SINGLE_TO_SINGLE) { |
Lee Yongjae | 6dc7e4f | 2017-12-06 16:17:51 +0900 | [diff] [blame] | 826 | // allocate intent per (srcPrefix, dstPrefix) |
Jian Li | e2d8751 | 2018-08-23 17:33:05 +0900 | [diff] [blame] | 827 | key = Key.of(srcPrefix.toString() + "-to-" + dstPrefix.toString() + keyProtoTag, appId); |
Lee Yongjae | 6dc7e4f | 2017-12-06 16:17:51 +0900 | [diff] [blame] | 828 | } else { |
| 829 | // allocate intent per (srcDeviceId, dstPrefix) |
Jian Li | e2d8751 | 2018-08-23 17:33:05 +0900 | [diff] [blame] | 830 | key = Key.of(srcCp.deviceId().toString() + "-to-" + dstPrefix.toString() + keyProtoTag, appId); |
Lee Yongjae | 6dc7e4f | 2017-12-06 16:17:51 +0900 | [diff] [blame] | 831 | } |
| 832 | |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 833 | // check and merge already existing ingress points |
Ray Milkey | 27cff8c | 2018-03-05 14:58:26 -0800 | [diff] [blame] | 834 | Set<FilteredConnectPoint> ingressPoints = new HashSet<>(); |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 835 | MultiPointToSinglePointIntent existingIntent = (MultiPointToSinglePointIntent) intentService.getIntent(key); |
| 836 | if (existingIntent != null) { |
Ray Milkey | 27cff8c | 2018-03-05 14:58:26 -0800 | [diff] [blame] | 837 | ingressPoints.addAll(existingIntent.filteredIngressPoints()); |
| 838 | if (!ingressPoints.add(new FilteredConnectPoint(srcCp)) // alread exists and dst not changed |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 839 | && egressPoint.equals(existingIntent.egressPoint()) |
| 840 | && treatment.equals(existingIntent.treatment())) { |
| 841 | log.warn("srcCP is already in mp2p intent: srcPrefix={} dstPrefix={} srcCp={}", |
| 842 | srcPrefix, dstPrefix, srcCp); |
| 843 | return false; |
| 844 | } |
| 845 | log.info("update mp2p intent: srcPrefix={} dstPrefix={} srcCp={}", |
| 846 | srcPrefix, dstPrefix, srcCp); |
| 847 | } else { |
| 848 | log.info("create mp2p intent: srcPrefix={} dstPrefix={} srcCp={}", |
| 849 | srcPrefix, dstPrefix, srcCp); |
Ray Milkey | 27cff8c | 2018-03-05 14:58:26 -0800 | [diff] [blame] | 850 | ingressPoints.add(new FilteredConnectPoint(srcCp)); |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 851 | } |
| 852 | |
| 853 | // priority for forwarding case |
Lee Yongjae | 6dc7e4f | 2017-12-06 16:17:51 +0900 | [diff] [blame] | 854 | int priority = reactivePriority(true, isDstLocalSubnet, borderRoutePrefixLength); |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 855 | |
| 856 | MultiPointToSinglePointIntent newIntent = MultiPointToSinglePointIntent.builder() |
| 857 | .key(key) |
Jian Li | e2d8751 | 2018-08-23 17:33:05 +0900 | [diff] [blame] | 858 | .appId(appId) |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 859 | .selector(selector.build()) |
| 860 | .treatment(treatment) |
Ray Milkey | 27cff8c | 2018-03-05 14:58:26 -0800 | [diff] [blame] | 861 | .filteredIngressPoints(ingressPoints) |
| 862 | .filteredEgressPoint(new FilteredConnectPoint(egressPoint)) |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 863 | .priority(priority) |
| 864 | .constraints(buildConstraints(reactiveConstraints, encap)) |
| 865 | .build(); |
Jian Li | e2d8751 | 2018-08-23 17:33:05 +0900 | [diff] [blame] | 866 | log.info("submit mp2p intent: srcPrefix={} dstPrefix={} srcCp={} " |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 867 | + "newIntent={} nextHopIp={} nextHopMac={} priority={}", |
| 868 | srcPrefix, dstPrefix, ingressPoints, newIntent, nextHopIp, nextHopMac, priority); |
| 869 | toBePurgedIntentKeys.remove(newIntent.key()); |
| 870 | intentService.submit(newIntent); |
| 871 | return true; |
| 872 | } |
| 873 | |
| 874 | // generate treatment to target |
| 875 | private TrafficTreatment generateSetMacTreatment(MacAddress dstMac, MacAddress srcMac) { |
| 876 | return DefaultTrafficTreatment.builder() |
| 877 | // NOTE: Cisco Switch requires both src and dst mac set |
| 878 | .setEthDst(dstMac) |
| 879 | .setEthSrc(srcMac) |
| 880 | .build(); |
| 881 | } |
| 882 | |
| 883 | // monitor border peers for routeService lookup to be effective |
| 884 | private void monitorBorderPeers() { |
Jian Li | e2d8751 | 2018-08-23 17:33:05 +0900 | [diff] [blame] | 885 | for (FabricRoute route : simpleFabric.fabricRoutes()) { |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 886 | hostService.startMonitoringIp(route.nextHop()); |
| 887 | simpleFabric.requestMac(route.nextHop()); |
| 888 | } |
| 889 | } |
| 890 | |
| 891 | // priority calculator |
Lee Yongjae | 6dc7e4f | 2017-12-06 16:17:51 +0900 | [diff] [blame] | 892 | private int reactivePriority(boolean isForward, boolean isDstLocalSubnet, int borderRoutePrefixLength) { |
| 893 | if (isDstLocalSubnet) { // -> dst:localSubnet |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 894 | if (isForward) { |
Jian Li | 8df54a9 | 2018-08-23 17:01:31 +0900 | [diff] [blame] | 895 | return PRI_REACTIVE_LOCAL_FORWARD; |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 896 | } else { // isInterncept |
Jian Li | 8df54a9 | 2018-08-23 17:01:31 +0900 | [diff] [blame] | 897 | return PRI_REACTIVE_LOCAL_INTERCEPT; |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 898 | } |
Lee Yongjae | 6dc7e4f | 2017-12-06 16:17:51 +0900 | [diff] [blame] | 899 | } else { // -> dst:boarderRouteNextHop |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 900 | int offset; |
| 901 | if (isForward) { |
Jian Li | 8df54a9 | 2018-08-23 17:01:31 +0900 | [diff] [blame] | 902 | offset = PRI_REACTIVE_BORDER_FORWARD; |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 903 | } else { // isIntercept |
Jian Li | 8df54a9 | 2018-08-23 17:01:31 +0900 | [diff] [blame] | 904 | offset = PRI_REACTIVE_BORDER_INTERCEPT; |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 905 | } |
Jian Li | 8df54a9 | 2018-08-23 17:01:31 +0900 | [diff] [blame] | 906 | return PRI_REACTIVE_BORDER_BASE |
| 907 | + borderRoutePrefixLength * PRI_REACTIVE_BORDER_STEP + offset; |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 908 | } |
| 909 | } |
| 910 | |
| 911 | // constraints generator |
| 912 | private List<Constraint> buildConstraints(List<Constraint> constraints, EncapsulationType encap) { |
| 913 | if (!encap.equals(EncapsulationType.NONE)) { |
| 914 | List<Constraint> newConstraints = new ArrayList<>(constraints); |
| 915 | constraints.stream() |
| 916 | .filter(c -> c instanceof EncapsulationConstraint) |
| 917 | .forEach(newConstraints::remove); |
| 918 | newConstraints.add(new EncapsulationConstraint(encap)); |
| 919 | return ImmutableList.copyOf(newConstraints); |
| 920 | } |
| 921 | return constraints; |
| 922 | } |
| 923 | |
| 924 | // Dump Cli Handler |
| 925 | private void dump(String subject, PrintStream out) { |
Ray Milkey | 2ff6716 | 2018-01-22 10:14:19 -0800 | [diff] [blame] | 926 | if ("intents".equals(subject)) { |
Jian Li | e2d8751 | 2018-08-23 17:33:05 +0900 | [diff] [blame] | 927 | out.println("Routing Route Intents:\n"); |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 928 | for (Intent entry : intentService.getIntents()) { |
Jian Li | e2d8751 | 2018-08-23 17:33:05 +0900 | [diff] [blame] | 929 | if (appId.equals(entry.appId())) { |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 930 | MultiPointToSinglePointIntent intent = (MultiPointToSinglePointIntent) entry; |
| 931 | out.println(" " + intent.key().toString() |
| 932 | + " to " + intent.egressPoint().toString() |
| 933 | + " set " + intent.treatment().immediate().toString() |
| 934 | + " from " + intent.ingressPoints().toString()); |
| 935 | } |
| 936 | } |
| 937 | out.println(""); |
| 938 | |
Jian Li | e2d8751 | 2018-08-23 17:33:05 +0900 | [diff] [blame] | 939 | out.println("Routing Intercept Flow Rules:\n"); |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 940 | List<FlowRule> rules = new ArrayList(interceptFlowRules); |
| 941 | Collections.sort(rules, new Comparator<FlowRule>() { |
| 942 | @Override |
| 943 | public int compare(FlowRule a, FlowRule b) { |
| 944 | int r = a.deviceId().toString().compareTo(b.deviceId().toString()); |
| 945 | return (r != 0) ? r : Integer.compare(b.priority(), a.priority()); // descending on priority |
| 946 | } |
| 947 | }); |
| 948 | for (FlowRule rule : rules) { |
| 949 | out.println(" device=" + rule.deviceId().toString() |
| 950 | + " priority=" + rule.priority() |
| 951 | + " selector=" + rule.selector().criteria().toString()); |
| 952 | } |
| 953 | out.println(""); |
Jian Li | e2d8751 | 2018-08-23 17:33:05 +0900 | [diff] [blame] | 954 | out.println("Routing Intents to Be Purged:\n"); |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 955 | for (Key key: toBePurgedIntentKeys) { |
| 956 | out.println(" " + key.toString()); |
| 957 | } |
| 958 | out.println(""); |
| 959 | |
Ray Milkey | 2ff6716 | 2018-01-22 10:14:19 -0800 | [diff] [blame] | 960 | } else if ("reactive-intents".equals(subject)) { |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 961 | for (Intent entry : intentService.getIntents()) { |
Jian Li | e2d8751 | 2018-08-23 17:33:05 +0900 | [diff] [blame] | 962 | if (appId.equals(entry.appId())) { |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 963 | MultiPointToSinglePointIntent intent = (MultiPointToSinglePointIntent) entry; |
| 964 | out.println(intent.key().toString() |
| 965 | + " to " + intent.egressPoint().toString() |
| 966 | + " set " + intent.treatment().immediate().toString() |
| 967 | + " from " + intent.ingressPoints().toString()); |
| 968 | } |
| 969 | } |
| 970 | } |
| 971 | } |
| 972 | |
| 973 | // Listener |
| 974 | private class InternalSimpleFabricListener implements SimpleFabricListener { |
| 975 | @Override |
| 976 | public void event(SimpleFabricEvent event) { |
| 977 | switch (event.type()) { |
| 978 | case SIMPLE_FABRIC_UPDATED: |
| 979 | refreshIntercepts(); |
| 980 | refreshRouteIntents(); |
| 981 | checkIntentsPurge(); |
| 982 | break; |
| 983 | case SIMPLE_FABRIC_FLUSH: |
| 984 | withdrawAllReactiveIntents(); |
| 985 | checkIntentsPurge(); |
| 986 | break; |
| 987 | case SIMPLE_FABRIC_IDLE: |
| 988 | refreshIntercepts(); |
| 989 | refreshRouteIntents(); |
| 990 | checkIntentsPurge(); |
| 991 | monitorBorderPeers(); |
| 992 | break; |
| 993 | case SIMPLE_FABRIC_DUMP: |
| 994 | dump(event.subject(), event.out()); |
| 995 | break; |
| 996 | default: |
| 997 | break; |
| 998 | } |
| 999 | } |
| 1000 | } |
| 1001 | |
| 1002 | } |
| 1003 | |