blob: b9d8d89f2b00f32747798b064f1f59179a3fa24d [file] [log] [blame]
Saurav Das822c4e22015-10-23 10:51:11 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Saurav Das822c4e22015-10-23 10:51:11 -07003 *
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
Saurav Das822c4e22015-10-23 10:51:11 -070018import static org.slf4j.LoggerFactory.getLogger;
19
Saurav Das8a0732e2015-11-20 15:27:53 -080020import java.util.ArrayDeque;
Saurav Das822c4e22015-10-23 10:51:11 -070021import java.util.ArrayList;
22import java.util.Collection;
23import java.util.Collections;
Saurav Das8a0732e2015-11-20 15:27:53 -080024import java.util.Deque;
Saurav Das822c4e22015-10-23 10:51:11 -070025import java.util.List;
Sho SHIMIZU45906042016-01-13 23:05:54 -080026import java.util.Objects;
Saurav Das822c4e22015-10-23 10:51:11 -070027import java.util.Set;
28import java.util.concurrent.ConcurrentHashMap;
Saurav Das822c4e22015-10-23 10:51:11 -070029
Charles Chan5270ed02016-01-30 23:22:37 -080030import com.google.common.collect.ImmutableList;
Charles Chan5b9df8d2016-03-28 22:21:40 -070031import com.google.common.collect.ImmutableSet;
Saurav Das822c4e22015-10-23 10:51:11 -070032import org.onlab.osgi.ServiceDirectory;
Saurav Das822c4e22015-10-23 10:51:11 -070033import org.onlab.packet.Ethernet;
Charles Chan14967c22015-12-07 11:11:50 -080034import org.onlab.packet.IpPrefix;
Saurav Das822c4e22015-10-23 10:51:11 -070035import org.onlab.packet.MacAddress;
Saurav Das822c4e22015-10-23 10:51:11 -070036import org.onlab.packet.VlanId;
37import org.onlab.util.KryoNamespace;
38import org.onosproject.core.ApplicationId;
39import org.onosproject.core.CoreService;
Charles Chan14967c22015-12-07 11:11:50 -080040import org.onosproject.driver.extensions.OfdpaMatchVlanVid;
41import org.onosproject.driver.extensions.OfdpaSetVlanVid;
Saurav Das822c4e22015-10-23 10:51:11 -070042import org.onosproject.net.DeviceId;
43import org.onosproject.net.Port;
44import org.onosproject.net.PortNumber;
45import org.onosproject.net.behaviour.NextGroup;
46import org.onosproject.net.behaviour.Pipeliner;
47import org.onosproject.net.behaviour.PipelinerContext;
48import org.onosproject.net.device.DeviceService;
49import org.onosproject.net.driver.AbstractHandlerBehaviour;
50import org.onosproject.net.flow.DefaultFlowRule;
51import org.onosproject.net.flow.DefaultTrafficSelector;
52import org.onosproject.net.flow.DefaultTrafficTreatment;
53import org.onosproject.net.flow.FlowRule;
54import org.onosproject.net.flow.FlowRuleOperations;
55import org.onosproject.net.flow.FlowRuleOperationsContext;
56import org.onosproject.net.flow.FlowRuleService;
57import org.onosproject.net.flow.TrafficSelector;
58import org.onosproject.net.flow.TrafficTreatment;
59import org.onosproject.net.flow.criteria.Criteria;
60import org.onosproject.net.flow.criteria.Criterion;
61import org.onosproject.net.flow.criteria.EthCriterion;
62import org.onosproject.net.flow.criteria.EthTypeCriterion;
Charles Chan14967c22015-12-07 11:11:50 -080063import org.onosproject.net.flow.criteria.ExtensionCriterion;
Saurav Das822c4e22015-10-23 10:51:11 -070064import org.onosproject.net.flow.criteria.IPCriterion;
Saurav Das8a0732e2015-11-20 15:27:53 -080065import org.onosproject.net.flow.criteria.MplsBosCriterion;
66import org.onosproject.net.flow.criteria.MplsCriterion;
Saurav Das822c4e22015-10-23 10:51:11 -070067import org.onosproject.net.flow.criteria.PortCriterion;
68import org.onosproject.net.flow.criteria.VlanIdCriterion;
69import org.onosproject.net.flow.instructions.Instruction;
70import org.onosproject.net.flow.instructions.Instructions.OutputInstruction;
71import org.onosproject.net.flow.instructions.L2ModificationInstruction;
Saurav Das8a0732e2015-11-20 15:27:53 -080072import org.onosproject.net.flow.instructions.L2ModificationInstruction.L2SubType;
Saurav Das822c4e22015-10-23 10:51:11 -070073import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModVlanIdInstruction;
Charles Chan14967c22015-12-07 11:11:50 -080074import org.onosproject.net.flow.instructions.L3ModificationInstruction;
75import org.onosproject.net.flow.instructions.L3ModificationInstruction.L3SubType;
Saurav Das822c4e22015-10-23 10:51:11 -070076import org.onosproject.net.flowobjective.FilteringObjective;
77import org.onosproject.net.flowobjective.FlowObjectiveStore;
78import org.onosproject.net.flowobjective.ForwardingObjective;
79import org.onosproject.net.flowobjective.NextObjective;
80import org.onosproject.net.flowobjective.Objective;
81import org.onosproject.net.flowobjective.ObjectiveError;
Saurav Das822c4e22015-10-23 10:51:11 -070082import org.onosproject.net.group.DefaultGroupKey;
83import org.onosproject.net.group.Group;
Saurav Das822c4e22015-10-23 10:51:11 -070084import org.onosproject.net.group.GroupKey;
Saurav Das822c4e22015-10-23 10:51:11 -070085import org.onosproject.net.group.GroupService;
Saurav Das822c4e22015-10-23 10:51:11 -070086import org.onosproject.store.serializers.KryoNamespaces;
87import org.slf4j.Logger;
88
Saurav Das822c4e22015-10-23 10:51:11 -070089/**
90 * Driver for Broadcom's OF-DPA v2.0 TTP.
91 *
92 */
Charles Chan361154b2016-03-24 10:23:39 -070093public class Ofdpa2Pipeline extends AbstractHandlerBehaviour implements Pipeliner {
Saurav Das822c4e22015-10-23 10:51:11 -070094 protected static final int PORT_TABLE = 0;
95 protected static final int VLAN_TABLE = 10;
96 protected static final int TMAC_TABLE = 20;
97 protected static final int UNICAST_ROUTING_TABLE = 30;
98 protected static final int MULTICAST_ROUTING_TABLE = 40;
99 protected static final int MPLS_TABLE_0 = 23;
100 protected static final int MPLS_TABLE_1 = 24;
101 protected static final int BRIDGING_TABLE = 50;
102 protected static final int ACL_TABLE = 60;
103 protected static final int MAC_LEARNING_TABLE = 254;
104 protected static final long OFPP_MAX = 0xffffff00L;
105
Saurav Das52025962016-01-28 22:30:01 -0800106 protected static final int HIGHEST_PRIORITY = 0xffff;
Saurav Das2857f382015-11-03 14:39:27 -0800107 protected static final int DEFAULT_PRIORITY = 0x8000;
Saurav Das822c4e22015-10-23 10:51:11 -0700108 protected static final int LOWEST_PRIORITY = 0x0;
109
Saurav Das822c4e22015-10-23 10:51:11 -0700110 private final Logger log = getLogger(getClass());
Charles Chan425854b2016-04-11 15:32:12 -0700111 protected ServiceDirectory serviceDirectory;
Saurav Das822c4e22015-10-23 10:51:11 -0700112 protected FlowRuleService flowRuleService;
Charles Chan425854b2016-04-11 15:32:12 -0700113 protected CoreService coreService;
Saurav Das8a0732e2015-11-20 15:27:53 -0800114 protected GroupService groupService;
115 protected FlowObjectiveStore flowObjectiveStore;
Saurav Das822c4e22015-10-23 10:51:11 -0700116 protected DeviceId deviceId;
117 protected ApplicationId driverId;
Saurav Das822c4e22015-10-23 10:51:11 -0700118 protected DeviceService deviceService;
Charles Chan188ebf52015-12-23 00:15:11 -0800119 protected static KryoNamespace appKryo = new KryoNamespace.Builder()
Saurav Das822c4e22015-10-23 10:51:11 -0700120 .register(KryoNamespaces.API)
121 .register(GroupKey.class)
122 .register(DefaultGroupKey.class)
Charles Chan361154b2016-03-24 10:23:39 -0700123 .register(Ofdpa2GroupHandler.OfdpaNextGroup.class)
Saurav Das8a0732e2015-11-20 15:27:53 -0800124 .register(ArrayDeque.class)
Charles Chaneefdedf2016-05-23 16:45:45 -0700125 .build("Ofdpa2Pipeline");
Saurav Das822c4e22015-10-23 10:51:11 -0700126
Charles Chan425854b2016-04-11 15:32:12 -0700127 protected Ofdpa2GroupHandler groupHandler;
Saurav Das822c4e22015-10-23 10:51:11 -0700128
Saurav Das52025962016-01-28 22:30:01 -0800129 protected Set<IPCriterion> sentIpFilters = Collections.newSetFromMap(
Charles Chan188ebf52015-12-23 00:15:11 -0800130 new ConcurrentHashMap<>());
Saurav Das4f980082015-11-05 13:39:15 -0800131
Saurav Das822c4e22015-10-23 10:51:11 -0700132 @Override
133 public void init(DeviceId deviceId, PipelinerContext context) {
Saurav Das822c4e22015-10-23 10:51:11 -0700134 this.deviceId = deviceId;
135
Charles Chan188ebf52015-12-23 00:15:11 -0800136 // Initialize OFDPA group handler
Charles Chan425854b2016-04-11 15:32:12 -0700137 groupHandler = new Ofdpa2GroupHandler();
138 groupHandler.init(deviceId, context);
Saurav Das822c4e22015-10-23 10:51:11 -0700139
Charles Chan425854b2016-04-11 15:32:12 -0700140 serviceDirectory = context.directory();
Saurav Das822c4e22015-10-23 10:51:11 -0700141 coreService = serviceDirectory.get(CoreService.class);
142 flowRuleService = serviceDirectory.get(FlowRuleService.class);
143 groupService = serviceDirectory.get(GroupService.class);
144 flowObjectiveStore = context.store();
Saurav Das822c4e22015-10-23 10:51:11 -0700145 deviceService = serviceDirectory.get(DeviceService.class);
Saurav Das822c4e22015-10-23 10:51:11 -0700146
147 driverId = coreService.registerApplication(
Charles Chan425854b2016-04-11 15:32:12 -0700148 "org.onosproject.driver.Ofdpa2Pipeline");
Saurav Das822c4e22015-10-23 10:51:11 -0700149
Saurav Das822c4e22015-10-23 10:51:11 -0700150 initializePipeline();
Saurav Das822c4e22015-10-23 10:51:11 -0700151 }
152
153 protected void initializePipeline() {
Charles Chan188ebf52015-12-23 00:15:11 -0800154 // OF-DPA does not require initializing the pipeline as it puts default
155 // rules automatically in the hardware. However emulation of OFDPA in
156 // software switches does require table-miss-entries.
Saurav Das822c4e22015-10-23 10:51:11 -0700157 }
158
159 //////////////////////////////////////
160 // Flow Objectives
161 //////////////////////////////////////
162
163 @Override
164 public void filter(FilteringObjective filteringObjective) {
165 if (filteringObjective.type() == FilteringObjective.Type.PERMIT) {
166 processFilter(filteringObjective,
167 filteringObjective.op() == Objective.Operation.ADD,
168 filteringObjective.appId());
169 } else {
170 // Note that packets that don't match the PERMIT filter are
171 // automatically denied. The DENY filter is used to deny packets
172 // that are otherwise permitted by the PERMIT filter.
173 // Use ACL table flow rules here for DENY filtering objectives
174 log.debug("filter objective other than PERMIT currently not supported");
175 fail(filteringObjective, ObjectiveError.UNSUPPORTED);
176 }
177 }
178
179 @Override
180 public void forward(ForwardingObjective fwd) {
181 Collection<FlowRule> rules;
182 FlowRuleOperations.Builder flowOpsBuilder = FlowRuleOperations.builder();
183
184 rules = processForward(fwd);
Saurav Das25190812016-05-27 13:54:07 -0700185 if (rules == null || rules.isEmpty()) {
186 // Assumes fail message has already been generated to the objective
187 // context. Returning here prevents spurious pass message to be
188 // generated by FlowRule service for empty flowOps.
189 return;
190 }
Saurav Das822c4e22015-10-23 10:51:11 -0700191 switch (fwd.op()) {
192 case ADD:
193 rules.stream()
Sho SHIMIZU45906042016-01-13 23:05:54 -0800194 .filter(Objects::nonNull)
Saurav Das822c4e22015-10-23 10:51:11 -0700195 .forEach(flowOpsBuilder::add);
196 break;
197 case REMOVE:
198 rules.stream()
Sho SHIMIZU45906042016-01-13 23:05:54 -0800199 .filter(Objects::nonNull)
Saurav Das822c4e22015-10-23 10:51:11 -0700200 .forEach(flowOpsBuilder::remove);
201 break;
202 default:
203 fail(fwd, ObjectiveError.UNKNOWN);
204 log.warn("Unknown forwarding type {}", fwd.op());
205 }
206
Saurav Das822c4e22015-10-23 10:51:11 -0700207 flowRuleService.apply(flowOpsBuilder.build(new FlowRuleOperationsContext() {
208 @Override
209 public void onSuccess(FlowRuleOperations ops) {
210 pass(fwd);
211 }
212
213 @Override
214 public void onError(FlowRuleOperations ops) {
215 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
216 }
217 }));
Saurav Das822c4e22015-10-23 10:51:11 -0700218 }
219
220 @Override
221 public void next(NextObjective nextObjective) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800222 NextGroup nextGroup = flowObjectiveStore.getNextGroup(nextObjective.id());
223 switch (nextObjective.op()) {
224 case ADD:
Saurav Das4f980082015-11-05 13:39:15 -0800225 if (nextGroup != null) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800226 log.warn("Cannot add next {} that already exists in device {}",
227 nextObjective.id(), deviceId);
228 return;
229 }
230 log.debug("Processing NextObjective id{} in dev{} - add group",
231 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700232 groupHandler.addGroup(nextObjective);
Saurav Das8a0732e2015-11-20 15:27:53 -0800233 break;
234 case ADD_TO_EXISTING:
235 if (nextGroup != null) {
236 log.debug("Processing NextObjective id{} in dev{} - add bucket",
237 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700238 groupHandler.addBucketToGroup(nextObjective, nextGroup);
Saurav Das4f980082015-11-05 13:39:15 -0800239 } else {
Saurav Das8a0732e2015-11-20 15:27:53 -0800240 // it is possible that group-chain has not been fully created yet
Saurav Das423fe2b2015-12-04 10:52:59 -0800241 log.debug("Waiting to add bucket to group for next-id:{} in dev:{}",
242 nextObjective.id(), deviceId);
243 // by design only one pending bucket is allowed for the group
Charles Chan425854b2016-04-11 15:32:12 -0700244 groupHandler.pendingBuckets.put(nextObjective.id(), nextObjective);
Saurav Das4f980082015-11-05 13:39:15 -0800245 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800246 break;
247 case REMOVE:
248 if (nextGroup == null) {
249 log.warn("Cannot remove next {} that does not exist in device {}",
250 nextObjective.id(), deviceId);
251 return;
252 }
253 log.debug("Processing NextObjective id{} in dev{} - remove group",
254 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700255 groupHandler.removeGroup(nextObjective, nextGroup);
Saurav Das8a0732e2015-11-20 15:27:53 -0800256 break;
257 case REMOVE_FROM_EXISTING:
258 if (nextGroup == null) {
259 log.warn("Cannot remove from next {} that does not exist in device {}",
260 nextObjective.id(), deviceId);
261 return;
262 }
263 log.debug("Processing NextObjective id{} in dev{} - remove bucket",
264 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700265 groupHandler.removeBucketFromGroup(nextObjective, nextGroup);
Saurav Das8a0732e2015-11-20 15:27:53 -0800266 break;
267 default:
Saurav Das4f980082015-11-05 13:39:15 -0800268 log.warn("Unsupported operation {}", nextObjective.op());
Saurav Das822c4e22015-10-23 10:51:11 -0700269 }
270 }
271
272 //////////////////////////////////////
273 // Flow handling
274 //////////////////////////////////////
275
276 /**
277 * As per OFDPA 2.0 TTP, filtering of VLAN ids, MAC addresses (for routing)
278 * and IP addresses configured on switch ports happen in different tables.
279 * Note that IP filtering rules need to be added to the ACL table, as there
280 * is no mechanism to send to controller via IP table.
281 *
282 * @param filt the filtering objective
283 * @param install indicates whether to add or remove the objective
284 * @param applicationId the application that sent this objective
285 */
Saurav Das52025962016-01-28 22:30:01 -0800286 protected void processFilter(FilteringObjective filt,
287 boolean install, ApplicationId applicationId) {
Saurav Das822c4e22015-10-23 10:51:11 -0700288 // This driver only processes filtering criteria defined with switch
289 // ports as the key
290 PortCriterion portCriterion = null;
291 EthCriterion ethCriterion = null;
292 VlanIdCriterion vidCriterion = null;
293 Collection<IPCriterion> ips = new ArrayList<IPCriterion>();
294 if (!filt.key().equals(Criteria.dummy()) &&
295 filt.key().type() == Criterion.Type.IN_PORT) {
296 portCriterion = (PortCriterion) filt.key();
297 } else {
298 log.warn("No key defined in filtering objective from app: {}. Not"
299 + "processing filtering objective", applicationId);
Saurav Das59232cf2016-04-27 18:35:50 -0700300 fail(filt, ObjectiveError.BADPARAMS);
Saurav Das822c4e22015-10-23 10:51:11 -0700301 return;
302 }
Saurav Das59232cf2016-04-27 18:35:50 -0700303 log.debug("Received filtering objective for dev/port: {}/{}", deviceId,
304 portCriterion.port());
Saurav Das822c4e22015-10-23 10:51:11 -0700305 // convert filtering conditions for switch-intfs into flowrules
306 FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
307 for (Criterion criterion : filt.conditions()) {
Charles Chan5b9df8d2016-03-28 22:21:40 -0700308 if (criterion.type() == Criterion.Type.ETH_DST ||
309 criterion.type() == Criterion.Type.ETH_DST_MASKED) {
Saurav Das822c4e22015-10-23 10:51:11 -0700310 ethCriterion = (EthCriterion) criterion;
311 } else if (criterion.type() == Criterion.Type.VLAN_VID) {
312 vidCriterion = (VlanIdCriterion) criterion;
313 } else if (criterion.type() == Criterion.Type.IPV4_DST) {
314 ips.add((IPCriterion) criterion);
315 } else {
316 log.error("Unsupported filter {}", criterion);
317 fail(filt, ObjectiveError.UNSUPPORTED);
318 return;
319 }
320 }
321
Saurav Das0e99e2b2015-10-28 12:39:42 -0700322 VlanId assignedVlan = null;
Charles Chane849c192016-01-11 18:28:54 -0800323 if (vidCriterion != null) {
Charles Chand55e84d2016-03-30 17:54:24 -0700324 // Use the VLAN in metadata whenever a metadata is provided
325 if (filt.meta() != null) {
326 assignedVlan = readVlanFromTreatment(filt.meta());
327 // Use the VLAN in criterion if metadata is not present and the traffic is tagged
328 } else if (!vidCriterion.vlanId().equals(VlanId.NONE)) {
Charles Chane849c192016-01-11 18:28:54 -0800329 assignedVlan = vidCriterion.vlanId();
Charles Chand55e84d2016-03-30 17:54:24 -0700330 }
Charles Chane849c192016-01-11 18:28:54 -0800331
Charles Chand55e84d2016-03-30 17:54:24 -0700332 if (assignedVlan == null) {
333 log.error("Driver fails to extract VLAN information. "
334 + "Not proccessing VLAN filters on device {}.", deviceId);
335 log.debug("VLAN ID in criterion={}, metadata={}",
336 readVlanFromTreatment(filt.meta()), vidCriterion.vlanId());
337 fail(filt, ObjectiveError.BADPARAMS);
338 return;
Saurav Das0e99e2b2015-10-28 12:39:42 -0700339 }
340 }
341
Charles Chane849c192016-01-11 18:28:54 -0800342 if (ethCriterion == null || ethCriterion.mac().equals(MacAddress.NONE)) {
Charles Chan985b12e2016-05-11 19:47:22 -0700343 // NOTE: it is possible that a filtering objective only has vidCriterion
344 log.debug("filtering objective missing dstMac, cannot program TMAC table");
Saurav Das822c4e22015-10-23 10:51:11 -0700345 } else {
346 for (FlowRule tmacRule : processEthDstFilter(portCriterion, ethCriterion,
Saurav Das0e99e2b2015-10-28 12:39:42 -0700347 vidCriterion, assignedVlan,
348 applicationId)) {
Saurav Das822c4e22015-10-23 10:51:11 -0700349 log.debug("adding MAC filtering rules in TMAC table: {} for dev: {}",
350 tmacRule, deviceId);
351 ops = install ? ops.add(tmacRule) : ops.remove(tmacRule);
352 }
353 }
354
Charles Chan985b12e2016-05-11 19:47:22 -0700355 if (vidCriterion == null) {
356 // NOTE: it is possible that a filtering objective only has ethCriterion
357 log.debug("filtering objective missing dstMac or VLAN, "
Charles Chane849c192016-01-11 18:28:54 -0800358 + "cannot program VLAN Table");
Saurav Das822c4e22015-10-23 10:51:11 -0700359 } else {
Charles Chan14967c22015-12-07 11:11:50 -0800360 /*
361 * NOTE: Separate vlan filtering rules and assignment rules
362 * into different stage in order to guarantee that filtering rules
Saurav Das52025962016-01-28 22:30:01 -0800363 * always go first, as required by ofdpa.
Charles Chan14967c22015-12-07 11:11:50 -0800364 */
365 List<FlowRule> allRules = processVlanIdFilter(
366 portCriterion, vidCriterion, assignedVlan, applicationId);
367 List<FlowRule> filteringRules = new ArrayList<>();
368 List<FlowRule> assignmentRules = new ArrayList<>();
369
370 allRules.forEach(flowRule -> {
371 ExtensionCriterion extCriterion =
372 (ExtensionCriterion) flowRule.selector().getCriterion(Criterion.Type.EXTENSION);
373 VlanId vlanId = ((OfdpaMatchVlanVid) extCriterion.extensionSelector()).vlanId();
Charles Chanbe8aea42016-02-24 12:04:47 -0800374 if (!vlanId.equals(VlanId.NONE)) {
Charles Chan14967c22015-12-07 11:11:50 -0800375 filteringRules.add(flowRule);
376 } else {
377 assignmentRules.add(flowRule);
378 }
379 });
380
381 for (FlowRule filteringRule : filteringRules) {
Saurav Das822c4e22015-10-23 10:51:11 -0700382 log.debug("adding VLAN filtering rule in VLAN table: {} for dev: {}",
Charles Chan14967c22015-12-07 11:11:50 -0800383 filteringRule, deviceId);
384 ops = install ? ops.add(filteringRule) : ops.remove(filteringRule);
385 }
386
387 ops.newStage();
388
389 for (FlowRule assignmentRule : assignmentRules) {
390 log.debug("adding VLAN assignment rule in VLAN table: {} for dev: {}",
391 assignmentRule, deviceId);
392 ops = install ? ops.add(assignmentRule) : ops.remove(assignmentRule);
Saurav Das822c4e22015-10-23 10:51:11 -0700393 }
394 }
395
396 for (IPCriterion ipaddr : ips) {
397 // since we ignore port information for IP rules, and the same (gateway) IP
398 // can be configured on multiple ports, we make sure that we send
399 // only a single rule to the switch.
400 if (!sentIpFilters.contains(ipaddr)) {
401 sentIpFilters.add(ipaddr);
402 log.debug("adding IP filtering rules in ACL table {} for dev: {}",
403 ipaddr, deviceId);
404 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
405 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
406 selector.matchEthType(Ethernet.TYPE_IPV4);
407 selector.matchIPDst(ipaddr.ip());
408 treatment.setOutput(PortNumber.CONTROLLER);
409 FlowRule rule = DefaultFlowRule.builder()
410 .forDevice(deviceId)
411 .withSelector(selector.build())
412 .withTreatment(treatment.build())
413 .withPriority(HIGHEST_PRIORITY)
414 .fromApp(applicationId)
415 .makePermanent()
416 .forTable(ACL_TABLE).build();
417 ops = install ? ops.add(rule) : ops.remove(rule);
418 }
419 }
420
421 // apply filtering flow rules
422 flowRuleService.apply(ops.build(new FlowRuleOperationsContext() {
423 @Override
424 public void onSuccess(FlowRuleOperations ops) {
425 log.info("Applied {} filtering rules in device {}",
426 ops.stages().get(0).size(), deviceId);
427 pass(filt);
428 }
429
430 @Override
431 public void onError(FlowRuleOperations ops) {
432 log.info("Failed to apply all filtering rules in dev {}", deviceId);
433 fail(filt, ObjectiveError.FLOWINSTALLATIONFAILED);
434 }
435 }));
436
437 }
438
439 /**
440 * Allows untagged packets into pipeline by assigning a vlan id.
Saurav Das0e99e2b2015-10-28 12:39:42 -0700441 * Vlan assignment is done by the application.
Saurav Das822c4e22015-10-23 10:51:11 -0700442 * Allows tagged packets into pipeline as per configured port-vlan info.
Saurav Das0e99e2b2015-10-28 12:39:42 -0700443 *
Charles Chanf9e98652016-09-07 16:54:23 -0700444 * @param portCriterion port on device for which this filter is programmed
445 * @param vidCriterion vlan assigned to port, or NONE for untagged
446 * @param assignedVlan assigned vlan-id for untagged packets
447 * @param applicationId for application programming this filter
Saurav Das822c4e22015-10-23 10:51:11 -0700448 * @return list of FlowRule for port-vlan filters
449 */
450 protected List<FlowRule> processVlanIdFilter(PortCriterion portCriterion,
451 VlanIdCriterion vidCriterion,
Saurav Das0e99e2b2015-10-28 12:39:42 -0700452 VlanId assignedVlan,
Saurav Das822c4e22015-10-23 10:51:11 -0700453 ApplicationId applicationId) {
Charles Chanf9e98652016-09-07 16:54:23 -0700454 return processVlanIdFilterInternal(portCriterion, vidCriterion, assignedVlan,
455 applicationId, true);
456 }
457
458 /**
459 * Internal implementation of processVlanIdFilter.
460 * <p>
461 * The is_present bit in set_vlan_vid action is required to be 0 in OFDPA i12.
462 * Since it is non-OF spec, we need an extension treatment for that.
463 * The useSetVlanExtension must be set to false for OFDPA i12.
464 * </p>
465 *
466 * @param portCriterion port on device for which this filter is programmed
467 * @param vidCriterion vlan assigned to port, or NONE for untagged
468 * @param assignedVlan assigned vlan-id for untagged packets
469 * @param applicationId for application programming this filter
470 * @param useSetVlanExtension use the setVlanVid extension that has is_present bit set to 0.
471 * @return list of FlowRule for port-vlan filters
472 */
473 protected List<FlowRule> processVlanIdFilterInternal(PortCriterion portCriterion,
474 VlanIdCriterion vidCriterion,
475 VlanId assignedVlan,
476 ApplicationId applicationId,
477 boolean useSetVlanExtension) {
Charles Chan14967c22015-12-07 11:11:50 -0800478 List<FlowRule> rules = new ArrayList<>();
Saurav Das822c4e22015-10-23 10:51:11 -0700479 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
480 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
Charles Chan14967c22015-12-07 11:11:50 -0800481 TrafficSelector.Builder preSelector = null;
482 TrafficTreatment.Builder preTreatment = null;
483
Saurav Das4f980082015-11-05 13:39:15 -0800484 treatment.transition(TMAC_TABLE);
485
Saurav Das822c4e22015-10-23 10:51:11 -0700486 if (vidCriterion.vlanId() == VlanId.NONE) {
487 // untagged packets are assigned vlans
Charles Chanbe8aea42016-02-24 12:04:47 -0800488 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(VlanId.NONE);
Charles Chan14967c22015-12-07 11:11:50 -0800489 selector.extension(ofdpaMatchVlanVid, deviceId);
Charles Chanf9e98652016-09-07 16:54:23 -0700490 if (useSetVlanExtension) {
491 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(assignedVlan);
492 treatment.extension(ofdpaSetVlanVid, deviceId);
493 } else {
494 treatment.setVlanId(assignedVlan);
495 }
Charles Chan14967c22015-12-07 11:11:50 -0800496
497 preSelector = DefaultTrafficSelector.builder();
498 OfdpaMatchVlanVid preOfdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
499 preSelector.extension(preOfdpaMatchVlanVid, deviceId);
500 preTreatment = DefaultTrafficTreatment.builder().transition(TMAC_TABLE);
501
Saurav Das4f980082015-11-05 13:39:15 -0800502 } else {
Charles Chan14967c22015-12-07 11:11:50 -0800503 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vidCriterion.vlanId());
504 selector.extension(ofdpaMatchVlanVid, deviceId);
Charles Chand55e84d2016-03-30 17:54:24 -0700505
506 if (!assignedVlan.equals(vidCriterion.vlanId())) {
507 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(assignedVlan);
508 treatment.extension(ofdpaSetVlanVid, deviceId);
509 }
Saurav Das822c4e22015-10-23 10:51:11 -0700510 }
Saurav Das822c4e22015-10-23 10:51:11 -0700511
512 // ofdpa cannot match on ALL portnumber, so we need to use separate
513 // rules for each port.
Charles Chan14967c22015-12-07 11:11:50 -0800514 List<PortNumber> portnums = new ArrayList<>();
Saurav Das822c4e22015-10-23 10:51:11 -0700515 if (portCriterion.port() == PortNumber.ALL) {
516 for (Port port : deviceService.getPorts(deviceId)) {
517 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
518 portnums.add(port.number());
519 }
520 }
521 } else {
522 portnums.add(portCriterion.port());
523 }
Saurav Das4f980082015-11-05 13:39:15 -0800524
Saurav Das822c4e22015-10-23 10:51:11 -0700525 for (PortNumber pnum : portnums) {
Saurav Das4f980082015-11-05 13:39:15 -0800526 // create rest of flowrule
Saurav Das822c4e22015-10-23 10:51:11 -0700527 selector.matchInPort(pnum);
528 FlowRule rule = DefaultFlowRule.builder()
529 .forDevice(deviceId)
530 .withSelector(selector.build())
531 .withTreatment(treatment.build())
532 .withPriority(DEFAULT_PRIORITY)
533 .fromApp(applicationId)
534 .makePermanent()
535 .forTable(VLAN_TABLE).build();
Charles Chan14967c22015-12-07 11:11:50 -0800536
537 if (preSelector != null) {
538 preSelector.matchInPort(pnum);
539 FlowRule preRule = DefaultFlowRule.builder()
540 .forDevice(deviceId)
541 .withSelector(preSelector.build())
542 .withTreatment(preTreatment.build())
543 .withPriority(DEFAULT_PRIORITY)
544 .fromApp(applicationId)
545 .makePermanent()
546 .forTable(VLAN_TABLE).build();
547 rules.add(preRule);
548 }
549
Saurav Das822c4e22015-10-23 10:51:11 -0700550 rules.add(rule);
551 }
552 return rules;
553 }
554
555 /**
556 * Allows routed packets with correct destination MAC to be directed
557 * to unicast-IP routing table or MPLS forwarding table.
Saurav Das822c4e22015-10-23 10:51:11 -0700558 *
559 * @param portCriterion port on device for which this filter is programmed
560 * @param ethCriterion dstMac of device for which is filter is programmed
561 * @param vidCriterion vlan assigned to port, or NONE for untagged
Saurav Das0e99e2b2015-10-28 12:39:42 -0700562 * @param assignedVlan assigned vlan-id for untagged packets
Saurav Das822c4e22015-10-23 10:51:11 -0700563 * @param applicationId for application programming this filter
564 * @return list of FlowRule for port-vlan filters
565
566 */
567 protected List<FlowRule> processEthDstFilter(PortCriterion portCriterion,
568 EthCriterion ethCriterion,
569 VlanIdCriterion vidCriterion,
Saurav Das0e99e2b2015-10-28 12:39:42 -0700570 VlanId assignedVlan,
Saurav Das822c4e22015-10-23 10:51:11 -0700571 ApplicationId applicationId) {
Charles Chan5270ed02016-01-30 23:22:37 -0800572 // Consider PortNumber.ANY as wildcard. Match ETH_DST only
573 if (portCriterion != null && portCriterion.port() == PortNumber.ANY) {
574 return processEthDstOnlyFilter(ethCriterion, applicationId);
575 }
576
Charles Chan5b9df8d2016-03-28 22:21:40 -0700577 // Multicast MAC
578 if (ethCriterion.mask() != null) {
579 return processMcastEthDstFilter(ethCriterion, applicationId);
580 }
581
Saurav Das822c4e22015-10-23 10:51:11 -0700582 //handling untagged packets via assigned VLAN
583 if (vidCriterion.vlanId() == VlanId.NONE) {
Saurav Das0e99e2b2015-10-28 12:39:42 -0700584 vidCriterion = (VlanIdCriterion) Criteria.matchVlanId(assignedVlan);
Saurav Das822c4e22015-10-23 10:51:11 -0700585 }
586 // ofdpa cannot match on ALL portnumber, so we need to use separate
587 // rules for each port.
588 List<PortNumber> portnums = new ArrayList<PortNumber>();
589 if (portCriterion.port() == PortNumber.ALL) {
590 for (Port port : deviceService.getPorts(deviceId)) {
591 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
592 portnums.add(port.number());
593 }
594 }
595 } else {
596 portnums.add(portCriterion.port());
597 }
598
599 List<FlowRule> rules = new ArrayList<FlowRule>();
600 for (PortNumber pnum : portnums) {
Charles Chan14967c22015-12-07 11:11:50 -0800601 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vidCriterion.vlanId());
Saurav Das822c4e22015-10-23 10:51:11 -0700602 // for unicast IP packets
603 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
604 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
605 selector.matchInPort(pnum);
Charles Chan14967c22015-12-07 11:11:50 -0800606 selector.extension(ofdpaMatchVlanVid, deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700607 selector.matchEthType(Ethernet.TYPE_IPV4);
608 selector.matchEthDst(ethCriterion.mac());
609 treatment.transition(UNICAST_ROUTING_TABLE);
610 FlowRule rule = DefaultFlowRule.builder()
611 .forDevice(deviceId)
612 .withSelector(selector.build())
613 .withTreatment(treatment.build())
614 .withPriority(DEFAULT_PRIORITY)
615 .fromApp(applicationId)
616 .makePermanent()
617 .forTable(TMAC_TABLE).build();
618 rules.add(rule);
619 //for MPLS packets
620 selector = DefaultTrafficSelector.builder();
621 treatment = DefaultTrafficTreatment.builder();
622 selector.matchInPort(pnum);
Charles Chan14967c22015-12-07 11:11:50 -0800623 selector.extension(ofdpaMatchVlanVid, deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700624 selector.matchEthType(Ethernet.MPLS_UNICAST);
625 selector.matchEthDst(ethCriterion.mac());
626 treatment.transition(MPLS_TABLE_0);
627 rule = DefaultFlowRule.builder()
628 .forDevice(deviceId)
629 .withSelector(selector.build())
630 .withTreatment(treatment.build())
631 .withPriority(DEFAULT_PRIORITY)
632 .fromApp(applicationId)
633 .makePermanent()
634 .forTable(TMAC_TABLE).build();
635 rules.add(rule);
636 }
637 return rules;
638 }
639
Charles Chan5270ed02016-01-30 23:22:37 -0800640 protected List<FlowRule> processEthDstOnlyFilter(EthCriterion ethCriterion,
641 ApplicationId applicationId) {
642 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
643 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
644 selector.matchEthType(Ethernet.TYPE_IPV4);
645 selector.matchEthDst(ethCriterion.mac());
646 treatment.transition(UNICAST_ROUTING_TABLE);
647 FlowRule rule = DefaultFlowRule.builder()
648 .forDevice(deviceId)
649 .withSelector(selector.build())
650 .withTreatment(treatment.build())
651 .withPriority(DEFAULT_PRIORITY)
652 .fromApp(applicationId)
653 .makePermanent()
654 .forTable(TMAC_TABLE).build();
655 return ImmutableList.<FlowRule>builder().add(rule).build();
656 }
657
Charles Chan5b9df8d2016-03-28 22:21:40 -0700658 protected List<FlowRule> processMcastEthDstFilter(EthCriterion ethCriterion,
659 ApplicationId applicationId) {
660 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
661 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
662 selector.matchEthType(Ethernet.TYPE_IPV4);
663 selector.matchEthDstMasked(ethCriterion.mac(), ethCriterion.mask());
664 treatment.transition(MULTICAST_ROUTING_TABLE);
665 FlowRule rule = DefaultFlowRule.builder()
666 .forDevice(deviceId)
667 .withSelector(selector.build())
668 .withTreatment(treatment.build())
669 .withPriority(DEFAULT_PRIORITY)
670 .fromApp(applicationId)
671 .makePermanent()
672 .forTable(TMAC_TABLE).build();
673 return ImmutableList.<FlowRule>builder().add(rule).build();
674 }
675
Saurav Das822c4e22015-10-23 10:51:11 -0700676 private Collection<FlowRule> processForward(ForwardingObjective fwd) {
677 switch (fwd.flag()) {
678 case SPECIFIC:
679 return processSpecific(fwd);
680 case VERSATILE:
681 return processVersatile(fwd);
682 default:
683 fail(fwd, ObjectiveError.UNKNOWN);
684 log.warn("Unknown forwarding flag {}", fwd.flag());
685 }
686 return Collections.emptySet();
687 }
688
689 /**
690 * In the OF-DPA 2.0 pipeline, versatile forwarding objectives go to the
691 * ACL table.
692 * @param fwd the forwarding objective of type 'versatile'
693 * @return a collection of flow rules to be sent to the switch. An empty
694 * collection may be returned if there is a problem in processing
695 * the flow rule
696 */
Saurav Das52025962016-01-28 22:30:01 -0800697 protected Collection<FlowRule> processVersatile(ForwardingObjective fwd) {
Saurav Das822c4e22015-10-23 10:51:11 -0700698 log.info("Processing versatile forwarding objective");
Saurav Das822c4e22015-10-23 10:51:11 -0700699
700 EthTypeCriterion ethType =
Saurav Das77b5e902016-01-27 17:01:59 -0800701 (EthTypeCriterion) fwd.selector().getCriterion(Criterion.Type.ETH_TYPE);
Saurav Das822c4e22015-10-23 10:51:11 -0700702 if (ethType == null) {
703 log.error("Versatile forwarding objective must include ethType");
704 fail(fwd, ObjectiveError.BADPARAMS);
705 return Collections.emptySet();
706 }
707 if (fwd.nextId() == null && fwd.treatment() == null) {
708 log.error("Forwarding objective {} from {} must contain "
709 + "nextId or Treatment", fwd.selector(), fwd.appId());
Zsolt Haraszti9faab752016-02-17 15:55:20 -0800710 fail(fwd, ObjectiveError.BADPARAMS);
Saurav Das822c4e22015-10-23 10:51:11 -0700711 return Collections.emptySet();
712 }
Saurav Das49cb5a12016-01-16 22:54:07 -0800713
Saurav Das77b5e902016-01-27 17:01:59 -0800714 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
715 fwd.selector().criteria().forEach(criterion -> {
716 if (criterion instanceof VlanIdCriterion) {
717 VlanId vlanId = ((VlanIdCriterion) criterion).vlanId();
718 // ensure that match does not include vlan = NONE as OF-DPA does not
719 // match untagged packets this way in the ACL table.
720 if (vlanId.equals(VlanId.NONE)) {
721 return;
722 }
723 OfdpaMatchVlanVid ofdpaMatchVlanVid =
724 new OfdpaMatchVlanVid(vlanId);
725 sbuilder.extension(ofdpaMatchVlanVid, deviceId);
726 } else {
727 sbuilder.add(criterion);
728 }
729 });
730
Saurav Das822c4e22015-10-23 10:51:11 -0700731 // XXX driver does not currently do type checking as per Tables 65-67 in
732 // OFDPA 2.0 spec. The only allowed treatment is a punt to the controller.
Saurav Das49cb5a12016-01-16 22:54:07 -0800733 TrafficTreatment.Builder ttBuilder = DefaultTrafficTreatment.builder();
734 if (fwd.treatment() != null) {
735 for (Instruction ins : fwd.treatment().allInstructions()) {
736 if (ins instanceof OutputInstruction) {
737 OutputInstruction o = (OutputInstruction) ins;
738 if (o.port() == PortNumber.CONTROLLER) {
739 ttBuilder.add(o);
740 } else {
741 log.warn("Only allowed treatments in versatile forwarding "
742 + "objectives are punts to the controller");
743 }
744 } else {
745 log.warn("Cannot process instruction in versatile fwd {}", ins);
746 }
Saurav Das822c4e22015-10-23 10:51:11 -0700747 }
748 }
Saurav Das822c4e22015-10-23 10:51:11 -0700749 if (fwd.nextId() != null) {
Saurav Das49cb5a12016-01-16 22:54:07 -0800750 // overide case
751 NextGroup next = getGroupForNextObjective(fwd.nextId());
752 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
753 // we only need the top level group's key to point the flow to it
754 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
755 if (group == null) {
756 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
757 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
758 fail(fwd, ObjectiveError.GROUPMISSING);
759 return Collections.emptySet();
760 }
761 ttBuilder.deferred().group(group.id());
Saurav Das822c4e22015-10-23 10:51:11 -0700762 }
Saurav Das49cb5a12016-01-16 22:54:07 -0800763
764 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
765 .fromApp(fwd.appId())
766 .withPriority(fwd.priority())
767 .forDevice(deviceId)
Saurav Das77b5e902016-01-27 17:01:59 -0800768 .withSelector(sbuilder.build())
Saurav Das49cb5a12016-01-16 22:54:07 -0800769 .withTreatment(ttBuilder.build())
770 .makePermanent()
771 .forTable(ACL_TABLE);
772 return Collections.singletonList(ruleBuilder.build());
Saurav Das822c4e22015-10-23 10:51:11 -0700773 }
774
775 /**
776 * In the OF-DPA 2.0 pipeline, specific forwarding refers to the IP table
Saurav Das8a0732e2015-11-20 15:27:53 -0800777 * (unicast or multicast) or the L2 table (mac + vlan) or the MPLS table.
Saurav Das822c4e22015-10-23 10:51:11 -0700778 *
779 * @param fwd the forwarding objective of type 'specific'
780 * @return a collection of flow rules. Typically there will be only one
781 * for this type of forwarding objective. An empty set may be
782 * returned if there is an issue in processing the objective.
783 */
Saurav Das8a0732e2015-11-20 15:27:53 -0800784 protected Collection<FlowRule> processSpecific(ForwardingObjective fwd) {
Saurav Das25190812016-05-27 13:54:07 -0700785 log.debug("Processing specific fwd objective:{} in dev:{} with next:{}",
Saurav Das4ce45962015-11-24 23:21:05 -0800786 fwd.id(), deviceId, fwd.nextId());
787 boolean isEthTypeObj = isSupportedEthTypeObjective(fwd);
788 boolean isEthDstObj = isSupportedEthDstObjective(fwd);
789
790 if (isEthTypeObj) {
791 return processEthTypeSpecific(fwd);
792 } else if (isEthDstObj) {
793 return processEthDstSpecific(fwd);
794 } else {
795 log.warn("processSpecific: Unsupported forwarding objective "
796 + "criteria fwd:{} in dev:{}", fwd.nextId(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700797 fail(fwd, ObjectiveError.UNSUPPORTED);
798 return Collections.emptySet();
799 }
Saurav Das4ce45962015-11-24 23:21:05 -0800800 }
801
802 private boolean isSupportedEthTypeObjective(ForwardingObjective fwd) {
803 TrafficSelector selector = fwd.selector();
804 EthTypeCriterion ethType = (EthTypeCriterion) selector
805 .getCriterion(Criterion.Type.ETH_TYPE);
Charles Chan188ebf52015-12-23 00:15:11 -0800806 return !((ethType == null) ||
Saurav Das4ce45962015-11-24 23:21:05 -0800807 ((ethType.ethType().toShort() != Ethernet.TYPE_IPV4) &&
Charles Chan188ebf52015-12-23 00:15:11 -0800808 (ethType.ethType().toShort() != Ethernet.MPLS_UNICAST)));
Saurav Das4ce45962015-11-24 23:21:05 -0800809 }
810
811 private boolean isSupportedEthDstObjective(ForwardingObjective fwd) {
812 TrafficSelector selector = fwd.selector();
813 EthCriterion ethDst = (EthCriterion) selector
814 .getCriterion(Criterion.Type.ETH_DST);
815 VlanIdCriterion vlanId = (VlanIdCriterion) selector
816 .getCriterion(Criterion.Type.VLAN_VID);
Charles Chan188ebf52015-12-23 00:15:11 -0800817 return !(ethDst == null && vlanId == null);
Saurav Das4ce45962015-11-24 23:21:05 -0800818 }
819
820 /**
821 * Handles forwarding rules to the IP and MPLS tables.
822 *
823 * @param fwd the forwarding objective
824 * @return A collection of flow rules, or an empty set
825 */
826 protected Collection<FlowRule> processEthTypeSpecific(ForwardingObjective fwd) {
Charles Chanf9e98652016-09-07 16:54:23 -0700827 return processEthTypeSpecificInternal(fwd, false);
828 }
829
830 /**
831 * Internal implementation of processEthTypeSpecific.
832 * <p>
833 * Wildcarded IPv4_DST is not supported in OFDPA i12. Therefore, we break
834 * the rule into 0.0.0.0/1 and 128.0.0.0/1.
835 * The allowDefaultRoute must be set to false for OFDPA i12.
836 * </p>
837 *
838 * @param fwd the forwarding objective
839 * @param allowDefaultRoute allow wildcarded IPv4_DST or not
840 * @return A collection of flow rules, or an empty set
841 */
842 protected Collection<FlowRule> processEthTypeSpecificInternal(ForwardingObjective fwd,
843 boolean allowDefaultRoute) {
Saurav Das4ce45962015-11-24 23:21:05 -0800844 TrafficSelector selector = fwd.selector();
845 EthTypeCriterion ethType =
846 (EthTypeCriterion) selector.getCriterion(Criterion.Type.ETH_TYPE);
Flavio Castroe10fa242016-01-15 12:43:51 -0800847 boolean defaultRule = false;
848 boolean popMpls = false;
Charles Chan188ebf52015-12-23 00:15:11 -0800849 int forTableId;
Saurav Das8a0732e2015-11-20 15:27:53 -0800850 TrafficSelector.Builder filteredSelector = DefaultTrafficSelector.builder();
Charles Chan14967c22015-12-07 11:11:50 -0800851 TrafficTreatment.Builder tb = DefaultTrafficTreatment.builder();
Flavio Castroe10fa242016-01-15 12:43:51 -0800852 TrafficSelector.Builder complementarySelector = DefaultTrafficSelector.builder();
Charles Chan14967c22015-12-07 11:11:50 -0800853
Saurav Das8a0732e2015-11-20 15:27:53 -0800854 if (ethType.ethType().toShort() == Ethernet.TYPE_IPV4) {
Flavio Castroe10fa242016-01-15 12:43:51 -0800855 IpPrefix ipv4Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV4_DST)).ip();
Charles Chan5b9df8d2016-03-28 22:21:40 -0700856 if (ipv4Dst.isMulticast()) {
857 if (ipv4Dst.prefixLength() != 32) {
858 log.warn("Multicast specific forwarding objective can only be /32");
859 fail(fwd, ObjectiveError.BADPARAMS);
860 return ImmutableSet.of();
861 }
862 VlanId assignedVlan = readVlanFromSelector(fwd.meta());
863 if (assignedVlan == null) {
864 log.warn("VLAN ID required by multicast specific fwd obj is missing. Abort.");
865 fail(fwd, ObjectiveError.BADPARAMS);
866 return ImmutableSet.of();
867 }
868 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
869 filteredSelector.extension(ofdpaMatchVlanVid, deviceId);
870 filteredSelector.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipv4Dst);
871 forTableId = MULTICAST_ROUTING_TABLE;
872 log.debug("processing IPv4 multicast specific forwarding objective {} -> next:{}"
873 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
Flavio Castroe10fa242016-01-15 12:43:51 -0800874 } else {
Charles Chanf9e98652016-09-07 16:54:23 -0700875 if (ipv4Dst.prefixLength() == 0) {
876 if (allowDefaultRoute) {
877 // The entire IPV4_DST field is wildcarded intentionally
878 filteredSelector.matchEthType(Ethernet.TYPE_IPV4);
879 } else {
880 /*
881 * NOTE: The switch does not support matching 0.0.0.0/0
882 * Split it into 0.0.0.0/1 and 128.0.0.0/1
883 */
884 filteredSelector.matchEthType(Ethernet.TYPE_IPV4)
885 .matchIPDst(IpPrefix.valueOf("0.0.0.0/1"));
886 complementarySelector.matchEthType(Ethernet.TYPE_IPV4)
887 .matchIPDst(IpPrefix.valueOf("128.0.0.0/1"));
888 defaultRule = true;
889 }
Charles Chan5b9df8d2016-03-28 22:21:40 -0700890 } else {
Charles Chanf9e98652016-09-07 16:54:23 -0700891 filteredSelector.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipv4Dst);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700892 }
893 forTableId = UNICAST_ROUTING_TABLE;
894 log.debug("processing IPv4 unicast specific forwarding objective {} -> next:{}"
895 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
Flavio Castroe10fa242016-01-15 12:43:51 -0800896 }
Charles Chan14967c22015-12-07 11:11:50 -0800897
898 if (fwd.treatment() != null) {
899 for (Instruction instr : fwd.treatment().allInstructions()) {
900 if (instr instanceof L3ModificationInstruction &&
901 ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
902 tb.deferred().add(instr);
903 }
904 }
905 }
906
Saurav Das8a0732e2015-11-20 15:27:53 -0800907 } else {
908 filteredSelector
909 .matchEthType(Ethernet.MPLS_UNICAST)
910 .matchMplsLabel(((MplsCriterion)
911 selector.getCriterion(Criterion.Type.MPLS_LABEL)).label());
912 MplsBosCriterion bos = (MplsBosCriterion) selector
913 .getCriterion(Criterion.Type.MPLS_BOS);
914 if (bos != null) {
915 filteredSelector.matchMplsBos(bos.mplsBos());
916 }
917 forTableId = MPLS_TABLE_1;
Saurav Das4ce45962015-11-24 23:21:05 -0800918 log.debug("processing MPLS specific forwarding objective {} -> next:{}"
919 + " in dev {}", fwd.id(), fwd.nextId(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700920
Charles Chan14967c22015-12-07 11:11:50 -0800921 if (fwd.treatment() != null) {
922 for (Instruction instr : fwd.treatment().allInstructions()) {
923 if (instr instanceof L2ModificationInstruction &&
924 ((L2ModificationInstruction) instr).subtype() == L2SubType.MPLS_POP) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800925 popMpls = true;
Charles Chan14967c22015-12-07 11:11:50 -0800926 tb.immediate().add(instr);
927 }
928 if (instr instanceof L3ModificationInstruction &&
929 ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
930 // FIXME Should modify the app to send the correct DEC_MPLS_TTL instruction
931 tb.immediate().decMplsTtl();
932 }
933 if (instr instanceof L3ModificationInstruction &&
934 ((L3ModificationInstruction) instr).subtype() == L3SubType.TTL_IN) {
935 tb.immediate().add(instr);
936 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800937 }
938 }
939 }
Saurav Das822c4e22015-10-23 10:51:11 -0700940
941 if (fwd.nextId() != null) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800942 if (forTableId == MPLS_TABLE_1 && !popMpls) {
943 log.warn("SR CONTINUE case cannot be handled as MPLS ECMP "
Saurav Das25190812016-05-27 13:54:07 -0700944 + "is not implemented in OF-DPA yet. Aborting this flow {} -> next:{}"
945 + "in this device {}", fwd.id(), fwd.nextId(), deviceId);
Saurav Das8a0732e2015-11-20 15:27:53 -0800946 // XXX We could convert to forwarding to a single-port, via a
947 // MPLS interface, or a MPLS SWAP (with-same) but that would
948 // have to be handled in the next-objective. Also the pop-mpls
949 // logic used here won't work in non-BoS case.
Saurav Das4ce45962015-11-24 23:21:05 -0800950 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
Saurav Das8a0732e2015-11-20 15:27:53 -0800951 return Collections.emptySet();
952 }
953
Saurav Das423fe2b2015-12-04 10:52:59 -0800954 NextGroup next = getGroupForNextObjective(fwd.nextId());
955 if (next != null) {
956 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
957 // we only need the top level group's key to point the flow to it
958 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
959 if (group == null) {
960 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
961 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
962 fail(fwd, ObjectiveError.GROUPMISSING);
963 return Collections.emptySet();
964 }
965 tb.deferred().group(group.id());
Saurav Das25190812016-05-27 13:54:07 -0700966 } else {
967 log.warn("Cannot find group for nextId:{} in dev:{}. Aborting fwd:{}",
968 fwd.nextId(), deviceId, fwd.id());
969 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
970 return Collections.emptySet();
Saurav Das822c4e22015-10-23 10:51:11 -0700971 }
Saurav Das822c4e22015-10-23 10:51:11 -0700972 }
973 tb.transition(ACL_TABLE);
974 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
975 .fromApp(fwd.appId())
976 .withPriority(fwd.priority())
977 .forDevice(deviceId)
Saurav Das8a0732e2015-11-20 15:27:53 -0800978 .withSelector(filteredSelector.build())
979 .withTreatment(tb.build())
980 .forTable(forTableId);
Saurav Das822c4e22015-10-23 10:51:11 -0700981
982 if (fwd.permanent()) {
983 ruleBuilder.makePermanent();
984 } else {
985 ruleBuilder.makeTemporary(fwd.timeout());
986 }
Flavio Castroe10fa242016-01-15 12:43:51 -0800987 Collection<FlowRule> flowRuleCollection = new ArrayList<>();
988 flowRuleCollection.add(ruleBuilder.build());
989 if (defaultRule) {
990 FlowRule.Builder rule = DefaultFlowRule.builder()
991 .fromApp(fwd.appId())
992 .withPriority(fwd.priority())
993 .forDevice(deviceId)
994 .withSelector(complementarySelector.build())
995 .withTreatment(tb.build())
996 .forTable(forTableId);
997 if (fwd.permanent()) {
998 rule.makePermanent();
999 } else {
1000 rule.makeTemporary(fwd.timeout());
1001 }
1002 flowRuleCollection.add(rule.build());
1003 log.debug("Default rule 0.0.0.0/0 is being installed two rules");
1004 }
Saurav Das822c4e22015-10-23 10:51:11 -07001005
Flavio Castroe10fa242016-01-15 12:43:51 -08001006 return flowRuleCollection;
Saurav Das822c4e22015-10-23 10:51:11 -07001007 }
1008
Saurav Das4ce45962015-11-24 23:21:05 -08001009 /**
1010 * Handles forwarding rules to the L2 bridging table. Flow actions are not
1011 * allowed in the bridging table - instead we use L2 Interface group or
1012 * L2 flood group
1013 *
1014 * @param fwd the forwarding objective
1015 * @return A collection of flow rules, or an empty set
1016 */
1017 protected Collection<FlowRule> processEthDstSpecific(ForwardingObjective fwd) {
1018 List<FlowRule> rules = new ArrayList<>();
1019
1020 // Build filtered selector
1021 TrafficSelector selector = fwd.selector();
1022 EthCriterion ethCriterion = (EthCriterion) selector
1023 .getCriterion(Criterion.Type.ETH_DST);
1024 VlanIdCriterion vlanIdCriterion = (VlanIdCriterion) selector
1025 .getCriterion(Criterion.Type.VLAN_VID);
1026
1027 if (vlanIdCriterion == null) {
1028 log.warn("Forwarding objective for bridging requires vlan. Not "
1029 + "installing fwd:{} in dev:{}", fwd.id(), deviceId);
1030 fail(fwd, ObjectiveError.BADPARAMS);
1031 return Collections.emptySet();
1032 }
1033
1034 TrafficSelector.Builder filteredSelectorBuilder =
1035 DefaultTrafficSelector.builder();
1036 // Do not match MacAddress for subnet broadcast entry
1037 if (!ethCriterion.mac().equals(MacAddress.NONE)) {
1038 filteredSelectorBuilder.matchEthDst(ethCriterion.mac());
1039 log.debug("processing L2 forwarding objective:{} -> next:{} in dev:{}",
1040 fwd.id(), fwd.nextId(), deviceId);
1041 } else {
1042 log.debug("processing L2 Broadcast forwarding objective:{} -> next:{} "
1043 + "in dev:{} for vlan:{}",
1044 fwd.id(), fwd.nextId(), deviceId, vlanIdCriterion.vlanId());
1045 }
Charles Chan14967c22015-12-07 11:11:50 -08001046 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vlanIdCriterion.vlanId());
1047 filteredSelectorBuilder.extension(ofdpaMatchVlanVid, deviceId);
Saurav Das4ce45962015-11-24 23:21:05 -08001048 TrafficSelector filteredSelector = filteredSelectorBuilder.build();
1049
1050 if (fwd.treatment() != null) {
1051 log.warn("Ignoring traffic treatment in fwd rule {} meant for L2 table"
1052 + "for dev:{}. Expecting only nextId", fwd.id(), deviceId);
1053 }
1054
1055 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
1056 if (fwd.nextId() != null) {
Saurav Das423fe2b2015-12-04 10:52:59 -08001057 NextGroup next = getGroupForNextObjective(fwd.nextId());
Saurav Das4ce45962015-11-24 23:21:05 -08001058 if (next != null) {
1059 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1060 // we only need the top level group's key to point the flow to it
1061 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
1062 if (group != null) {
1063 treatmentBuilder.deferred().group(group.id());
1064 } else {
1065 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
1066 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
1067 fail(fwd, ObjectiveError.GROUPMISSING);
1068 return Collections.emptySet();
1069 }
1070 }
1071 }
1072 treatmentBuilder.immediate().transition(ACL_TABLE);
1073 TrafficTreatment filteredTreatment = treatmentBuilder.build();
1074
1075 // Build bridging table entries
1076 FlowRule.Builder flowRuleBuilder = DefaultFlowRule.builder();
1077 flowRuleBuilder.fromApp(fwd.appId())
1078 .withPriority(fwd.priority())
1079 .forDevice(deviceId)
1080 .withSelector(filteredSelector)
1081 .withTreatment(filteredTreatment)
1082 .forTable(BRIDGING_TABLE);
1083 if (fwd.permanent()) {
1084 flowRuleBuilder.makePermanent();
1085 } else {
1086 flowRuleBuilder.makeTemporary(fwd.timeout());
1087 }
1088 rules.add(flowRuleBuilder.build());
1089 return rules;
1090 }
1091
Saurav Das423fe2b2015-12-04 10:52:59 -08001092 protected NextGroup getGroupForNextObjective(Integer nextId) {
1093 NextGroup next = flowObjectiveStore.getNextGroup(nextId);
1094 if (next != null) {
1095 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1096 if (gkeys != null && !gkeys.isEmpty()) {
1097 return next;
1098 } else {
1099 log.warn("Empty next group found in FlowObjective store for "
1100 + "next-id:{} in dev:{}", nextId, deviceId);
1101 }
1102 } else {
1103 log.warn("next-id {} not found in Flow objective store for dev:{}",
1104 nextId, deviceId);
1105 }
1106 return null;
1107 }
1108
Charles Chan188ebf52015-12-23 00:15:11 -08001109 protected static void pass(Objective obj) {
Sho SHIMIZUef7e2902016-02-12 18:38:29 -08001110 obj.context().ifPresent(context -> context.onSuccess(obj));
Saurav Das822c4e22015-10-23 10:51:11 -07001111 }
1112
Charles Chan188ebf52015-12-23 00:15:11 -08001113 protected static void fail(Objective obj, ObjectiveError error) {
Sho SHIMIZUef7e2902016-02-12 18:38:29 -08001114 obj.context().ifPresent(context -> context.onError(obj, error));
Saurav Das822c4e22015-10-23 10:51:11 -07001115 }
Saurav Das24431192016-03-07 19:13:00 -08001116
Saurav Das24431192016-03-07 19:13:00 -08001117 @Override
1118 public List<String> getNextMappings(NextGroup nextGroup) {
1119 List<String> mappings = new ArrayList<>();
1120 List<Deque<GroupKey>> gkeys = appKryo.deserialize(nextGroup.data());
1121 for (Deque<GroupKey> gkd : gkeys) {
1122 Group lastGroup = null;
Saurav Das8be4e3a2016-03-11 17:19:07 -08001123 StringBuffer gchain = new StringBuffer();
Saurav Das24431192016-03-07 19:13:00 -08001124 for (GroupKey gk : gkd) {
1125 Group g = groupService.getGroup(deviceId, gk);
Saurav Das8be4e3a2016-03-11 17:19:07 -08001126 if (g == null) {
Saurav Das25190812016-05-27 13:54:07 -07001127 gchain.append(" NoGrp").append(" -->");
Saurav Das8be4e3a2016-03-11 17:19:07 -08001128 continue;
1129 }
1130 gchain.append(" 0x").append(Integer.toHexString(g.id().id()))
1131 .append(" -->");
Saurav Das24431192016-03-07 19:13:00 -08001132 lastGroup = g;
1133 }
1134 // add port information for last group in group-chain
Saurav Das25190812016-05-27 13:54:07 -07001135 List<Instruction> lastGroupIns = new ArrayList<Instruction>();
Saurav Dasb5c236e2016-06-07 10:08:06 -07001136 if (lastGroup != null) {
Saurav Das25190812016-05-27 13:54:07 -07001137 lastGroupIns = lastGroup.buckets().buckets().get(0)
1138 .treatment().allInstructions();
1139 }
1140 for (Instruction i: lastGroupIns) {
Saurav Das24431192016-03-07 19:13:00 -08001141 if (i instanceof OutputInstruction) {
Saurav Das8be4e3a2016-03-11 17:19:07 -08001142 gchain.append(" port:").append(((OutputInstruction) i).port());
Saurav Das24431192016-03-07 19:13:00 -08001143 }
1144 }
Saurav Das8be4e3a2016-03-11 17:19:07 -08001145 mappings.add(gchain.toString());
Saurav Das24431192016-03-07 19:13:00 -08001146 }
1147 return mappings;
1148 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001149
1150 protected static VlanId readVlanFromSelector(TrafficSelector selector) {
Saurav Das59232cf2016-04-27 18:35:50 -07001151 if (selector == null) {
1152 return null;
1153 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001154 Criterion criterion = selector.getCriterion(Criterion.Type.VLAN_VID);
1155 return (criterion == null)
1156 ? null : ((VlanIdCriterion) criterion).vlanId();
1157 }
1158
1159 protected static IpPrefix readIpDstFromSelector(TrafficSelector selector) {
Saurav Das59232cf2016-04-27 18:35:50 -07001160 if (selector == null) {
1161 return null;
1162 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001163 Criterion criterion = selector.getCriterion(Criterion.Type.IPV4_DST);
1164 return (criterion == null) ? null : ((IPCriterion) criterion).ip();
1165 }
Charles Chand55e84d2016-03-30 17:54:24 -07001166
1167 private static VlanId readVlanFromTreatment(TrafficTreatment treatment) {
Saurav Das59232cf2016-04-27 18:35:50 -07001168 if (treatment == null) {
1169 return null;
1170 }
Charles Chand55e84d2016-03-30 17:54:24 -07001171 for (Instruction i : treatment.allInstructions()) {
1172 if (i instanceof ModVlanIdInstruction) {
1173 return ((ModVlanIdInstruction) i).vlanId();
1174 }
1175 }
1176 return null;
1177 }
Saurav Das822c4e22015-10-23 10:51:11 -07001178}