blob: eca84124a8f70aeb1d52ea1f023b64ea05e3e1a0 [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 Das822c4e22015-10-23 10:51:11 -0700124 .register(byte[].class)
Saurav Das8a0732e2015-11-20 15:27:53 -0800125 .register(ArrayDeque.class)
Saurav Das822c4e22015-10-23 10:51:11 -0700126 .build();
127
Charles Chan425854b2016-04-11 15:32:12 -0700128 protected Ofdpa2GroupHandler groupHandler;
Saurav Das822c4e22015-10-23 10:51:11 -0700129
Saurav Das52025962016-01-28 22:30:01 -0800130 protected Set<IPCriterion> sentIpFilters = Collections.newSetFromMap(
Charles Chan188ebf52015-12-23 00:15:11 -0800131 new ConcurrentHashMap<>());
Saurav Das4f980082015-11-05 13:39:15 -0800132
Saurav Das822c4e22015-10-23 10:51:11 -0700133 @Override
134 public void init(DeviceId deviceId, PipelinerContext context) {
Saurav Das822c4e22015-10-23 10:51:11 -0700135 this.deviceId = deviceId;
136
Charles Chan188ebf52015-12-23 00:15:11 -0800137 // Initialize OFDPA group handler
Charles Chan425854b2016-04-11 15:32:12 -0700138 groupHandler = new Ofdpa2GroupHandler();
139 groupHandler.init(deviceId, context);
Saurav Das822c4e22015-10-23 10:51:11 -0700140
Charles Chan425854b2016-04-11 15:32:12 -0700141 serviceDirectory = context.directory();
Saurav Das822c4e22015-10-23 10:51:11 -0700142 coreService = serviceDirectory.get(CoreService.class);
143 flowRuleService = serviceDirectory.get(FlowRuleService.class);
144 groupService = serviceDirectory.get(GroupService.class);
145 flowObjectiveStore = context.store();
Saurav Das822c4e22015-10-23 10:51:11 -0700146 deviceService = serviceDirectory.get(DeviceService.class);
Saurav Das822c4e22015-10-23 10:51:11 -0700147
148 driverId = coreService.registerApplication(
Charles Chan425854b2016-04-11 15:32:12 -0700149 "org.onosproject.driver.Ofdpa2Pipeline");
Saurav Das822c4e22015-10-23 10:51:11 -0700150
Saurav Das822c4e22015-10-23 10:51:11 -0700151 initializePipeline();
Saurav Das822c4e22015-10-23 10:51:11 -0700152 }
153
154 protected void initializePipeline() {
Charles Chan188ebf52015-12-23 00:15:11 -0800155 // OF-DPA does not require initializing the pipeline as it puts default
156 // rules automatically in the hardware. However emulation of OFDPA in
157 // software switches does require table-miss-entries.
Saurav Das822c4e22015-10-23 10:51:11 -0700158 }
159
160 //////////////////////////////////////
161 // Flow Objectives
162 //////////////////////////////////////
163
164 @Override
165 public void filter(FilteringObjective filteringObjective) {
166 if (filteringObjective.type() == FilteringObjective.Type.PERMIT) {
167 processFilter(filteringObjective,
168 filteringObjective.op() == Objective.Operation.ADD,
169 filteringObjective.appId());
170 } else {
171 // Note that packets that don't match the PERMIT filter are
172 // automatically denied. The DENY filter is used to deny packets
173 // that are otherwise permitted by the PERMIT filter.
174 // Use ACL table flow rules here for DENY filtering objectives
175 log.debug("filter objective other than PERMIT currently not supported");
176 fail(filteringObjective, ObjectiveError.UNSUPPORTED);
177 }
178 }
179
180 @Override
181 public void forward(ForwardingObjective fwd) {
182 Collection<FlowRule> rules;
183 FlowRuleOperations.Builder flowOpsBuilder = FlowRuleOperations.builder();
184
185 rules = processForward(fwd);
186 switch (fwd.op()) {
187 case ADD:
188 rules.stream()
Sho SHIMIZU45906042016-01-13 23:05:54 -0800189 .filter(Objects::nonNull)
Saurav Das822c4e22015-10-23 10:51:11 -0700190 .forEach(flowOpsBuilder::add);
191 break;
192 case REMOVE:
193 rules.stream()
Sho SHIMIZU45906042016-01-13 23:05:54 -0800194 .filter(Objects::nonNull)
Saurav Das822c4e22015-10-23 10:51:11 -0700195 .forEach(flowOpsBuilder::remove);
196 break;
197 default:
198 fail(fwd, ObjectiveError.UNKNOWN);
199 log.warn("Unknown forwarding type {}", fwd.op());
200 }
201
Saurav Das822c4e22015-10-23 10:51:11 -0700202 flowRuleService.apply(flowOpsBuilder.build(new FlowRuleOperationsContext() {
203 @Override
204 public void onSuccess(FlowRuleOperations ops) {
205 pass(fwd);
206 }
207
208 @Override
209 public void onError(FlowRuleOperations ops) {
210 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
211 }
212 }));
Saurav Das822c4e22015-10-23 10:51:11 -0700213 }
214
215 @Override
216 public void next(NextObjective nextObjective) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800217 NextGroup nextGroup = flowObjectiveStore.getNextGroup(nextObjective.id());
218 switch (nextObjective.op()) {
219 case ADD:
Saurav Das4f980082015-11-05 13:39:15 -0800220 if (nextGroup != null) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800221 log.warn("Cannot add next {} that already exists in device {}",
222 nextObjective.id(), deviceId);
223 return;
224 }
225 log.debug("Processing NextObjective id{} in dev{} - add group",
226 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700227 groupHandler.addGroup(nextObjective);
Saurav Das8a0732e2015-11-20 15:27:53 -0800228 break;
229 case ADD_TO_EXISTING:
230 if (nextGroup != null) {
231 log.debug("Processing NextObjective id{} in dev{} - add bucket",
232 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700233 groupHandler.addBucketToGroup(nextObjective, nextGroup);
Saurav Das4f980082015-11-05 13:39:15 -0800234 } else {
Saurav Das8a0732e2015-11-20 15:27:53 -0800235 // it is possible that group-chain has not been fully created yet
Saurav Das423fe2b2015-12-04 10:52:59 -0800236 log.debug("Waiting to add bucket to group for next-id:{} in dev:{}",
237 nextObjective.id(), deviceId);
238 // by design only one pending bucket is allowed for the group
Charles Chan425854b2016-04-11 15:32:12 -0700239 groupHandler.pendingBuckets.put(nextObjective.id(), nextObjective);
Saurav Das4f980082015-11-05 13:39:15 -0800240 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800241 break;
242 case REMOVE:
243 if (nextGroup == null) {
244 log.warn("Cannot remove next {} that does not exist in device {}",
245 nextObjective.id(), deviceId);
246 return;
247 }
248 log.debug("Processing NextObjective id{} in dev{} - remove group",
249 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700250 groupHandler.removeGroup(nextObjective, nextGroup);
Saurav Das8a0732e2015-11-20 15:27:53 -0800251 break;
252 case REMOVE_FROM_EXISTING:
253 if (nextGroup == null) {
254 log.warn("Cannot remove from next {} that does not exist in device {}",
255 nextObjective.id(), deviceId);
256 return;
257 }
258 log.debug("Processing NextObjective id{} in dev{} - remove bucket",
259 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700260 groupHandler.removeBucketFromGroup(nextObjective, nextGroup);
Saurav Das8a0732e2015-11-20 15:27:53 -0800261 break;
262 default:
Saurav Das4f980082015-11-05 13:39:15 -0800263 log.warn("Unsupported operation {}", nextObjective.op());
Saurav Das822c4e22015-10-23 10:51:11 -0700264 }
265 }
266
267 //////////////////////////////////////
268 // Flow handling
269 //////////////////////////////////////
270
271 /**
272 * As per OFDPA 2.0 TTP, filtering of VLAN ids, MAC addresses (for routing)
273 * and IP addresses configured on switch ports happen in different tables.
274 * Note that IP filtering rules need to be added to the ACL table, as there
275 * is no mechanism to send to controller via IP table.
276 *
277 * @param filt the filtering objective
278 * @param install indicates whether to add or remove the objective
279 * @param applicationId the application that sent this objective
280 */
Saurav Das52025962016-01-28 22:30:01 -0800281 protected void processFilter(FilteringObjective filt,
282 boolean install, ApplicationId applicationId) {
Saurav Das822c4e22015-10-23 10:51:11 -0700283 // This driver only processes filtering criteria defined with switch
284 // ports as the key
285 PortCriterion portCriterion = null;
286 EthCriterion ethCriterion = null;
287 VlanIdCriterion vidCriterion = null;
288 Collection<IPCriterion> ips = new ArrayList<IPCriterion>();
289 if (!filt.key().equals(Criteria.dummy()) &&
290 filt.key().type() == Criterion.Type.IN_PORT) {
291 portCriterion = (PortCriterion) filt.key();
292 } else {
293 log.warn("No key defined in filtering objective from app: {}. Not"
294 + "processing filtering objective", applicationId);
Saurav Das59232cf2016-04-27 18:35:50 -0700295 fail(filt, ObjectiveError.BADPARAMS);
Saurav Das822c4e22015-10-23 10:51:11 -0700296 return;
297 }
Saurav Das59232cf2016-04-27 18:35:50 -0700298 log.debug("Received filtering objective for dev/port: {}/{}", deviceId,
299 portCriterion.port());
Saurav Das822c4e22015-10-23 10:51:11 -0700300 // convert filtering conditions for switch-intfs into flowrules
301 FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
302 for (Criterion criterion : filt.conditions()) {
Charles Chan5b9df8d2016-03-28 22:21:40 -0700303 if (criterion.type() == Criterion.Type.ETH_DST ||
304 criterion.type() == Criterion.Type.ETH_DST_MASKED) {
Saurav Das822c4e22015-10-23 10:51:11 -0700305 ethCriterion = (EthCriterion) criterion;
306 } else if (criterion.type() == Criterion.Type.VLAN_VID) {
307 vidCriterion = (VlanIdCriterion) criterion;
308 } else if (criterion.type() == Criterion.Type.IPV4_DST) {
309 ips.add((IPCriterion) criterion);
310 } else {
311 log.error("Unsupported filter {}", criterion);
312 fail(filt, ObjectiveError.UNSUPPORTED);
313 return;
314 }
315 }
316
Saurav Das0e99e2b2015-10-28 12:39:42 -0700317 VlanId assignedVlan = null;
Charles Chane849c192016-01-11 18:28:54 -0800318 if (vidCriterion != null) {
Charles Chand55e84d2016-03-30 17:54:24 -0700319 // Use the VLAN in metadata whenever a metadata is provided
320 if (filt.meta() != null) {
321 assignedVlan = readVlanFromTreatment(filt.meta());
322 // Use the VLAN in criterion if metadata is not present and the traffic is tagged
323 } else if (!vidCriterion.vlanId().equals(VlanId.NONE)) {
Charles Chane849c192016-01-11 18:28:54 -0800324 assignedVlan = vidCriterion.vlanId();
Charles Chand55e84d2016-03-30 17:54:24 -0700325 }
Charles Chane849c192016-01-11 18:28:54 -0800326
Charles Chand55e84d2016-03-30 17:54:24 -0700327 if (assignedVlan == null) {
328 log.error("Driver fails to extract VLAN information. "
329 + "Not proccessing VLAN filters on device {}.", deviceId);
330 log.debug("VLAN ID in criterion={}, metadata={}",
331 readVlanFromTreatment(filt.meta()), vidCriterion.vlanId());
332 fail(filt, ObjectiveError.BADPARAMS);
333 return;
Saurav Das0e99e2b2015-10-28 12:39:42 -0700334 }
335 }
336
Charles Chane849c192016-01-11 18:28:54 -0800337 if (ethCriterion == null || ethCriterion.mac().equals(MacAddress.NONE)) {
Charles Chan985b12e2016-05-11 19:47:22 -0700338 // NOTE: it is possible that a filtering objective only has vidCriterion
339 log.debug("filtering objective missing dstMac, cannot program TMAC table");
Saurav Das822c4e22015-10-23 10:51:11 -0700340 } else {
341 for (FlowRule tmacRule : processEthDstFilter(portCriterion, ethCriterion,
Saurav Das0e99e2b2015-10-28 12:39:42 -0700342 vidCriterion, assignedVlan,
343 applicationId)) {
Saurav Das822c4e22015-10-23 10:51:11 -0700344 log.debug("adding MAC filtering rules in TMAC table: {} for dev: {}",
345 tmacRule, deviceId);
346 ops = install ? ops.add(tmacRule) : ops.remove(tmacRule);
347 }
348 }
349
Charles Chan985b12e2016-05-11 19:47:22 -0700350 if (vidCriterion == null) {
351 // NOTE: it is possible that a filtering objective only has ethCriterion
352 log.debug("filtering objective missing dstMac or VLAN, "
Charles Chane849c192016-01-11 18:28:54 -0800353 + "cannot program VLAN Table");
Saurav Das822c4e22015-10-23 10:51:11 -0700354 } else {
Charles Chan14967c22015-12-07 11:11:50 -0800355 /*
356 * NOTE: Separate vlan filtering rules and assignment rules
357 * into different stage in order to guarantee that filtering rules
Saurav Das52025962016-01-28 22:30:01 -0800358 * always go first, as required by ofdpa.
Charles Chan14967c22015-12-07 11:11:50 -0800359 */
360 List<FlowRule> allRules = processVlanIdFilter(
361 portCriterion, vidCriterion, assignedVlan, applicationId);
362 List<FlowRule> filteringRules = new ArrayList<>();
363 List<FlowRule> assignmentRules = new ArrayList<>();
364
365 allRules.forEach(flowRule -> {
366 ExtensionCriterion extCriterion =
367 (ExtensionCriterion) flowRule.selector().getCriterion(Criterion.Type.EXTENSION);
368 VlanId vlanId = ((OfdpaMatchVlanVid) extCriterion.extensionSelector()).vlanId();
Charles Chanbe8aea42016-02-24 12:04:47 -0800369 if (!vlanId.equals(VlanId.NONE)) {
Charles Chan14967c22015-12-07 11:11:50 -0800370 filteringRules.add(flowRule);
371 } else {
372 assignmentRules.add(flowRule);
373 }
374 });
375
376 for (FlowRule filteringRule : filteringRules) {
Saurav Das822c4e22015-10-23 10:51:11 -0700377 log.debug("adding VLAN filtering rule in VLAN table: {} for dev: {}",
Charles Chan14967c22015-12-07 11:11:50 -0800378 filteringRule, deviceId);
379 ops = install ? ops.add(filteringRule) : ops.remove(filteringRule);
380 }
381
382 ops.newStage();
383
384 for (FlowRule assignmentRule : assignmentRules) {
385 log.debug("adding VLAN assignment rule in VLAN table: {} for dev: {}",
386 assignmentRule, deviceId);
387 ops = install ? ops.add(assignmentRule) : ops.remove(assignmentRule);
Saurav Das822c4e22015-10-23 10:51:11 -0700388 }
389 }
390
391 for (IPCriterion ipaddr : ips) {
392 // since we ignore port information for IP rules, and the same (gateway) IP
393 // can be configured on multiple ports, we make sure that we send
394 // only a single rule to the switch.
395 if (!sentIpFilters.contains(ipaddr)) {
396 sentIpFilters.add(ipaddr);
397 log.debug("adding IP filtering rules in ACL table {} for dev: {}",
398 ipaddr, deviceId);
399 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
400 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
401 selector.matchEthType(Ethernet.TYPE_IPV4);
402 selector.matchIPDst(ipaddr.ip());
403 treatment.setOutput(PortNumber.CONTROLLER);
404 FlowRule rule = DefaultFlowRule.builder()
405 .forDevice(deviceId)
406 .withSelector(selector.build())
407 .withTreatment(treatment.build())
408 .withPriority(HIGHEST_PRIORITY)
409 .fromApp(applicationId)
410 .makePermanent()
411 .forTable(ACL_TABLE).build();
412 ops = install ? ops.add(rule) : ops.remove(rule);
413 }
414 }
415
416 // apply filtering flow rules
417 flowRuleService.apply(ops.build(new FlowRuleOperationsContext() {
418 @Override
419 public void onSuccess(FlowRuleOperations ops) {
420 log.info("Applied {} filtering rules in device {}",
421 ops.stages().get(0).size(), deviceId);
422 pass(filt);
423 }
424
425 @Override
426 public void onError(FlowRuleOperations ops) {
427 log.info("Failed to apply all filtering rules in dev {}", deviceId);
428 fail(filt, ObjectiveError.FLOWINSTALLATIONFAILED);
429 }
430 }));
431
432 }
433
434 /**
435 * Allows untagged packets into pipeline by assigning a vlan id.
Saurav Das0e99e2b2015-10-28 12:39:42 -0700436 * Vlan assignment is done by the application.
Saurav Das822c4e22015-10-23 10:51:11 -0700437 * Allows tagged packets into pipeline as per configured port-vlan info.
Saurav Das0e99e2b2015-10-28 12:39:42 -0700438 *
Saurav Das822c4e22015-10-23 10:51:11 -0700439 * @param portCriterion port on device for which this filter is programmed
440 * @param vidCriterion vlan assigned to port, or NONE for untagged
Saurav Das0e99e2b2015-10-28 12:39:42 -0700441 * @param assignedVlan assigned vlan-id for untagged packets
Saurav Das822c4e22015-10-23 10:51:11 -0700442 * @param applicationId for application programming this filter
443 * @return list of FlowRule for port-vlan filters
444 */
445 protected List<FlowRule> processVlanIdFilter(PortCriterion portCriterion,
446 VlanIdCriterion vidCriterion,
Saurav Das0e99e2b2015-10-28 12:39:42 -0700447 VlanId assignedVlan,
Saurav Das822c4e22015-10-23 10:51:11 -0700448 ApplicationId applicationId) {
Charles Chan14967c22015-12-07 11:11:50 -0800449 List<FlowRule> rules = new ArrayList<>();
Saurav Das822c4e22015-10-23 10:51:11 -0700450 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
451 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
Charles Chan14967c22015-12-07 11:11:50 -0800452 TrafficSelector.Builder preSelector = null;
453 TrafficTreatment.Builder preTreatment = null;
454
Saurav Das4f980082015-11-05 13:39:15 -0800455 treatment.transition(TMAC_TABLE);
456
Saurav Das822c4e22015-10-23 10:51:11 -0700457 if (vidCriterion.vlanId() == VlanId.NONE) {
458 // untagged packets are assigned vlans
Charles Chanbe8aea42016-02-24 12:04:47 -0800459 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(VlanId.NONE);
Charles Chan14967c22015-12-07 11:11:50 -0800460 selector.extension(ofdpaMatchVlanVid, deviceId);
461 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(assignedVlan);
462 treatment.extension(ofdpaSetVlanVid, deviceId);
Charles Chan14967c22015-12-07 11:11:50 -0800463
464 preSelector = DefaultTrafficSelector.builder();
465 OfdpaMatchVlanVid preOfdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
466 preSelector.extension(preOfdpaMatchVlanVid, deviceId);
467 preTreatment = DefaultTrafficTreatment.builder().transition(TMAC_TABLE);
468
Saurav Das4f980082015-11-05 13:39:15 -0800469 } else {
Charles Chan14967c22015-12-07 11:11:50 -0800470 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vidCriterion.vlanId());
471 selector.extension(ofdpaMatchVlanVid, deviceId);
Charles Chand55e84d2016-03-30 17:54:24 -0700472
473 if (!assignedVlan.equals(vidCriterion.vlanId())) {
474 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(assignedVlan);
475 treatment.extension(ofdpaSetVlanVid, deviceId);
476 }
Saurav Das822c4e22015-10-23 10:51:11 -0700477 }
Saurav Das822c4e22015-10-23 10:51:11 -0700478
479 // ofdpa cannot match on ALL portnumber, so we need to use separate
480 // rules for each port.
Charles Chan14967c22015-12-07 11:11:50 -0800481 List<PortNumber> portnums = new ArrayList<>();
Saurav Das822c4e22015-10-23 10:51:11 -0700482 if (portCriterion.port() == PortNumber.ALL) {
483 for (Port port : deviceService.getPorts(deviceId)) {
484 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
485 portnums.add(port.number());
486 }
487 }
488 } else {
489 portnums.add(portCriterion.port());
490 }
Saurav Das4f980082015-11-05 13:39:15 -0800491
Saurav Das822c4e22015-10-23 10:51:11 -0700492 for (PortNumber pnum : portnums) {
Saurav Das4f980082015-11-05 13:39:15 -0800493 // create rest of flowrule
Saurav Das822c4e22015-10-23 10:51:11 -0700494 selector.matchInPort(pnum);
495 FlowRule rule = DefaultFlowRule.builder()
496 .forDevice(deviceId)
497 .withSelector(selector.build())
498 .withTreatment(treatment.build())
499 .withPriority(DEFAULT_PRIORITY)
500 .fromApp(applicationId)
501 .makePermanent()
502 .forTable(VLAN_TABLE).build();
Charles Chan14967c22015-12-07 11:11:50 -0800503
504 if (preSelector != null) {
505 preSelector.matchInPort(pnum);
506 FlowRule preRule = DefaultFlowRule.builder()
507 .forDevice(deviceId)
508 .withSelector(preSelector.build())
509 .withTreatment(preTreatment.build())
510 .withPriority(DEFAULT_PRIORITY)
511 .fromApp(applicationId)
512 .makePermanent()
513 .forTable(VLAN_TABLE).build();
514 rules.add(preRule);
515 }
516
Saurav Das822c4e22015-10-23 10:51:11 -0700517 rules.add(rule);
518 }
519 return rules;
520 }
521
522 /**
523 * Allows routed packets with correct destination MAC to be directed
524 * to unicast-IP routing table or MPLS forwarding table.
Saurav Das822c4e22015-10-23 10:51:11 -0700525 *
526 * @param portCriterion port on device for which this filter is programmed
527 * @param ethCriterion dstMac of device for which is filter is programmed
528 * @param vidCriterion vlan assigned to port, or NONE for untagged
Saurav Das0e99e2b2015-10-28 12:39:42 -0700529 * @param assignedVlan assigned vlan-id for untagged packets
Saurav Das822c4e22015-10-23 10:51:11 -0700530 * @param applicationId for application programming this filter
531 * @return list of FlowRule for port-vlan filters
532
533 */
534 protected List<FlowRule> processEthDstFilter(PortCriterion portCriterion,
535 EthCriterion ethCriterion,
536 VlanIdCriterion vidCriterion,
Saurav Das0e99e2b2015-10-28 12:39:42 -0700537 VlanId assignedVlan,
Saurav Das822c4e22015-10-23 10:51:11 -0700538 ApplicationId applicationId) {
Charles Chan5270ed02016-01-30 23:22:37 -0800539 // Consider PortNumber.ANY as wildcard. Match ETH_DST only
540 if (portCriterion != null && portCriterion.port() == PortNumber.ANY) {
541 return processEthDstOnlyFilter(ethCriterion, applicationId);
542 }
543
Charles Chan5b9df8d2016-03-28 22:21:40 -0700544 // Multicast MAC
545 if (ethCriterion.mask() != null) {
546 return processMcastEthDstFilter(ethCriterion, applicationId);
547 }
548
Saurav Das822c4e22015-10-23 10:51:11 -0700549 //handling untagged packets via assigned VLAN
550 if (vidCriterion.vlanId() == VlanId.NONE) {
Saurav Das0e99e2b2015-10-28 12:39:42 -0700551 vidCriterion = (VlanIdCriterion) Criteria.matchVlanId(assignedVlan);
Saurav Das822c4e22015-10-23 10:51:11 -0700552 }
553 // ofdpa cannot match on ALL portnumber, so we need to use separate
554 // rules for each port.
555 List<PortNumber> portnums = new ArrayList<PortNumber>();
556 if (portCriterion.port() == PortNumber.ALL) {
557 for (Port port : deviceService.getPorts(deviceId)) {
558 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
559 portnums.add(port.number());
560 }
561 }
562 } else {
563 portnums.add(portCriterion.port());
564 }
565
566 List<FlowRule> rules = new ArrayList<FlowRule>();
567 for (PortNumber pnum : portnums) {
Charles Chan14967c22015-12-07 11:11:50 -0800568 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vidCriterion.vlanId());
Saurav Das822c4e22015-10-23 10:51:11 -0700569 // for unicast IP packets
570 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
571 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
572 selector.matchInPort(pnum);
Charles Chan14967c22015-12-07 11:11:50 -0800573 selector.extension(ofdpaMatchVlanVid, deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700574 selector.matchEthType(Ethernet.TYPE_IPV4);
575 selector.matchEthDst(ethCriterion.mac());
576 treatment.transition(UNICAST_ROUTING_TABLE);
577 FlowRule rule = DefaultFlowRule.builder()
578 .forDevice(deviceId)
579 .withSelector(selector.build())
580 .withTreatment(treatment.build())
581 .withPriority(DEFAULT_PRIORITY)
582 .fromApp(applicationId)
583 .makePermanent()
584 .forTable(TMAC_TABLE).build();
585 rules.add(rule);
586 //for MPLS packets
587 selector = DefaultTrafficSelector.builder();
588 treatment = DefaultTrafficTreatment.builder();
589 selector.matchInPort(pnum);
Charles Chan14967c22015-12-07 11:11:50 -0800590 selector.extension(ofdpaMatchVlanVid, deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700591 selector.matchEthType(Ethernet.MPLS_UNICAST);
592 selector.matchEthDst(ethCriterion.mac());
593 treatment.transition(MPLS_TABLE_0);
594 rule = DefaultFlowRule.builder()
595 .forDevice(deviceId)
596 .withSelector(selector.build())
597 .withTreatment(treatment.build())
598 .withPriority(DEFAULT_PRIORITY)
599 .fromApp(applicationId)
600 .makePermanent()
601 .forTable(TMAC_TABLE).build();
602 rules.add(rule);
603 }
604 return rules;
605 }
606
Charles Chan5270ed02016-01-30 23:22:37 -0800607 protected List<FlowRule> processEthDstOnlyFilter(EthCriterion ethCriterion,
608 ApplicationId applicationId) {
609 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
610 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
611 selector.matchEthType(Ethernet.TYPE_IPV4);
612 selector.matchEthDst(ethCriterion.mac());
613 treatment.transition(UNICAST_ROUTING_TABLE);
614 FlowRule rule = DefaultFlowRule.builder()
615 .forDevice(deviceId)
616 .withSelector(selector.build())
617 .withTreatment(treatment.build())
618 .withPriority(DEFAULT_PRIORITY)
619 .fromApp(applicationId)
620 .makePermanent()
621 .forTable(TMAC_TABLE).build();
622 return ImmutableList.<FlowRule>builder().add(rule).build();
623 }
624
Charles Chan5b9df8d2016-03-28 22:21:40 -0700625 protected List<FlowRule> processMcastEthDstFilter(EthCriterion ethCriterion,
626 ApplicationId applicationId) {
627 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
628 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
629 selector.matchEthType(Ethernet.TYPE_IPV4);
630 selector.matchEthDstMasked(ethCriterion.mac(), ethCriterion.mask());
631 treatment.transition(MULTICAST_ROUTING_TABLE);
632 FlowRule rule = DefaultFlowRule.builder()
633 .forDevice(deviceId)
634 .withSelector(selector.build())
635 .withTreatment(treatment.build())
636 .withPriority(DEFAULT_PRIORITY)
637 .fromApp(applicationId)
638 .makePermanent()
639 .forTable(TMAC_TABLE).build();
640 return ImmutableList.<FlowRule>builder().add(rule).build();
641 }
642
Saurav Das822c4e22015-10-23 10:51:11 -0700643 private Collection<FlowRule> processForward(ForwardingObjective fwd) {
644 switch (fwd.flag()) {
645 case SPECIFIC:
646 return processSpecific(fwd);
647 case VERSATILE:
648 return processVersatile(fwd);
649 default:
650 fail(fwd, ObjectiveError.UNKNOWN);
651 log.warn("Unknown forwarding flag {}", fwd.flag());
652 }
653 return Collections.emptySet();
654 }
655
656 /**
657 * In the OF-DPA 2.0 pipeline, versatile forwarding objectives go to the
658 * ACL table.
659 * @param fwd the forwarding objective of type 'versatile'
660 * @return a collection of flow rules to be sent to the switch. An empty
661 * collection may be returned if there is a problem in processing
662 * the flow rule
663 */
Saurav Das52025962016-01-28 22:30:01 -0800664 protected Collection<FlowRule> processVersatile(ForwardingObjective fwd) {
Saurav Das822c4e22015-10-23 10:51:11 -0700665 log.info("Processing versatile forwarding objective");
Saurav Das822c4e22015-10-23 10:51:11 -0700666
667 EthTypeCriterion ethType =
Saurav Das77b5e902016-01-27 17:01:59 -0800668 (EthTypeCriterion) fwd.selector().getCriterion(Criterion.Type.ETH_TYPE);
Saurav Das822c4e22015-10-23 10:51:11 -0700669 if (ethType == null) {
670 log.error("Versatile forwarding objective must include ethType");
671 fail(fwd, ObjectiveError.BADPARAMS);
672 return Collections.emptySet();
673 }
674 if (fwd.nextId() == null && fwd.treatment() == null) {
675 log.error("Forwarding objective {} from {} must contain "
676 + "nextId or Treatment", fwd.selector(), fwd.appId());
Zsolt Haraszti9faab752016-02-17 15:55:20 -0800677 fail(fwd, ObjectiveError.BADPARAMS);
Saurav Das822c4e22015-10-23 10:51:11 -0700678 return Collections.emptySet();
679 }
Saurav Das49cb5a12016-01-16 22:54:07 -0800680
Saurav Das77b5e902016-01-27 17:01:59 -0800681 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
682 fwd.selector().criteria().forEach(criterion -> {
683 if (criterion instanceof VlanIdCriterion) {
684 VlanId vlanId = ((VlanIdCriterion) criterion).vlanId();
685 // ensure that match does not include vlan = NONE as OF-DPA does not
686 // match untagged packets this way in the ACL table.
687 if (vlanId.equals(VlanId.NONE)) {
688 return;
689 }
690 OfdpaMatchVlanVid ofdpaMatchVlanVid =
691 new OfdpaMatchVlanVid(vlanId);
692 sbuilder.extension(ofdpaMatchVlanVid, deviceId);
693 } else {
694 sbuilder.add(criterion);
695 }
696 });
697
Saurav Das822c4e22015-10-23 10:51:11 -0700698 // XXX driver does not currently do type checking as per Tables 65-67 in
699 // OFDPA 2.0 spec. The only allowed treatment is a punt to the controller.
Saurav Das49cb5a12016-01-16 22:54:07 -0800700 TrafficTreatment.Builder ttBuilder = DefaultTrafficTreatment.builder();
701 if (fwd.treatment() != null) {
702 for (Instruction ins : fwd.treatment().allInstructions()) {
703 if (ins instanceof OutputInstruction) {
704 OutputInstruction o = (OutputInstruction) ins;
705 if (o.port() == PortNumber.CONTROLLER) {
706 ttBuilder.add(o);
707 } else {
708 log.warn("Only allowed treatments in versatile forwarding "
709 + "objectives are punts to the controller");
710 }
711 } else {
712 log.warn("Cannot process instruction in versatile fwd {}", ins);
713 }
Saurav Das822c4e22015-10-23 10:51:11 -0700714 }
715 }
Saurav Das822c4e22015-10-23 10:51:11 -0700716 if (fwd.nextId() != null) {
Saurav Das49cb5a12016-01-16 22:54:07 -0800717 // overide case
718 NextGroup next = getGroupForNextObjective(fwd.nextId());
719 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
720 // we only need the top level group's key to point the flow to it
721 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
722 if (group == null) {
723 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
724 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
725 fail(fwd, ObjectiveError.GROUPMISSING);
726 return Collections.emptySet();
727 }
728 ttBuilder.deferred().group(group.id());
Saurav Das822c4e22015-10-23 10:51:11 -0700729 }
Saurav Das49cb5a12016-01-16 22:54:07 -0800730
731 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
732 .fromApp(fwd.appId())
733 .withPriority(fwd.priority())
734 .forDevice(deviceId)
Saurav Das77b5e902016-01-27 17:01:59 -0800735 .withSelector(sbuilder.build())
Saurav Das49cb5a12016-01-16 22:54:07 -0800736 .withTreatment(ttBuilder.build())
737 .makePermanent()
738 .forTable(ACL_TABLE);
739 return Collections.singletonList(ruleBuilder.build());
Saurav Das822c4e22015-10-23 10:51:11 -0700740 }
741
742 /**
743 * In the OF-DPA 2.0 pipeline, specific forwarding refers to the IP table
Saurav Das8a0732e2015-11-20 15:27:53 -0800744 * (unicast or multicast) or the L2 table (mac + vlan) or the MPLS table.
Saurav Das822c4e22015-10-23 10:51:11 -0700745 *
746 * @param fwd the forwarding objective of type 'specific'
747 * @return a collection of flow rules. Typically there will be only one
748 * for this type of forwarding objective. An empty set may be
749 * returned if there is an issue in processing the objective.
750 */
Saurav Das8a0732e2015-11-20 15:27:53 -0800751 protected Collection<FlowRule> processSpecific(ForwardingObjective fwd) {
Saurav Das4ce45962015-11-24 23:21:05 -0800752 log.trace("Processing specific fwd objective:{} in dev:{} with next:{}",
753 fwd.id(), deviceId, fwd.nextId());
754 boolean isEthTypeObj = isSupportedEthTypeObjective(fwd);
755 boolean isEthDstObj = isSupportedEthDstObjective(fwd);
756
757 if (isEthTypeObj) {
758 return processEthTypeSpecific(fwd);
759 } else if (isEthDstObj) {
760 return processEthDstSpecific(fwd);
761 } else {
762 log.warn("processSpecific: Unsupported forwarding objective "
763 + "criteria fwd:{} in dev:{}", fwd.nextId(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700764 fail(fwd, ObjectiveError.UNSUPPORTED);
765 return Collections.emptySet();
766 }
Saurav Das4ce45962015-11-24 23:21:05 -0800767 }
768
769 private boolean isSupportedEthTypeObjective(ForwardingObjective fwd) {
770 TrafficSelector selector = fwd.selector();
771 EthTypeCriterion ethType = (EthTypeCriterion) selector
772 .getCriterion(Criterion.Type.ETH_TYPE);
Charles Chan188ebf52015-12-23 00:15:11 -0800773 return !((ethType == null) ||
Saurav Das4ce45962015-11-24 23:21:05 -0800774 ((ethType.ethType().toShort() != Ethernet.TYPE_IPV4) &&
Charles Chan188ebf52015-12-23 00:15:11 -0800775 (ethType.ethType().toShort() != Ethernet.MPLS_UNICAST)));
Saurav Das4ce45962015-11-24 23:21:05 -0800776 }
777
778 private boolean isSupportedEthDstObjective(ForwardingObjective fwd) {
779 TrafficSelector selector = fwd.selector();
780 EthCriterion ethDst = (EthCriterion) selector
781 .getCriterion(Criterion.Type.ETH_DST);
782 VlanIdCriterion vlanId = (VlanIdCriterion) selector
783 .getCriterion(Criterion.Type.VLAN_VID);
Charles Chan188ebf52015-12-23 00:15:11 -0800784 return !(ethDst == null && vlanId == null);
Saurav Das4ce45962015-11-24 23:21:05 -0800785 }
786
787 /**
788 * Handles forwarding rules to the IP and MPLS tables.
789 *
790 * @param fwd the forwarding objective
791 * @return A collection of flow rules, or an empty set
792 */
793 protected Collection<FlowRule> processEthTypeSpecific(ForwardingObjective fwd) {
794 TrafficSelector selector = fwd.selector();
795 EthTypeCriterion ethType =
796 (EthTypeCriterion) selector.getCriterion(Criterion.Type.ETH_TYPE);
Flavio Castroe10fa242016-01-15 12:43:51 -0800797 boolean defaultRule = false;
798 boolean popMpls = false;
Charles Chan188ebf52015-12-23 00:15:11 -0800799 int forTableId;
Saurav Das8a0732e2015-11-20 15:27:53 -0800800 TrafficSelector.Builder filteredSelector = DefaultTrafficSelector.builder();
Charles Chan14967c22015-12-07 11:11:50 -0800801 TrafficTreatment.Builder tb = DefaultTrafficTreatment.builder();
Flavio Castroe10fa242016-01-15 12:43:51 -0800802 TrafficSelector.Builder complementarySelector = DefaultTrafficSelector.builder();
Charles Chan14967c22015-12-07 11:11:50 -0800803
Flavio Castroe10fa242016-01-15 12:43:51 -0800804 /*
805 * NOTE: The switch does not support matching 0.0.0.0/0.
806 * Split it into 0.0.0.0/1 and 128.0.0.0/1
807 */
Saurav Das8a0732e2015-11-20 15:27:53 -0800808 if (ethType.ethType().toShort() == Ethernet.TYPE_IPV4) {
Flavio Castroe10fa242016-01-15 12:43:51 -0800809 IpPrefix ipv4Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV4_DST)).ip();
Charles Chan5b9df8d2016-03-28 22:21:40 -0700810 if (ipv4Dst.isMulticast()) {
811 if (ipv4Dst.prefixLength() != 32) {
812 log.warn("Multicast specific forwarding objective can only be /32");
813 fail(fwd, ObjectiveError.BADPARAMS);
814 return ImmutableSet.of();
815 }
816 VlanId assignedVlan = readVlanFromSelector(fwd.meta());
817 if (assignedVlan == null) {
818 log.warn("VLAN ID required by multicast specific fwd obj is missing. Abort.");
819 fail(fwd, ObjectiveError.BADPARAMS);
820 return ImmutableSet.of();
821 }
822 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
823 filteredSelector.extension(ofdpaMatchVlanVid, deviceId);
824 filteredSelector.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipv4Dst);
825 forTableId = MULTICAST_ROUTING_TABLE;
826 log.debug("processing IPv4 multicast specific forwarding objective {} -> next:{}"
827 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
Flavio Castroe10fa242016-01-15 12:43:51 -0800828 } else {
Charles Chan5b9df8d2016-03-28 22:21:40 -0700829 if (ipv4Dst.prefixLength() > 0) {
830 filteredSelector.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipv4Dst);
831 } else {
832 filteredSelector.matchEthType(Ethernet.TYPE_IPV4)
833 .matchIPDst(IpPrefix.valueOf("0.0.0.0/1"));
834 complementarySelector.matchEthType(Ethernet.TYPE_IPV4)
835 .matchIPDst(IpPrefix.valueOf("128.0.0.0/1"));
836 defaultRule = true;
837 }
838 forTableId = UNICAST_ROUTING_TABLE;
839 log.debug("processing IPv4 unicast specific forwarding objective {} -> next:{}"
840 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
Flavio Castroe10fa242016-01-15 12:43:51 -0800841 }
Charles Chan14967c22015-12-07 11:11:50 -0800842
843 if (fwd.treatment() != null) {
844 for (Instruction instr : fwd.treatment().allInstructions()) {
845 if (instr instanceof L3ModificationInstruction &&
846 ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
847 tb.deferred().add(instr);
848 }
849 }
850 }
851
Saurav Das8a0732e2015-11-20 15:27:53 -0800852 } else {
853 filteredSelector
854 .matchEthType(Ethernet.MPLS_UNICAST)
855 .matchMplsLabel(((MplsCriterion)
856 selector.getCriterion(Criterion.Type.MPLS_LABEL)).label());
857 MplsBosCriterion bos = (MplsBosCriterion) selector
858 .getCriterion(Criterion.Type.MPLS_BOS);
859 if (bos != null) {
860 filteredSelector.matchMplsBos(bos.mplsBos());
861 }
862 forTableId = MPLS_TABLE_1;
Saurav Das4ce45962015-11-24 23:21:05 -0800863 log.debug("processing MPLS specific forwarding objective {} -> next:{}"
864 + " in dev {}", fwd.id(), fwd.nextId(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700865
Charles Chan14967c22015-12-07 11:11:50 -0800866 if (fwd.treatment() != null) {
867 for (Instruction instr : fwd.treatment().allInstructions()) {
868 if (instr instanceof L2ModificationInstruction &&
869 ((L2ModificationInstruction) instr).subtype() == L2SubType.MPLS_POP) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800870 popMpls = true;
Charles Chan14967c22015-12-07 11:11:50 -0800871 tb.immediate().add(instr);
872 }
873 if (instr instanceof L3ModificationInstruction &&
874 ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
875 // FIXME Should modify the app to send the correct DEC_MPLS_TTL instruction
876 tb.immediate().decMplsTtl();
877 }
878 if (instr instanceof L3ModificationInstruction &&
879 ((L3ModificationInstruction) instr).subtype() == L3SubType.TTL_IN) {
880 tb.immediate().add(instr);
881 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800882 }
883 }
884 }
Saurav Das822c4e22015-10-23 10:51:11 -0700885
886 if (fwd.nextId() != null) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800887 if (forTableId == MPLS_TABLE_1 && !popMpls) {
888 log.warn("SR CONTINUE case cannot be handled as MPLS ECMP "
889 + "is not implemented in OF-DPA yet. Aborting this flow "
890 + "in this device {}", deviceId);
891 // XXX We could convert to forwarding to a single-port, via a
892 // MPLS interface, or a MPLS SWAP (with-same) but that would
893 // have to be handled in the next-objective. Also the pop-mpls
894 // logic used here won't work in non-BoS case.
Saurav Das4ce45962015-11-24 23:21:05 -0800895 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
Saurav Das8a0732e2015-11-20 15:27:53 -0800896 return Collections.emptySet();
897 }
898
Saurav Das423fe2b2015-12-04 10:52:59 -0800899 NextGroup next = getGroupForNextObjective(fwd.nextId());
900 if (next != null) {
901 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
902 // we only need the top level group's key to point the flow to it
903 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
904 if (group == null) {
905 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
906 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
907 fail(fwd, ObjectiveError.GROUPMISSING);
908 return Collections.emptySet();
909 }
910 tb.deferred().group(group.id());
Saurav Das822c4e22015-10-23 10:51:11 -0700911 }
Saurav Das822c4e22015-10-23 10:51:11 -0700912 }
913 tb.transition(ACL_TABLE);
914 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
915 .fromApp(fwd.appId())
916 .withPriority(fwd.priority())
917 .forDevice(deviceId)
Saurav Das8a0732e2015-11-20 15:27:53 -0800918 .withSelector(filteredSelector.build())
919 .withTreatment(tb.build())
920 .forTable(forTableId);
Saurav Das822c4e22015-10-23 10:51:11 -0700921
922 if (fwd.permanent()) {
923 ruleBuilder.makePermanent();
924 } else {
925 ruleBuilder.makeTemporary(fwd.timeout());
926 }
Flavio Castroe10fa242016-01-15 12:43:51 -0800927 Collection<FlowRule> flowRuleCollection = new ArrayList<>();
928 flowRuleCollection.add(ruleBuilder.build());
929 if (defaultRule) {
930 FlowRule.Builder rule = DefaultFlowRule.builder()
931 .fromApp(fwd.appId())
932 .withPriority(fwd.priority())
933 .forDevice(deviceId)
934 .withSelector(complementarySelector.build())
935 .withTreatment(tb.build())
936 .forTable(forTableId);
937 if (fwd.permanent()) {
938 rule.makePermanent();
939 } else {
940 rule.makeTemporary(fwd.timeout());
941 }
942 flowRuleCollection.add(rule.build());
943 log.debug("Default rule 0.0.0.0/0 is being installed two rules");
944 }
Saurav Das822c4e22015-10-23 10:51:11 -0700945
Flavio Castroe10fa242016-01-15 12:43:51 -0800946 return flowRuleCollection;
Saurav Das822c4e22015-10-23 10:51:11 -0700947 }
948
Saurav Das4ce45962015-11-24 23:21:05 -0800949 /**
950 * Handles forwarding rules to the L2 bridging table. Flow actions are not
951 * allowed in the bridging table - instead we use L2 Interface group or
952 * L2 flood group
953 *
954 * @param fwd the forwarding objective
955 * @return A collection of flow rules, or an empty set
956 */
957 protected Collection<FlowRule> processEthDstSpecific(ForwardingObjective fwd) {
958 List<FlowRule> rules = new ArrayList<>();
959
960 // Build filtered selector
961 TrafficSelector selector = fwd.selector();
962 EthCriterion ethCriterion = (EthCriterion) selector
963 .getCriterion(Criterion.Type.ETH_DST);
964 VlanIdCriterion vlanIdCriterion = (VlanIdCriterion) selector
965 .getCriterion(Criterion.Type.VLAN_VID);
966
967 if (vlanIdCriterion == null) {
968 log.warn("Forwarding objective for bridging requires vlan. Not "
969 + "installing fwd:{} in dev:{}", fwd.id(), deviceId);
970 fail(fwd, ObjectiveError.BADPARAMS);
971 return Collections.emptySet();
972 }
973
974 TrafficSelector.Builder filteredSelectorBuilder =
975 DefaultTrafficSelector.builder();
976 // Do not match MacAddress for subnet broadcast entry
977 if (!ethCriterion.mac().equals(MacAddress.NONE)) {
978 filteredSelectorBuilder.matchEthDst(ethCriterion.mac());
979 log.debug("processing L2 forwarding objective:{} -> next:{} in dev:{}",
980 fwd.id(), fwd.nextId(), deviceId);
981 } else {
982 log.debug("processing L2 Broadcast forwarding objective:{} -> next:{} "
983 + "in dev:{} for vlan:{}",
984 fwd.id(), fwd.nextId(), deviceId, vlanIdCriterion.vlanId());
985 }
Charles Chan14967c22015-12-07 11:11:50 -0800986 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vlanIdCriterion.vlanId());
987 filteredSelectorBuilder.extension(ofdpaMatchVlanVid, deviceId);
Saurav Das4ce45962015-11-24 23:21:05 -0800988 TrafficSelector filteredSelector = filteredSelectorBuilder.build();
989
990 if (fwd.treatment() != null) {
991 log.warn("Ignoring traffic treatment in fwd rule {} meant for L2 table"
992 + "for dev:{}. Expecting only nextId", fwd.id(), deviceId);
993 }
994
995 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
996 if (fwd.nextId() != null) {
Saurav Das423fe2b2015-12-04 10:52:59 -0800997 NextGroup next = getGroupForNextObjective(fwd.nextId());
Saurav Das4ce45962015-11-24 23:21:05 -0800998 if (next != null) {
999 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1000 // we only need the top level group's key to point the flow to it
1001 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
1002 if (group != null) {
1003 treatmentBuilder.deferred().group(group.id());
1004 } else {
1005 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
1006 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
1007 fail(fwd, ObjectiveError.GROUPMISSING);
1008 return Collections.emptySet();
1009 }
1010 }
1011 }
1012 treatmentBuilder.immediate().transition(ACL_TABLE);
1013 TrafficTreatment filteredTreatment = treatmentBuilder.build();
1014
1015 // Build bridging table entries
1016 FlowRule.Builder flowRuleBuilder = DefaultFlowRule.builder();
1017 flowRuleBuilder.fromApp(fwd.appId())
1018 .withPriority(fwd.priority())
1019 .forDevice(deviceId)
1020 .withSelector(filteredSelector)
1021 .withTreatment(filteredTreatment)
1022 .forTable(BRIDGING_TABLE);
1023 if (fwd.permanent()) {
1024 flowRuleBuilder.makePermanent();
1025 } else {
1026 flowRuleBuilder.makeTemporary(fwd.timeout());
1027 }
1028 rules.add(flowRuleBuilder.build());
1029 return rules;
1030 }
1031
Saurav Das423fe2b2015-12-04 10:52:59 -08001032 protected NextGroup getGroupForNextObjective(Integer nextId) {
1033 NextGroup next = flowObjectiveStore.getNextGroup(nextId);
1034 if (next != null) {
1035 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1036 if (gkeys != null && !gkeys.isEmpty()) {
1037 return next;
1038 } else {
1039 log.warn("Empty next group found in FlowObjective store for "
1040 + "next-id:{} in dev:{}", nextId, deviceId);
1041 }
1042 } else {
1043 log.warn("next-id {} not found in Flow objective store for dev:{}",
1044 nextId, deviceId);
1045 }
1046 return null;
1047 }
1048
Charles Chan188ebf52015-12-23 00:15:11 -08001049 protected static void pass(Objective obj) {
Sho SHIMIZUef7e2902016-02-12 18:38:29 -08001050 obj.context().ifPresent(context -> context.onSuccess(obj));
Saurav Das822c4e22015-10-23 10:51:11 -07001051 }
1052
Charles Chan188ebf52015-12-23 00:15:11 -08001053 protected static void fail(Objective obj, ObjectiveError error) {
Sho SHIMIZUef7e2902016-02-12 18:38:29 -08001054 obj.context().ifPresent(context -> context.onError(obj, error));
Saurav Das822c4e22015-10-23 10:51:11 -07001055 }
Saurav Das24431192016-03-07 19:13:00 -08001056
Saurav Das24431192016-03-07 19:13:00 -08001057 @Override
1058 public List<String> getNextMappings(NextGroup nextGroup) {
1059 List<String> mappings = new ArrayList<>();
1060 List<Deque<GroupKey>> gkeys = appKryo.deserialize(nextGroup.data());
1061 for (Deque<GroupKey> gkd : gkeys) {
1062 Group lastGroup = null;
Saurav Das8be4e3a2016-03-11 17:19:07 -08001063 StringBuffer gchain = new StringBuffer();
Saurav Das24431192016-03-07 19:13:00 -08001064 for (GroupKey gk : gkd) {
1065 Group g = groupService.getGroup(deviceId, gk);
Saurav Das8be4e3a2016-03-11 17:19:07 -08001066 if (g == null) {
1067 gchain.append(" ERROR").append(" -->");
1068 continue;
1069 }
1070 gchain.append(" 0x").append(Integer.toHexString(g.id().id()))
1071 .append(" -->");
Saurav Das24431192016-03-07 19:13:00 -08001072 lastGroup = g;
1073 }
1074 // add port information for last group in group-chain
1075 for (Instruction i: lastGroup.buckets().buckets().get(0).treatment().allInstructions()) {
1076 if (i instanceof OutputInstruction) {
Saurav Das8be4e3a2016-03-11 17:19:07 -08001077 gchain.append(" port:").append(((OutputInstruction) i).port());
Saurav Das24431192016-03-07 19:13:00 -08001078 }
1079 }
Saurav Das8be4e3a2016-03-11 17:19:07 -08001080 mappings.add(gchain.toString());
Saurav Das24431192016-03-07 19:13:00 -08001081 }
1082 return mappings;
1083 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001084
1085 protected static VlanId readVlanFromSelector(TrafficSelector selector) {
Saurav Das59232cf2016-04-27 18:35:50 -07001086 if (selector == null) {
1087 return null;
1088 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001089 Criterion criterion = selector.getCriterion(Criterion.Type.VLAN_VID);
1090 return (criterion == null)
1091 ? null : ((VlanIdCriterion) criterion).vlanId();
1092 }
1093
1094 protected static IpPrefix readIpDstFromSelector(TrafficSelector selector) {
Saurav Das59232cf2016-04-27 18:35:50 -07001095 if (selector == null) {
1096 return null;
1097 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001098 Criterion criterion = selector.getCriterion(Criterion.Type.IPV4_DST);
1099 return (criterion == null) ? null : ((IPCriterion) criterion).ip();
1100 }
Charles Chand55e84d2016-03-30 17:54:24 -07001101
1102 private static VlanId readVlanFromTreatment(TrafficTreatment treatment) {
Saurav Das59232cf2016-04-27 18:35:50 -07001103 if (treatment == null) {
1104 return null;
1105 }
Charles Chand55e84d2016-03-30 17:54:24 -07001106 for (Instruction i : treatment.allInstructions()) {
1107 if (i instanceof ModVlanIdInstruction) {
1108 return ((ModVlanIdInstruction) i).vlanId();
1109 }
1110 }
1111 return null;
1112 }
Saurav Das822c4e22015-10-23 10:51:11 -07001113}