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