blob: 7219fc6822ada68573e61e2ca42715d28d1af799 [file] [log] [blame]
Saurav Das52025962016-01-28 22:30:01 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
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) {
101 return processMcastEthDstFilter(ethCriterion, applicationId);
102 }
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>();
111 if (portCriterion.port() == PortNumber.ALL) {
112 for (Port port : deviceService.getPorts(deviceId)) {
113 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
114 portnums.add(port.number());
115 }
116 }
117 } else {
118 portnums.add(portCriterion.port());
119 }
120
121 List<FlowRule> rules = new ArrayList<FlowRule>();
122 for (PortNumber pnum : portnums) {
123 // for unicast IP packets
124 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
125 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
126 selector.matchInPort(pnum);
127 selector.matchVlanId(vidCriterion.vlanId());
128 selector.matchEthType(Ethernet.TYPE_IPV4);
129 selector.matchEthDst(ethCriterion.mac());
130
131 treatment.transition(UNICAST_ROUTING_TABLE);
132 FlowRule rule = DefaultFlowRule.builder()
133 .forDevice(deviceId)
134 .withSelector(selector.build())
135 .withTreatment(treatment.build())
136 .withPriority(DEFAULT_PRIORITY)
137 .fromApp(applicationId)
138 .makePermanent()
139 .forTable(TMAC_TABLE).build();
140 rules.add(rule);
141 }
142 return rules;
143 }
144
145 /*
146 * In the OF-DPA 2.0 pipeline, versatile forwarding objectives go to the
147 * ACL table. Since we do not pop off vlans in the TMAC table we can continue
148 * to match on vlans in the ACL table if necessary.
149 */
150 @Override
151 protected Collection<FlowRule> processVersatile(ForwardingObjective fwd) {
152 log.info("Processing versatile forwarding objective");
153
154 EthTypeCriterion ethType =
155 (EthTypeCriterion) fwd.selector().getCriterion(Criterion.Type.ETH_TYPE);
156 if (ethType == null) {
157 log.error("Versatile forwarding objective must include ethType");
158 fail(fwd, ObjectiveError.BADPARAMS);
159 return Collections.emptySet();
160 }
161 if (fwd.nextId() == null && fwd.treatment() == null) {
162 log.error("Forwarding objective {} from {} must contain "
163 + "nextId or Treatment", fwd.selector(), fwd.appId());
164 return Collections.emptySet();
165 }
166
167 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
168 fwd.selector().criteria().forEach(criterion -> {
169 if (criterion instanceof VlanIdCriterion) {
170 VlanId vlanId = ((VlanIdCriterion) criterion).vlanId();
171 // ensure that match does not include vlan = NONE as OF-DPA does not
172 // match untagged packets this way in the ACL table.
173 if (vlanId.equals(VlanId.NONE)) {
174 return;
175 }
176 }
177 sbuilder.add(criterion);
178 });
179
180 // XXX driver does not currently do type checking as per Tables 65-67 in
181 // OFDPA 2.0 spec. The only allowed treatment is a punt to the controller.
182 TrafficTreatment.Builder ttBuilder = DefaultTrafficTreatment.builder();
183 if (fwd.treatment() != null) {
184 for (Instruction ins : fwd.treatment().allInstructions()) {
185 if (ins instanceof OutputInstruction) {
186 OutputInstruction o = (OutputInstruction) ins;
187 if (o.port() == PortNumber.CONTROLLER) {
188 ttBuilder.add(o);
189 } else {
190 log.warn("Only allowed treatments in versatile forwarding "
191 + "objectives are punts to the controller");
192 }
193 } else {
194 log.warn("Cannot process instruction in versatile fwd {}", ins);
195 }
196 }
197 }
198 if (fwd.nextId() != null) {
199 // overide case
200 NextGroup next = getGroupForNextObjective(fwd.nextId());
201 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
202 // we only need the top level group's key to point the flow to it
203 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
204 if (group == null) {
205 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
206 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
207 fail(fwd, ObjectiveError.GROUPMISSING);
208 return Collections.emptySet();
209 }
210 ttBuilder.deferred().group(group.id());
211 }
212
213 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
214 .fromApp(fwd.appId())
215 .withPriority(fwd.priority())
216 .forDevice(deviceId)
217 .withSelector(sbuilder.build())
218 .withTreatment(ttBuilder.build())
219 .makePermanent()
220 .forTable(ACL_TABLE);
221 return Collections.singletonList(ruleBuilder.build());
222 }
223
224
225}