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