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