Thomas Vachuska | 58de416 | 2015-09-10 16:15:33 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Thomas Vachuska | 58de416 | 2015-09-10 16:15:33 -0700 | [diff] [blame] | 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 | */ |
Saurav Das | e3274c8 | 2015-05-24 17:21:56 -0700 | [diff] [blame] | 16 | package org.onosproject.driver.pipeline; |
| 17 | |
| 18 | |
| 19 | import com.google.common.cache.Cache; |
| 20 | import com.google.common.cache.CacheBuilder; |
| 21 | import com.google.common.cache.RemovalCause; |
| 22 | import com.google.common.cache.RemovalNotification; |
| 23 | |
| 24 | import org.onlab.osgi.ServiceDirectory; |
| 25 | import org.onlab.packet.Ethernet; |
| 26 | import org.onlab.packet.IPv4; |
Hyunsun Moon | cf732fb | 2015-08-22 21:04:23 -0700 | [diff] [blame] | 27 | import org.onlab.packet.TpPort; |
Saurav Das | e3274c8 | 2015-05-24 17:21:56 -0700 | [diff] [blame] | 28 | import org.onlab.packet.VlanId; |
| 29 | import org.onlab.util.KryoNamespace; |
| 30 | import org.onosproject.core.ApplicationId; |
| 31 | import org.onosproject.core.CoreService; |
| 32 | import org.onosproject.net.DeviceId; |
Saurav Das | 6c44a63 | 2015-05-30 22:05:22 -0700 | [diff] [blame] | 33 | import org.onosproject.net.PortNumber; |
Saurav Das | e3274c8 | 2015-05-24 17:21:56 -0700 | [diff] [blame] | 34 | import org.onosproject.net.behaviour.NextGroup; |
| 35 | import org.onosproject.net.behaviour.Pipeliner; |
| 36 | import org.onosproject.net.behaviour.PipelinerContext; |
| 37 | import org.onosproject.net.driver.AbstractHandlerBehaviour; |
| 38 | import org.onosproject.net.flow.DefaultFlowRule; |
| 39 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 40 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 41 | import org.onosproject.net.flow.FlowRule; |
| 42 | import org.onosproject.net.flow.FlowRuleOperations; |
| 43 | import org.onosproject.net.flow.FlowRuleOperationsContext; |
| 44 | import org.onosproject.net.flow.FlowRuleService; |
| 45 | import org.onosproject.net.flow.TrafficSelector; |
| 46 | import org.onosproject.net.flow.TrafficTreatment; |
| 47 | import org.onosproject.net.flow.criteria.Criterion; |
| 48 | import org.onosproject.net.flow.criteria.Criteria; |
| 49 | import org.onosproject.net.flow.criteria.EthCriterion; |
| 50 | import org.onosproject.net.flow.criteria.PortCriterion; |
| 51 | import org.onosproject.net.flow.criteria.EthTypeCriterion; |
| 52 | import org.onosproject.net.flow.criteria.IPCriterion; |
| 53 | import org.onosproject.net.flow.criteria.VlanIdCriterion; |
| 54 | import org.onosproject.net.flow.instructions.Instruction; |
| 55 | import org.onosproject.net.flow.instructions.L2ModificationInstruction; |
| 56 | import org.onosproject.net.flowobjective.FilteringObjective; |
| 57 | import org.onosproject.net.flowobjective.FlowObjectiveStore; |
| 58 | import org.onosproject.net.flowobjective.ForwardingObjective; |
| 59 | import org.onosproject.net.flowobjective.NextObjective; |
| 60 | import org.onosproject.net.flowobjective.Objective; |
| 61 | import org.onosproject.net.flowobjective.ObjectiveError; |
| 62 | import org.onosproject.net.group.DefaultGroupBucket; |
| 63 | import org.onosproject.net.group.DefaultGroupDescription; |
| 64 | import org.onosproject.net.group.DefaultGroupKey; |
| 65 | import org.onosproject.net.group.Group; |
| 66 | import org.onosproject.net.group.GroupBucket; |
| 67 | import org.onosproject.net.group.GroupBuckets; |
| 68 | import org.onosproject.net.group.GroupDescription; |
| 69 | import org.onosproject.net.group.GroupEvent; |
| 70 | import org.onosproject.net.group.GroupKey; |
| 71 | import org.onosproject.net.group.GroupListener; |
| 72 | import org.onosproject.net.group.GroupService; |
| 73 | import org.slf4j.Logger; |
| 74 | |
| 75 | import java.util.Collection; |
| 76 | import java.util.Collections; |
Saurav Das | 2443119 | 2016-03-07 19:13:00 -0800 | [diff] [blame] | 77 | import java.util.List; |
Sho SHIMIZU | 4590604 | 2016-01-13 23:05:54 -0800 | [diff] [blame] | 78 | import java.util.Objects; |
Saurav Das | e3274c8 | 2015-05-24 17:21:56 -0700 | [diff] [blame] | 79 | import java.util.Set; |
| 80 | import java.util.concurrent.Executors; |
| 81 | import java.util.concurrent.ScheduledExecutorService; |
| 82 | import java.util.concurrent.TimeUnit; |
| 83 | import java.util.stream.Collectors; |
| 84 | |
| 85 | import static org.onlab.util.Tools.groupedThreads; |
| 86 | import static org.slf4j.LoggerFactory.getLogger; |
| 87 | |
| 88 | /** |
| 89 | * Driver for Centec's V350 switches. |
| 90 | */ |
| 91 | public class CentecV350Pipeline extends AbstractHandlerBehaviour implements Pipeliner { |
| 92 | |
| 93 | protected static final int PORT_VLAN_TABLE = 0; |
| 94 | protected static final int FILTER_TABLE = 1; |
| 95 | // TMAC is configured in MAC Table to redirect packets to ROUTE_TABLE. |
| 96 | protected static final int MAC_TABLE = 2; |
| 97 | protected static final int ROUTE_TABLE = 3; |
| 98 | |
| 99 | private static final long DEFAULT_METADATA = 100; |
Saurav Das | 6c44a63 | 2015-05-30 22:05:22 -0700 | [diff] [blame] | 100 | private static final long DEFAULT_METADATA_MASK = 0xffffffffffffffffL; |
Saurav Das | e3274c8 | 2015-05-24 17:21:56 -0700 | [diff] [blame] | 101 | |
| 102 | // Priority used in PORT_VLAN Table, the only priority accepted is PORT_VLAN_TABLE_PRIORITY. |
| 103 | // The packet passed PORT+VLAN check will goto FILTER Table. |
| 104 | private static final int PORT_VLAN_TABLE_PRIORITY = 0xffff; |
| 105 | |
| 106 | // Priority used in Filter Table. |
| 107 | private static final int FILTER_TABLE_CONTROLLER_PRIORITY = 500; |
| 108 | // TMAC priority should be lower than controller. |
| 109 | private static final int FILTER_TABLE_TMAC_PRIORITY = 200; |
| 110 | private static final int FILTER_TABLE_HIGHEST_PRIORITY = 0xffff; |
| 111 | |
| 112 | // Priority used in MAC Table. |
| 113 | // We do exact matching for DMAC+metadata, so priority is ignored and required to be set to 0xffff. |
| 114 | private static final int MAC_TABLE_PRIORITY = 0xffff; |
| 115 | |
| 116 | // Priority used in Route Table. |
| 117 | // We do LPM matching in Route Table, so priority is ignored and required to be set to 0xffff. |
| 118 | private static final int ROUTE_TABLE_PRIORITY = 0xffff; |
| 119 | |
| 120 | private static final short BGP_PORT = 179; |
| 121 | |
| 122 | private final Logger log = getLogger(getClass()); |
| 123 | |
| 124 | private ServiceDirectory serviceDirectory; |
| 125 | private FlowRuleService flowRuleService; |
| 126 | private CoreService coreService; |
| 127 | private GroupService groupService; |
| 128 | private FlowObjectiveStore flowObjectiveStore; |
| 129 | private DeviceId deviceId; |
| 130 | private ApplicationId appId; |
| 131 | |
| 132 | private KryoNamespace appKryo = new KryoNamespace.Builder() |
| 133 | .register(GroupKey.class) |
| 134 | .register(DefaultGroupKey.class) |
| 135 | .register(CentecV350Group.class) |
| 136 | .register(byte[].class) |
Charles Chan | eefdedf | 2016-05-23 16:45:45 -0700 | [diff] [blame] | 137 | .build("CentecV350Pipeline"); |
Saurav Das | e3274c8 | 2015-05-24 17:21:56 -0700 | [diff] [blame] | 138 | |
| 139 | private Cache<GroupKey, NextObjective> pendingGroups; |
| 140 | |
| 141 | private ScheduledExecutorService groupChecker = |
| 142 | Executors.newScheduledThreadPool(2, groupedThreads("onos/pipeliner", |
HIGUCHI Yuta | d9e0105 | 2016-04-14 09:31:42 -0700 | [diff] [blame] | 143 | "centec-V350-%d", log)); |
Saurav Das | e3274c8 | 2015-05-24 17:21:56 -0700 | [diff] [blame] | 144 | |
| 145 | @Override |
| 146 | public void init(DeviceId deviceId, PipelinerContext context) { |
| 147 | this.serviceDirectory = context.directory(); |
| 148 | this.deviceId = deviceId; |
| 149 | |
| 150 | pendingGroups = CacheBuilder.newBuilder() |
| 151 | .expireAfterWrite(20, TimeUnit.SECONDS) |
| 152 | .removalListener((RemovalNotification<GroupKey, NextObjective> notification) -> { |
| 153 | if (notification.getCause() == RemovalCause.EXPIRED) { |
| 154 | fail(notification.getValue(), ObjectiveError.GROUPINSTALLATIONFAILED); |
| 155 | } |
| 156 | }).build(); |
| 157 | |
| 158 | groupChecker.scheduleAtFixedRate(new GroupChecker(), 0, 500, TimeUnit.MILLISECONDS); |
| 159 | |
| 160 | coreService = serviceDirectory.get(CoreService.class); |
| 161 | flowRuleService = serviceDirectory.get(FlowRuleService.class); |
| 162 | groupService = serviceDirectory.get(GroupService.class); |
| 163 | flowObjectiveStore = context.store(); |
| 164 | |
| 165 | groupService.addListener(new InnerGroupListener()); |
| 166 | |
| 167 | appId = coreService.registerApplication( |
| 168 | "org.onosproject.driver.CentecV350Pipeline"); |
| 169 | |
| 170 | initializePipeline(); |
| 171 | } |
| 172 | |
| 173 | @Override |
| 174 | public void filter(FilteringObjective filteringObjective) { |
| 175 | if (filteringObjective.type() == FilteringObjective.Type.PERMIT) { |
| 176 | processFilter(filteringObjective, |
| 177 | filteringObjective.op() == Objective.Operation.ADD, |
| 178 | filteringObjective.appId()); |
| 179 | } else { |
| 180 | fail(filteringObjective, ObjectiveError.UNSUPPORTED); |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | @Override |
| 185 | public void forward(ForwardingObjective fwd) { |
| 186 | Collection<FlowRule> rules; |
| 187 | FlowRuleOperations.Builder flowBuilder = FlowRuleOperations.builder(); |
| 188 | |
| 189 | rules = processForward(fwd); |
| 190 | switch (fwd.op()) { |
| 191 | case ADD: |
| 192 | rules.stream() |
Sho SHIMIZU | 4590604 | 2016-01-13 23:05:54 -0800 | [diff] [blame] | 193 | .filter(Objects::nonNull) |
Saurav Das | e3274c8 | 2015-05-24 17:21:56 -0700 | [diff] [blame] | 194 | .forEach(flowBuilder::add); |
| 195 | break; |
| 196 | case REMOVE: |
| 197 | rules.stream() |
Sho SHIMIZU | 4590604 | 2016-01-13 23:05:54 -0800 | [diff] [blame] | 198 | .filter(Objects::nonNull) |
Saurav Das | e3274c8 | 2015-05-24 17:21:56 -0700 | [diff] [blame] | 199 | .forEach(flowBuilder::remove); |
| 200 | break; |
| 201 | default: |
| 202 | fail(fwd, ObjectiveError.UNKNOWN); |
| 203 | log.warn("Unknown forwarding type {}", fwd.op()); |
| 204 | } |
| 205 | |
| 206 | |
| 207 | flowRuleService.apply(flowBuilder.build(new FlowRuleOperationsContext() { |
| 208 | @Override |
| 209 | public void onSuccess(FlowRuleOperations ops) { |
| 210 | pass(fwd); |
| 211 | } |
| 212 | |
| 213 | @Override |
| 214 | public void onError(FlowRuleOperations ops) { |
| 215 | fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED); |
| 216 | } |
| 217 | })); |
| 218 | |
| 219 | } |
| 220 | |
| 221 | @Override |
| 222 | public void next(NextObjective nextObjective) { |
| 223 | switch (nextObjective.type()) { |
| 224 | case SIMPLE: |
| 225 | Collection<TrafficTreatment> treatments = nextObjective.next(); |
| 226 | if (treatments.size() == 1) { |
| 227 | TrafficTreatment treatment = treatments.iterator().next(); |
| 228 | |
| 229 | // Since we do not support strip_vlan in PORT_VLAN table, we use mod_vlan |
| 230 | // to modify the packet to desired vlan. |
| 231 | // Note: if we use push_vlan here, the switch will add a second VLAN tag to the outgoing |
| 232 | // packet, which is not what we want. |
| 233 | TrafficTreatment.Builder treatmentWithoutPushVlan = DefaultTrafficTreatment.builder(); |
| 234 | VlanId modVlanId; |
| 235 | for (Instruction ins : treatment.allInstructions()) { |
| 236 | if (ins.type() == Instruction.Type.L2MODIFICATION) { |
| 237 | L2ModificationInstruction l2ins = (L2ModificationInstruction) ins; |
| 238 | switch (l2ins.subtype()) { |
| 239 | case ETH_DST: |
| 240 | treatmentWithoutPushVlan.setEthDst( |
| 241 | ((L2ModificationInstruction.ModEtherInstruction) l2ins).mac()); |
| 242 | break; |
| 243 | case ETH_SRC: |
| 244 | treatmentWithoutPushVlan.setEthSrc( |
| 245 | ((L2ModificationInstruction.ModEtherInstruction) l2ins).mac()); |
| 246 | break; |
| 247 | case VLAN_ID: |
| 248 | modVlanId = ((L2ModificationInstruction.ModVlanIdInstruction) l2ins).vlanId(); |
| 249 | treatmentWithoutPushVlan.setVlanId(modVlanId); |
| 250 | break; |
| 251 | default: |
| 252 | break; |
| 253 | } |
| 254 | } else if (ins.type() == Instruction.Type.OUTPUT) { |
| 255 | //long portNum = ((Instructions.OutputInstruction) ins).port().toLong(); |
| 256 | treatmentWithoutPushVlan.add(ins); |
| 257 | } else { |
| 258 | // Ignore the vlan_pcp action since it's does matter much. |
| 259 | log.warn("Driver does not handle this type of TrafficTreatment" |
| 260 | + " instruction in nextObjectives: {}", ins.type()); |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | GroupBucket bucket = |
| 265 | DefaultGroupBucket.createIndirectGroupBucket(treatmentWithoutPushVlan.build()); |
| 266 | final GroupKey key = new DefaultGroupKey(appKryo.serialize(nextObjective.id())); |
| 267 | GroupDescription groupDescription |
| 268 | = new DefaultGroupDescription(deviceId, |
| 269 | GroupDescription.Type.INDIRECT, |
| 270 | new GroupBuckets(Collections |
| 271 | .singletonList(bucket)), |
| 272 | key, |
| 273 | null, // let group service determine group id |
| 274 | nextObjective.appId()); |
| 275 | groupService.addGroup(groupDescription); |
| 276 | pendingGroups.put(key, nextObjective); |
| 277 | } |
| 278 | break; |
| 279 | case HASHED: |
| 280 | case BROADCAST: |
| 281 | case FAILOVER: |
| 282 | fail(nextObjective, ObjectiveError.UNSUPPORTED); |
| 283 | log.warn("Unsupported next objective type {}", nextObjective.type()); |
| 284 | break; |
| 285 | default: |
| 286 | fail(nextObjective, ObjectiveError.UNKNOWN); |
| 287 | log.warn("Unknown next objective type {}", nextObjective.type()); |
| 288 | } |
| 289 | |
| 290 | } |
| 291 | |
| 292 | private Collection<FlowRule> processForward(ForwardingObjective fwd) { |
| 293 | switch (fwd.flag()) { |
| 294 | case SPECIFIC: |
| 295 | return processSpecific(fwd); |
| 296 | case VERSATILE: |
| 297 | return processVersatile(fwd); |
| 298 | default: |
| 299 | fail(fwd, ObjectiveError.UNKNOWN); |
| 300 | log.warn("Unknown forwarding flag {}", fwd.flag()); |
| 301 | } |
| 302 | return Collections.emptySet(); |
| 303 | } |
| 304 | |
| 305 | private Collection<FlowRule> processVersatile(ForwardingObjective fwd) { |
| 306 | log.warn("Driver does not support versatile forwarding objective"); |
| 307 | fail(fwd, ObjectiveError.UNSUPPORTED); |
| 308 | return Collections.emptySet(); |
| 309 | } |
| 310 | |
| 311 | private Collection<FlowRule> processSpecific(ForwardingObjective fwd) { |
| 312 | log.debug("Processing specific forwarding objective"); |
| 313 | TrafficSelector selector = fwd.selector(); |
| 314 | EthTypeCriterion ethType = |
| 315 | (EthTypeCriterion) selector.getCriterion(Criterion.Type.ETH_TYPE); |
alshabib | caf1ca2 | 2015-06-25 15:18:16 -0700 | [diff] [blame] | 316 | if (ethType == null || ethType.ethType().toShort() != Ethernet.TYPE_IPV4) { |
Saurav Das | e3274c8 | 2015-05-24 17:21:56 -0700 | [diff] [blame] | 317 | fail(fwd, ObjectiveError.UNSUPPORTED); |
| 318 | return Collections.emptySet(); |
| 319 | } |
| 320 | |
| 321 | // Must have metadata as key. |
| 322 | TrafficSelector filteredSelector = |
| 323 | DefaultTrafficSelector.builder() |
| 324 | .matchEthType(Ethernet.TYPE_IPV4) |
| 325 | .matchMetadata(DEFAULT_METADATA) |
| 326 | .matchIPDst( |
| 327 | ((IPCriterion) |
| 328 | selector.getCriterion(Criterion.Type.IPV4_DST)).ip()) |
| 329 | .build(); |
| 330 | |
| 331 | TrafficTreatment.Builder tb = DefaultTrafficTreatment.builder(); |
| 332 | |
| 333 | if (fwd.nextId() != null) { |
| 334 | NextGroup next = flowObjectiveStore.getNextGroup(fwd.nextId()); |
| 335 | GroupKey key = appKryo.deserialize(next.data()); |
| 336 | Group group = groupService.getGroup(deviceId, key); |
| 337 | if (group == null) { |
| 338 | log.warn("The group left!"); |
| 339 | fail(fwd, ObjectiveError.GROUPMISSING); |
| 340 | return Collections.emptySet(); |
| 341 | } |
| 342 | tb.group(group.id()); |
| 343 | } |
| 344 | |
| 345 | FlowRule.Builder ruleBuilder = DefaultFlowRule.builder() |
| 346 | .fromApp(fwd.appId()) |
| 347 | .withPriority(ROUTE_TABLE_PRIORITY) |
| 348 | .forDevice(deviceId) |
| 349 | .withSelector(filteredSelector) |
| 350 | .withTreatment(tb.build()); |
| 351 | |
| 352 | if (fwd.permanent()) { |
| 353 | ruleBuilder.makePermanent(); |
| 354 | } else { |
| 355 | ruleBuilder.makeTemporary(fwd.timeout()); |
| 356 | } |
| 357 | |
| 358 | ruleBuilder.forTable(ROUTE_TABLE); |
| 359 | |
| 360 | return Collections.singletonList(ruleBuilder.build()); |
| 361 | |
| 362 | } |
| 363 | |
| 364 | private void processFilter(FilteringObjective filt, boolean install, |
| 365 | ApplicationId applicationId) { |
| 366 | PortCriterion p; |
| 367 | if (!filt.key().equals(Criteria.dummy()) && |
| 368 | filt.key().type() == Criterion.Type.IN_PORT) { |
| 369 | p = (PortCriterion) filt.key(); |
| 370 | } else { |
| 371 | log.warn("No key defined in filtering objective from app: {}. Not" |
| 372 | + "processing filtering objective", applicationId); |
| 373 | fail(filt, ObjectiveError.UNKNOWN); |
| 374 | return; |
| 375 | } |
| 376 | |
| 377 | // Convert filtering conditions for switch-intfs into flow rules. |
| 378 | FlowRuleOperations.Builder ops = FlowRuleOperations.builder(); |
| 379 | |
| 380 | for (Criterion c : filt.conditions()) { |
| 381 | // Here we do a trick to install 2 flow rules to MAC_TABLE and ROUTE_TABLE. |
| 382 | if (c.type() == Criterion.Type.ETH_DST) { |
| 383 | EthCriterion e = (EthCriterion) c; |
| 384 | |
| 385 | // Install TMAC flow rule. |
| 386 | log.debug("adding rule for Termination MAC in Filter Table: {}", e.mac()); |
| 387 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
| 388 | TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder(); |
| 389 | selector.matchEthDst(e.mac()); |
| 390 | // Add IPv4 matching explicitly since we will redirect it to ROUTE Table |
| 391 | // through MAC table. |
| 392 | selector.matchEthType(Ethernet.TYPE_IPV4); |
| 393 | treatment.transition(MAC_TABLE); |
| 394 | FlowRule rule = DefaultFlowRule.builder() |
| 395 | .forDevice(deviceId) |
| 396 | .withSelector(selector.build()) |
| 397 | .withTreatment(treatment.build()) |
| 398 | .withPriority(FILTER_TABLE_TMAC_PRIORITY) |
| 399 | .fromApp(applicationId) |
| 400 | .makePermanent() |
| 401 | .forTable(FILTER_TABLE).build(); |
| 402 | ops = install ? ops.add(rule) : ops.remove(rule); |
| 403 | |
| 404 | // Must install another rule to direct the IPv4 packets that hit TMAC to |
| 405 | // Route table. |
| 406 | log.debug("adding rule for Termination MAC in MAC Table: {}", e.mac()); |
| 407 | selector = DefaultTrafficSelector.builder(); |
| 408 | treatment = DefaultTrafficTreatment.builder(); |
| 409 | selector.matchEthDst(e.mac()); |
| 410 | // MAC_Table must have metadata matching configured, use the default metadata. |
| 411 | selector.matchMetadata(DEFAULT_METADATA); |
| 412 | treatment.transition(ROUTE_TABLE); |
| 413 | rule = DefaultFlowRule.builder() |
| 414 | .forDevice(deviceId) |
| 415 | .withSelector(selector.build()) |
| 416 | .withTreatment(treatment.build()) |
| 417 | .withPriority(MAC_TABLE_PRIORITY) |
| 418 | .fromApp(applicationId) |
| 419 | .makePermanent() |
| 420 | .forTable(MAC_TABLE).build(); |
| 421 | ops = install ? ops.add(rule) : ops.remove(rule); |
| 422 | } else if (c.type() == Criterion.Type.VLAN_VID) { |
| 423 | VlanIdCriterion v = (VlanIdCriterion) c; |
| 424 | log.debug("adding rule for VLAN: {}", v.vlanId()); |
| 425 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
| 426 | TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder(); |
| 427 | selector.matchVlanId(v.vlanId()); |
| 428 | selector.matchInPort(p.port()); |
| 429 | // Although the accepted packets will be sent to filter table, we must |
| 430 | // explicitly set goto_table instruction here. |
Saurav Das | 86af8f1 | 2015-05-25 23:55:33 -0700 | [diff] [blame] | 431 | treatment.writeMetadata(DEFAULT_METADATA, DEFAULT_METADATA_MASK); |
| 432 | // set default metadata written by PORT_VLAN Table. |
Saurav Das | e3274c8 | 2015-05-24 17:21:56 -0700 | [diff] [blame] | 433 | treatment.transition(FILTER_TABLE); |
| 434 | // We do not support strip vlan here, treatment.deferred().popVlan(); |
Saurav Das | e3274c8 | 2015-05-24 17:21:56 -0700 | [diff] [blame] | 435 | // PORT_VLAN table only accept 0xffff priority since it does exact match only. |
| 436 | FlowRule rule = DefaultFlowRule.builder() |
| 437 | .forDevice(deviceId) |
| 438 | .withSelector(selector.build()) |
| 439 | .withTreatment(treatment.build()) |
| 440 | .withPriority(PORT_VLAN_TABLE_PRIORITY) |
| 441 | .fromApp(applicationId) |
| 442 | .makePermanent() |
| 443 | .forTable(PORT_VLAN_TABLE).build(); |
| 444 | ops = install ? ops.add(rule) : ops.remove(rule); |
| 445 | } else if (c.type() == Criterion.Type.IPV4_DST) { |
Saurav Das | 6c44a63 | 2015-05-30 22:05:22 -0700 | [diff] [blame] | 446 | IPCriterion ipaddr = (IPCriterion) c; |
| 447 | log.debug("adding IP filtering rules in FILTER table: {}", ipaddr.ip()); |
| 448 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
| 449 | TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder(); |
| 450 | selector.matchEthType(Ethernet.TYPE_IPV4); |
| 451 | selector.matchIPDst(ipaddr.ip()); // router IPs to the controller |
| 452 | treatment.setOutput(PortNumber.CONTROLLER); |
| 453 | FlowRule rule = DefaultFlowRule.builder() |
| 454 | .forDevice(deviceId) |
| 455 | .withSelector(selector.build()) |
| 456 | .withTreatment(treatment.build()) |
| 457 | .withPriority(FILTER_TABLE_CONTROLLER_PRIORITY) |
| 458 | .fromApp(applicationId) |
| 459 | .makePermanent() |
| 460 | .forTable(FILTER_TABLE).build(); |
| 461 | ops = install ? ops.add(rule) : ops.remove(rule); |
Saurav Das | e3274c8 | 2015-05-24 17:21:56 -0700 | [diff] [blame] | 462 | } else { |
| 463 | log.warn("Driver does not currently process filtering condition" |
| 464 | + " of type: {}", c.type()); |
| 465 | fail(filt, ObjectiveError.UNSUPPORTED); |
| 466 | } |
| 467 | } |
| 468 | |
| 469 | // apply filtering flow rules |
| 470 | flowRuleService.apply(ops.build(new FlowRuleOperationsContext() { |
| 471 | @Override |
| 472 | public void onSuccess(FlowRuleOperations ops) { |
| 473 | pass(filt); |
| 474 | log.info("Applied filtering rules"); |
| 475 | } |
| 476 | |
| 477 | @Override |
| 478 | public void onError(FlowRuleOperations ops) { |
| 479 | fail(filt, ObjectiveError.FLOWINSTALLATIONFAILED); |
| 480 | log.info("Failed to apply filtering rules"); |
| 481 | } |
| 482 | })); |
| 483 | } |
| 484 | |
| 485 | private void pass(Objective obj) { |
Sho SHIMIZU | ef7e290 | 2016-02-12 18:38:29 -0800 | [diff] [blame] | 486 | obj.context().ifPresent(context -> context.onSuccess(obj)); |
Saurav Das | e3274c8 | 2015-05-24 17:21:56 -0700 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | private void fail(Objective obj, ObjectiveError error) { |
Sho SHIMIZU | ef7e290 | 2016-02-12 18:38:29 -0800 | [diff] [blame] | 490 | obj.context().ifPresent(context -> context.onError(obj, error)); |
Saurav Das | e3274c8 | 2015-05-24 17:21:56 -0700 | [diff] [blame] | 491 | } |
| 492 | |
| 493 | private void initializePipeline() { |
| 494 | // CENTEC_V350: PORT_VLAN_TABLE->FILTER_TABLE->MAC_TABLE(TMAC)->ROUTE_TABLE. |
| 495 | processPortVlanTable(true); |
| 496 | processFilterTable(true); |
| 497 | } |
| 498 | |
| 499 | private void processPortVlanTable(boolean install) { |
| 500 | // By default the packet are dropped, need install port+vlan by some ways. |
| 501 | |
| 502 | // XXX can we add table-miss-entry to drop? Code says drops by default |
| 503 | // XXX TTP description says default goes to table1. |
| 504 | // It also says that match is only on vlan -- not port-vlan -- which one is true? |
| 505 | } |
| 506 | |
| 507 | private void processFilterTable(boolean install) { |
| 508 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
| 509 | TrafficTreatment.Builder treatment = DefaultTrafficTreatment |
| 510 | .builder(); |
| 511 | FlowRuleOperations.Builder ops = FlowRuleOperations.builder(); |
| 512 | FlowRule rule; |
| 513 | |
| 514 | // Punt ARP packets to controller by default. |
| 515 | selector.matchEthType(Ethernet.TYPE_ARP); |
| 516 | treatment.punt(); |
| 517 | rule = DefaultFlowRule.builder() |
| 518 | .forDevice(deviceId) |
| 519 | .withSelector(selector.build()) |
| 520 | .withTreatment(treatment.build()) |
| 521 | .withPriority(FILTER_TABLE_CONTROLLER_PRIORITY) |
| 522 | .fromApp(appId) |
| 523 | .makePermanent() |
| 524 | .forTable(FILTER_TABLE).build(); |
| 525 | ops = install ? ops.add(rule) : ops.remove(rule); |
| 526 | |
| 527 | // Punt BGP packets to controller directly. |
| 528 | selector = DefaultTrafficSelector.builder(); |
| 529 | treatment = DefaultTrafficTreatment.builder(); |
| 530 | selector.matchEthType(Ethernet.TYPE_IPV4) |
| 531 | .matchIPProtocol(IPv4.PROTOCOL_TCP) |
Hyunsun Moon | cf732fb | 2015-08-22 21:04:23 -0700 | [diff] [blame] | 532 | .matchTcpSrc(TpPort.tpPort(BGP_PORT)); |
Saurav Das | e3274c8 | 2015-05-24 17:21:56 -0700 | [diff] [blame] | 533 | treatment.punt(); |
| 534 | rule = DefaultFlowRule.builder() |
| 535 | .forDevice(deviceId) |
| 536 | .withPriority(FILTER_TABLE_HIGHEST_PRIORITY) |
| 537 | .withSelector(selector.build()) |
| 538 | .withTreatment(treatment.build()) |
| 539 | .fromApp(appId) |
| 540 | .makePermanent() |
| 541 | .forTable(FILTER_TABLE).build(); |
| 542 | ops = install ? ops.add(rule) : ops.remove(rule); |
| 543 | |
| 544 | selector = DefaultTrafficSelector.builder(); |
| 545 | treatment = DefaultTrafficTreatment.builder(); |
| 546 | selector.matchEthType(Ethernet.TYPE_IPV4) |
| 547 | .matchIPProtocol(IPv4.PROTOCOL_TCP) |
Hyunsun Moon | cf732fb | 2015-08-22 21:04:23 -0700 | [diff] [blame] | 548 | .matchTcpDst(TpPort.tpPort(BGP_PORT)); |
Saurav Das | e3274c8 | 2015-05-24 17:21:56 -0700 | [diff] [blame] | 549 | treatment.punt(); |
| 550 | rule = DefaultFlowRule.builder() |
| 551 | .forDevice(deviceId) |
| 552 | .withPriority(FILTER_TABLE_HIGHEST_PRIORITY) |
| 553 | .withSelector(selector.build()) |
| 554 | .withTreatment(treatment.build()) |
| 555 | .fromApp(appId) |
| 556 | .makePermanent() |
| 557 | .forTable(FILTER_TABLE).build(); |
| 558 | |
| 559 | ops = install ? ops.add(rule) : ops.remove(rule); |
| 560 | |
| 561 | // Packet will be discard in PORT_VLAN table, no need to install rule in |
| 562 | // filter table. |
| 563 | |
| 564 | // XXX does not tell me if packets are going to be dropped by default in |
| 565 | // filter table or not? TTP says it will be dropped by default |
| 566 | |
| 567 | flowRuleService.apply(ops.build(new FlowRuleOperationsContext() { |
| 568 | @Override |
| 569 | public void onSuccess(FlowRuleOperations ops) { |
| 570 | log.info("Provisioned filter table"); |
| 571 | } |
| 572 | |
| 573 | @Override |
| 574 | public void onError(FlowRuleOperations ops) { |
| 575 | log.info("Failed to provision filter table"); |
| 576 | } |
| 577 | })); |
| 578 | } |
| 579 | |
| 580 | private class InnerGroupListener implements GroupListener { |
| 581 | @Override |
| 582 | public void event(GroupEvent event) { |
| 583 | if (event.type() == GroupEvent.Type.GROUP_ADDED) { |
| 584 | GroupKey key = event.subject().appCookie(); |
| 585 | |
| 586 | NextObjective obj = pendingGroups.getIfPresent(key); |
| 587 | if (obj != null) { |
| 588 | flowObjectiveStore.putNextGroup(obj.id(), new CentecV350Group(key)); |
| 589 | pass(obj); |
| 590 | pendingGroups.invalidate(key); |
| 591 | } |
| 592 | } |
| 593 | } |
| 594 | } |
| 595 | |
| 596 | |
| 597 | private class GroupChecker implements Runnable { |
| 598 | |
| 599 | @Override |
| 600 | public void run() { |
| 601 | Set<GroupKey> keys = pendingGroups.asMap().keySet().stream() |
| 602 | .filter(key -> groupService.getGroup(deviceId, key) != null) |
| 603 | .collect(Collectors.toSet()); |
| 604 | |
Sho SHIMIZU | a09e1bb | 2016-08-01 14:25:25 -0700 | [diff] [blame] | 605 | keys.forEach(key -> { |
Saurav Das | e3274c8 | 2015-05-24 17:21:56 -0700 | [diff] [blame] | 606 | NextObjective obj = pendingGroups.getIfPresent(key); |
| 607 | if (obj == null) { |
| 608 | return; |
| 609 | } |
| 610 | pass(obj); |
| 611 | pendingGroups.invalidate(key); |
| 612 | log.info("Heard back from group service for group {}. " |
| 613 | + "Applying pending forwarding objectives", obj.id()); |
| 614 | flowObjectiveStore.putNextGroup(obj.id(), new CentecV350Group(key)); |
| 615 | }); |
| 616 | } |
| 617 | } |
| 618 | |
| 619 | private class CentecV350Group implements NextGroup { |
| 620 | |
| 621 | private final GroupKey key; |
| 622 | |
| 623 | public CentecV350Group(GroupKey key) { |
| 624 | this.key = key; |
| 625 | } |
| 626 | |
| 627 | @SuppressWarnings("unused") |
| 628 | public GroupKey key() { |
| 629 | return key; |
| 630 | } |
| 631 | |
| 632 | @Override |
| 633 | public byte[] data() { |
| 634 | return appKryo.serialize(key); |
| 635 | } |
| 636 | |
| 637 | } |
Saurav Das | 2443119 | 2016-03-07 19:13:00 -0800 | [diff] [blame] | 638 | |
| 639 | @Override |
| 640 | public List<String> getNextMappings(NextGroup nextGroup) { |
| 641 | // TODO Implementation deferred to vendor |
| 642 | return null; |
| 643 | } |
Saurav Das | e3274c8 | 2015-05-24 17:21:56 -0700 | [diff] [blame] | 644 | } |