blob: fc801c5095b6bef9caf03ae99277bb956cfc89d5 [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 *
Saurav Das822c4e22015-10-23 10:51:11 -0700444 * @param portCriterion port on device for which this filter is programmed
445 * @param vidCriterion vlan assigned to port, or NONE for untagged
Saurav Das0e99e2b2015-10-28 12:39:42 -0700446 * @param assignedVlan assigned vlan-id for untagged packets
Saurav Das822c4e22015-10-23 10:51:11 -0700447 * @param applicationId for application programming this filter
448 * @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 Chan14967c22015-12-07 11:11:50 -0800454 List<FlowRule> rules = new ArrayList<>();
Saurav Das822c4e22015-10-23 10:51:11 -0700455 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
456 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
Charles Chan14967c22015-12-07 11:11:50 -0800457 TrafficSelector.Builder preSelector = null;
458 TrafficTreatment.Builder preTreatment = null;
459
Saurav Das4f980082015-11-05 13:39:15 -0800460 treatment.transition(TMAC_TABLE);
461
Saurav Das822c4e22015-10-23 10:51:11 -0700462 if (vidCriterion.vlanId() == VlanId.NONE) {
463 // untagged packets are assigned vlans
Charles Chanbe8aea42016-02-24 12:04:47 -0800464 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(VlanId.NONE);
Charles Chan14967c22015-12-07 11:11:50 -0800465 selector.extension(ofdpaMatchVlanVid, deviceId);
466 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(assignedVlan);
467 treatment.extension(ofdpaSetVlanVid, deviceId);
Charles Chan14967c22015-12-07 11:11:50 -0800468
469 preSelector = DefaultTrafficSelector.builder();
470 OfdpaMatchVlanVid preOfdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
471 preSelector.extension(preOfdpaMatchVlanVid, deviceId);
472 preTreatment = DefaultTrafficTreatment.builder().transition(TMAC_TABLE);
473
Saurav Das4f980082015-11-05 13:39:15 -0800474 } else {
Charles Chan14967c22015-12-07 11:11:50 -0800475 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vidCriterion.vlanId());
476 selector.extension(ofdpaMatchVlanVid, deviceId);
Charles Chand55e84d2016-03-30 17:54:24 -0700477
478 if (!assignedVlan.equals(vidCriterion.vlanId())) {
479 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(assignedVlan);
480 treatment.extension(ofdpaSetVlanVid, deviceId);
481 }
Saurav Das822c4e22015-10-23 10:51:11 -0700482 }
Saurav Das822c4e22015-10-23 10:51:11 -0700483
484 // ofdpa cannot match on ALL portnumber, so we need to use separate
485 // rules for each port.
Charles Chan14967c22015-12-07 11:11:50 -0800486 List<PortNumber> portnums = new ArrayList<>();
Saurav Das822c4e22015-10-23 10:51:11 -0700487 if (portCriterion.port() == PortNumber.ALL) {
488 for (Port port : deviceService.getPorts(deviceId)) {
489 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
490 portnums.add(port.number());
491 }
492 }
493 } else {
494 portnums.add(portCriterion.port());
495 }
Saurav Das4f980082015-11-05 13:39:15 -0800496
Saurav Das822c4e22015-10-23 10:51:11 -0700497 for (PortNumber pnum : portnums) {
Saurav Das4f980082015-11-05 13:39:15 -0800498 // create rest of flowrule
Saurav Das822c4e22015-10-23 10:51:11 -0700499 selector.matchInPort(pnum);
500 FlowRule rule = DefaultFlowRule.builder()
501 .forDevice(deviceId)
502 .withSelector(selector.build())
503 .withTreatment(treatment.build())
504 .withPriority(DEFAULT_PRIORITY)
505 .fromApp(applicationId)
506 .makePermanent()
507 .forTable(VLAN_TABLE).build();
Charles Chan14967c22015-12-07 11:11:50 -0800508
509 if (preSelector != null) {
510 preSelector.matchInPort(pnum);
511 FlowRule preRule = DefaultFlowRule.builder()
512 .forDevice(deviceId)
513 .withSelector(preSelector.build())
514 .withTreatment(preTreatment.build())
515 .withPriority(DEFAULT_PRIORITY)
516 .fromApp(applicationId)
517 .makePermanent()
518 .forTable(VLAN_TABLE).build();
519 rules.add(preRule);
520 }
521
Saurav Das822c4e22015-10-23 10:51:11 -0700522 rules.add(rule);
523 }
524 return rules;
525 }
526
527 /**
528 * Allows routed packets with correct destination MAC to be directed
529 * to unicast-IP routing table or MPLS forwarding table.
Saurav Das822c4e22015-10-23 10:51:11 -0700530 *
531 * @param portCriterion port on device for which this filter is programmed
532 * @param ethCriterion dstMac of device for which is filter is programmed
533 * @param vidCriterion vlan assigned to port, or NONE for untagged
Saurav Das0e99e2b2015-10-28 12:39:42 -0700534 * @param assignedVlan assigned vlan-id for untagged packets
Saurav Das822c4e22015-10-23 10:51:11 -0700535 * @param applicationId for application programming this filter
536 * @return list of FlowRule for port-vlan filters
537
538 */
539 protected List<FlowRule> processEthDstFilter(PortCriterion portCriterion,
540 EthCriterion ethCriterion,
541 VlanIdCriterion vidCriterion,
Saurav Das0e99e2b2015-10-28 12:39:42 -0700542 VlanId assignedVlan,
Saurav Das822c4e22015-10-23 10:51:11 -0700543 ApplicationId applicationId) {
Charles Chan5270ed02016-01-30 23:22:37 -0800544 // Consider PortNumber.ANY as wildcard. Match ETH_DST only
545 if (portCriterion != null && portCriterion.port() == PortNumber.ANY) {
546 return processEthDstOnlyFilter(ethCriterion, applicationId);
547 }
548
Charles Chan5b9df8d2016-03-28 22:21:40 -0700549 // Multicast MAC
550 if (ethCriterion.mask() != null) {
551 return processMcastEthDstFilter(ethCriterion, applicationId);
552 }
553
Saurav Das822c4e22015-10-23 10:51:11 -0700554 //handling untagged packets via assigned VLAN
555 if (vidCriterion.vlanId() == VlanId.NONE) {
Saurav Das0e99e2b2015-10-28 12:39:42 -0700556 vidCriterion = (VlanIdCriterion) Criteria.matchVlanId(assignedVlan);
Saurav Das822c4e22015-10-23 10:51:11 -0700557 }
558 // ofdpa cannot match on ALL portnumber, so we need to use separate
559 // rules for each port.
560 List<PortNumber> portnums = new ArrayList<PortNumber>();
561 if (portCriterion.port() == PortNumber.ALL) {
562 for (Port port : deviceService.getPorts(deviceId)) {
563 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
564 portnums.add(port.number());
565 }
566 }
567 } else {
568 portnums.add(portCriterion.port());
569 }
570
571 List<FlowRule> rules = new ArrayList<FlowRule>();
572 for (PortNumber pnum : portnums) {
Charles Chan14967c22015-12-07 11:11:50 -0800573 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vidCriterion.vlanId());
Saurav Das822c4e22015-10-23 10:51:11 -0700574 // for unicast IP packets
575 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
576 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
577 selector.matchInPort(pnum);
Charles Chan14967c22015-12-07 11:11:50 -0800578 selector.extension(ofdpaMatchVlanVid, deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700579 selector.matchEthType(Ethernet.TYPE_IPV4);
580 selector.matchEthDst(ethCriterion.mac());
581 treatment.transition(UNICAST_ROUTING_TABLE);
582 FlowRule rule = DefaultFlowRule.builder()
583 .forDevice(deviceId)
584 .withSelector(selector.build())
585 .withTreatment(treatment.build())
586 .withPriority(DEFAULT_PRIORITY)
587 .fromApp(applicationId)
588 .makePermanent()
589 .forTable(TMAC_TABLE).build();
590 rules.add(rule);
591 //for MPLS packets
592 selector = DefaultTrafficSelector.builder();
593 treatment = DefaultTrafficTreatment.builder();
594 selector.matchInPort(pnum);
Charles Chan14967c22015-12-07 11:11:50 -0800595 selector.extension(ofdpaMatchVlanVid, deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700596 selector.matchEthType(Ethernet.MPLS_UNICAST);
597 selector.matchEthDst(ethCriterion.mac());
598 treatment.transition(MPLS_TABLE_0);
599 rule = DefaultFlowRule.builder()
600 .forDevice(deviceId)
601 .withSelector(selector.build())
602 .withTreatment(treatment.build())
603 .withPriority(DEFAULT_PRIORITY)
604 .fromApp(applicationId)
605 .makePermanent()
606 .forTable(TMAC_TABLE).build();
607 rules.add(rule);
608 }
609 return rules;
610 }
611
Charles Chan5270ed02016-01-30 23:22:37 -0800612 protected List<FlowRule> processEthDstOnlyFilter(EthCriterion ethCriterion,
613 ApplicationId applicationId) {
614 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
615 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
616 selector.matchEthType(Ethernet.TYPE_IPV4);
617 selector.matchEthDst(ethCriterion.mac());
618 treatment.transition(UNICAST_ROUTING_TABLE);
619 FlowRule rule = DefaultFlowRule.builder()
620 .forDevice(deviceId)
621 .withSelector(selector.build())
622 .withTreatment(treatment.build())
623 .withPriority(DEFAULT_PRIORITY)
624 .fromApp(applicationId)
625 .makePermanent()
626 .forTable(TMAC_TABLE).build();
627 return ImmutableList.<FlowRule>builder().add(rule).build();
628 }
629
Charles Chan5b9df8d2016-03-28 22:21:40 -0700630 protected List<FlowRule> processMcastEthDstFilter(EthCriterion ethCriterion,
631 ApplicationId applicationId) {
632 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
633 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
634 selector.matchEthType(Ethernet.TYPE_IPV4);
635 selector.matchEthDstMasked(ethCriterion.mac(), ethCriterion.mask());
636 treatment.transition(MULTICAST_ROUTING_TABLE);
637 FlowRule rule = DefaultFlowRule.builder()
638 .forDevice(deviceId)
639 .withSelector(selector.build())
640 .withTreatment(treatment.build())
641 .withPriority(DEFAULT_PRIORITY)
642 .fromApp(applicationId)
643 .makePermanent()
644 .forTable(TMAC_TABLE).build();
645 return ImmutableList.<FlowRule>builder().add(rule).build();
646 }
647
Saurav Das822c4e22015-10-23 10:51:11 -0700648 private Collection<FlowRule> processForward(ForwardingObjective fwd) {
649 switch (fwd.flag()) {
650 case SPECIFIC:
651 return processSpecific(fwd);
652 case VERSATILE:
653 return processVersatile(fwd);
654 default:
655 fail(fwd, ObjectiveError.UNKNOWN);
656 log.warn("Unknown forwarding flag {}", fwd.flag());
657 }
658 return Collections.emptySet();
659 }
660
661 /**
662 * In the OF-DPA 2.0 pipeline, versatile forwarding objectives go to the
663 * ACL table.
664 * @param fwd the forwarding objective of type 'versatile'
665 * @return a collection of flow rules to be sent to the switch. An empty
666 * collection may be returned if there is a problem in processing
667 * the flow rule
668 */
Saurav Das52025962016-01-28 22:30:01 -0800669 protected Collection<FlowRule> processVersatile(ForwardingObjective fwd) {
Saurav Das822c4e22015-10-23 10:51:11 -0700670 log.info("Processing versatile forwarding objective");
Saurav Das822c4e22015-10-23 10:51:11 -0700671
672 EthTypeCriterion ethType =
Saurav Das77b5e902016-01-27 17:01:59 -0800673 (EthTypeCriterion) fwd.selector().getCriterion(Criterion.Type.ETH_TYPE);
Saurav Das822c4e22015-10-23 10:51:11 -0700674 if (ethType == null) {
675 log.error("Versatile forwarding objective must include ethType");
676 fail(fwd, ObjectiveError.BADPARAMS);
677 return Collections.emptySet();
678 }
679 if (fwd.nextId() == null && fwd.treatment() == null) {
680 log.error("Forwarding objective {} from {} must contain "
681 + "nextId or Treatment", fwd.selector(), fwd.appId());
Zsolt Haraszti9faab752016-02-17 15:55:20 -0800682 fail(fwd, ObjectiveError.BADPARAMS);
Saurav Das822c4e22015-10-23 10:51:11 -0700683 return Collections.emptySet();
684 }
Saurav Das49cb5a12016-01-16 22:54:07 -0800685
Saurav Das77b5e902016-01-27 17:01:59 -0800686 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
687 fwd.selector().criteria().forEach(criterion -> {
688 if (criterion instanceof VlanIdCriterion) {
689 VlanId vlanId = ((VlanIdCriterion) criterion).vlanId();
690 // ensure that match does not include vlan = NONE as OF-DPA does not
691 // match untagged packets this way in the ACL table.
692 if (vlanId.equals(VlanId.NONE)) {
693 return;
694 }
695 OfdpaMatchVlanVid ofdpaMatchVlanVid =
696 new OfdpaMatchVlanVid(vlanId);
697 sbuilder.extension(ofdpaMatchVlanVid, deviceId);
698 } else {
699 sbuilder.add(criterion);
700 }
701 });
702
Saurav Das822c4e22015-10-23 10:51:11 -0700703 // XXX driver does not currently do type checking as per Tables 65-67 in
704 // OFDPA 2.0 spec. The only allowed treatment is a punt to the controller.
Saurav Das49cb5a12016-01-16 22:54:07 -0800705 TrafficTreatment.Builder ttBuilder = DefaultTrafficTreatment.builder();
706 if (fwd.treatment() != null) {
707 for (Instruction ins : fwd.treatment().allInstructions()) {
708 if (ins instanceof OutputInstruction) {
709 OutputInstruction o = (OutputInstruction) ins;
710 if (o.port() == PortNumber.CONTROLLER) {
711 ttBuilder.add(o);
712 } else {
713 log.warn("Only allowed treatments in versatile forwarding "
714 + "objectives are punts to the controller");
715 }
716 } else {
717 log.warn("Cannot process instruction in versatile fwd {}", ins);
718 }
Saurav Das822c4e22015-10-23 10:51:11 -0700719 }
720 }
Saurav Das822c4e22015-10-23 10:51:11 -0700721 if (fwd.nextId() != null) {
Saurav Das49cb5a12016-01-16 22:54:07 -0800722 // overide case
723 NextGroup next = getGroupForNextObjective(fwd.nextId());
724 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
725 // we only need the top level group's key to point the flow to it
726 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
727 if (group == null) {
728 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
729 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
730 fail(fwd, ObjectiveError.GROUPMISSING);
731 return Collections.emptySet();
732 }
733 ttBuilder.deferred().group(group.id());
Saurav Das822c4e22015-10-23 10:51:11 -0700734 }
Saurav Das49cb5a12016-01-16 22:54:07 -0800735
736 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
737 .fromApp(fwd.appId())
738 .withPriority(fwd.priority())
739 .forDevice(deviceId)
Saurav Das77b5e902016-01-27 17:01:59 -0800740 .withSelector(sbuilder.build())
Saurav Das49cb5a12016-01-16 22:54:07 -0800741 .withTreatment(ttBuilder.build())
742 .makePermanent()
743 .forTable(ACL_TABLE);
744 return Collections.singletonList(ruleBuilder.build());
Saurav Das822c4e22015-10-23 10:51:11 -0700745 }
746
747 /**
748 * In the OF-DPA 2.0 pipeline, specific forwarding refers to the IP table
Saurav Das8a0732e2015-11-20 15:27:53 -0800749 * (unicast or multicast) or the L2 table (mac + vlan) or the MPLS table.
Saurav Das822c4e22015-10-23 10:51:11 -0700750 *
751 * @param fwd the forwarding objective of type 'specific'
752 * @return a collection of flow rules. Typically there will be only one
753 * for this type of forwarding objective. An empty set may be
754 * returned if there is an issue in processing the objective.
755 */
Saurav Das8a0732e2015-11-20 15:27:53 -0800756 protected Collection<FlowRule> processSpecific(ForwardingObjective fwd) {
Saurav Das25190812016-05-27 13:54:07 -0700757 log.debug("Processing specific fwd objective:{} in dev:{} with next:{}",
Saurav Das4ce45962015-11-24 23:21:05 -0800758 fwd.id(), deviceId, fwd.nextId());
759 boolean isEthTypeObj = isSupportedEthTypeObjective(fwd);
760 boolean isEthDstObj = isSupportedEthDstObjective(fwd);
761
762 if (isEthTypeObj) {
763 return processEthTypeSpecific(fwd);
764 } else if (isEthDstObj) {
765 return processEthDstSpecific(fwd);
766 } else {
767 log.warn("processSpecific: Unsupported forwarding objective "
768 + "criteria fwd:{} in dev:{}", fwd.nextId(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700769 fail(fwd, ObjectiveError.UNSUPPORTED);
770 return Collections.emptySet();
771 }
Saurav Das4ce45962015-11-24 23:21:05 -0800772 }
773
774 private boolean isSupportedEthTypeObjective(ForwardingObjective fwd) {
775 TrafficSelector selector = fwd.selector();
776 EthTypeCriterion ethType = (EthTypeCriterion) selector
777 .getCriterion(Criterion.Type.ETH_TYPE);
Charles Chan188ebf52015-12-23 00:15:11 -0800778 return !((ethType == null) ||
Saurav Das4ce45962015-11-24 23:21:05 -0800779 ((ethType.ethType().toShort() != Ethernet.TYPE_IPV4) &&
Charles Chan188ebf52015-12-23 00:15:11 -0800780 (ethType.ethType().toShort() != Ethernet.MPLS_UNICAST)));
Saurav Das4ce45962015-11-24 23:21:05 -0800781 }
782
783 private boolean isSupportedEthDstObjective(ForwardingObjective fwd) {
784 TrafficSelector selector = fwd.selector();
785 EthCriterion ethDst = (EthCriterion) selector
786 .getCriterion(Criterion.Type.ETH_DST);
787 VlanIdCriterion vlanId = (VlanIdCriterion) selector
788 .getCriterion(Criterion.Type.VLAN_VID);
Charles Chan188ebf52015-12-23 00:15:11 -0800789 return !(ethDst == null && vlanId == null);
Saurav Das4ce45962015-11-24 23:21:05 -0800790 }
791
792 /**
793 * Handles forwarding rules to the IP and MPLS tables.
794 *
795 * @param fwd the forwarding objective
796 * @return A collection of flow rules, or an empty set
797 */
798 protected Collection<FlowRule> processEthTypeSpecific(ForwardingObjective fwd) {
799 TrafficSelector selector = fwd.selector();
800 EthTypeCriterion ethType =
801 (EthTypeCriterion) selector.getCriterion(Criterion.Type.ETH_TYPE);
Flavio Castroe10fa242016-01-15 12:43:51 -0800802 boolean defaultRule = false;
803 boolean popMpls = false;
Charles Chan188ebf52015-12-23 00:15:11 -0800804 int forTableId;
Saurav Das8a0732e2015-11-20 15:27:53 -0800805 TrafficSelector.Builder filteredSelector = DefaultTrafficSelector.builder();
Charles Chan14967c22015-12-07 11:11:50 -0800806 TrafficTreatment.Builder tb = DefaultTrafficTreatment.builder();
Flavio Castroe10fa242016-01-15 12:43:51 -0800807 TrafficSelector.Builder complementarySelector = DefaultTrafficSelector.builder();
Charles Chan14967c22015-12-07 11:11:50 -0800808
Flavio Castroe10fa242016-01-15 12:43:51 -0800809 /*
810 * NOTE: The switch does not support matching 0.0.0.0/0.
811 * Split it into 0.0.0.0/1 and 128.0.0.0/1
812 */
Saurav Das8a0732e2015-11-20 15:27:53 -0800813 if (ethType.ethType().toShort() == Ethernet.TYPE_IPV4) {
Flavio Castroe10fa242016-01-15 12:43:51 -0800814 IpPrefix ipv4Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV4_DST)).ip();
Charles Chan5b9df8d2016-03-28 22:21:40 -0700815 if (ipv4Dst.isMulticast()) {
816 if (ipv4Dst.prefixLength() != 32) {
817 log.warn("Multicast specific forwarding objective can only be /32");
818 fail(fwd, ObjectiveError.BADPARAMS);
819 return ImmutableSet.of();
820 }
821 VlanId assignedVlan = readVlanFromSelector(fwd.meta());
822 if (assignedVlan == null) {
823 log.warn("VLAN ID required by multicast specific fwd obj is missing. Abort.");
824 fail(fwd, ObjectiveError.BADPARAMS);
825 return ImmutableSet.of();
826 }
827 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
828 filteredSelector.extension(ofdpaMatchVlanVid, deviceId);
829 filteredSelector.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipv4Dst);
830 forTableId = MULTICAST_ROUTING_TABLE;
831 log.debug("processing IPv4 multicast specific forwarding objective {} -> next:{}"
832 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
Flavio Castroe10fa242016-01-15 12:43:51 -0800833 } else {
Charles Chan5b9df8d2016-03-28 22:21:40 -0700834 if (ipv4Dst.prefixLength() > 0) {
835 filteredSelector.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipv4Dst);
836 } else {
837 filteredSelector.matchEthType(Ethernet.TYPE_IPV4)
838 .matchIPDst(IpPrefix.valueOf("0.0.0.0/1"));
839 complementarySelector.matchEthType(Ethernet.TYPE_IPV4)
840 .matchIPDst(IpPrefix.valueOf("128.0.0.0/1"));
841 defaultRule = true;
842 }
843 forTableId = UNICAST_ROUTING_TABLE;
844 log.debug("processing IPv4 unicast specific forwarding objective {} -> next:{}"
845 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
Flavio Castroe10fa242016-01-15 12:43:51 -0800846 }
Charles Chan14967c22015-12-07 11:11:50 -0800847
848 if (fwd.treatment() != null) {
849 for (Instruction instr : fwd.treatment().allInstructions()) {
850 if (instr instanceof L3ModificationInstruction &&
851 ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
852 tb.deferred().add(instr);
853 }
854 }
855 }
856
Saurav Das8a0732e2015-11-20 15:27:53 -0800857 } else {
858 filteredSelector
859 .matchEthType(Ethernet.MPLS_UNICAST)
860 .matchMplsLabel(((MplsCriterion)
861 selector.getCriterion(Criterion.Type.MPLS_LABEL)).label());
862 MplsBosCriterion bos = (MplsBosCriterion) selector
863 .getCriterion(Criterion.Type.MPLS_BOS);
864 if (bos != null) {
865 filteredSelector.matchMplsBos(bos.mplsBos());
866 }
867 forTableId = MPLS_TABLE_1;
Saurav Das4ce45962015-11-24 23:21:05 -0800868 log.debug("processing MPLS specific forwarding objective {} -> next:{}"
869 + " in dev {}", fwd.id(), fwd.nextId(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700870
Charles Chan14967c22015-12-07 11:11:50 -0800871 if (fwd.treatment() != null) {
872 for (Instruction instr : fwd.treatment().allInstructions()) {
873 if (instr instanceof L2ModificationInstruction &&
874 ((L2ModificationInstruction) instr).subtype() == L2SubType.MPLS_POP) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800875 popMpls = true;
Charles Chan14967c22015-12-07 11:11:50 -0800876 tb.immediate().add(instr);
877 }
878 if (instr instanceof L3ModificationInstruction &&
879 ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
880 // FIXME Should modify the app to send the correct DEC_MPLS_TTL instruction
881 tb.immediate().decMplsTtl();
882 }
883 if (instr instanceof L3ModificationInstruction &&
884 ((L3ModificationInstruction) instr).subtype() == L3SubType.TTL_IN) {
885 tb.immediate().add(instr);
886 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800887 }
888 }
889 }
Saurav Das822c4e22015-10-23 10:51:11 -0700890
891 if (fwd.nextId() != null) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800892 if (forTableId == MPLS_TABLE_1 && !popMpls) {
893 log.warn("SR CONTINUE case cannot be handled as MPLS ECMP "
Saurav Das25190812016-05-27 13:54:07 -0700894 + "is not implemented in OF-DPA yet. Aborting this flow {} -> next:{}"
895 + "in this device {}", fwd.id(), fwd.nextId(), deviceId);
Saurav Das8a0732e2015-11-20 15:27:53 -0800896 // XXX We could convert to forwarding to a single-port, via a
897 // MPLS interface, or a MPLS SWAP (with-same) but that would
898 // have to be handled in the next-objective. Also the pop-mpls
899 // logic used here won't work in non-BoS case.
Saurav Das4ce45962015-11-24 23:21:05 -0800900 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
Saurav Das8a0732e2015-11-20 15:27:53 -0800901 return Collections.emptySet();
902 }
903
Saurav Das423fe2b2015-12-04 10:52:59 -0800904 NextGroup next = getGroupForNextObjective(fwd.nextId());
905 if (next != null) {
906 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
907 // we only need the top level group's key to point the flow to it
908 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
909 if (group == null) {
910 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
911 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
912 fail(fwd, ObjectiveError.GROUPMISSING);
913 return Collections.emptySet();
914 }
915 tb.deferred().group(group.id());
Saurav Das25190812016-05-27 13:54:07 -0700916 } else {
917 log.warn("Cannot find group for nextId:{} in dev:{}. Aborting fwd:{}",
918 fwd.nextId(), deviceId, fwd.id());
919 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
920 return Collections.emptySet();
Saurav Das822c4e22015-10-23 10:51:11 -0700921 }
Saurav Das822c4e22015-10-23 10:51:11 -0700922 }
923 tb.transition(ACL_TABLE);
924 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
925 .fromApp(fwd.appId())
926 .withPriority(fwd.priority())
927 .forDevice(deviceId)
Saurav Das8a0732e2015-11-20 15:27:53 -0800928 .withSelector(filteredSelector.build())
929 .withTreatment(tb.build())
930 .forTable(forTableId);
Saurav Das822c4e22015-10-23 10:51:11 -0700931
932 if (fwd.permanent()) {
933 ruleBuilder.makePermanent();
934 } else {
935 ruleBuilder.makeTemporary(fwd.timeout());
936 }
Flavio Castroe10fa242016-01-15 12:43:51 -0800937 Collection<FlowRule> flowRuleCollection = new ArrayList<>();
938 flowRuleCollection.add(ruleBuilder.build());
939 if (defaultRule) {
940 FlowRule.Builder rule = DefaultFlowRule.builder()
941 .fromApp(fwd.appId())
942 .withPriority(fwd.priority())
943 .forDevice(deviceId)
944 .withSelector(complementarySelector.build())
945 .withTreatment(tb.build())
946 .forTable(forTableId);
947 if (fwd.permanent()) {
948 rule.makePermanent();
949 } else {
950 rule.makeTemporary(fwd.timeout());
951 }
952 flowRuleCollection.add(rule.build());
953 log.debug("Default rule 0.0.0.0/0 is being installed two rules");
954 }
Saurav Das822c4e22015-10-23 10:51:11 -0700955
Flavio Castroe10fa242016-01-15 12:43:51 -0800956 return flowRuleCollection;
Saurav Das822c4e22015-10-23 10:51:11 -0700957 }
958
Saurav Das4ce45962015-11-24 23:21:05 -0800959 /**
960 * Handles forwarding rules to the L2 bridging table. Flow actions are not
961 * allowed in the bridging table - instead we use L2 Interface group or
962 * L2 flood group
963 *
964 * @param fwd the forwarding objective
965 * @return A collection of flow rules, or an empty set
966 */
967 protected Collection<FlowRule> processEthDstSpecific(ForwardingObjective fwd) {
968 List<FlowRule> rules = new ArrayList<>();
969
970 // Build filtered selector
971 TrafficSelector selector = fwd.selector();
972 EthCriterion ethCriterion = (EthCriterion) selector
973 .getCriterion(Criterion.Type.ETH_DST);
974 VlanIdCriterion vlanIdCriterion = (VlanIdCriterion) selector
975 .getCriterion(Criterion.Type.VLAN_VID);
976
977 if (vlanIdCriterion == null) {
978 log.warn("Forwarding objective for bridging requires vlan. Not "
979 + "installing fwd:{} in dev:{}", fwd.id(), deviceId);
980 fail(fwd, ObjectiveError.BADPARAMS);
981 return Collections.emptySet();
982 }
983
984 TrafficSelector.Builder filteredSelectorBuilder =
985 DefaultTrafficSelector.builder();
986 // Do not match MacAddress for subnet broadcast entry
987 if (!ethCriterion.mac().equals(MacAddress.NONE)) {
988 filteredSelectorBuilder.matchEthDst(ethCriterion.mac());
989 log.debug("processing L2 forwarding objective:{} -> next:{} in dev:{}",
990 fwd.id(), fwd.nextId(), deviceId);
991 } else {
992 log.debug("processing L2 Broadcast forwarding objective:{} -> next:{} "
993 + "in dev:{} for vlan:{}",
994 fwd.id(), fwd.nextId(), deviceId, vlanIdCriterion.vlanId());
995 }
Charles Chan14967c22015-12-07 11:11:50 -0800996 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vlanIdCriterion.vlanId());
997 filteredSelectorBuilder.extension(ofdpaMatchVlanVid, deviceId);
Saurav Das4ce45962015-11-24 23:21:05 -0800998 TrafficSelector filteredSelector = filteredSelectorBuilder.build();
999
1000 if (fwd.treatment() != null) {
1001 log.warn("Ignoring traffic treatment in fwd rule {} meant for L2 table"
1002 + "for dev:{}. Expecting only nextId", fwd.id(), deviceId);
1003 }
1004
1005 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
1006 if (fwd.nextId() != null) {
Saurav Das423fe2b2015-12-04 10:52:59 -08001007 NextGroup next = getGroupForNextObjective(fwd.nextId());
Saurav Das4ce45962015-11-24 23:21:05 -08001008 if (next != null) {
1009 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1010 // we only need the top level group's key to point the flow to it
1011 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
1012 if (group != null) {
1013 treatmentBuilder.deferred().group(group.id());
1014 } else {
1015 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
1016 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
1017 fail(fwd, ObjectiveError.GROUPMISSING);
1018 return Collections.emptySet();
1019 }
1020 }
1021 }
1022 treatmentBuilder.immediate().transition(ACL_TABLE);
1023 TrafficTreatment filteredTreatment = treatmentBuilder.build();
1024
1025 // Build bridging table entries
1026 FlowRule.Builder flowRuleBuilder = DefaultFlowRule.builder();
1027 flowRuleBuilder.fromApp(fwd.appId())
1028 .withPriority(fwd.priority())
1029 .forDevice(deviceId)
1030 .withSelector(filteredSelector)
1031 .withTreatment(filteredTreatment)
1032 .forTable(BRIDGING_TABLE);
1033 if (fwd.permanent()) {
1034 flowRuleBuilder.makePermanent();
1035 } else {
1036 flowRuleBuilder.makeTemporary(fwd.timeout());
1037 }
1038 rules.add(flowRuleBuilder.build());
1039 return rules;
1040 }
1041
Saurav Das423fe2b2015-12-04 10:52:59 -08001042 protected NextGroup getGroupForNextObjective(Integer nextId) {
1043 NextGroup next = flowObjectiveStore.getNextGroup(nextId);
1044 if (next != null) {
1045 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1046 if (gkeys != null && !gkeys.isEmpty()) {
1047 return next;
1048 } else {
1049 log.warn("Empty next group found in FlowObjective store for "
1050 + "next-id:{} in dev:{}", nextId, deviceId);
1051 }
1052 } else {
1053 log.warn("next-id {} not found in Flow objective store for dev:{}",
1054 nextId, deviceId);
1055 }
1056 return null;
1057 }
1058
Charles Chan188ebf52015-12-23 00:15:11 -08001059 protected static void pass(Objective obj) {
Sho SHIMIZUef7e2902016-02-12 18:38:29 -08001060 obj.context().ifPresent(context -> context.onSuccess(obj));
Saurav Das822c4e22015-10-23 10:51:11 -07001061 }
1062
Charles Chan188ebf52015-12-23 00:15:11 -08001063 protected static void fail(Objective obj, ObjectiveError error) {
Sho SHIMIZUef7e2902016-02-12 18:38:29 -08001064 obj.context().ifPresent(context -> context.onError(obj, error));
Saurav Das822c4e22015-10-23 10:51:11 -07001065 }
Saurav Das24431192016-03-07 19:13:00 -08001066
Saurav Das24431192016-03-07 19:13:00 -08001067 @Override
1068 public List<String> getNextMappings(NextGroup nextGroup) {
1069 List<String> mappings = new ArrayList<>();
1070 List<Deque<GroupKey>> gkeys = appKryo.deserialize(nextGroup.data());
1071 for (Deque<GroupKey> gkd : gkeys) {
1072 Group lastGroup = null;
Saurav Das8be4e3a2016-03-11 17:19:07 -08001073 StringBuffer gchain = new StringBuffer();
Saurav Das24431192016-03-07 19:13:00 -08001074 for (GroupKey gk : gkd) {
1075 Group g = groupService.getGroup(deviceId, gk);
Saurav Das8be4e3a2016-03-11 17:19:07 -08001076 if (g == null) {
Saurav Das25190812016-05-27 13:54:07 -07001077 gchain.append(" NoGrp").append(" -->");
Saurav Das8be4e3a2016-03-11 17:19:07 -08001078 continue;
1079 }
1080 gchain.append(" 0x").append(Integer.toHexString(g.id().id()))
1081 .append(" -->");
Saurav Das24431192016-03-07 19:13:00 -08001082 lastGroup = g;
1083 }
1084 // add port information for last group in group-chain
Saurav Das25190812016-05-27 13:54:07 -07001085 List<Instruction> lastGroupIns = new ArrayList<Instruction>();
Saurav Dasb5c236e2016-06-07 10:08:06 -07001086 if (lastGroup != null) {
Saurav Das25190812016-05-27 13:54:07 -07001087 lastGroupIns = lastGroup.buckets().buckets().get(0)
1088 .treatment().allInstructions();
1089 }
1090 for (Instruction i: lastGroupIns) {
Saurav Das24431192016-03-07 19:13:00 -08001091 if (i instanceof OutputInstruction) {
Saurav Das8be4e3a2016-03-11 17:19:07 -08001092 gchain.append(" port:").append(((OutputInstruction) i).port());
Saurav Das24431192016-03-07 19:13:00 -08001093 }
1094 }
Saurav Das8be4e3a2016-03-11 17:19:07 -08001095 mappings.add(gchain.toString());
Saurav Das24431192016-03-07 19:13:00 -08001096 }
1097 return mappings;
1098 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001099
1100 protected static VlanId readVlanFromSelector(TrafficSelector selector) {
Saurav Das59232cf2016-04-27 18:35:50 -07001101 if (selector == null) {
1102 return null;
1103 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001104 Criterion criterion = selector.getCriterion(Criterion.Type.VLAN_VID);
1105 return (criterion == null)
1106 ? null : ((VlanIdCriterion) criterion).vlanId();
1107 }
1108
1109 protected static IpPrefix readIpDstFromSelector(TrafficSelector selector) {
Saurav Das59232cf2016-04-27 18:35:50 -07001110 if (selector == null) {
1111 return null;
1112 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001113 Criterion criterion = selector.getCriterion(Criterion.Type.IPV4_DST);
1114 return (criterion == null) ? null : ((IPCriterion) criterion).ip();
1115 }
Charles Chand55e84d2016-03-30 17:54:24 -07001116
1117 private static VlanId readVlanFromTreatment(TrafficTreatment treatment) {
Saurav Das59232cf2016-04-27 18:35:50 -07001118 if (treatment == null) {
1119 return null;
1120 }
Charles Chand55e84d2016-03-30 17:54:24 -07001121 for (Instruction i : treatment.allInstructions()) {
1122 if (i instanceof ModVlanIdInstruction) {
1123 return ((ModVlanIdInstruction) i).vlanId();
1124 }
1125 }
1126 return null;
1127 }
Saurav Das822c4e22015-10-23 10:51:11 -07001128}