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