Saurav Das | 5202596 | 2016-01-28 22:30:01 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Saurav Das | 5202596 | 2016-01-28 22:30:01 -0800 | [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 | */ |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 16 | package org.onosproject.driver.pipeline.ofdpa; |
Saurav Das | 5202596 | 2016-01-28 22:30:01 -0800 | [diff] [blame] | 17 | |
| 18 | import static org.slf4j.LoggerFactory.getLogger; |
| 19 | |
| 20 | import java.util.ArrayList; |
| 21 | import java.util.Collection; |
| 22 | import java.util.Collections; |
| 23 | import java.util.Deque; |
| 24 | import java.util.List; |
Charles Chan | 206506b | 2018-03-02 16:43:28 -0800 | [diff] [blame^] | 25 | |
| 26 | import com.google.common.collect.ImmutableList; |
Saurav Das | 5202596 | 2016-01-28 22:30:01 -0800 | [diff] [blame] | 27 | import org.onlab.packet.Ethernet; |
| 28 | import org.onlab.packet.VlanId; |
| 29 | import org.onosproject.core.ApplicationId; |
| 30 | import org.onosproject.net.Port; |
| 31 | import org.onosproject.net.PortNumber; |
| 32 | import org.onosproject.net.behaviour.NextGroup; |
Charles Chan | 425854b | 2016-04-11 15:32:12 -0700 | [diff] [blame] | 33 | import org.onosproject.net.behaviour.PipelinerContext; |
Saurav Das | 5202596 | 2016-01-28 22:30:01 -0800 | [diff] [blame] | 34 | import org.onosproject.net.flow.DefaultFlowRule; |
| 35 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 36 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 37 | import org.onosproject.net.flow.FlowRule; |
| 38 | import org.onosproject.net.flow.TrafficSelector; |
| 39 | import org.onosproject.net.flow.TrafficTreatment; |
| 40 | import org.onosproject.net.flow.criteria.Criteria; |
| 41 | import org.onosproject.net.flow.criteria.Criterion; |
| 42 | import org.onosproject.net.flow.criteria.EthCriterion; |
| 43 | import org.onosproject.net.flow.criteria.EthTypeCriterion; |
| 44 | import org.onosproject.net.flow.criteria.PortCriterion; |
| 45 | import org.onosproject.net.flow.criteria.VlanIdCriterion; |
| 46 | import org.onosproject.net.flow.instructions.Instruction; |
| 47 | import org.onosproject.net.flow.instructions.Instructions.OutputInstruction; |
| 48 | import org.onosproject.net.flowobjective.ForwardingObjective; |
| 49 | import org.onosproject.net.flowobjective.ObjectiveError; |
| 50 | import org.onosproject.net.group.Group; |
| 51 | import org.onosproject.net.group.GroupKey; |
| 52 | import org.slf4j.Logger; |
| 53 | |
| 54 | |
| 55 | /** |
| 56 | * Driver for software switch emulation of the OFDPA 2.0 pipeline. |
| 57 | * The software switch is the CPqD OF 1.3 switch. Unfortunately the CPqD switch |
| 58 | * does not handle vlan tags and mpls labels simultaneously, which requires us |
| 59 | * to do some workarounds in the driver. This driver is meant for the use of |
| 60 | * the cpqd switch when MPLS is not a requirement from the ofdpa pipeline. As a |
| 61 | * result this driver correctly handles both incoming untagged and vlan-tagged |
| 62 | * packets. |
| 63 | * |
| 64 | */ |
Charles Chan | 361154b | 2016-03-24 10:23:39 -0700 | [diff] [blame] | 65 | public class CpqdOfdpa2VlanPipeline extends CpqdOfdpa2Pipeline { |
Saurav Das | 5202596 | 2016-01-28 22:30:01 -0800 | [diff] [blame] | 66 | |
| 67 | private final Logger log = getLogger(getClass()); |
| 68 | |
Charles Chan | 425854b | 2016-04-11 15:32:12 -0700 | [diff] [blame] | 69 | @Override |
Charles Chan | 40132b3 | 2017-01-22 00:19:37 -0800 | [diff] [blame] | 70 | protected void initDriverId() { |
Charles Chan | 425854b | 2016-04-11 15:32:12 -0700 | [diff] [blame] | 71 | driverId = coreService.registerApplication( |
| 72 | "org.onosproject.driver.CpqdOfdpa2VlanPipeline"); |
Charles Chan | 40132b3 | 2017-01-22 00:19:37 -0800 | [diff] [blame] | 73 | } |
Charles Chan | 425854b | 2016-04-11 15:32:12 -0700 | [diff] [blame] | 74 | |
Charles Chan | 40132b3 | 2017-01-22 00:19:37 -0800 | [diff] [blame] | 75 | @Override |
| 76 | protected void initGroupHander(PipelinerContext context) { |
| 77 | groupHandler = new CpqdOfdpa2GroupHandler(); |
| 78 | groupHandler.init(deviceId, context); |
Charles Chan | 425854b | 2016-04-11 15:32:12 -0700 | [diff] [blame] | 79 | } |
| 80 | |
Saurav Das | 5202596 | 2016-01-28 22:30:01 -0800 | [diff] [blame] | 81 | /* |
| 82 | * Cpqd emulation does not handle vlan tags and mpls labels correctly. |
| 83 | * Since this driver does not deal with MPLS, there is no need for |
| 84 | * working around VLAN tags. In particular we do not pop off vlan tags in |
| 85 | * the middle of the pipeline. |
| 86 | * |
| 87 | * (non-Javadoc) |
| 88 | * @see org.onosproject.driver.pipeline.OFDPA2Pipeline#processEthDstFilter |
| 89 | */ |
| 90 | @Override |
Charles Chan | 206506b | 2018-03-02 16:43:28 -0800 | [diff] [blame^] | 91 | protected List<List<FlowRule>> processEthDstFilter(PortCriterion portCriterion, |
Saurav Das | 5202596 | 2016-01-28 22:30:01 -0800 | [diff] [blame] | 92 | EthCriterion ethCriterion, |
| 93 | VlanIdCriterion vidCriterion, |
| 94 | VlanId assignedVlan, |
| 95 | ApplicationId applicationId) { |
Charles Chan | 5270ed0 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 96 | // Consider PortNumber.ANY as wildcard. Match ETH_DST only |
| 97 | if (portCriterion != null && portCriterion.port() == PortNumber.ANY) { |
| 98 | return processEthDstOnlyFilter(ethCriterion, applicationId); |
| 99 | } |
| 100 | |
Charles Chan | 5b9df8d | 2016-03-28 22:21:40 -0700 | [diff] [blame] | 101 | // Multicast MAC |
| 102 | if (ethCriterion.mask() != null) { |
Charles Chan | b4879a5 | 2017-10-20 19:09:16 -0700 | [diff] [blame] | 103 | return processMcastEthDstFilter(ethCriterion, assignedVlan, applicationId); |
Charles Chan | 5b9df8d | 2016-03-28 22:21:40 -0700 | [diff] [blame] | 104 | } |
| 105 | |
Saurav Das | 5202596 | 2016-01-28 22:30:01 -0800 | [diff] [blame] | 106 | //handling untagged packets via assigned VLAN |
| 107 | if (vidCriterion.vlanId() == VlanId.NONE) { |
| 108 | vidCriterion = (VlanIdCriterion) Criteria.matchVlanId(assignedVlan); |
| 109 | } |
| 110 | // ofdpa cannot match on ALL portnumber, so we need to use separate |
| 111 | // rules for each port. |
Charles Chan | 9309035 | 2018-03-02 13:26:22 -0800 | [diff] [blame] | 112 | List<PortNumber> portnums = new ArrayList<>(); |
Ray Milkey | ae4e5ed | 2018-01-17 15:24:52 -0800 | [diff] [blame] | 113 | if (portCriterion != null) { |
| 114 | if (portCriterion.port() == PortNumber.ALL) { |
| 115 | for (Port port : deviceService.getPorts(deviceId)) { |
| 116 | if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) { |
| 117 | portnums.add(port.number()); |
| 118 | } |
Saurav Das | 5202596 | 2016-01-28 22:30:01 -0800 | [diff] [blame] | 119 | } |
Ray Milkey | ae4e5ed | 2018-01-17 15:24:52 -0800 | [diff] [blame] | 120 | } else { |
| 121 | portnums.add(portCriterion.port()); |
Saurav Das | 5202596 | 2016-01-28 22:30:01 -0800 | [diff] [blame] | 122 | } |
Saurav Das | 5202596 | 2016-01-28 22:30:01 -0800 | [diff] [blame] | 123 | } |
| 124 | |
Charles Chan | 9309035 | 2018-03-02 13:26:22 -0800 | [diff] [blame] | 125 | List<FlowRule> rules = new ArrayList<>(); |
Saurav Das | 5202596 | 2016-01-28 22:30:01 -0800 | [diff] [blame] | 126 | for (PortNumber pnum : portnums) { |
| 127 | // for unicast IP packets |
| 128 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
| 129 | TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder(); |
| 130 | selector.matchInPort(pnum); |
| 131 | selector.matchVlanId(vidCriterion.vlanId()); |
| 132 | selector.matchEthType(Ethernet.TYPE_IPV4); |
| 133 | selector.matchEthDst(ethCriterion.mac()); |
| 134 | |
| 135 | treatment.transition(UNICAST_ROUTING_TABLE); |
| 136 | FlowRule rule = DefaultFlowRule.builder() |
| 137 | .forDevice(deviceId) |
| 138 | .withSelector(selector.build()) |
| 139 | .withTreatment(treatment.build()) |
| 140 | .withPriority(DEFAULT_PRIORITY) |
| 141 | .fromApp(applicationId) |
| 142 | .makePermanent() |
| 143 | .forTable(TMAC_TABLE).build(); |
| 144 | rules.add(rule); |
| 145 | } |
Charles Chan | 206506b | 2018-03-02 16:43:28 -0800 | [diff] [blame^] | 146 | return ImmutableList.of(rules); |
Saurav Das | 5202596 | 2016-01-28 22:30:01 -0800 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | /* |
| 150 | * In the OF-DPA 2.0 pipeline, versatile forwarding objectives go to the |
| 151 | * ACL table. Since we do not pop off vlans in the TMAC table we can continue |
| 152 | * to match on vlans in the ACL table if necessary. |
| 153 | */ |
| 154 | @Override |
| 155 | protected Collection<FlowRule> processVersatile(ForwardingObjective fwd) { |
| 156 | log.info("Processing versatile forwarding objective"); |
| 157 | |
| 158 | EthTypeCriterion ethType = |
| 159 | (EthTypeCriterion) fwd.selector().getCriterion(Criterion.Type.ETH_TYPE); |
| 160 | if (ethType == null) { |
| 161 | log.error("Versatile forwarding objective must include ethType"); |
| 162 | fail(fwd, ObjectiveError.BADPARAMS); |
| 163 | return Collections.emptySet(); |
| 164 | } |
| 165 | if (fwd.nextId() == null && fwd.treatment() == null) { |
| 166 | log.error("Forwarding objective {} from {} must contain " |
| 167 | + "nextId or Treatment", fwd.selector(), fwd.appId()); |
| 168 | return Collections.emptySet(); |
| 169 | } |
| 170 | |
| 171 | TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder(); |
| 172 | fwd.selector().criteria().forEach(criterion -> { |
| 173 | if (criterion instanceof VlanIdCriterion) { |
| 174 | VlanId vlanId = ((VlanIdCriterion) criterion).vlanId(); |
| 175 | // ensure that match does not include vlan = NONE as OF-DPA does not |
| 176 | // match untagged packets this way in the ACL table. |
| 177 | if (vlanId.equals(VlanId.NONE)) { |
| 178 | return; |
| 179 | } |
| 180 | } |
| 181 | sbuilder.add(criterion); |
| 182 | }); |
| 183 | |
| 184 | // XXX driver does not currently do type checking as per Tables 65-67 in |
| 185 | // OFDPA 2.0 spec. The only allowed treatment is a punt to the controller. |
| 186 | TrafficTreatment.Builder ttBuilder = DefaultTrafficTreatment.builder(); |
| 187 | if (fwd.treatment() != null) { |
| 188 | for (Instruction ins : fwd.treatment().allInstructions()) { |
| 189 | if (ins instanceof OutputInstruction) { |
| 190 | OutputInstruction o = (OutputInstruction) ins; |
| 191 | if (o.port() == PortNumber.CONTROLLER) { |
| 192 | ttBuilder.add(o); |
| 193 | } else { |
| 194 | log.warn("Only allowed treatments in versatile forwarding " |
| 195 | + "objectives are punts to the controller"); |
| 196 | } |
| 197 | } else { |
| 198 | log.warn("Cannot process instruction in versatile fwd {}", ins); |
| 199 | } |
| 200 | } |
| 201 | } |
| 202 | if (fwd.nextId() != null) { |
| 203 | // overide case |
| 204 | NextGroup next = getGroupForNextObjective(fwd.nextId()); |
| 205 | List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data()); |
| 206 | // we only need the top level group's key to point the flow to it |
| 207 | Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst()); |
| 208 | if (group == null) { |
| 209 | log.warn("Group with key:{} for next-id:{} not found in dev:{}", |
| 210 | gkeys.get(0).peekFirst(), fwd.nextId(), deviceId); |
| 211 | fail(fwd, ObjectiveError.GROUPMISSING); |
| 212 | return Collections.emptySet(); |
| 213 | } |
| 214 | ttBuilder.deferred().group(group.id()); |
| 215 | } |
| 216 | |
| 217 | FlowRule.Builder ruleBuilder = DefaultFlowRule.builder() |
| 218 | .fromApp(fwd.appId()) |
| 219 | .withPriority(fwd.priority()) |
| 220 | .forDevice(deviceId) |
| 221 | .withSelector(sbuilder.build()) |
| 222 | .withTreatment(ttBuilder.build()) |
| 223 | .makePermanent() |
| 224 | .forTable(ACL_TABLE); |
| 225 | return Collections.singletonList(ruleBuilder.build()); |
| 226 | } |
| 227 | |
| 228 | |
| 229 | } |