blob: 36be98dcbd5e6c6bb96e3edf7dcc97d475ecec68 [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;
Charles Chan3aed59a2018-03-02 16:43:28 -080025
26import com.google.common.collect.ImmutableList;
Saurav Das52025962016-01-28 22:30:01 -080027import org.onlab.packet.Ethernet;
28import org.onlab.packet.VlanId;
29import org.onosproject.core.ApplicationId;
30import org.onosproject.net.Port;
31import org.onosproject.net.PortNumber;
32import org.onosproject.net.behaviour.NextGroup;
Charles Chan425854b2016-04-11 15:32:12 -070033import org.onosproject.net.behaviour.PipelinerContext;
Saurav Das52025962016-01-28 22:30:01 -080034import org.onosproject.net.flow.DefaultFlowRule;
35import org.onosproject.net.flow.DefaultTrafficSelector;
36import org.onosproject.net.flow.DefaultTrafficTreatment;
37import org.onosproject.net.flow.FlowRule;
38import org.onosproject.net.flow.TrafficSelector;
39import org.onosproject.net.flow.TrafficTreatment;
40import org.onosproject.net.flow.criteria.Criteria;
41import org.onosproject.net.flow.criteria.Criterion;
42import org.onosproject.net.flow.criteria.EthCriterion;
43import org.onosproject.net.flow.criteria.EthTypeCriterion;
44import org.onosproject.net.flow.criteria.PortCriterion;
45import org.onosproject.net.flow.criteria.VlanIdCriterion;
46import org.onosproject.net.flow.instructions.Instruction;
47import org.onosproject.net.flow.instructions.Instructions.OutputInstruction;
48import org.onosproject.net.flowobjective.ForwardingObjective;
49import org.onosproject.net.flowobjective.ObjectiveError;
50import org.onosproject.net.group.Group;
51import org.onosproject.net.group.GroupKey;
52import 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 Chan361154b2016-03-24 10:23:39 -070065public class CpqdOfdpa2VlanPipeline extends CpqdOfdpa2Pipeline {
Saurav Das52025962016-01-28 22:30:01 -080066
67 private final Logger log = getLogger(getClass());
68
Charles Chan425854b2016-04-11 15:32:12 -070069 @Override
Charles Chan40132b32017-01-22 00:19:37 -080070 protected void initDriverId() {
Charles Chan425854b2016-04-11 15:32:12 -070071 driverId = coreService.registerApplication(
72 "org.onosproject.driver.CpqdOfdpa2VlanPipeline");
Charles Chan40132b32017-01-22 00:19:37 -080073 }
Charles Chan425854b2016-04-11 15:32:12 -070074
Charles Chan40132b32017-01-22 00:19:37 -080075 @Override
76 protected void initGroupHander(PipelinerContext context) {
77 groupHandler = new CpqdOfdpa2GroupHandler();
78 groupHandler.init(deviceId, context);
Charles Chan425854b2016-04-11 15:32:12 -070079 }
80
Saurav Das52025962016-01-28 22:30:01 -080081 /*
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 Chan3aed59a2018-03-02 16:43:28 -080091 protected List<List<FlowRule>> processEthDstFilter(PortCriterion portCriterion,
Saurav Das52025962016-01-28 22:30:01 -080092 EthCriterion ethCriterion,
93 VlanIdCriterion vidCriterion,
94 VlanId assignedVlan,
95 ApplicationId applicationId) {
Charles Chan5270ed02016-01-30 23:22:37 -080096 // 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 Chan5b9df8d2016-03-28 22:21:40 -0700101 // Multicast MAC
102 if (ethCriterion.mask() != null) {
Charles Chan206cac02017-10-20 19:09:16 -0700103 return processMcastEthDstFilter(ethCriterion, assignedVlan, applicationId);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700104 }
105
Saurav Das52025962016-01-28 22:30:01 -0800106 //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 Chanc6e64bb2018-03-02 13:26:22 -0800112 List<PortNumber> portnums = new ArrayList<>();
Ray Milkeyfd4f8d32018-01-17 15:24:52 -0800113 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 Das52025962016-01-28 22:30:01 -0800119 }
Ray Milkeyfd4f8d32018-01-17 15:24:52 -0800120 } else {
121 portnums.add(portCriterion.port());
Saurav Das52025962016-01-28 22:30:01 -0800122 }
Saurav Das52025962016-01-28 22:30:01 -0800123 }
124
Charles Chanc6e64bb2018-03-02 13:26:22 -0800125 List<FlowRule> rules = new ArrayList<>();
Saurav Das52025962016-01-28 22:30:01 -0800126 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 Chan3aed59a2018-03-02 16:43:28 -0800146 return ImmutableList.of(rules);
Saurav Das52025962016-01-28 22:30:01 -0800147 }
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}