Jonghwan Hyun | ed478dc | 2018-08-06 15:35:18 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-present Open Networking Foundation |
| 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 | package org.onosproject.pipelines.fabric; |
| 17 | |
Jonghwan Hyun | ed478dc | 2018-08-06 15:35:18 +0900 | [diff] [blame] | 18 | import com.google.common.collect.Sets; |
Jonghwan Hyun | ed478dc | 2018-08-06 15:35:18 +0900 | [diff] [blame] | 19 | import org.onosproject.core.ApplicationId; |
| 20 | import org.onosproject.core.CoreService; |
| 21 | import org.onosproject.inbandtelemetry.api.IntConfig; |
| 22 | import org.onosproject.inbandtelemetry.api.IntIntent; |
| 23 | import org.onosproject.inbandtelemetry.api.IntObjective; |
| 24 | import org.onosproject.inbandtelemetry.api.IntProgrammable; |
Jonghwan Hyun | ed478dc | 2018-08-06 15:35:18 +0900 | [diff] [blame] | 25 | import org.onosproject.net.DeviceId; |
Jonghwan Hyun | ed478dc | 2018-08-06 15:35:18 +0900 | [diff] [blame] | 26 | import org.onosproject.net.PortNumber; |
Carmelo Cascone | fa42158 | 2018-09-13 10:05:57 -0700 | [diff] [blame] | 27 | import org.onosproject.net.config.NetworkConfigService; |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame] | 28 | import org.onosproject.net.device.DeviceService; |
Jonghwan Hyun | ed478dc | 2018-08-06 15:35:18 +0900 | [diff] [blame] | 29 | import org.onosproject.net.flow.DefaultFlowRule; |
| 30 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 31 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 32 | import org.onosproject.net.flow.FlowRule; |
| 33 | import org.onosproject.net.flow.FlowRuleService; |
Carmelo Cascone | fa42158 | 2018-09-13 10:05:57 -0700 | [diff] [blame] | 34 | import org.onosproject.net.flow.TableId; |
Jonghwan Hyun | ed478dc | 2018-08-06 15:35:18 +0900 | [diff] [blame] | 35 | import org.onosproject.net.flow.TrafficSelector; |
| 36 | import org.onosproject.net.flow.TrafficTreatment; |
| 37 | import org.onosproject.net.flow.criteria.Criterion; |
| 38 | import org.onosproject.net.flow.criteria.IPCriterion; |
| 39 | import org.onosproject.net.flow.criteria.PiCriterion; |
| 40 | import org.onosproject.net.flow.criteria.TcpPortCriterion; |
| 41 | import org.onosproject.net.flow.criteria.UdpPortCriterion; |
Jonghwan Hyun | ed478dc | 2018-08-06 15:35:18 +0900 | [diff] [blame] | 42 | import org.onosproject.net.pi.model.PiTableId; |
| 43 | import org.onosproject.net.pi.runtime.PiAction; |
| 44 | import org.onosproject.net.pi.runtime.PiActionParam; |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame] | 45 | import org.osgi.service.component.annotations.Reference; |
| 46 | import org.osgi.service.component.annotations.ReferenceCardinality; |
Jonghwan Hyun | ed478dc | 2018-08-06 15:35:18 +0900 | [diff] [blame] | 47 | |
Jonghwan Hyun | ed478dc | 2018-08-06 15:35:18 +0900 | [diff] [blame] | 48 | import java.util.Set; |
Jonghwan Hyun | ed478dc | 2018-08-06 15:35:18 +0900 | [diff] [blame] | 49 | import java.util.stream.Collectors; |
Carmelo Cascone | fa42158 | 2018-09-13 10:05:57 -0700 | [diff] [blame] | 50 | import java.util.stream.StreamSupport; |
Jonghwan Hyun | ed478dc | 2018-08-06 15:35:18 +0900 | [diff] [blame] | 51 | |
Carmelo Cascone | fa42158 | 2018-09-13 10:05:57 -0700 | [diff] [blame] | 52 | import static org.onlab.util.ImmutableByteSequence.copyFrom; |
Jonghwan Hyun | ed478dc | 2018-08-06 15:35:18 +0900 | [diff] [blame] | 53 | |
Carmelo Cascone | fa42158 | 2018-09-13 10:05:57 -0700 | [diff] [blame] | 54 | /** |
| 55 | * Implementation of INT programmable behavior for fabric.p4. Currently supports |
| 56 | * only SOURCE and TRANSIT functionalities. |
| 57 | */ |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 58 | public class FabricIntProgrammable extends AbstractFabricHandlerBehavior |
| 59 | implements IntProgrammable { |
Carmelo Cascone | fa42158 | 2018-09-13 10:05:57 -0700 | [diff] [blame] | 60 | |
Jonghwan Hyun | ed478dc | 2018-08-06 15:35:18 +0900 | [diff] [blame] | 61 | // TODO: change this value to the value of diameter of a network. |
Carmelo Cascone | fa42158 | 2018-09-13 10:05:57 -0700 | [diff] [blame] | 62 | private static final int DEFAULT_PRIORITY = 10000; |
Jonghwan Hyun | ed478dc | 2018-08-06 15:35:18 +0900 | [diff] [blame] | 63 | private static final int MAXHOP = 64; |
| 64 | private static final int PORTMASK = 0xffff; |
Jonghwan Hyun | ed478dc | 2018-08-06 15:35:18 +0900 | [diff] [blame] | 65 | private static final int PKT_INSTANCE_TYPE_INGRESS_CLONE = 1; |
Jonghwan Hyun | ed478dc | 2018-08-06 15:35:18 +0900 | [diff] [blame] | 66 | |
| 67 | private static final Set<Criterion.Type> SUPPORTED_CRITERION = Sets.newHashSet( |
| 68 | Criterion.Type.IPV4_DST, Criterion.Type.IPV4_SRC, |
| 69 | Criterion.Type.UDP_SRC, Criterion.Type.UDP_DST, |
| 70 | Criterion.Type.TCP_SRC, Criterion.Type.TCP_DST, |
| 71 | Criterion.Type.IP_PROTO); |
| 72 | |
Carmelo Cascone | fa42158 | 2018-09-13 10:05:57 -0700 | [diff] [blame] | 73 | private static final Set<PiTableId> TABLES_TO_CLEANUP = Sets.newHashSet( |
| 74 | FabricConstants.FABRIC_EGRESS_PROCESS_INT_MAIN_PROCESS_INT_TRANSIT_TB_INT_INSERT, |
| 75 | FabricConstants.FABRIC_INGRESS_PROCESS_SET_SOURCE_SINK_TB_SET_SOURCE, |
| 76 | FabricConstants.FABRIC_INGRESS_PROCESS_SET_SOURCE_SINK_TB_SET_SINK, |
| 77 | FabricConstants.FABRIC_EGRESS_PROCESS_INT_MAIN_PROCESS_INT_SOURCE_TB_INT_SOURCE, |
| 78 | FabricConstants.FABRIC_EGRESS_PROCESS_INT_MAIN_PROCESS_INT_REPORT_TB_GENERATE_REPORT |
| 79 | ); |
| 80 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 81 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Jonghwan Hyun | ed478dc | 2018-08-06 15:35:18 +0900 | [diff] [blame] | 82 | private FlowRuleService flowRuleService; |
| 83 | |
Jonghwan Hyun | ed478dc | 2018-08-06 15:35:18 +0900 | [diff] [blame] | 84 | private CoreService coreService; |
Carmelo Cascone | fa42158 | 2018-09-13 10:05:57 -0700 | [diff] [blame] | 85 | private NetworkConfigService cfgService; |
Jonghwan Hyun | ed478dc | 2018-08-06 15:35:18 +0900 | [diff] [blame] | 86 | private DeviceId deviceId; |
Carmelo Cascone | fa42158 | 2018-09-13 10:05:57 -0700 | [diff] [blame] | 87 | private ApplicationId appId; |
Jonghwan Hyun | ed478dc | 2018-08-06 15:35:18 +0900 | [diff] [blame] | 88 | |
Carmelo Cascone | fa42158 | 2018-09-13 10:05:57 -0700 | [diff] [blame] | 89 | private boolean setupBehaviour() { |
Jonghwan Hyun | ed478dc | 2018-08-06 15:35:18 +0900 | [diff] [blame] | 90 | deviceId = this.data().deviceId(); |
| 91 | flowRuleService = handler().get(FlowRuleService.class); |
Jonghwan Hyun | ed478dc | 2018-08-06 15:35:18 +0900 | [diff] [blame] | 92 | coreService = handler().get(CoreService.class); |
Carmelo Cascone | fa42158 | 2018-09-13 10:05:57 -0700 | [diff] [blame] | 93 | cfgService = handler().get(NetworkConfigService.class); |
| 94 | appId = coreService.getAppId(PipeconfLoader.PIPELINE_APP_NAME); |
Jonghwan Hyun | ed478dc | 2018-08-06 15:35:18 +0900 | [diff] [blame] | 95 | if (appId == null) { |
Carmelo Cascone | fa42158 | 2018-09-13 10:05:57 -0700 | [diff] [blame] | 96 | log.warn("Application ID is null. Cannot initialize behaviour."); |
| 97 | return false; |
| 98 | } |
| 99 | return true; |
| 100 | } |
| 101 | |
| 102 | @Override |
| 103 | public boolean init() { |
| 104 | |
| 105 | if (!setupBehaviour()) { |
| 106 | return false; |
Jonghwan Hyun | ed478dc | 2018-08-06 15:35:18 +0900 | [diff] [blame] | 107 | } |
| 108 | |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame] | 109 | // FIXME: create config class for INT to allow specifying arbitrary |
| 110 | // switch IDs. The one for the GeneralDeviceProvider was temporary and |
| 111 | // now has been removed. For now we use the chassis ID. |
| 112 | // final GeneralProviderDeviceConfig cfg = cfgService.getConfig( |
| 113 | // deviceId, GeneralProviderDeviceConfig.class); |
| 114 | // if (cfg == null) { |
| 115 | // log.warn("Missing GeneralProviderDevice config for {}", deviceId); |
| 116 | // return false; |
| 117 | // } |
| 118 | // final String switchId = cfg.protocolsInfo().containsKey("int") ? |
| 119 | // cfg.protocolsInfo().get("int").configValues().get("switchId") |
| 120 | // : null; |
| 121 | // if (switchId == null || switchId.isEmpty()) { |
| 122 | // log.warn("Missing INT device config for {}", deviceId); |
| 123 | // return false; |
| 124 | // } |
Jonghwan Hyun | ed478dc | 2018-08-06 15:35:18 +0900 | [diff] [blame] | 125 | |
Carmelo Cascone | 3977ea4 | 2019-02-28 13:43:42 -0800 | [diff] [blame] | 126 | PiActionParam transitIdParam = new PiActionParam( |
| 127 | FabricConstants.SWITCH_ID, |
| 128 | copyFrom(handler().get(DeviceService.class) |
| 129 | .getDevice(deviceId).chassisId().id())); |
Carmelo Cascone | fa42158 | 2018-09-13 10:05:57 -0700 | [diff] [blame] | 130 | |
Jonghwan Hyun | ed478dc | 2018-08-06 15:35:18 +0900 | [diff] [blame] | 131 | PiAction transitAction = PiAction.builder() |
Carmelo Cascone | 79a3a31 | 2018-08-16 17:14:43 -0700 | [diff] [blame] | 132 | .withId(FabricConstants.FABRIC_EGRESS_PROCESS_INT_MAIN_PROCESS_INT_TRANSIT_INIT_METADATA) |
Jonghwan Hyun | ed478dc | 2018-08-06 15:35:18 +0900 | [diff] [blame] | 133 | .withParameter(transitIdParam) |
| 134 | .build(); |
| 135 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 136 | .piTableAction(transitAction) |
| 137 | .build(); |
Carmelo Cascone | fa42158 | 2018-09-13 10:05:57 -0700 | [diff] [blame] | 138 | TrafficSelector selector = DefaultTrafficSelector.builder() |
| 139 | .matchPi(PiCriterion.builder().matchExact( |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 140 | FabricConstants.HDR_INT_IS_VALID, (byte) 0x01) |
Carmelo Cascone | fa42158 | 2018-09-13 10:05:57 -0700 | [diff] [blame] | 141 | .build()) |
| 142 | .build(); |
Jonghwan Hyun | ed478dc | 2018-08-06 15:35:18 +0900 | [diff] [blame] | 143 | |
| 144 | FlowRule transitFlowRule = DefaultFlowRule.builder() |
Carmelo Cascone | fa42158 | 2018-09-13 10:05:57 -0700 | [diff] [blame] | 145 | .withSelector(selector) |
Jonghwan Hyun | ed478dc | 2018-08-06 15:35:18 +0900 | [diff] [blame] | 146 | .withTreatment(treatment) |
| 147 | .fromApp(appId) |
| 148 | .withPriority(DEFAULT_PRIORITY) |
| 149 | .makePermanent() |
| 150 | .forDevice(deviceId) |
Carmelo Cascone | 79a3a31 | 2018-08-16 17:14:43 -0700 | [diff] [blame] | 151 | .forTable(FabricConstants.FABRIC_EGRESS_PROCESS_INT_MAIN_PROCESS_INT_TRANSIT_TB_INT_INSERT) |
Jonghwan Hyun | ed478dc | 2018-08-06 15:35:18 +0900 | [diff] [blame] | 152 | .build(); |
Jonghwan Hyun | ed478dc | 2018-08-06 15:35:18 +0900 | [diff] [blame] | 153 | |
Carmelo Cascone | fa42158 | 2018-09-13 10:05:57 -0700 | [diff] [blame] | 154 | flowRuleService.applyFlowRules(transitFlowRule); |
| 155 | return true; |
| 156 | } |
Jonghwan Hyun | ed478dc | 2018-08-06 15:35:18 +0900 | [diff] [blame] | 157 | |
Carmelo Cascone | fa42158 | 2018-09-13 10:05:57 -0700 | [diff] [blame] | 158 | @Override |
| 159 | public boolean setSourcePort(PortNumber port) { |
| 160 | |
| 161 | if (!setupBehaviour()) { |
| 162 | return false; |
Jonghwan Hyun | ed478dc | 2018-08-06 15:35:18 +0900 | [diff] [blame] | 163 | } |
Jonghwan Hyun | ed478dc | 2018-08-06 15:35:18 +0900 | [diff] [blame] | 164 | |
Carmelo Cascone | fa42158 | 2018-09-13 10:05:57 -0700 | [diff] [blame] | 165 | PiCriterion ingressCriterion = PiCriterion.builder() |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 166 | .matchExact(FabricConstants.HDR_IG_PORT, port.toLong()) |
Jonghwan Hyun | ed478dc | 2018-08-06 15:35:18 +0900 | [diff] [blame] | 167 | .build(); |
Carmelo Cascone | fa42158 | 2018-09-13 10:05:57 -0700 | [diff] [blame] | 168 | TrafficSelector srcSelector = DefaultTrafficSelector.builder() |
| 169 | .matchPi(ingressCriterion) |
Jonghwan Hyun | ed478dc | 2018-08-06 15:35:18 +0900 | [diff] [blame] | 170 | .build(); |
Carmelo Cascone | fa42158 | 2018-09-13 10:05:57 -0700 | [diff] [blame] | 171 | PiAction setSourceAct = PiAction.builder() |
| 172 | .withId(FabricConstants.FABRIC_INGRESS_PROCESS_SET_SOURCE_SINK_INT_SET_SOURCE) |
Jonghwan Hyun | ed478dc | 2018-08-06 15:35:18 +0900 | [diff] [blame] | 173 | .build(); |
Carmelo Cascone | fa42158 | 2018-09-13 10:05:57 -0700 | [diff] [blame] | 174 | TrafficTreatment srcTreatment = DefaultTrafficTreatment.builder() |
| 175 | .piTableAction(setSourceAct) |
Jonghwan Hyun | ed478dc | 2018-08-06 15:35:18 +0900 | [diff] [blame] | 176 | .build(); |
Carmelo Cascone | fa42158 | 2018-09-13 10:05:57 -0700 | [diff] [blame] | 177 | FlowRule srcFlowRule = DefaultFlowRule.builder() |
| 178 | .withSelector(srcSelector) |
| 179 | .withTreatment(srcTreatment) |
| 180 | .fromApp(appId) |
Jonghwan Hyun | ed478dc | 2018-08-06 15:35:18 +0900 | [diff] [blame] | 181 | .withPriority(DEFAULT_PRIORITY) |
| 182 | .makePermanent() |
| 183 | .forDevice(deviceId) |
Carmelo Cascone | fa42158 | 2018-09-13 10:05:57 -0700 | [diff] [blame] | 184 | .forTable(FabricConstants.FABRIC_INGRESS_PROCESS_SET_SOURCE_SINK_TB_SET_SOURCE) |
Jonghwan Hyun | ed478dc | 2018-08-06 15:35:18 +0900 | [diff] [blame] | 185 | .build(); |
Carmelo Cascone | fa42158 | 2018-09-13 10:05:57 -0700 | [diff] [blame] | 186 | flowRuleService.applyFlowRules(srcFlowRule); |
| 187 | return true; |
| 188 | } |
Jonghwan Hyun | ed478dc | 2018-08-06 15:35:18 +0900 | [diff] [blame] | 189 | |
Carmelo Cascone | fa42158 | 2018-09-13 10:05:57 -0700 | [diff] [blame] | 190 | @Override |
| 191 | public boolean setSinkPort(PortNumber port) { |
| 192 | |
| 193 | if (!setupBehaviour()) { |
| 194 | return false; |
| 195 | } |
| 196 | |
| 197 | PiCriterion egressCriterion = PiCriterion.builder() |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 198 | .matchExact(FabricConstants.HDR_EG_PORT, port.toLong()) |
Carmelo Cascone | fa42158 | 2018-09-13 10:05:57 -0700 | [diff] [blame] | 199 | .build(); |
| 200 | TrafficSelector sinkSelector = DefaultTrafficSelector.builder() |
| 201 | .matchPi(egressCriterion) |
| 202 | .build(); |
| 203 | PiAction setSinkAct = PiAction.builder() |
| 204 | .withId(FabricConstants.FABRIC_INGRESS_PROCESS_SET_SOURCE_SINK_INT_SET_SINK) |
| 205 | .build(); |
| 206 | TrafficTreatment sinkTreatment = DefaultTrafficTreatment.builder() |
| 207 | .piTableAction(setSinkAct) |
| 208 | .build(); |
| 209 | FlowRule sinkFlowRule = DefaultFlowRule.builder() |
| 210 | .withSelector(sinkSelector) |
| 211 | .withTreatment(sinkTreatment) |
| 212 | .fromApp(appId) |
| 213 | .withPriority(DEFAULT_PRIORITY) |
| 214 | .makePermanent() |
| 215 | .forDevice(deviceId) |
| 216 | .forTable(FabricConstants.FABRIC_INGRESS_PROCESS_SET_SOURCE_SINK_TB_SET_SINK) |
| 217 | .build(); |
| 218 | flowRuleService.applyFlowRules(sinkFlowRule); |
| 219 | return true; |
| 220 | } |
| 221 | |
| 222 | @Override |
| 223 | public boolean addIntObjective(IntObjective obj) { |
| 224 | |
| 225 | if (!setupBehaviour()) { |
| 226 | return false; |
| 227 | } |
| 228 | |
| 229 | return processIntObjective(obj, true); |
| 230 | } |
| 231 | |
| 232 | @Override |
| 233 | public boolean removeIntObjective(IntObjective obj) { |
| 234 | |
| 235 | if (!setupBehaviour()) { |
| 236 | return false; |
| 237 | } |
| 238 | |
| 239 | return processIntObjective(obj, false); |
| 240 | } |
| 241 | |
| 242 | @Override |
| 243 | public boolean setupIntConfig(IntConfig config) { |
| 244 | |
| 245 | if (!setupBehaviour()) { |
| 246 | return false; |
| 247 | } |
| 248 | |
| 249 | return setupIntReportInternal(config); |
| 250 | } |
| 251 | |
| 252 | @Override |
| 253 | public void cleanup() { |
| 254 | |
| 255 | if (!setupBehaviour()) { |
| 256 | return; |
| 257 | } |
| 258 | |
| 259 | StreamSupport.stream(flowRuleService.getFlowEntries( |
| 260 | data().deviceId()).spliterator(), false) |
| 261 | .filter(f -> f.table().type() == TableId.Type.PIPELINE_INDEPENDENT) |
| 262 | .filter(f -> TABLES_TO_CLEANUP.contains((PiTableId) f.table())) |
| 263 | .forEach(flowRuleService::removeFlowRules); |
| 264 | } |
| 265 | |
| 266 | @Override |
| 267 | public boolean supportsFunctionality(IntFunctionality functionality) { |
| 268 | // Sink not fully supported yet. |
| 269 | return functionality == IntFunctionality.SOURCE || functionality == IntFunctionality.TRANSIT; |
Jonghwan Hyun | ed478dc | 2018-08-06 15:35:18 +0900 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | private FlowRule buildWatchlistEntry(IntObjective obj) { |
Jonghwan Hyun | ed478dc | 2018-08-06 15:35:18 +0900 | [diff] [blame] | 273 | int instructionBitmap = buildInstructionBitmap(obj.metadataTypes()); |
| 274 | PiActionParam maxHopParam = new PiActionParam( |
| 275 | FabricConstants.MAX_HOP, |
Carmelo Cascone | fa42158 | 2018-09-13 10:05:57 -0700 | [diff] [blame] | 276 | copyFrom(MAXHOP)); |
Jonghwan Hyun | ed478dc | 2018-08-06 15:35:18 +0900 | [diff] [blame] | 277 | PiActionParam instCntParam = new PiActionParam( |
| 278 | FabricConstants.INS_CNT, |
Carmelo Cascone | fa42158 | 2018-09-13 10:05:57 -0700 | [diff] [blame] | 279 | copyFrom(Integer.bitCount(instructionBitmap))); |
Jonghwan Hyun | ed478dc | 2018-08-06 15:35:18 +0900 | [diff] [blame] | 280 | PiActionParam inst0003Param = new PiActionParam( |
| 281 | FabricConstants.INS_MASK0003, |
Carmelo Cascone | fa42158 | 2018-09-13 10:05:57 -0700 | [diff] [blame] | 282 | copyFrom((instructionBitmap >> 12) & 0xF)); |
Jonghwan Hyun | ed478dc | 2018-08-06 15:35:18 +0900 | [diff] [blame] | 283 | PiActionParam inst0407Param = new PiActionParam( |
| 284 | FabricConstants.INS_MASK0407, |
Carmelo Cascone | fa42158 | 2018-09-13 10:05:57 -0700 | [diff] [blame] | 285 | copyFrom((instructionBitmap >> 8) & 0xF)); |
Jonghwan Hyun | ed478dc | 2018-08-06 15:35:18 +0900 | [diff] [blame] | 286 | |
| 287 | PiAction intSourceAction = PiAction.builder() |
Carmelo Cascone | 79a3a31 | 2018-08-16 17:14:43 -0700 | [diff] [blame] | 288 | .withId(FabricConstants.FABRIC_EGRESS_PROCESS_INT_MAIN_PROCESS_INT_SOURCE_INT_SOURCE_DSCP) |
Jonghwan Hyun | ed478dc | 2018-08-06 15:35:18 +0900 | [diff] [blame] | 289 | .withParameter(maxHopParam) |
| 290 | .withParameter(instCntParam) |
| 291 | .withParameter(inst0003Param) |
| 292 | .withParameter(inst0407Param) |
| 293 | .build(); |
| 294 | |
| 295 | TrafficTreatment instTreatment = DefaultTrafficTreatment.builder() |
| 296 | .piTableAction(intSourceAction) |
| 297 | .build(); |
| 298 | |
| 299 | TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder(); |
| 300 | for (Criterion criterion : obj.selector().criteria()) { |
| 301 | switch (criterion.type()) { |
| 302 | case IPV4_SRC: |
| 303 | sBuilder.matchIPSrc(((IPCriterion) criterion).ip()); |
| 304 | break; |
| 305 | case IPV4_DST: |
| 306 | sBuilder.matchIPDst(((IPCriterion) criterion).ip()); |
| 307 | break; |
| 308 | case TCP_SRC: |
| 309 | sBuilder.matchPi( |
| 310 | PiCriterion.builder().matchTernary( |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 311 | FabricConstants.HDR_L4_SPORT, |
Jonghwan Hyun | ed478dc | 2018-08-06 15:35:18 +0900 | [diff] [blame] | 312 | ((TcpPortCriterion) criterion).tcpPort().toInt(), PORTMASK) |
| 313 | .build()); |
| 314 | break; |
| 315 | case UDP_SRC: |
| 316 | sBuilder.matchPi( |
| 317 | PiCriterion.builder().matchTernary( |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 318 | FabricConstants.HDR_L4_SPORT, |
Jonghwan Hyun | ed478dc | 2018-08-06 15:35:18 +0900 | [diff] [blame] | 319 | ((UdpPortCriterion) criterion).udpPort().toInt(), PORTMASK) |
| 320 | .build()); |
| 321 | break; |
| 322 | case TCP_DST: |
| 323 | sBuilder.matchPi( |
| 324 | PiCriterion.builder().matchTernary( |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 325 | FabricConstants.HDR_L4_DPORT, |
Jonghwan Hyun | ed478dc | 2018-08-06 15:35:18 +0900 | [diff] [blame] | 326 | ((TcpPortCriterion) criterion).tcpPort().toInt(), PORTMASK) |
| 327 | .build()); |
| 328 | break; |
| 329 | case UDP_DST: |
| 330 | sBuilder.matchPi( |
| 331 | PiCriterion.builder().matchTernary( |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 332 | FabricConstants.HDR_L4_DPORT, |
Jonghwan Hyun | ed478dc | 2018-08-06 15:35:18 +0900 | [diff] [blame] | 333 | ((UdpPortCriterion) criterion).udpPort().toInt(), PORTMASK) |
| 334 | .build()); |
| 335 | break; |
| 336 | default: |
| 337 | log.warn("Unsupported criterion type: {}", criterion.type()); |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | return DefaultFlowRule.builder() |
Carmelo Cascone | fa42158 | 2018-09-13 10:05:57 -0700 | [diff] [blame] | 342 | .forDevice(deviceId) |
Jonghwan Hyun | ed478dc | 2018-08-06 15:35:18 +0900 | [diff] [blame] | 343 | .withSelector(sBuilder.build()) |
| 344 | .withTreatment(instTreatment) |
| 345 | .withPriority(DEFAULT_PRIORITY) |
Carmelo Cascone | 79a3a31 | 2018-08-16 17:14:43 -0700 | [diff] [blame] | 346 | .forTable(FabricConstants.FABRIC_EGRESS_PROCESS_INT_MAIN_PROCESS_INT_SOURCE_TB_INT_SOURCE) |
Jonghwan Hyun | ed478dc | 2018-08-06 15:35:18 +0900 | [diff] [blame] | 347 | .fromApp(appId) |
Carmelo Cascone | fa42158 | 2018-09-13 10:05:57 -0700 | [diff] [blame] | 348 | .makePermanent() |
Jonghwan Hyun | ed478dc | 2018-08-06 15:35:18 +0900 | [diff] [blame] | 349 | .build(); |
| 350 | } |
| 351 | |
| 352 | private int buildInstructionBitmap(Set<IntIntent.IntMetadataType> metadataTypes) { |
| 353 | int instBitmap = 0; |
| 354 | for (IntIntent.IntMetadataType metadataType : metadataTypes) { |
| 355 | switch (metadataType) { |
| 356 | case SWITCH_ID: |
| 357 | instBitmap |= (1 << 15); |
| 358 | break; |
| 359 | case L1_PORT_ID: |
| 360 | instBitmap |= (1 << 14); |
| 361 | break; |
| 362 | case HOP_LATENCY: |
| 363 | instBitmap |= (1 << 13); |
| 364 | break; |
| 365 | case QUEUE_OCCUPANCY: |
| 366 | instBitmap |= (1 << 12); |
| 367 | break; |
| 368 | case INGRESS_TIMESTAMP: |
| 369 | instBitmap |= (1 << 11); |
| 370 | break; |
| 371 | case EGRESS_TIMESTAMP: |
| 372 | instBitmap |= (1 << 10); |
| 373 | break; |
| 374 | case L2_PORT_ID: |
| 375 | instBitmap |= (1 << 9); |
| 376 | break; |
| 377 | case EGRESS_TX_UTIL: |
| 378 | instBitmap |= (1 << 8); |
| 379 | break; |
| 380 | default: |
| 381 | log.info("Unsupported metadata type {}. Ignoring...", metadataType); |
| 382 | break; |
| 383 | } |
| 384 | } |
| 385 | return instBitmap; |
| 386 | } |
| 387 | |
| 388 | /** |
Carmelo Cascone | 79a3a31 | 2018-08-16 17:14:43 -0700 | [diff] [blame] | 389 | * Returns a subset of Criterion from given selector, which is unsupported |
| 390 | * by this INT pipeline. |
Jonghwan Hyun | ed478dc | 2018-08-06 15:35:18 +0900 | [diff] [blame] | 391 | * |
| 392 | * @param selector a traffic selector |
Carmelo Cascone | 79a3a31 | 2018-08-16 17:14:43 -0700 | [diff] [blame] | 393 | * @return a subset of Criterion from given selector, unsupported by this |
| 394 | * INT pipeline, empty if all criteria are supported. |
Jonghwan Hyun | ed478dc | 2018-08-06 15:35:18 +0900 | [diff] [blame] | 395 | */ |
| 396 | private Set<Criterion> unsupportedSelectors(TrafficSelector selector) { |
| 397 | return selector.criteria().stream() |
| 398 | .filter(criterion -> !SUPPORTED_CRITERION.contains(criterion.type())) |
| 399 | .collect(Collectors.toSet()); |
| 400 | } |
| 401 | |
| 402 | private boolean processIntObjective(IntObjective obj, boolean install) { |
Jonghwan Hyun | ed478dc | 2018-08-06 15:35:18 +0900 | [diff] [blame] | 403 | if (install && !unsupportedSelectors(obj.selector()).isEmpty()) { |
Carmelo Cascone | fa42158 | 2018-09-13 10:05:57 -0700 | [diff] [blame] | 404 | log.warn("Criteria {} not supported by {} for INT watchlist", |
| 405 | unsupportedSelectors(obj.selector()), deviceId); |
Jonghwan Hyun | ed478dc | 2018-08-06 15:35:18 +0900 | [diff] [blame] | 406 | return false; |
| 407 | } |
| 408 | |
| 409 | FlowRule flowRule = buildWatchlistEntry(obj); |
| 410 | if (flowRule != null) { |
| 411 | if (install) { |
| 412 | flowRuleService.applyFlowRules(flowRule); |
| 413 | } else { |
| 414 | flowRuleService.removeFlowRules(flowRule); |
| 415 | } |
| 416 | log.debug("IntObjective {} has been {} {}", |
| 417 | obj, install ? "installed to" : "removed from", deviceId); |
| 418 | return true; |
| 419 | } else { |
| 420 | log.warn("Failed to {} IntObjective {} on {}", |
| 421 | install ? "install" : "remove", obj, deviceId); |
| 422 | return false; |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | private boolean setupIntReportInternal(IntConfig cfg) { |
Carmelo Cascone | fa42158 | 2018-09-13 10:05:57 -0700 | [diff] [blame] | 427 | // Report not fully supported yet. |
| 428 | return true; |
| 429 | // FlowRule reportRule = buildReportEntry(cfg, PKT_INSTANCE_TYPE_INGRESS_CLONE); |
| 430 | // if (reportRule != null) { |
| 431 | // flowRuleService.applyFlowRules(reportRule); |
| 432 | // log.info("Report entry {} has been added to {}", reportRule, this.data().deviceId()); |
| 433 | // return true; |
| 434 | // } else { |
| 435 | // log.warn("Failed to add report entry on {}", this.data().deviceId()); |
| 436 | // return false; |
| 437 | // } |
Jonghwan Hyun | ed478dc | 2018-08-06 15:35:18 +0900 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | private FlowRule buildReportEntry(IntConfig cfg, int type) { |
Carmelo Cascone | fa42158 | 2018-09-13 10:05:57 -0700 | [diff] [blame] | 441 | |
| 442 | if (!setupBehaviour()) { |
Jonghwan Hyun | ed478dc | 2018-08-06 15:35:18 +0900 | [diff] [blame] | 443 | return null; |
| 444 | } |
| 445 | |
| 446 | PiActionParam srcMacParam = new PiActionParam( |
| 447 | FabricConstants.SRC_MAC, |
Carmelo Cascone | fa42158 | 2018-09-13 10:05:57 -0700 | [diff] [blame] | 448 | copyFrom(cfg.sinkMac().toBytes())); |
Jonghwan Hyun | ed478dc | 2018-08-06 15:35:18 +0900 | [diff] [blame] | 449 | PiActionParam nextHopMacParam = new PiActionParam( |
| 450 | FabricConstants.MON_MAC, |
Carmelo Cascone | fa42158 | 2018-09-13 10:05:57 -0700 | [diff] [blame] | 451 | copyFrom(cfg.collectorNextHopMac().toBytes())); |
Jonghwan Hyun | ed478dc | 2018-08-06 15:35:18 +0900 | [diff] [blame] | 452 | PiActionParam srcIpParam = new PiActionParam( |
| 453 | FabricConstants.SRC_IP, |
Carmelo Cascone | fa42158 | 2018-09-13 10:05:57 -0700 | [diff] [blame] | 454 | copyFrom(cfg.sinkIp().toOctets())); |
Jonghwan Hyun | ed478dc | 2018-08-06 15:35:18 +0900 | [diff] [blame] | 455 | PiActionParam monIpParam = new PiActionParam( |
| 456 | FabricConstants.MON_IP, |
Carmelo Cascone | fa42158 | 2018-09-13 10:05:57 -0700 | [diff] [blame] | 457 | copyFrom(cfg.collectorIp().toOctets())); |
Jonghwan Hyun | ed478dc | 2018-08-06 15:35:18 +0900 | [diff] [blame] | 458 | PiActionParam monPortParam = new PiActionParam( |
| 459 | FabricConstants.MON_PORT, |
Carmelo Cascone | fa42158 | 2018-09-13 10:05:57 -0700 | [diff] [blame] | 460 | copyFrom(cfg.collectorPort().toInt())); |
Jonghwan Hyun | ed478dc | 2018-08-06 15:35:18 +0900 | [diff] [blame] | 461 | PiAction reportAction = PiAction.builder() |
Carmelo Cascone | 79a3a31 | 2018-08-16 17:14:43 -0700 | [diff] [blame] | 462 | .withId(FabricConstants.FABRIC_EGRESS_PROCESS_INT_MAIN_PROCESS_INT_REPORT_DO_REPORT_ENCAPSULATION) |
Jonghwan Hyun | ed478dc | 2018-08-06 15:35:18 +0900 | [diff] [blame] | 463 | .withParameter(srcMacParam) |
| 464 | .withParameter(nextHopMacParam) |
| 465 | .withParameter(srcIpParam) |
| 466 | .withParameter(monIpParam) |
| 467 | .withParameter(monPortParam) |
| 468 | .build(); |
| 469 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 470 | .piTableAction(reportAction) |
| 471 | .build(); |
| 472 | |
| 473 | return DefaultFlowRule.builder() |
| 474 | .withTreatment(treatment) |
| 475 | .fromApp(appId) |
| 476 | .withPriority(DEFAULT_PRIORITY) |
| 477 | .makePermanent() |
| 478 | .forDevice(this.data().deviceId()) |
Carmelo Cascone | 79a3a31 | 2018-08-16 17:14:43 -0700 | [diff] [blame] | 479 | .forTable(FabricConstants.FABRIC_EGRESS_PROCESS_INT_MAIN_PROCESS_INT_REPORT_TB_GENERATE_REPORT) |
Jonghwan Hyun | ed478dc | 2018-08-06 15:35:18 +0900 | [diff] [blame] | 480 | .build(); |
| 481 | } |
| 482 | |
| 483 | } |