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