blob: ec61771baa5279ecfdb99539e924381421ff53ce [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)) {
Saurav Das59232cf2016-04-27 18:35:50 -0700338 log.warn("filtering objective missing dstMac, cannot program TMAC table");
339 fail(filt, ObjectiveError.BADPARAMS);
340 return;
Saurav Das822c4e22015-10-23 10:51:11 -0700341 } else {
342 for (FlowRule tmacRule : processEthDstFilter(portCriterion, ethCriterion,
Saurav Das0e99e2b2015-10-28 12:39:42 -0700343 vidCriterion, assignedVlan,
344 applicationId)) {
Saurav Das822c4e22015-10-23 10:51:11 -0700345 log.debug("adding MAC filtering rules in TMAC table: {} for dev: {}",
346 tmacRule, deviceId);
347 ops = install ? ops.add(tmacRule) : ops.remove(tmacRule);
348 }
349 }
350
351 if (ethCriterion == null || vidCriterion == null) {
Saurav Das59232cf2016-04-27 18:35:50 -0700352 log.warn("filtering objective missing dstMac or VLAN, "
Charles Chane849c192016-01-11 18:28:54 -0800353 + "cannot program VLAN Table");
Saurav Das59232cf2016-04-27 18:35:50 -0700354 fail(filt, ObjectiveError.BADPARAMS);
355 return;
Saurav Das822c4e22015-10-23 10:51:11 -0700356 } else {
Charles Chan14967c22015-12-07 11:11:50 -0800357 /*
358 * NOTE: Separate vlan filtering rules and assignment rules
359 * into different stage in order to guarantee that filtering rules
Saurav Das52025962016-01-28 22:30:01 -0800360 * always go first, as required by ofdpa.
Charles Chan14967c22015-12-07 11:11:50 -0800361 */
362 List<FlowRule> allRules = processVlanIdFilter(
363 portCriterion, vidCriterion, assignedVlan, applicationId);
364 List<FlowRule> filteringRules = new ArrayList<>();
365 List<FlowRule> assignmentRules = new ArrayList<>();
366
367 allRules.forEach(flowRule -> {
368 ExtensionCriterion extCriterion =
369 (ExtensionCriterion) flowRule.selector().getCriterion(Criterion.Type.EXTENSION);
370 VlanId vlanId = ((OfdpaMatchVlanVid) extCriterion.extensionSelector()).vlanId();
Charles Chanbe8aea42016-02-24 12:04:47 -0800371 if (!vlanId.equals(VlanId.NONE)) {
Charles Chan14967c22015-12-07 11:11:50 -0800372 filteringRules.add(flowRule);
373 } else {
374 assignmentRules.add(flowRule);
375 }
376 });
377
378 for (FlowRule filteringRule : filteringRules) {
Saurav Das822c4e22015-10-23 10:51:11 -0700379 log.debug("adding VLAN filtering rule in VLAN table: {} for dev: {}",
Charles Chan14967c22015-12-07 11:11:50 -0800380 filteringRule, deviceId);
381 ops = install ? ops.add(filteringRule) : ops.remove(filteringRule);
382 }
383
384 ops.newStage();
385
386 for (FlowRule assignmentRule : assignmentRules) {
387 log.debug("adding VLAN assignment rule in VLAN table: {} for dev: {}",
388 assignmentRule, deviceId);
389 ops = install ? ops.add(assignmentRule) : ops.remove(assignmentRule);
Saurav Das822c4e22015-10-23 10:51:11 -0700390 }
391 }
392
393 for (IPCriterion ipaddr : ips) {
394 // since we ignore port information for IP rules, and the same (gateway) IP
395 // can be configured on multiple ports, we make sure that we send
396 // only a single rule to the switch.
397 if (!sentIpFilters.contains(ipaddr)) {
398 sentIpFilters.add(ipaddr);
399 log.debug("adding IP filtering rules in ACL table {} for dev: {}",
400 ipaddr, deviceId);
401 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
402 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
403 selector.matchEthType(Ethernet.TYPE_IPV4);
404 selector.matchIPDst(ipaddr.ip());
405 treatment.setOutput(PortNumber.CONTROLLER);
406 FlowRule rule = DefaultFlowRule.builder()
407 .forDevice(deviceId)
408 .withSelector(selector.build())
409 .withTreatment(treatment.build())
410 .withPriority(HIGHEST_PRIORITY)
411 .fromApp(applicationId)
412 .makePermanent()
413 .forTable(ACL_TABLE).build();
414 ops = install ? ops.add(rule) : ops.remove(rule);
415 }
416 }
417
418 // apply filtering flow rules
419 flowRuleService.apply(ops.build(new FlowRuleOperationsContext() {
420 @Override
421 public void onSuccess(FlowRuleOperations ops) {
422 log.info("Applied {} filtering rules in device {}",
423 ops.stages().get(0).size(), deviceId);
424 pass(filt);
425 }
426
427 @Override
428 public void onError(FlowRuleOperations ops) {
429 log.info("Failed to apply all filtering rules in dev {}", deviceId);
430 fail(filt, ObjectiveError.FLOWINSTALLATIONFAILED);
431 }
432 }));
433
434 }
435
436 /**
437 * Allows untagged packets into pipeline by assigning a vlan id.
Saurav Das0e99e2b2015-10-28 12:39:42 -0700438 * Vlan assignment is done by the application.
Saurav Das822c4e22015-10-23 10:51:11 -0700439 * Allows tagged packets into pipeline as per configured port-vlan info.
Saurav Das0e99e2b2015-10-28 12:39:42 -0700440 *
Saurav Das822c4e22015-10-23 10:51:11 -0700441 * @param portCriterion port on device for which this filter is programmed
442 * @param vidCriterion vlan assigned to port, or NONE for untagged
Saurav Das0e99e2b2015-10-28 12:39:42 -0700443 * @param assignedVlan assigned vlan-id for untagged packets
Saurav Das822c4e22015-10-23 10:51:11 -0700444 * @param applicationId for application programming this filter
445 * @return list of FlowRule for port-vlan filters
446 */
447 protected List<FlowRule> processVlanIdFilter(PortCriterion portCriterion,
448 VlanIdCriterion vidCriterion,
Saurav Das0e99e2b2015-10-28 12:39:42 -0700449 VlanId assignedVlan,
Saurav Das822c4e22015-10-23 10:51:11 -0700450 ApplicationId applicationId) {
Charles Chan14967c22015-12-07 11:11:50 -0800451 List<FlowRule> rules = new ArrayList<>();
Saurav Das822c4e22015-10-23 10:51:11 -0700452 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
453 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
Charles Chan14967c22015-12-07 11:11:50 -0800454 TrafficSelector.Builder preSelector = null;
455 TrafficTreatment.Builder preTreatment = null;
456
Saurav Das4f980082015-11-05 13:39:15 -0800457 treatment.transition(TMAC_TABLE);
458
Saurav Das822c4e22015-10-23 10:51:11 -0700459 if (vidCriterion.vlanId() == VlanId.NONE) {
460 // untagged packets are assigned vlans
Charles Chanbe8aea42016-02-24 12:04:47 -0800461 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(VlanId.NONE);
Charles Chan14967c22015-12-07 11:11:50 -0800462 selector.extension(ofdpaMatchVlanVid, deviceId);
463 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(assignedVlan);
464 treatment.extension(ofdpaSetVlanVid, deviceId);
Charles Chan14967c22015-12-07 11:11:50 -0800465
466 preSelector = DefaultTrafficSelector.builder();
467 OfdpaMatchVlanVid preOfdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
468 preSelector.extension(preOfdpaMatchVlanVid, deviceId);
469 preTreatment = DefaultTrafficTreatment.builder().transition(TMAC_TABLE);
470
Saurav Das4f980082015-11-05 13:39:15 -0800471 } else {
Charles Chan14967c22015-12-07 11:11:50 -0800472 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vidCriterion.vlanId());
473 selector.extension(ofdpaMatchVlanVid, deviceId);
Charles Chand55e84d2016-03-30 17:54:24 -0700474
475 if (!assignedVlan.equals(vidCriterion.vlanId())) {
476 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(assignedVlan);
477 treatment.extension(ofdpaSetVlanVid, deviceId);
478 }
Saurav Das822c4e22015-10-23 10:51:11 -0700479 }
Saurav Das822c4e22015-10-23 10:51:11 -0700480
481 // ofdpa cannot match on ALL portnumber, so we need to use separate
482 // rules for each port.
Charles Chan14967c22015-12-07 11:11:50 -0800483 List<PortNumber> portnums = new ArrayList<>();
Saurav Das822c4e22015-10-23 10:51:11 -0700484 if (portCriterion.port() == PortNumber.ALL) {
485 for (Port port : deviceService.getPorts(deviceId)) {
486 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
487 portnums.add(port.number());
488 }
489 }
490 } else {
491 portnums.add(portCriterion.port());
492 }
Saurav Das4f980082015-11-05 13:39:15 -0800493
Saurav Das822c4e22015-10-23 10:51:11 -0700494 for (PortNumber pnum : portnums) {
Saurav Das4f980082015-11-05 13:39:15 -0800495 // create rest of flowrule
Saurav Das822c4e22015-10-23 10:51:11 -0700496 selector.matchInPort(pnum);
497 FlowRule rule = DefaultFlowRule.builder()
498 .forDevice(deviceId)
499 .withSelector(selector.build())
500 .withTreatment(treatment.build())
501 .withPriority(DEFAULT_PRIORITY)
502 .fromApp(applicationId)
503 .makePermanent()
504 .forTable(VLAN_TABLE).build();
Charles Chan14967c22015-12-07 11:11:50 -0800505
506 if (preSelector != null) {
507 preSelector.matchInPort(pnum);
508 FlowRule preRule = DefaultFlowRule.builder()
509 .forDevice(deviceId)
510 .withSelector(preSelector.build())
511 .withTreatment(preTreatment.build())
512 .withPriority(DEFAULT_PRIORITY)
513 .fromApp(applicationId)
514 .makePermanent()
515 .forTable(VLAN_TABLE).build();
516 rules.add(preRule);
517 }
518
Saurav Das822c4e22015-10-23 10:51:11 -0700519 rules.add(rule);
520 }
521 return rules;
522 }
523
524 /**
525 * Allows routed packets with correct destination MAC to be directed
526 * to unicast-IP routing table or MPLS forwarding table.
Saurav Das822c4e22015-10-23 10:51:11 -0700527 *
528 * @param portCriterion port on device for which this filter is programmed
529 * @param ethCriterion dstMac of device for which is filter is programmed
530 * @param vidCriterion vlan assigned to port, or NONE for untagged
Saurav Das0e99e2b2015-10-28 12:39:42 -0700531 * @param assignedVlan assigned vlan-id for untagged packets
Saurav Das822c4e22015-10-23 10:51:11 -0700532 * @param applicationId for application programming this filter
533 * @return list of FlowRule for port-vlan filters
534
535 */
536 protected List<FlowRule> processEthDstFilter(PortCriterion portCriterion,
537 EthCriterion ethCriterion,
538 VlanIdCriterion vidCriterion,
Saurav Das0e99e2b2015-10-28 12:39:42 -0700539 VlanId assignedVlan,
Saurav Das822c4e22015-10-23 10:51:11 -0700540 ApplicationId applicationId) {
Charles Chan5270ed02016-01-30 23:22:37 -0800541 // Consider PortNumber.ANY as wildcard. Match ETH_DST only
542 if (portCriterion != null && portCriterion.port() == PortNumber.ANY) {
543 return processEthDstOnlyFilter(ethCriterion, applicationId);
544 }
545
Charles Chan5b9df8d2016-03-28 22:21:40 -0700546 // Multicast MAC
547 if (ethCriterion.mask() != null) {
548 return processMcastEthDstFilter(ethCriterion, applicationId);
549 }
550
Saurav Das822c4e22015-10-23 10:51:11 -0700551 //handling untagged packets via assigned VLAN
552 if (vidCriterion.vlanId() == VlanId.NONE) {
Saurav Das0e99e2b2015-10-28 12:39:42 -0700553 vidCriterion = (VlanIdCriterion) Criteria.matchVlanId(assignedVlan);
Saurav Das822c4e22015-10-23 10:51:11 -0700554 }
555 // ofdpa cannot match on ALL portnumber, so we need to use separate
556 // rules for each port.
557 List<PortNumber> portnums = new ArrayList<PortNumber>();
558 if (portCriterion.port() == PortNumber.ALL) {
559 for (Port port : deviceService.getPorts(deviceId)) {
560 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
561 portnums.add(port.number());
562 }
563 }
564 } else {
565 portnums.add(portCriterion.port());
566 }
567
568 List<FlowRule> rules = new ArrayList<FlowRule>();
569 for (PortNumber pnum : portnums) {
Charles Chan14967c22015-12-07 11:11:50 -0800570 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vidCriterion.vlanId());
Saurav Das822c4e22015-10-23 10:51:11 -0700571 // for unicast IP packets
572 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
573 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
574 selector.matchInPort(pnum);
Charles Chan14967c22015-12-07 11:11:50 -0800575 selector.extension(ofdpaMatchVlanVid, deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700576 selector.matchEthType(Ethernet.TYPE_IPV4);
577 selector.matchEthDst(ethCriterion.mac());
578 treatment.transition(UNICAST_ROUTING_TABLE);
579 FlowRule rule = DefaultFlowRule.builder()
580 .forDevice(deviceId)
581 .withSelector(selector.build())
582 .withTreatment(treatment.build())
583 .withPriority(DEFAULT_PRIORITY)
584 .fromApp(applicationId)
585 .makePermanent()
586 .forTable(TMAC_TABLE).build();
587 rules.add(rule);
588 //for MPLS packets
589 selector = DefaultTrafficSelector.builder();
590 treatment = DefaultTrafficTreatment.builder();
591 selector.matchInPort(pnum);
Charles Chan14967c22015-12-07 11:11:50 -0800592 selector.extension(ofdpaMatchVlanVid, deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700593 selector.matchEthType(Ethernet.MPLS_UNICAST);
594 selector.matchEthDst(ethCriterion.mac());
595 treatment.transition(MPLS_TABLE_0);
596 rule = DefaultFlowRule.builder()
597 .forDevice(deviceId)
598 .withSelector(selector.build())
599 .withTreatment(treatment.build())
600 .withPriority(DEFAULT_PRIORITY)
601 .fromApp(applicationId)
602 .makePermanent()
603 .forTable(TMAC_TABLE).build();
604 rules.add(rule);
605 }
606 return rules;
607 }
608
Charles Chan5270ed02016-01-30 23:22:37 -0800609 protected List<FlowRule> processEthDstOnlyFilter(EthCriterion ethCriterion,
610 ApplicationId applicationId) {
611 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
612 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
613 selector.matchEthType(Ethernet.TYPE_IPV4);
614 selector.matchEthDst(ethCriterion.mac());
615 treatment.transition(UNICAST_ROUTING_TABLE);
616 FlowRule rule = DefaultFlowRule.builder()
617 .forDevice(deviceId)
618 .withSelector(selector.build())
619 .withTreatment(treatment.build())
620 .withPriority(DEFAULT_PRIORITY)
621 .fromApp(applicationId)
622 .makePermanent()
623 .forTable(TMAC_TABLE).build();
624 return ImmutableList.<FlowRule>builder().add(rule).build();
625 }
626
Charles Chan5b9df8d2016-03-28 22:21:40 -0700627 protected List<FlowRule> processMcastEthDstFilter(EthCriterion ethCriterion,
628 ApplicationId applicationId) {
629 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
630 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
631 selector.matchEthType(Ethernet.TYPE_IPV4);
632 selector.matchEthDstMasked(ethCriterion.mac(), ethCriterion.mask());
633 treatment.transition(MULTICAST_ROUTING_TABLE);
634 FlowRule rule = DefaultFlowRule.builder()
635 .forDevice(deviceId)
636 .withSelector(selector.build())
637 .withTreatment(treatment.build())
638 .withPriority(DEFAULT_PRIORITY)
639 .fromApp(applicationId)
640 .makePermanent()
641 .forTable(TMAC_TABLE).build();
642 return ImmutableList.<FlowRule>builder().add(rule).build();
643 }
644
Saurav Das822c4e22015-10-23 10:51:11 -0700645 private Collection<FlowRule> processForward(ForwardingObjective fwd) {
646 switch (fwd.flag()) {
647 case SPECIFIC:
648 return processSpecific(fwd);
649 case VERSATILE:
650 return processVersatile(fwd);
651 default:
652 fail(fwd, ObjectiveError.UNKNOWN);
653 log.warn("Unknown forwarding flag {}", fwd.flag());
654 }
655 return Collections.emptySet();
656 }
657
658 /**
659 * In the OF-DPA 2.0 pipeline, versatile forwarding objectives go to the
660 * ACL table.
661 * @param fwd the forwarding objective of type 'versatile'
662 * @return a collection of flow rules to be sent to the switch. An empty
663 * collection may be returned if there is a problem in processing
664 * the flow rule
665 */
Saurav Das52025962016-01-28 22:30:01 -0800666 protected Collection<FlowRule> processVersatile(ForwardingObjective fwd) {
Saurav Das822c4e22015-10-23 10:51:11 -0700667 log.info("Processing versatile forwarding objective");
Saurav Das822c4e22015-10-23 10:51:11 -0700668
669 EthTypeCriterion ethType =
Saurav Das77b5e902016-01-27 17:01:59 -0800670 (EthTypeCriterion) fwd.selector().getCriterion(Criterion.Type.ETH_TYPE);
Saurav Das822c4e22015-10-23 10:51:11 -0700671 if (ethType == null) {
672 log.error("Versatile forwarding objective must include ethType");
673 fail(fwd, ObjectiveError.BADPARAMS);
674 return Collections.emptySet();
675 }
676 if (fwd.nextId() == null && fwd.treatment() == null) {
677 log.error("Forwarding objective {} from {} must contain "
678 + "nextId or Treatment", fwd.selector(), fwd.appId());
Zsolt Haraszti9faab752016-02-17 15:55:20 -0800679 fail(fwd, ObjectiveError.BADPARAMS);
Saurav Das822c4e22015-10-23 10:51:11 -0700680 return Collections.emptySet();
681 }
Saurav Das49cb5a12016-01-16 22:54:07 -0800682
Saurav Das77b5e902016-01-27 17:01:59 -0800683 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
684 fwd.selector().criteria().forEach(criterion -> {
685 if (criterion instanceof VlanIdCriterion) {
686 VlanId vlanId = ((VlanIdCriterion) criterion).vlanId();
687 // ensure that match does not include vlan = NONE as OF-DPA does not
688 // match untagged packets this way in the ACL table.
689 if (vlanId.equals(VlanId.NONE)) {
690 return;
691 }
692 OfdpaMatchVlanVid ofdpaMatchVlanVid =
693 new OfdpaMatchVlanVid(vlanId);
694 sbuilder.extension(ofdpaMatchVlanVid, deviceId);
695 } else {
696 sbuilder.add(criterion);
697 }
698 });
699
Saurav Das822c4e22015-10-23 10:51:11 -0700700 // XXX driver does not currently do type checking as per Tables 65-67 in
701 // OFDPA 2.0 spec. The only allowed treatment is a punt to the controller.
Saurav Das49cb5a12016-01-16 22:54:07 -0800702 TrafficTreatment.Builder ttBuilder = DefaultTrafficTreatment.builder();
703 if (fwd.treatment() != null) {
704 for (Instruction ins : fwd.treatment().allInstructions()) {
705 if (ins instanceof OutputInstruction) {
706 OutputInstruction o = (OutputInstruction) ins;
707 if (o.port() == PortNumber.CONTROLLER) {
708 ttBuilder.add(o);
709 } else {
710 log.warn("Only allowed treatments in versatile forwarding "
711 + "objectives are punts to the controller");
712 }
713 } else {
714 log.warn("Cannot process instruction in versatile fwd {}", ins);
715 }
Saurav Das822c4e22015-10-23 10:51:11 -0700716 }
717 }
Saurav Das822c4e22015-10-23 10:51:11 -0700718 if (fwd.nextId() != null) {
Saurav Das49cb5a12016-01-16 22:54:07 -0800719 // overide case
720 NextGroup next = getGroupForNextObjective(fwd.nextId());
721 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
722 // we only need the top level group's key to point the flow to it
723 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
724 if (group == null) {
725 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
726 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
727 fail(fwd, ObjectiveError.GROUPMISSING);
728 return Collections.emptySet();
729 }
730 ttBuilder.deferred().group(group.id());
Saurav Das822c4e22015-10-23 10:51:11 -0700731 }
Saurav Das49cb5a12016-01-16 22:54:07 -0800732
733 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
734 .fromApp(fwd.appId())
735 .withPriority(fwd.priority())
736 .forDevice(deviceId)
Saurav Das77b5e902016-01-27 17:01:59 -0800737 .withSelector(sbuilder.build())
Saurav Das49cb5a12016-01-16 22:54:07 -0800738 .withTreatment(ttBuilder.build())
739 .makePermanent()
740 .forTable(ACL_TABLE);
741 return Collections.singletonList(ruleBuilder.build());
Saurav Das822c4e22015-10-23 10:51:11 -0700742 }
743
744 /**
745 * In the OF-DPA 2.0 pipeline, specific forwarding refers to the IP table
Saurav Das8a0732e2015-11-20 15:27:53 -0800746 * (unicast or multicast) or the L2 table (mac + vlan) or the MPLS table.
Saurav Das822c4e22015-10-23 10:51:11 -0700747 *
748 * @param fwd the forwarding objective of type 'specific'
749 * @return a collection of flow rules. Typically there will be only one
750 * for this type of forwarding objective. An empty set may be
751 * returned if there is an issue in processing the objective.
752 */
Saurav Das8a0732e2015-11-20 15:27:53 -0800753 protected Collection<FlowRule> processSpecific(ForwardingObjective fwd) {
Saurav Das4ce45962015-11-24 23:21:05 -0800754 log.trace("Processing specific fwd objective:{} in dev:{} with next:{}",
755 fwd.id(), deviceId, fwd.nextId());
756 boolean isEthTypeObj = isSupportedEthTypeObjective(fwd);
757 boolean isEthDstObj = isSupportedEthDstObjective(fwd);
758
759 if (isEthTypeObj) {
760 return processEthTypeSpecific(fwd);
761 } else if (isEthDstObj) {
762 return processEthDstSpecific(fwd);
763 } else {
764 log.warn("processSpecific: Unsupported forwarding objective "
765 + "criteria fwd:{} in dev:{}", fwd.nextId(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700766 fail(fwd, ObjectiveError.UNSUPPORTED);
767 return Collections.emptySet();
768 }
Saurav Das4ce45962015-11-24 23:21:05 -0800769 }
770
771 private boolean isSupportedEthTypeObjective(ForwardingObjective fwd) {
772 TrafficSelector selector = fwd.selector();
773 EthTypeCriterion ethType = (EthTypeCriterion) selector
774 .getCriterion(Criterion.Type.ETH_TYPE);
Charles Chan188ebf52015-12-23 00:15:11 -0800775 return !((ethType == null) ||
Saurav Das4ce45962015-11-24 23:21:05 -0800776 ((ethType.ethType().toShort() != Ethernet.TYPE_IPV4) &&
Charles Chan188ebf52015-12-23 00:15:11 -0800777 (ethType.ethType().toShort() != Ethernet.MPLS_UNICAST)));
Saurav Das4ce45962015-11-24 23:21:05 -0800778 }
779
780 private boolean isSupportedEthDstObjective(ForwardingObjective fwd) {
781 TrafficSelector selector = fwd.selector();
782 EthCriterion ethDst = (EthCriterion) selector
783 .getCriterion(Criterion.Type.ETH_DST);
784 VlanIdCriterion vlanId = (VlanIdCriterion) selector
785 .getCriterion(Criterion.Type.VLAN_VID);
Charles Chan188ebf52015-12-23 00:15:11 -0800786 return !(ethDst == null && vlanId == null);
Saurav Das4ce45962015-11-24 23:21:05 -0800787 }
788
789 /**
790 * Handles forwarding rules to the IP and MPLS tables.
791 *
792 * @param fwd the forwarding objective
793 * @return A collection of flow rules, or an empty set
794 */
795 protected Collection<FlowRule> processEthTypeSpecific(ForwardingObjective fwd) {
796 TrafficSelector selector = fwd.selector();
797 EthTypeCriterion ethType =
798 (EthTypeCriterion) selector.getCriterion(Criterion.Type.ETH_TYPE);
Flavio Castroe10fa242016-01-15 12:43:51 -0800799 boolean defaultRule = false;
800 boolean popMpls = false;
Charles Chan188ebf52015-12-23 00:15:11 -0800801 int forTableId;
Saurav Das8a0732e2015-11-20 15:27:53 -0800802 TrafficSelector.Builder filteredSelector = DefaultTrafficSelector.builder();
Charles Chan14967c22015-12-07 11:11:50 -0800803 TrafficTreatment.Builder tb = DefaultTrafficTreatment.builder();
Flavio Castroe10fa242016-01-15 12:43:51 -0800804 TrafficSelector.Builder complementarySelector = DefaultTrafficSelector.builder();
Charles Chan14967c22015-12-07 11:11:50 -0800805
Flavio Castroe10fa242016-01-15 12:43:51 -0800806 /*
807 * NOTE: The switch does not support matching 0.0.0.0/0.
808 * Split it into 0.0.0.0/1 and 128.0.0.0/1
809 */
Saurav Das8a0732e2015-11-20 15:27:53 -0800810 if (ethType.ethType().toShort() == Ethernet.TYPE_IPV4) {
Flavio Castroe10fa242016-01-15 12:43:51 -0800811 IpPrefix ipv4Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV4_DST)).ip();
Charles Chan5b9df8d2016-03-28 22:21:40 -0700812 if (ipv4Dst.isMulticast()) {
813 if (ipv4Dst.prefixLength() != 32) {
814 log.warn("Multicast specific forwarding objective can only be /32");
815 fail(fwd, ObjectiveError.BADPARAMS);
816 return ImmutableSet.of();
817 }
818 VlanId assignedVlan = readVlanFromSelector(fwd.meta());
819 if (assignedVlan == null) {
820 log.warn("VLAN ID required by multicast specific fwd obj is missing. Abort.");
821 fail(fwd, ObjectiveError.BADPARAMS);
822 return ImmutableSet.of();
823 }
824 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
825 filteredSelector.extension(ofdpaMatchVlanVid, deviceId);
826 filteredSelector.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipv4Dst);
827 forTableId = MULTICAST_ROUTING_TABLE;
828 log.debug("processing IPv4 multicast specific forwarding objective {} -> next:{}"
829 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
Flavio Castroe10fa242016-01-15 12:43:51 -0800830 } else {
Charles Chan5b9df8d2016-03-28 22:21:40 -0700831 if (ipv4Dst.prefixLength() > 0) {
832 filteredSelector.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipv4Dst);
833 } else {
834 filteredSelector.matchEthType(Ethernet.TYPE_IPV4)
835 .matchIPDst(IpPrefix.valueOf("0.0.0.0/1"));
836 complementarySelector.matchEthType(Ethernet.TYPE_IPV4)
837 .matchIPDst(IpPrefix.valueOf("128.0.0.0/1"));
838 defaultRule = true;
839 }
840 forTableId = UNICAST_ROUTING_TABLE;
841 log.debug("processing IPv4 unicast specific forwarding objective {} -> next:{}"
842 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
Flavio Castroe10fa242016-01-15 12:43:51 -0800843 }
Charles Chan14967c22015-12-07 11:11:50 -0800844
845 if (fwd.treatment() != null) {
846 for (Instruction instr : fwd.treatment().allInstructions()) {
847 if (instr instanceof L3ModificationInstruction &&
848 ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
849 tb.deferred().add(instr);
850 }
851 }
852 }
853
Saurav Das8a0732e2015-11-20 15:27:53 -0800854 } else {
855 filteredSelector
856 .matchEthType(Ethernet.MPLS_UNICAST)
857 .matchMplsLabel(((MplsCriterion)
858 selector.getCriterion(Criterion.Type.MPLS_LABEL)).label());
859 MplsBosCriterion bos = (MplsBosCriterion) selector
860 .getCriterion(Criterion.Type.MPLS_BOS);
861 if (bos != null) {
862 filteredSelector.matchMplsBos(bos.mplsBos());
863 }
864 forTableId = MPLS_TABLE_1;
Saurav Das4ce45962015-11-24 23:21:05 -0800865 log.debug("processing MPLS specific forwarding objective {} -> next:{}"
866 + " in dev {}", fwd.id(), fwd.nextId(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700867
Charles Chan14967c22015-12-07 11:11:50 -0800868 if (fwd.treatment() != null) {
869 for (Instruction instr : fwd.treatment().allInstructions()) {
870 if (instr instanceof L2ModificationInstruction &&
871 ((L2ModificationInstruction) instr).subtype() == L2SubType.MPLS_POP) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800872 popMpls = true;
Charles Chan14967c22015-12-07 11:11:50 -0800873 tb.immediate().add(instr);
874 }
875 if (instr instanceof L3ModificationInstruction &&
876 ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
877 // FIXME Should modify the app to send the correct DEC_MPLS_TTL instruction
878 tb.immediate().decMplsTtl();
879 }
880 if (instr instanceof L3ModificationInstruction &&
881 ((L3ModificationInstruction) instr).subtype() == L3SubType.TTL_IN) {
882 tb.immediate().add(instr);
883 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800884 }
885 }
886 }
Saurav Das822c4e22015-10-23 10:51:11 -0700887
888 if (fwd.nextId() != null) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800889 if (forTableId == MPLS_TABLE_1 && !popMpls) {
890 log.warn("SR CONTINUE case cannot be handled as MPLS ECMP "
891 + "is not implemented in OF-DPA yet. Aborting this flow "
892 + "in this device {}", deviceId);
893 // XXX We could convert to forwarding to a single-port, via a
894 // MPLS interface, or a MPLS SWAP (with-same) but that would
895 // have to be handled in the next-objective. Also the pop-mpls
896 // logic used here won't work in non-BoS case.
Saurav Das4ce45962015-11-24 23:21:05 -0800897 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
Saurav Das8a0732e2015-11-20 15:27:53 -0800898 return Collections.emptySet();
899 }
900
Saurav Das423fe2b2015-12-04 10:52:59 -0800901 NextGroup next = getGroupForNextObjective(fwd.nextId());
902 if (next != null) {
903 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
904 // we only need the top level group's key to point the flow to it
905 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
906 if (group == null) {
907 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
908 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
909 fail(fwd, ObjectiveError.GROUPMISSING);
910 return Collections.emptySet();
911 }
912 tb.deferred().group(group.id());
Saurav Das822c4e22015-10-23 10:51:11 -0700913 }
Saurav Das822c4e22015-10-23 10:51:11 -0700914 }
915 tb.transition(ACL_TABLE);
916 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
917 .fromApp(fwd.appId())
918 .withPriority(fwd.priority())
919 .forDevice(deviceId)
Saurav Das8a0732e2015-11-20 15:27:53 -0800920 .withSelector(filteredSelector.build())
921 .withTreatment(tb.build())
922 .forTable(forTableId);
Saurav Das822c4e22015-10-23 10:51:11 -0700923
924 if (fwd.permanent()) {
925 ruleBuilder.makePermanent();
926 } else {
927 ruleBuilder.makeTemporary(fwd.timeout());
928 }
Flavio Castroe10fa242016-01-15 12:43:51 -0800929 Collection<FlowRule> flowRuleCollection = new ArrayList<>();
930 flowRuleCollection.add(ruleBuilder.build());
931 if (defaultRule) {
932 FlowRule.Builder rule = DefaultFlowRule.builder()
933 .fromApp(fwd.appId())
934 .withPriority(fwd.priority())
935 .forDevice(deviceId)
936 .withSelector(complementarySelector.build())
937 .withTreatment(tb.build())
938 .forTable(forTableId);
939 if (fwd.permanent()) {
940 rule.makePermanent();
941 } else {
942 rule.makeTemporary(fwd.timeout());
943 }
944 flowRuleCollection.add(rule.build());
945 log.debug("Default rule 0.0.0.0/0 is being installed two rules");
946 }
Saurav Das822c4e22015-10-23 10:51:11 -0700947
Flavio Castroe10fa242016-01-15 12:43:51 -0800948 return flowRuleCollection;
Saurav Das822c4e22015-10-23 10:51:11 -0700949 }
950
Saurav Das4ce45962015-11-24 23:21:05 -0800951 /**
952 * Handles forwarding rules to the L2 bridging table. Flow actions are not
953 * allowed in the bridging table - instead we use L2 Interface group or
954 * L2 flood group
955 *
956 * @param fwd the forwarding objective
957 * @return A collection of flow rules, or an empty set
958 */
959 protected Collection<FlowRule> processEthDstSpecific(ForwardingObjective fwd) {
960 List<FlowRule> rules = new ArrayList<>();
961
962 // Build filtered selector
963 TrafficSelector selector = fwd.selector();
964 EthCriterion ethCriterion = (EthCriterion) selector
965 .getCriterion(Criterion.Type.ETH_DST);
966 VlanIdCriterion vlanIdCriterion = (VlanIdCriterion) selector
967 .getCriterion(Criterion.Type.VLAN_VID);
968
969 if (vlanIdCriterion == null) {
970 log.warn("Forwarding objective for bridging requires vlan. Not "
971 + "installing fwd:{} in dev:{}", fwd.id(), deviceId);
972 fail(fwd, ObjectiveError.BADPARAMS);
973 return Collections.emptySet();
974 }
975
976 TrafficSelector.Builder filteredSelectorBuilder =
977 DefaultTrafficSelector.builder();
978 // Do not match MacAddress for subnet broadcast entry
979 if (!ethCriterion.mac().equals(MacAddress.NONE)) {
980 filteredSelectorBuilder.matchEthDst(ethCriterion.mac());
981 log.debug("processing L2 forwarding objective:{} -> next:{} in dev:{}",
982 fwd.id(), fwd.nextId(), deviceId);
983 } else {
984 log.debug("processing L2 Broadcast forwarding objective:{} -> next:{} "
985 + "in dev:{} for vlan:{}",
986 fwd.id(), fwd.nextId(), deviceId, vlanIdCriterion.vlanId());
987 }
Charles Chan14967c22015-12-07 11:11:50 -0800988 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vlanIdCriterion.vlanId());
989 filteredSelectorBuilder.extension(ofdpaMatchVlanVid, deviceId);
Saurav Das4ce45962015-11-24 23:21:05 -0800990 TrafficSelector filteredSelector = filteredSelectorBuilder.build();
991
992 if (fwd.treatment() != null) {
993 log.warn("Ignoring traffic treatment in fwd rule {} meant for L2 table"
994 + "for dev:{}. Expecting only nextId", fwd.id(), deviceId);
995 }
996
997 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
998 if (fwd.nextId() != null) {
Saurav Das423fe2b2015-12-04 10:52:59 -0800999 NextGroup next = getGroupForNextObjective(fwd.nextId());
Saurav Das4ce45962015-11-24 23:21:05 -08001000 if (next != null) {
1001 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1002 // we only need the top level group's key to point the flow to it
1003 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
1004 if (group != null) {
1005 treatmentBuilder.deferred().group(group.id());
1006 } else {
1007 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
1008 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
1009 fail(fwd, ObjectiveError.GROUPMISSING);
1010 return Collections.emptySet();
1011 }
1012 }
1013 }
1014 treatmentBuilder.immediate().transition(ACL_TABLE);
1015 TrafficTreatment filteredTreatment = treatmentBuilder.build();
1016
1017 // Build bridging table entries
1018 FlowRule.Builder flowRuleBuilder = DefaultFlowRule.builder();
1019 flowRuleBuilder.fromApp(fwd.appId())
1020 .withPriority(fwd.priority())
1021 .forDevice(deviceId)
1022 .withSelector(filteredSelector)
1023 .withTreatment(filteredTreatment)
1024 .forTable(BRIDGING_TABLE);
1025 if (fwd.permanent()) {
1026 flowRuleBuilder.makePermanent();
1027 } else {
1028 flowRuleBuilder.makeTemporary(fwd.timeout());
1029 }
1030 rules.add(flowRuleBuilder.build());
1031 return rules;
1032 }
1033
Saurav Das423fe2b2015-12-04 10:52:59 -08001034 protected NextGroup getGroupForNextObjective(Integer nextId) {
1035 NextGroup next = flowObjectiveStore.getNextGroup(nextId);
1036 if (next != null) {
1037 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1038 if (gkeys != null && !gkeys.isEmpty()) {
1039 return next;
1040 } else {
1041 log.warn("Empty next group found in FlowObjective store for "
1042 + "next-id:{} in dev:{}", nextId, deviceId);
1043 }
1044 } else {
1045 log.warn("next-id {} not found in Flow objective store for dev:{}",
1046 nextId, deviceId);
1047 }
1048 return null;
1049 }
1050
Charles Chan188ebf52015-12-23 00:15:11 -08001051 protected static void pass(Objective obj) {
Sho SHIMIZUef7e2902016-02-12 18:38:29 -08001052 obj.context().ifPresent(context -> context.onSuccess(obj));
Saurav Das822c4e22015-10-23 10:51:11 -07001053 }
1054
Charles Chan188ebf52015-12-23 00:15:11 -08001055 protected static void fail(Objective obj, ObjectiveError error) {
Sho SHIMIZUef7e2902016-02-12 18:38:29 -08001056 obj.context().ifPresent(context -> context.onError(obj, error));
Saurav Das822c4e22015-10-23 10:51:11 -07001057 }
Saurav Das24431192016-03-07 19:13:00 -08001058
Saurav Das24431192016-03-07 19:13:00 -08001059 @Override
1060 public List<String> getNextMappings(NextGroup nextGroup) {
1061 List<String> mappings = new ArrayList<>();
1062 List<Deque<GroupKey>> gkeys = appKryo.deserialize(nextGroup.data());
1063 for (Deque<GroupKey> gkd : gkeys) {
1064 Group lastGroup = null;
Saurav Das8be4e3a2016-03-11 17:19:07 -08001065 StringBuffer gchain = new StringBuffer();
Saurav Das24431192016-03-07 19:13:00 -08001066 for (GroupKey gk : gkd) {
1067 Group g = groupService.getGroup(deviceId, gk);
Saurav Das8be4e3a2016-03-11 17:19:07 -08001068 if (g == null) {
1069 gchain.append(" ERROR").append(" -->");
1070 continue;
1071 }
1072 gchain.append(" 0x").append(Integer.toHexString(g.id().id()))
1073 .append(" -->");
Saurav Das24431192016-03-07 19:13:00 -08001074 lastGroup = g;
1075 }
1076 // add port information for last group in group-chain
1077 for (Instruction i: lastGroup.buckets().buckets().get(0).treatment().allInstructions()) {
1078 if (i instanceof OutputInstruction) {
Saurav Das8be4e3a2016-03-11 17:19:07 -08001079 gchain.append(" port:").append(((OutputInstruction) i).port());
Saurav Das24431192016-03-07 19:13:00 -08001080 }
1081 }
Saurav Das8be4e3a2016-03-11 17:19:07 -08001082 mappings.add(gchain.toString());
Saurav Das24431192016-03-07 19:13:00 -08001083 }
1084 return mappings;
1085 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001086
1087 protected static VlanId readVlanFromSelector(TrafficSelector selector) {
Saurav Das59232cf2016-04-27 18:35:50 -07001088 if (selector == null) {
1089 return null;
1090 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001091 Criterion criterion = selector.getCriterion(Criterion.Type.VLAN_VID);
1092 return (criterion == null)
1093 ? null : ((VlanIdCriterion) criterion).vlanId();
1094 }
1095
1096 protected static IpPrefix readIpDstFromSelector(TrafficSelector selector) {
Saurav Das59232cf2016-04-27 18:35:50 -07001097 if (selector == null) {
1098 return null;
1099 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001100 Criterion criterion = selector.getCriterion(Criterion.Type.IPV4_DST);
1101 return (criterion == null) ? null : ((IPCriterion) criterion).ip();
1102 }
Charles Chand55e84d2016-03-30 17:54:24 -07001103
1104 private static VlanId readVlanFromTreatment(TrafficTreatment treatment) {
Saurav Das59232cf2016-04-27 18:35:50 -07001105 if (treatment == null) {
1106 return null;
1107 }
Charles Chand55e84d2016-03-30 17:54:24 -07001108 for (Instruction i : treatment.allInstructions()) {
1109 if (i instanceof ModVlanIdInstruction) {
1110 return ((ModVlanIdInstruction) i).vlanId();
1111 }
1112 }
1113 return null;
1114 }
Saurav Das822c4e22015-10-23 10:51:11 -07001115}