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