blob: e9dc316417e2a7a99951f4f7504398d0af954506 [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 Chan66291502018-03-02 16:43:28 -080025
26import com.google.common.collect.ImmutableList;
Saurav Das52025962016-01-28 22:30:01 -080027import org.onlab.packet.Ethernet;
Charles Chan45b69ab2018-03-02 15:41:41 -080028import org.onlab.packet.MacAddress;
Saurav Das52025962016-01-28 22:30:01 -080029import org.onlab.packet.VlanId;
30import org.onosproject.core.ApplicationId;
31import org.onosproject.net.Port;
32import org.onosproject.net.PortNumber;
33import org.onosproject.net.behaviour.NextGroup;
Charles Chan425854b2016-04-11 15:32:12 -070034import org.onosproject.net.behaviour.PipelinerContext;
Saurav Das52025962016-01-28 22:30:01 -080035import org.onosproject.net.flow.DefaultFlowRule;
36import org.onosproject.net.flow.DefaultTrafficSelector;
37import org.onosproject.net.flow.DefaultTrafficTreatment;
38import org.onosproject.net.flow.FlowRule;
39import org.onosproject.net.flow.TrafficSelector;
40import org.onosproject.net.flow.TrafficTreatment;
41import org.onosproject.net.flow.criteria.Criteria;
Saurav Das52025962016-01-28 22:30:01 -080042import org.onosproject.net.flow.criteria.EthCriterion;
Saurav Das52025962016-01-28 22:30:01 -080043import org.onosproject.net.flow.criteria.PortCriterion;
44import org.onosproject.net.flow.criteria.VlanIdCriterion;
45import org.onosproject.net.flow.instructions.Instruction;
46import org.onosproject.net.flow.instructions.Instructions.OutputInstruction;
Charles Chanf76de302018-06-15 18:54:18 -070047import org.onosproject.net.flow.instructions.Instructions.NoActionInstruction;
Saurav Das52025962016-01-28 22:30:01 -080048import 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 Chan66291502018-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,
Charles Chan45b69ab2018-03-02 15:41:41 -080095 MacAddress unicastMac,
Saurav Das52025962016-01-28 22:30:01 -080096 ApplicationId applicationId) {
Charles Chan5270ed02016-01-30 23:22:37 -080097 // Consider PortNumber.ANY as wildcard. Match ETH_DST only
98 if (portCriterion != null && portCriterion.port() == PortNumber.ANY) {
99 return processEthDstOnlyFilter(ethCriterion, applicationId);
100 }
101
Charles Chan5b9df8d2016-03-28 22:21:40 -0700102 // Multicast MAC
103 if (ethCriterion.mask() != null) {
Charles Chan45b69ab2018-03-02 15:41:41 -0800104 return processMcastEthDstFilter(ethCriterion, assignedVlan, unicastMac, applicationId);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700105 }
106
Saurav Das52025962016-01-28 22:30:01 -0800107 //handling untagged packets via assigned VLAN
108 if (vidCriterion.vlanId() == VlanId.NONE) {
109 vidCriterion = (VlanIdCriterion) Criteria.matchVlanId(assignedVlan);
110 }
111 // ofdpa cannot match on ALL portnumber, so we need to use separate
112 // rules for each port.
Charles Chan50d900c2018-03-02 13:26:22 -0800113 List<PortNumber> portnums = new ArrayList<>();
Ray Milkey74e59132018-01-17 15:24:52 -0800114 if (portCriterion != null) {
115 if (portCriterion.port() == PortNumber.ALL) {
116 for (Port port : deviceService.getPorts(deviceId)) {
117 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
118 portnums.add(port.number());
119 }
Saurav Das52025962016-01-28 22:30:01 -0800120 }
Ray Milkey74e59132018-01-17 15:24:52 -0800121 } else {
122 portnums.add(portCriterion.port());
Saurav Das52025962016-01-28 22:30:01 -0800123 }
Saurav Das52025962016-01-28 22:30:01 -0800124 }
125
Charles Chan50d900c2018-03-02 13:26:22 -0800126 List<FlowRule> rules = new ArrayList<>();
Saurav Das52025962016-01-28 22:30:01 -0800127 for (PortNumber pnum : portnums) {
128 // for unicast IP packets
129 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
130 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
131 selector.matchInPort(pnum);
132 selector.matchVlanId(vidCriterion.vlanId());
133 selector.matchEthType(Ethernet.TYPE_IPV4);
134 selector.matchEthDst(ethCriterion.mac());
135
136 treatment.transition(UNICAST_ROUTING_TABLE);
137 FlowRule rule = DefaultFlowRule.builder()
138 .forDevice(deviceId)
139 .withSelector(selector.build())
140 .withTreatment(treatment.build())
141 .withPriority(DEFAULT_PRIORITY)
142 .fromApp(applicationId)
143 .makePermanent()
144 .forTable(TMAC_TABLE).build();
145 rules.add(rule);
146 }
Charles Chan66291502018-03-02 16:43:28 -0800147 return ImmutableList.of(rules);
Saurav Das52025962016-01-28 22:30:01 -0800148 }
149
150 /*
151 * In the OF-DPA 2.0 pipeline, versatile forwarding objectives go to the
152 * ACL table. Since we do not pop off vlans in the TMAC table we can continue
153 * to match on vlans in the ACL table if necessary.
154 */
155 @Override
156 protected Collection<FlowRule> processVersatile(ForwardingObjective fwd) {
157 log.info("Processing versatile forwarding objective");
158
Saurav Das52025962016-01-28 22:30:01 -0800159 if (fwd.nextId() == null && fwd.treatment() == null) {
160 log.error("Forwarding objective {} from {} must contain "
161 + "nextId or Treatment", fwd.selector(), fwd.appId());
162 return Collections.emptySet();
163 }
164
165 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
166 fwd.selector().criteria().forEach(criterion -> {
167 if (criterion instanceof VlanIdCriterion) {
168 VlanId vlanId = ((VlanIdCriterion) criterion).vlanId();
169 // ensure that match does not include vlan = NONE as OF-DPA does not
170 // match untagged packets this way in the ACL table.
171 if (vlanId.equals(VlanId.NONE)) {
172 return;
173 }
174 }
175 sbuilder.add(criterion);
176 });
177
178 // XXX driver does not currently do type checking as per Tables 65-67 in
179 // OFDPA 2.0 spec. The only allowed treatment is a punt to the controller.
180 TrafficTreatment.Builder ttBuilder = DefaultTrafficTreatment.builder();
181 if (fwd.treatment() != null) {
182 for (Instruction ins : fwd.treatment().allInstructions()) {
183 if (ins instanceof OutputInstruction) {
184 OutputInstruction o = (OutputInstruction) ins;
185 if (o.port() == PortNumber.CONTROLLER) {
186 ttBuilder.add(o);
187 } else {
188 log.warn("Only allowed treatments in versatile forwarding "
189 + "objectives are punts to the controller");
190 }
Charles Chanf76de302018-06-15 18:54:18 -0700191 } else if (ins instanceof NoActionInstruction) {
192 // No action is allowed and nothing needs to be done
Saurav Das52025962016-01-28 22:30:01 -0800193 } 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}