blob: 6f42dbc66cf5bf521d153307b032a75d72caf3db [file] [log] [blame]
Saurav Das822c4e22015-10-23 10:51:11 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Saurav Das822c4e22015-10-23 10:51:11 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onosproject.driver.pipeline;
17
Saurav Das822c4e22015-10-23 10:51:11 -070018import static org.slf4j.LoggerFactory.getLogger;
19
Saurav Das8a0732e2015-11-20 15:27:53 -080020import java.util.ArrayDeque;
Saurav Das822c4e22015-10-23 10:51:11 -070021import java.util.ArrayList;
22import java.util.Collection;
23import java.util.Collections;
Saurav Das8a0732e2015-11-20 15:27:53 -080024import java.util.Deque;
Saurav Das822c4e22015-10-23 10:51:11 -070025import java.util.List;
Sho SHIMIZU45906042016-01-13 23:05:54 -080026import java.util.Objects;
Saurav Das822c4e22015-10-23 10:51:11 -070027import java.util.Set;
28import java.util.concurrent.ConcurrentHashMap;
Saurav Das822c4e22015-10-23 10:51:11 -070029
Charles Chan5270ed02016-01-30 23:22:37 -080030import com.google.common.collect.ImmutableList;
Charles Chan5b9df8d2016-03-28 22:21:40 -070031import com.google.common.collect.ImmutableSet;
Saurav Das822c4e22015-10-23 10:51:11 -070032import org.onlab.osgi.ServiceDirectory;
Saurav Das822c4e22015-10-23 10:51:11 -070033import org.onlab.packet.Ethernet;
Charles Chan14967c22015-12-07 11:11:50 -080034import org.onlab.packet.IpPrefix;
Saurav Das822c4e22015-10-23 10:51:11 -070035import org.onlab.packet.MacAddress;
Saurav Das822c4e22015-10-23 10:51:11 -070036import org.onlab.packet.VlanId;
37import org.onlab.util.KryoNamespace;
38import org.onosproject.core.ApplicationId;
39import org.onosproject.core.CoreService;
Charles Chan14967c22015-12-07 11:11:50 -080040import org.onosproject.driver.extensions.OfdpaMatchVlanVid;
41import org.onosproject.driver.extensions.OfdpaSetVlanVid;
Saurav Das822c4e22015-10-23 10:51:11 -070042import org.onosproject.net.DeviceId;
43import org.onosproject.net.Port;
44import org.onosproject.net.PortNumber;
45import org.onosproject.net.behaviour.NextGroup;
46import org.onosproject.net.behaviour.Pipeliner;
47import org.onosproject.net.behaviour.PipelinerContext;
48import org.onosproject.net.device.DeviceService;
49import org.onosproject.net.driver.AbstractHandlerBehaviour;
50import org.onosproject.net.flow.DefaultFlowRule;
51import org.onosproject.net.flow.DefaultTrafficSelector;
52import org.onosproject.net.flow.DefaultTrafficTreatment;
53import org.onosproject.net.flow.FlowRule;
54import org.onosproject.net.flow.FlowRuleOperations;
55import org.onosproject.net.flow.FlowRuleOperationsContext;
56import org.onosproject.net.flow.FlowRuleService;
57import org.onosproject.net.flow.TrafficSelector;
58import org.onosproject.net.flow.TrafficTreatment;
59import org.onosproject.net.flow.criteria.Criteria;
60import org.onosproject.net.flow.criteria.Criterion;
61import org.onosproject.net.flow.criteria.EthCriterion;
62import org.onosproject.net.flow.criteria.EthTypeCriterion;
Charles Chan14967c22015-12-07 11:11:50 -080063import org.onosproject.net.flow.criteria.ExtensionCriterion;
Saurav Das822c4e22015-10-23 10:51:11 -070064import org.onosproject.net.flow.criteria.IPCriterion;
Saurav Das8a0732e2015-11-20 15:27:53 -080065import org.onosproject.net.flow.criteria.MplsBosCriterion;
66import org.onosproject.net.flow.criteria.MplsCriterion;
Saurav Das822c4e22015-10-23 10:51:11 -070067import org.onosproject.net.flow.criteria.PortCriterion;
68import org.onosproject.net.flow.criteria.VlanIdCriterion;
69import org.onosproject.net.flow.instructions.Instruction;
70import org.onosproject.net.flow.instructions.Instructions.OutputInstruction;
71import org.onosproject.net.flow.instructions.L2ModificationInstruction;
Saurav Das8a0732e2015-11-20 15:27:53 -080072import org.onosproject.net.flow.instructions.L2ModificationInstruction.L2SubType;
Saurav Das822c4e22015-10-23 10:51:11 -070073import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModVlanIdInstruction;
Charles Chan14967c22015-12-07 11:11:50 -080074import org.onosproject.net.flow.instructions.L3ModificationInstruction;
75import org.onosproject.net.flow.instructions.L3ModificationInstruction.L3SubType;
Saurav Das822c4e22015-10-23 10:51:11 -070076import org.onosproject.net.flowobjective.FilteringObjective;
77import org.onosproject.net.flowobjective.FlowObjectiveStore;
78import org.onosproject.net.flowobjective.ForwardingObjective;
79import org.onosproject.net.flowobjective.NextObjective;
80import org.onosproject.net.flowobjective.Objective;
81import org.onosproject.net.flowobjective.ObjectiveError;
Saurav Das822c4e22015-10-23 10:51:11 -070082import org.onosproject.net.group.DefaultGroupKey;
83import org.onosproject.net.group.Group;
Saurav Das822c4e22015-10-23 10:51:11 -070084import org.onosproject.net.group.GroupKey;
Saurav Das822c4e22015-10-23 10:51:11 -070085import org.onosproject.net.group.GroupService;
Saurav Das822c4e22015-10-23 10:51:11 -070086import org.onosproject.store.serializers.KryoNamespaces;
87import org.slf4j.Logger;
88
Saurav Das822c4e22015-10-23 10:51:11 -070089/**
90 * Driver for Broadcom's OF-DPA v2.0 TTP.
91 *
92 */
Charles Chan361154b2016-03-24 10:23:39 -070093public class Ofdpa2Pipeline extends AbstractHandlerBehaviour implements Pipeliner {
Saurav Das822c4e22015-10-23 10:51:11 -070094 protected static final int PORT_TABLE = 0;
95 protected static final int VLAN_TABLE = 10;
96 protected static final int TMAC_TABLE = 20;
97 protected static final int UNICAST_ROUTING_TABLE = 30;
98 protected static final int MULTICAST_ROUTING_TABLE = 40;
99 protected static final int MPLS_TABLE_0 = 23;
100 protected static final int MPLS_TABLE_1 = 24;
101 protected static final int BRIDGING_TABLE = 50;
102 protected static final int ACL_TABLE = 60;
103 protected static final int MAC_LEARNING_TABLE = 254;
104 protected static final long OFPP_MAX = 0xffffff00L;
105
Saurav Das52025962016-01-28 22:30:01 -0800106 protected static final int HIGHEST_PRIORITY = 0xffff;
Saurav Das2857f382015-11-03 14:39:27 -0800107 protected static final int DEFAULT_PRIORITY = 0x8000;
Saurav Das822c4e22015-10-23 10:51:11 -0700108 protected static final int LOWEST_PRIORITY = 0x0;
109
Saurav Das822c4e22015-10-23 10:51:11 -0700110 private final Logger log = getLogger(getClass());
Charles Chan425854b2016-04-11 15:32:12 -0700111 protected ServiceDirectory serviceDirectory;
Saurav Das822c4e22015-10-23 10:51:11 -0700112 protected FlowRuleService flowRuleService;
Charles Chan425854b2016-04-11 15:32:12 -0700113 protected CoreService coreService;
Saurav Das8a0732e2015-11-20 15:27:53 -0800114 protected GroupService groupService;
115 protected FlowObjectiveStore flowObjectiveStore;
Saurav Das822c4e22015-10-23 10:51:11 -0700116 protected DeviceId deviceId;
117 protected ApplicationId driverId;
Saurav Das822c4e22015-10-23 10:51:11 -0700118 protected DeviceService deviceService;
Charles Chan188ebf52015-12-23 00:15:11 -0800119 protected static KryoNamespace appKryo = new KryoNamespace.Builder()
Saurav Das822c4e22015-10-23 10:51:11 -0700120 .register(KryoNamespaces.API)
121 .register(GroupKey.class)
122 .register(DefaultGroupKey.class)
Charles Chan361154b2016-03-24 10:23:39 -0700123 .register(Ofdpa2GroupHandler.OfdpaNextGroup.class)
Saurav Das8a0732e2015-11-20 15:27:53 -0800124 .register(ArrayDeque.class)
Charles Chaneefdedf2016-05-23 16:45:45 -0700125 .build("Ofdpa2Pipeline");
Saurav Das822c4e22015-10-23 10:51:11 -0700126
Charles Chan425854b2016-04-11 15:32:12 -0700127 protected Ofdpa2GroupHandler groupHandler;
Saurav Das822c4e22015-10-23 10:51:11 -0700128
Saurav Das52025962016-01-28 22:30:01 -0800129 protected Set<IPCriterion> sentIpFilters = Collections.newSetFromMap(
Charles Chan188ebf52015-12-23 00:15:11 -0800130 new ConcurrentHashMap<>());
Saurav Das4f980082015-11-05 13:39:15 -0800131
Saurav Das822c4e22015-10-23 10:51:11 -0700132 @Override
133 public void init(DeviceId deviceId, PipelinerContext context) {
Saurav Das822c4e22015-10-23 10:51:11 -0700134 this.deviceId = deviceId;
135
Charles Chan188ebf52015-12-23 00:15:11 -0800136 // Initialize OFDPA group handler
Charles Chan425854b2016-04-11 15:32:12 -0700137 groupHandler = new Ofdpa2GroupHandler();
138 groupHandler.init(deviceId, context);
Saurav Das822c4e22015-10-23 10:51:11 -0700139
Charles Chan425854b2016-04-11 15:32:12 -0700140 serviceDirectory = context.directory();
Saurav Das822c4e22015-10-23 10:51:11 -0700141 coreService = serviceDirectory.get(CoreService.class);
142 flowRuleService = serviceDirectory.get(FlowRuleService.class);
143 groupService = serviceDirectory.get(GroupService.class);
144 flowObjectiveStore = context.store();
Saurav Das822c4e22015-10-23 10:51:11 -0700145 deviceService = serviceDirectory.get(DeviceService.class);
Saurav Das822c4e22015-10-23 10:51:11 -0700146
147 driverId = coreService.registerApplication(
Charles Chan425854b2016-04-11 15:32:12 -0700148 "org.onosproject.driver.Ofdpa2Pipeline");
Saurav Das822c4e22015-10-23 10:51:11 -0700149
Saurav Das822c4e22015-10-23 10:51:11 -0700150 initializePipeline();
Saurav Das822c4e22015-10-23 10:51:11 -0700151 }
152
153 protected void initializePipeline() {
Charles Chan188ebf52015-12-23 00:15:11 -0800154 // OF-DPA does not require initializing the pipeline as it puts default
155 // rules automatically in the hardware. However emulation of OFDPA in
156 // software switches does require table-miss-entries.
Saurav Das822c4e22015-10-23 10:51:11 -0700157 }
158
159 //////////////////////////////////////
160 // Flow Objectives
161 //////////////////////////////////////
162
163 @Override
164 public void filter(FilteringObjective filteringObjective) {
165 if (filteringObjective.type() == FilteringObjective.Type.PERMIT) {
166 processFilter(filteringObjective,
167 filteringObjective.op() == Objective.Operation.ADD,
168 filteringObjective.appId());
169 } else {
170 // Note that packets that don't match the PERMIT filter are
171 // automatically denied. The DENY filter is used to deny packets
172 // that are otherwise permitted by the PERMIT filter.
173 // Use ACL table flow rules here for DENY filtering objectives
174 log.debug("filter objective other than PERMIT currently not supported");
175 fail(filteringObjective, ObjectiveError.UNSUPPORTED);
176 }
177 }
178
179 @Override
180 public void forward(ForwardingObjective fwd) {
181 Collection<FlowRule> rules;
182 FlowRuleOperations.Builder flowOpsBuilder = FlowRuleOperations.builder();
183
184 rules = processForward(fwd);
185 switch (fwd.op()) {
186 case ADD:
187 rules.stream()
Sho SHIMIZU45906042016-01-13 23:05:54 -0800188 .filter(Objects::nonNull)
Saurav Das822c4e22015-10-23 10:51:11 -0700189 .forEach(flowOpsBuilder::add);
190 break;
191 case REMOVE:
192 rules.stream()
Sho SHIMIZU45906042016-01-13 23:05:54 -0800193 .filter(Objects::nonNull)
Saurav Das822c4e22015-10-23 10:51:11 -0700194 .forEach(flowOpsBuilder::remove);
195 break;
196 default:
197 fail(fwd, ObjectiveError.UNKNOWN);
198 log.warn("Unknown forwarding type {}", fwd.op());
199 }
200
Saurav Das822c4e22015-10-23 10:51:11 -0700201 flowRuleService.apply(flowOpsBuilder.build(new FlowRuleOperationsContext() {
202 @Override
203 public void onSuccess(FlowRuleOperations ops) {
204 pass(fwd);
205 }
206
207 @Override
208 public void onError(FlowRuleOperations ops) {
209 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
210 }
211 }));
Saurav Das822c4e22015-10-23 10:51:11 -0700212 }
213
214 @Override
215 public void next(NextObjective nextObjective) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800216 NextGroup nextGroup = flowObjectiveStore.getNextGroup(nextObjective.id());
217 switch (nextObjective.op()) {
218 case ADD:
Saurav Das4f980082015-11-05 13:39:15 -0800219 if (nextGroup != null) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800220 log.warn("Cannot add next {} that already exists in device {}",
221 nextObjective.id(), deviceId);
222 return;
223 }
224 log.debug("Processing NextObjective id{} in dev{} - add group",
225 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700226 groupHandler.addGroup(nextObjective);
Saurav Das8a0732e2015-11-20 15:27:53 -0800227 break;
228 case ADD_TO_EXISTING:
229 if (nextGroup != null) {
230 log.debug("Processing NextObjective id{} in dev{} - add bucket",
231 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700232 groupHandler.addBucketToGroup(nextObjective, nextGroup);
Saurav Das4f980082015-11-05 13:39:15 -0800233 } else {
Saurav Das8a0732e2015-11-20 15:27:53 -0800234 // it is possible that group-chain has not been fully created yet
Saurav Das423fe2b2015-12-04 10:52:59 -0800235 log.debug("Waiting to add bucket to group for next-id:{} in dev:{}",
236 nextObjective.id(), deviceId);
237 // by design only one pending bucket is allowed for the group
Charles Chan425854b2016-04-11 15:32:12 -0700238 groupHandler.pendingBuckets.put(nextObjective.id(), nextObjective);
Saurav Das4f980082015-11-05 13:39:15 -0800239 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800240 break;
241 case REMOVE:
242 if (nextGroup == null) {
243 log.warn("Cannot remove next {} that does not exist in device {}",
244 nextObjective.id(), deviceId);
245 return;
246 }
247 log.debug("Processing NextObjective id{} in dev{} - remove group",
248 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700249 groupHandler.removeGroup(nextObjective, nextGroup);
Saurav Das8a0732e2015-11-20 15:27:53 -0800250 break;
251 case REMOVE_FROM_EXISTING:
252 if (nextGroup == null) {
253 log.warn("Cannot remove from next {} that does not exist in device {}",
254 nextObjective.id(), deviceId);
255 return;
256 }
257 log.debug("Processing NextObjective id{} in dev{} - remove bucket",
258 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700259 groupHandler.removeBucketFromGroup(nextObjective, nextGroup);
Saurav Das8a0732e2015-11-20 15:27:53 -0800260 break;
261 default:
Saurav Das4f980082015-11-05 13:39:15 -0800262 log.warn("Unsupported operation {}", nextObjective.op());
Saurav Das822c4e22015-10-23 10:51:11 -0700263 }
264 }
265
266 //////////////////////////////////////
267 // Flow handling
268 //////////////////////////////////////
269
270 /**
271 * As per OFDPA 2.0 TTP, filtering of VLAN ids, MAC addresses (for routing)
272 * and IP addresses configured on switch ports happen in different tables.
273 * Note that IP filtering rules need to be added to the ACL table, as there
274 * is no mechanism to send to controller via IP table.
275 *
276 * @param filt the filtering objective
277 * @param install indicates whether to add or remove the objective
278 * @param applicationId the application that sent this objective
279 */
Saurav Das52025962016-01-28 22:30:01 -0800280 protected void processFilter(FilteringObjective filt,
281 boolean install, ApplicationId applicationId) {
Saurav Das822c4e22015-10-23 10:51:11 -0700282 // This driver only processes filtering criteria defined with switch
283 // ports as the key
284 PortCriterion portCriterion = null;
285 EthCriterion ethCriterion = null;
286 VlanIdCriterion vidCriterion = null;
287 Collection<IPCriterion> ips = new ArrayList<IPCriterion>();
288 if (!filt.key().equals(Criteria.dummy()) &&
289 filt.key().type() == Criterion.Type.IN_PORT) {
290 portCriterion = (PortCriterion) filt.key();
291 } else {
292 log.warn("No key defined in filtering objective from app: {}. Not"
293 + "processing filtering objective", applicationId);
Saurav Das59232cf2016-04-27 18:35:50 -0700294 fail(filt, ObjectiveError.BADPARAMS);
Saurav Das822c4e22015-10-23 10:51:11 -0700295 return;
296 }
Saurav Das59232cf2016-04-27 18:35:50 -0700297 log.debug("Received filtering objective for dev/port: {}/{}", deviceId,
298 portCriterion.port());
Saurav Das822c4e22015-10-23 10:51:11 -0700299 // convert filtering conditions for switch-intfs into flowrules
300 FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
301 for (Criterion criterion : filt.conditions()) {
Charles Chan5b9df8d2016-03-28 22:21:40 -0700302 if (criterion.type() == Criterion.Type.ETH_DST ||
303 criterion.type() == Criterion.Type.ETH_DST_MASKED) {
Saurav Das822c4e22015-10-23 10:51:11 -0700304 ethCriterion = (EthCriterion) criterion;
305 } else if (criterion.type() == Criterion.Type.VLAN_VID) {
306 vidCriterion = (VlanIdCriterion) criterion;
307 } else if (criterion.type() == Criterion.Type.IPV4_DST) {
308 ips.add((IPCriterion) criterion);
309 } else {
310 log.error("Unsupported filter {}", criterion);
311 fail(filt, ObjectiveError.UNSUPPORTED);
312 return;
313 }
314 }
315
Saurav Das0e99e2b2015-10-28 12:39:42 -0700316 VlanId assignedVlan = null;
Charles Chane849c192016-01-11 18:28:54 -0800317 if (vidCriterion != null) {
Charles Chand55e84d2016-03-30 17:54:24 -0700318 // Use the VLAN in metadata whenever a metadata is provided
319 if (filt.meta() != null) {
320 assignedVlan = readVlanFromTreatment(filt.meta());
321 // Use the VLAN in criterion if metadata is not present and the traffic is tagged
322 } else if (!vidCriterion.vlanId().equals(VlanId.NONE)) {
Charles Chane849c192016-01-11 18:28:54 -0800323 assignedVlan = vidCriterion.vlanId();
Charles Chand55e84d2016-03-30 17:54:24 -0700324 }
Charles Chane849c192016-01-11 18:28:54 -0800325
Charles Chand55e84d2016-03-30 17:54:24 -0700326 if (assignedVlan == null) {
327 log.error("Driver fails to extract VLAN information. "
328 + "Not proccessing VLAN filters on device {}.", deviceId);
329 log.debug("VLAN ID in criterion={}, metadata={}",
330 readVlanFromTreatment(filt.meta()), vidCriterion.vlanId());
331 fail(filt, ObjectiveError.BADPARAMS);
332 return;
Saurav Das0e99e2b2015-10-28 12:39:42 -0700333 }
334 }
335
Charles Chane849c192016-01-11 18:28:54 -0800336 if (ethCriterion == null || ethCriterion.mac().equals(MacAddress.NONE)) {
Charles Chan985b12e2016-05-11 19:47:22 -0700337 // NOTE: it is possible that a filtering objective only has vidCriterion
338 log.debug("filtering objective missing dstMac, cannot program TMAC table");
Saurav Das822c4e22015-10-23 10:51:11 -0700339 } else {
340 for (FlowRule tmacRule : processEthDstFilter(portCriterion, ethCriterion,
Saurav Das0e99e2b2015-10-28 12:39:42 -0700341 vidCriterion, assignedVlan,
342 applicationId)) {
Saurav Das822c4e22015-10-23 10:51:11 -0700343 log.debug("adding MAC filtering rules in TMAC table: {} for dev: {}",
344 tmacRule, deviceId);
345 ops = install ? ops.add(tmacRule) : ops.remove(tmacRule);
346 }
347 }
348
Charles Chan985b12e2016-05-11 19:47:22 -0700349 if (vidCriterion == null) {
350 // NOTE: it is possible that a filtering objective only has ethCriterion
351 log.debug("filtering objective missing dstMac or VLAN, "
Charles Chane849c192016-01-11 18:28:54 -0800352 + "cannot program VLAN Table");
Saurav Das822c4e22015-10-23 10:51:11 -0700353 } else {
Charles Chan14967c22015-12-07 11:11:50 -0800354 /*
355 * NOTE: Separate vlan filtering rules and assignment rules
356 * into different stage in order to guarantee that filtering rules
Saurav Das52025962016-01-28 22:30:01 -0800357 * always go first, as required by ofdpa.
Charles Chan14967c22015-12-07 11:11:50 -0800358 */
359 List<FlowRule> allRules = processVlanIdFilter(
360 portCriterion, vidCriterion, assignedVlan, applicationId);
361 List<FlowRule> filteringRules = new ArrayList<>();
362 List<FlowRule> assignmentRules = new ArrayList<>();
363
364 allRules.forEach(flowRule -> {
365 ExtensionCriterion extCriterion =
366 (ExtensionCriterion) flowRule.selector().getCriterion(Criterion.Type.EXTENSION);
367 VlanId vlanId = ((OfdpaMatchVlanVid) extCriterion.extensionSelector()).vlanId();
Charles Chanbe8aea42016-02-24 12:04:47 -0800368 if (!vlanId.equals(VlanId.NONE)) {
Charles Chan14967c22015-12-07 11:11:50 -0800369 filteringRules.add(flowRule);
370 } else {
371 assignmentRules.add(flowRule);
372 }
373 });
374
375 for (FlowRule filteringRule : filteringRules) {
Saurav Das822c4e22015-10-23 10:51:11 -0700376 log.debug("adding VLAN filtering rule in VLAN table: {} for dev: {}",
Charles Chan14967c22015-12-07 11:11:50 -0800377 filteringRule, deviceId);
378 ops = install ? ops.add(filteringRule) : ops.remove(filteringRule);
379 }
380
381 ops.newStage();
382
383 for (FlowRule assignmentRule : assignmentRules) {
384 log.debug("adding VLAN assignment rule in VLAN table: {} for dev: {}",
385 assignmentRule, deviceId);
386 ops = install ? ops.add(assignmentRule) : ops.remove(assignmentRule);
Saurav Das822c4e22015-10-23 10:51:11 -0700387 }
388 }
389
390 for (IPCriterion ipaddr : ips) {
391 // since we ignore port information for IP rules, and the same (gateway) IP
392 // can be configured on multiple ports, we make sure that we send
393 // only a single rule to the switch.
394 if (!sentIpFilters.contains(ipaddr)) {
395 sentIpFilters.add(ipaddr);
396 log.debug("adding IP filtering rules in ACL table {} for dev: {}",
397 ipaddr, deviceId);
398 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
399 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
400 selector.matchEthType(Ethernet.TYPE_IPV4);
401 selector.matchIPDst(ipaddr.ip());
402 treatment.setOutput(PortNumber.CONTROLLER);
403 FlowRule rule = DefaultFlowRule.builder()
404 .forDevice(deviceId)
405 .withSelector(selector.build())
406 .withTreatment(treatment.build())
407 .withPriority(HIGHEST_PRIORITY)
408 .fromApp(applicationId)
409 .makePermanent()
410 .forTable(ACL_TABLE).build();
411 ops = install ? ops.add(rule) : ops.remove(rule);
412 }
413 }
414
415 // apply filtering flow rules
416 flowRuleService.apply(ops.build(new FlowRuleOperationsContext() {
417 @Override
418 public void onSuccess(FlowRuleOperations ops) {
419 log.info("Applied {} filtering rules in device {}",
420 ops.stages().get(0).size(), deviceId);
421 pass(filt);
422 }
423
424 @Override
425 public void onError(FlowRuleOperations ops) {
426 log.info("Failed to apply all filtering rules in dev {}", deviceId);
427 fail(filt, ObjectiveError.FLOWINSTALLATIONFAILED);
428 }
429 }));
430
431 }
432
433 /**
434 * Allows untagged packets into pipeline by assigning a vlan id.
Saurav Das0e99e2b2015-10-28 12:39:42 -0700435 * Vlan assignment is done by the application.
Saurav Das822c4e22015-10-23 10:51:11 -0700436 * Allows tagged packets into pipeline as per configured port-vlan info.
Saurav Das0e99e2b2015-10-28 12:39:42 -0700437 *
Saurav Das822c4e22015-10-23 10:51:11 -0700438 * @param portCriterion port on device for which this filter is programmed
439 * @param vidCriterion vlan assigned to port, or NONE for untagged
Saurav Das0e99e2b2015-10-28 12:39:42 -0700440 * @param assignedVlan assigned vlan-id for untagged packets
Saurav Das822c4e22015-10-23 10:51:11 -0700441 * @param applicationId for application programming this filter
442 * @return list of FlowRule for port-vlan filters
443 */
444 protected List<FlowRule> processVlanIdFilter(PortCriterion portCriterion,
445 VlanIdCriterion vidCriterion,
Saurav Das0e99e2b2015-10-28 12:39:42 -0700446 VlanId assignedVlan,
Saurav Das822c4e22015-10-23 10:51:11 -0700447 ApplicationId applicationId) {
Charles Chan14967c22015-12-07 11:11:50 -0800448 List<FlowRule> rules = new ArrayList<>();
Saurav Das822c4e22015-10-23 10:51:11 -0700449 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
450 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
Charles Chan14967c22015-12-07 11:11:50 -0800451 TrafficSelector.Builder preSelector = null;
452 TrafficTreatment.Builder preTreatment = null;
453
Saurav Das4f980082015-11-05 13:39:15 -0800454 treatment.transition(TMAC_TABLE);
455
Saurav Das822c4e22015-10-23 10:51:11 -0700456 if (vidCriterion.vlanId() == VlanId.NONE) {
457 // untagged packets are assigned vlans
Charles Chanbe8aea42016-02-24 12:04:47 -0800458 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(VlanId.NONE);
Charles Chan14967c22015-12-07 11:11:50 -0800459 selector.extension(ofdpaMatchVlanVid, deviceId);
460 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(assignedVlan);
461 treatment.extension(ofdpaSetVlanVid, deviceId);
Charles Chan14967c22015-12-07 11:11:50 -0800462
463 preSelector = DefaultTrafficSelector.builder();
464 OfdpaMatchVlanVid preOfdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
465 preSelector.extension(preOfdpaMatchVlanVid, deviceId);
466 preTreatment = DefaultTrafficTreatment.builder().transition(TMAC_TABLE);
467
Saurav Das4f980082015-11-05 13:39:15 -0800468 } else {
Charles Chan14967c22015-12-07 11:11:50 -0800469 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vidCriterion.vlanId());
470 selector.extension(ofdpaMatchVlanVid, deviceId);
Charles Chand55e84d2016-03-30 17:54:24 -0700471
472 if (!assignedVlan.equals(vidCriterion.vlanId())) {
473 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(assignedVlan);
474 treatment.extension(ofdpaSetVlanVid, deviceId);
475 }
Saurav Das822c4e22015-10-23 10:51:11 -0700476 }
Saurav Das822c4e22015-10-23 10:51:11 -0700477
478 // ofdpa cannot match on ALL portnumber, so we need to use separate
479 // rules for each port.
Charles Chan14967c22015-12-07 11:11:50 -0800480 List<PortNumber> portnums = new ArrayList<>();
Saurav Das822c4e22015-10-23 10:51:11 -0700481 if (portCriterion.port() == PortNumber.ALL) {
482 for (Port port : deviceService.getPorts(deviceId)) {
483 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
484 portnums.add(port.number());
485 }
486 }
487 } else {
488 portnums.add(portCriterion.port());
489 }
Saurav Das4f980082015-11-05 13:39:15 -0800490
Saurav Das822c4e22015-10-23 10:51:11 -0700491 for (PortNumber pnum : portnums) {
Saurav Das4f980082015-11-05 13:39:15 -0800492 // create rest of flowrule
Saurav Das822c4e22015-10-23 10:51:11 -0700493 selector.matchInPort(pnum);
494 FlowRule rule = DefaultFlowRule.builder()
495 .forDevice(deviceId)
496 .withSelector(selector.build())
497 .withTreatment(treatment.build())
498 .withPriority(DEFAULT_PRIORITY)
499 .fromApp(applicationId)
500 .makePermanent()
501 .forTable(VLAN_TABLE).build();
Charles Chan14967c22015-12-07 11:11:50 -0800502
503 if (preSelector != null) {
504 preSelector.matchInPort(pnum);
505 FlowRule preRule = DefaultFlowRule.builder()
506 .forDevice(deviceId)
507 .withSelector(preSelector.build())
508 .withTreatment(preTreatment.build())
509 .withPriority(DEFAULT_PRIORITY)
510 .fromApp(applicationId)
511 .makePermanent()
512 .forTable(VLAN_TABLE).build();
513 rules.add(preRule);
514 }
515
Saurav Das822c4e22015-10-23 10:51:11 -0700516 rules.add(rule);
517 }
518 return rules;
519 }
520
521 /**
522 * Allows routed packets with correct destination MAC to be directed
523 * to unicast-IP routing table or MPLS forwarding table.
Saurav Das822c4e22015-10-23 10:51:11 -0700524 *
525 * @param portCriterion port on device for which this filter is programmed
526 * @param ethCriterion dstMac of device for which is filter is programmed
527 * @param vidCriterion vlan assigned to port, or NONE for untagged
Saurav Das0e99e2b2015-10-28 12:39:42 -0700528 * @param assignedVlan assigned vlan-id for untagged packets
Saurav Das822c4e22015-10-23 10:51:11 -0700529 * @param applicationId for application programming this filter
530 * @return list of FlowRule for port-vlan filters
531
532 */
533 protected List<FlowRule> processEthDstFilter(PortCriterion portCriterion,
534 EthCriterion ethCriterion,
535 VlanIdCriterion vidCriterion,
Saurav Das0e99e2b2015-10-28 12:39:42 -0700536 VlanId assignedVlan,
Saurav Das822c4e22015-10-23 10:51:11 -0700537 ApplicationId applicationId) {
Charles Chan5270ed02016-01-30 23:22:37 -0800538 // Consider PortNumber.ANY as wildcard. Match ETH_DST only
539 if (portCriterion != null && portCriterion.port() == PortNumber.ANY) {
540 return processEthDstOnlyFilter(ethCriterion, applicationId);
541 }
542
Charles Chan5b9df8d2016-03-28 22:21:40 -0700543 // Multicast MAC
544 if (ethCriterion.mask() != null) {
545 return processMcastEthDstFilter(ethCriterion, applicationId);
546 }
547
Saurav Das822c4e22015-10-23 10:51:11 -0700548 //handling untagged packets via assigned VLAN
549 if (vidCriterion.vlanId() == VlanId.NONE) {
Saurav Das0e99e2b2015-10-28 12:39:42 -0700550 vidCriterion = (VlanIdCriterion) Criteria.matchVlanId(assignedVlan);
Saurav Das822c4e22015-10-23 10:51:11 -0700551 }
552 // ofdpa cannot match on ALL portnumber, so we need to use separate
553 // rules for each port.
554 List<PortNumber> portnums = new ArrayList<PortNumber>();
555 if (portCriterion.port() == PortNumber.ALL) {
556 for (Port port : deviceService.getPorts(deviceId)) {
557 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
558 portnums.add(port.number());
559 }
560 }
561 } else {
562 portnums.add(portCriterion.port());
563 }
564
565 List<FlowRule> rules = new ArrayList<FlowRule>();
566 for (PortNumber pnum : portnums) {
Charles Chan14967c22015-12-07 11:11:50 -0800567 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vidCriterion.vlanId());
Saurav Das822c4e22015-10-23 10:51:11 -0700568 // for unicast IP packets
569 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
570 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
571 selector.matchInPort(pnum);
Charles Chan14967c22015-12-07 11:11:50 -0800572 selector.extension(ofdpaMatchVlanVid, deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700573 selector.matchEthType(Ethernet.TYPE_IPV4);
574 selector.matchEthDst(ethCriterion.mac());
575 treatment.transition(UNICAST_ROUTING_TABLE);
576 FlowRule rule = DefaultFlowRule.builder()
577 .forDevice(deviceId)
578 .withSelector(selector.build())
579 .withTreatment(treatment.build())
580 .withPriority(DEFAULT_PRIORITY)
581 .fromApp(applicationId)
582 .makePermanent()
583 .forTable(TMAC_TABLE).build();
584 rules.add(rule);
585 //for MPLS packets
586 selector = DefaultTrafficSelector.builder();
587 treatment = DefaultTrafficTreatment.builder();
588 selector.matchInPort(pnum);
Charles Chan14967c22015-12-07 11:11:50 -0800589 selector.extension(ofdpaMatchVlanVid, deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700590 selector.matchEthType(Ethernet.MPLS_UNICAST);
591 selector.matchEthDst(ethCriterion.mac());
592 treatment.transition(MPLS_TABLE_0);
593 rule = DefaultFlowRule.builder()
594 .forDevice(deviceId)
595 .withSelector(selector.build())
596 .withTreatment(treatment.build())
597 .withPriority(DEFAULT_PRIORITY)
598 .fromApp(applicationId)
599 .makePermanent()
600 .forTable(TMAC_TABLE).build();
601 rules.add(rule);
602 }
603 return rules;
604 }
605
Charles Chan5270ed02016-01-30 23:22:37 -0800606 protected List<FlowRule> processEthDstOnlyFilter(EthCriterion ethCriterion,
607 ApplicationId applicationId) {
608 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
609 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
610 selector.matchEthType(Ethernet.TYPE_IPV4);
611 selector.matchEthDst(ethCriterion.mac());
612 treatment.transition(UNICAST_ROUTING_TABLE);
613 FlowRule rule = DefaultFlowRule.builder()
614 .forDevice(deviceId)
615 .withSelector(selector.build())
616 .withTreatment(treatment.build())
617 .withPriority(DEFAULT_PRIORITY)
618 .fromApp(applicationId)
619 .makePermanent()
620 .forTable(TMAC_TABLE).build();
621 return ImmutableList.<FlowRule>builder().add(rule).build();
622 }
623
Charles Chan5b9df8d2016-03-28 22:21:40 -0700624 protected List<FlowRule> processMcastEthDstFilter(EthCriterion ethCriterion,
625 ApplicationId applicationId) {
626 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
627 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
628 selector.matchEthType(Ethernet.TYPE_IPV4);
629 selector.matchEthDstMasked(ethCriterion.mac(), ethCriterion.mask());
630 treatment.transition(MULTICAST_ROUTING_TABLE);
631 FlowRule rule = DefaultFlowRule.builder()
632 .forDevice(deviceId)
633 .withSelector(selector.build())
634 .withTreatment(treatment.build())
635 .withPriority(DEFAULT_PRIORITY)
636 .fromApp(applicationId)
637 .makePermanent()
638 .forTable(TMAC_TABLE).build();
639 return ImmutableList.<FlowRule>builder().add(rule).build();
640 }
641
Saurav Das822c4e22015-10-23 10:51:11 -0700642 private Collection<FlowRule> processForward(ForwardingObjective fwd) {
643 switch (fwd.flag()) {
644 case SPECIFIC:
645 return processSpecific(fwd);
646 case VERSATILE:
647 return processVersatile(fwd);
648 default:
649 fail(fwd, ObjectiveError.UNKNOWN);
650 log.warn("Unknown forwarding flag {}", fwd.flag());
651 }
652 return Collections.emptySet();
653 }
654
655 /**
656 * In the OF-DPA 2.0 pipeline, versatile forwarding objectives go to the
657 * ACL table.
658 * @param fwd the forwarding objective of type 'versatile'
659 * @return a collection of flow rules to be sent to the switch. An empty
660 * collection may be returned if there is a problem in processing
661 * the flow rule
662 */
Saurav Das52025962016-01-28 22:30:01 -0800663 protected Collection<FlowRule> processVersatile(ForwardingObjective fwd) {
Saurav Das822c4e22015-10-23 10:51:11 -0700664 log.info("Processing versatile forwarding objective");
Saurav Das822c4e22015-10-23 10:51:11 -0700665
666 EthTypeCriterion ethType =
Saurav Das77b5e902016-01-27 17:01:59 -0800667 (EthTypeCriterion) fwd.selector().getCriterion(Criterion.Type.ETH_TYPE);
Saurav Das822c4e22015-10-23 10:51:11 -0700668 if (ethType == null) {
669 log.error("Versatile forwarding objective must include ethType");
670 fail(fwd, ObjectiveError.BADPARAMS);
671 return Collections.emptySet();
672 }
673 if (fwd.nextId() == null && fwd.treatment() == null) {
674 log.error("Forwarding objective {} from {} must contain "
675 + "nextId or Treatment", fwd.selector(), fwd.appId());
Zsolt Haraszti9faab752016-02-17 15:55:20 -0800676 fail(fwd, ObjectiveError.BADPARAMS);
Saurav Das822c4e22015-10-23 10:51:11 -0700677 return Collections.emptySet();
678 }
Saurav Das49cb5a12016-01-16 22:54:07 -0800679
Saurav Das77b5e902016-01-27 17:01:59 -0800680 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
681 fwd.selector().criteria().forEach(criterion -> {
682 if (criterion instanceof VlanIdCriterion) {
683 VlanId vlanId = ((VlanIdCriterion) criterion).vlanId();
684 // ensure that match does not include vlan = NONE as OF-DPA does not
685 // match untagged packets this way in the ACL table.
686 if (vlanId.equals(VlanId.NONE)) {
687 return;
688 }
689 OfdpaMatchVlanVid ofdpaMatchVlanVid =
690 new OfdpaMatchVlanVid(vlanId);
691 sbuilder.extension(ofdpaMatchVlanVid, deviceId);
692 } else {
693 sbuilder.add(criterion);
694 }
695 });
696
Saurav Das822c4e22015-10-23 10:51:11 -0700697 // XXX driver does not currently do type checking as per Tables 65-67 in
698 // OFDPA 2.0 spec. The only allowed treatment is a punt to the controller.
Saurav Das49cb5a12016-01-16 22:54:07 -0800699 TrafficTreatment.Builder ttBuilder = DefaultTrafficTreatment.builder();
700 if (fwd.treatment() != null) {
701 for (Instruction ins : fwd.treatment().allInstructions()) {
702 if (ins instanceof OutputInstruction) {
703 OutputInstruction o = (OutputInstruction) ins;
704 if (o.port() == PortNumber.CONTROLLER) {
705 ttBuilder.add(o);
706 } else {
707 log.warn("Only allowed treatments in versatile forwarding "
708 + "objectives are punts to the controller");
709 }
710 } else {
711 log.warn("Cannot process instruction in versatile fwd {}", ins);
712 }
Saurav Das822c4e22015-10-23 10:51:11 -0700713 }
714 }
Saurav Das822c4e22015-10-23 10:51:11 -0700715 if (fwd.nextId() != null) {
Saurav Das49cb5a12016-01-16 22:54:07 -0800716 // overide case
717 NextGroup next = getGroupForNextObjective(fwd.nextId());
718 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
719 // we only need the top level group's key to point the flow to it
720 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
721 if (group == null) {
722 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
723 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
724 fail(fwd, ObjectiveError.GROUPMISSING);
725 return Collections.emptySet();
726 }
727 ttBuilder.deferred().group(group.id());
Saurav Das822c4e22015-10-23 10:51:11 -0700728 }
Saurav Das49cb5a12016-01-16 22:54:07 -0800729
730 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
731 .fromApp(fwd.appId())
732 .withPriority(fwd.priority())
733 .forDevice(deviceId)
Saurav Das77b5e902016-01-27 17:01:59 -0800734 .withSelector(sbuilder.build())
Saurav Das49cb5a12016-01-16 22:54:07 -0800735 .withTreatment(ttBuilder.build())
736 .makePermanent()
737 .forTable(ACL_TABLE);
738 return Collections.singletonList(ruleBuilder.build());
Saurav Das822c4e22015-10-23 10:51:11 -0700739 }
740
741 /**
742 * In the OF-DPA 2.0 pipeline, specific forwarding refers to the IP table
Saurav Das8a0732e2015-11-20 15:27:53 -0800743 * (unicast or multicast) or the L2 table (mac + vlan) or the MPLS table.
Saurav Das822c4e22015-10-23 10:51:11 -0700744 *
745 * @param fwd the forwarding objective of type 'specific'
746 * @return a collection of flow rules. Typically there will be only one
747 * for this type of forwarding objective. An empty set may be
748 * returned if there is an issue in processing the objective.
749 */
Saurav Das8a0732e2015-11-20 15:27:53 -0800750 protected Collection<FlowRule> processSpecific(ForwardingObjective fwd) {
Saurav Das4ce45962015-11-24 23:21:05 -0800751 log.trace("Processing specific fwd objective:{} in dev:{} with next:{}",
752 fwd.id(), deviceId, fwd.nextId());
753 boolean isEthTypeObj = isSupportedEthTypeObjective(fwd);
754 boolean isEthDstObj = isSupportedEthDstObjective(fwd);
755
756 if (isEthTypeObj) {
757 return processEthTypeSpecific(fwd);
758 } else if (isEthDstObj) {
759 return processEthDstSpecific(fwd);
760 } else {
761 log.warn("processSpecific: Unsupported forwarding objective "
762 + "criteria fwd:{} in dev:{}", fwd.nextId(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700763 fail(fwd, ObjectiveError.UNSUPPORTED);
764 return Collections.emptySet();
765 }
Saurav Das4ce45962015-11-24 23:21:05 -0800766 }
767
768 private boolean isSupportedEthTypeObjective(ForwardingObjective fwd) {
769 TrafficSelector selector = fwd.selector();
770 EthTypeCriterion ethType = (EthTypeCriterion) selector
771 .getCriterion(Criterion.Type.ETH_TYPE);
Charles Chan188ebf52015-12-23 00:15:11 -0800772 return !((ethType == null) ||
Saurav Das4ce45962015-11-24 23:21:05 -0800773 ((ethType.ethType().toShort() != Ethernet.TYPE_IPV4) &&
Charles Chan188ebf52015-12-23 00:15:11 -0800774 (ethType.ethType().toShort() != Ethernet.MPLS_UNICAST)));
Saurav Das4ce45962015-11-24 23:21:05 -0800775 }
776
777 private boolean isSupportedEthDstObjective(ForwardingObjective fwd) {
778 TrafficSelector selector = fwd.selector();
779 EthCriterion ethDst = (EthCriterion) selector
780 .getCriterion(Criterion.Type.ETH_DST);
781 VlanIdCriterion vlanId = (VlanIdCriterion) selector
782 .getCriterion(Criterion.Type.VLAN_VID);
Charles Chan188ebf52015-12-23 00:15:11 -0800783 return !(ethDst == null && vlanId == null);
Saurav Das4ce45962015-11-24 23:21:05 -0800784 }
785
786 /**
787 * Handles forwarding rules to the IP and MPLS tables.
788 *
789 * @param fwd the forwarding objective
790 * @return A collection of flow rules, or an empty set
791 */
792 protected Collection<FlowRule> processEthTypeSpecific(ForwardingObjective fwd) {
793 TrafficSelector selector = fwd.selector();
794 EthTypeCriterion ethType =
795 (EthTypeCriterion) selector.getCriterion(Criterion.Type.ETH_TYPE);
Flavio Castroe10fa242016-01-15 12:43:51 -0800796 boolean defaultRule = false;
797 boolean popMpls = false;
Charles Chan188ebf52015-12-23 00:15:11 -0800798 int forTableId;
Saurav Das8a0732e2015-11-20 15:27:53 -0800799 TrafficSelector.Builder filteredSelector = DefaultTrafficSelector.builder();
Charles Chan14967c22015-12-07 11:11:50 -0800800 TrafficTreatment.Builder tb = DefaultTrafficTreatment.builder();
Flavio Castroe10fa242016-01-15 12:43:51 -0800801 TrafficSelector.Builder complementarySelector = DefaultTrafficSelector.builder();
Charles Chan14967c22015-12-07 11:11:50 -0800802
Flavio Castroe10fa242016-01-15 12:43:51 -0800803 /*
804 * NOTE: The switch does not support matching 0.0.0.0/0.
805 * Split it into 0.0.0.0/1 and 128.0.0.0/1
806 */
Saurav Das8a0732e2015-11-20 15:27:53 -0800807 if (ethType.ethType().toShort() == Ethernet.TYPE_IPV4) {
Flavio Castroe10fa242016-01-15 12:43:51 -0800808 IpPrefix ipv4Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV4_DST)).ip();
Charles Chan5b9df8d2016-03-28 22:21:40 -0700809 if (ipv4Dst.isMulticast()) {
810 if (ipv4Dst.prefixLength() != 32) {
811 log.warn("Multicast specific forwarding objective can only be /32");
812 fail(fwd, ObjectiveError.BADPARAMS);
813 return ImmutableSet.of();
814 }
815 VlanId assignedVlan = readVlanFromSelector(fwd.meta());
816 if (assignedVlan == null) {
817 log.warn("VLAN ID required by multicast specific fwd obj is missing. Abort.");
818 fail(fwd, ObjectiveError.BADPARAMS);
819 return ImmutableSet.of();
820 }
821 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
822 filteredSelector.extension(ofdpaMatchVlanVid, deviceId);
823 filteredSelector.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipv4Dst);
824 forTableId = MULTICAST_ROUTING_TABLE;
825 log.debug("processing IPv4 multicast specific forwarding objective {} -> next:{}"
826 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
Flavio Castroe10fa242016-01-15 12:43:51 -0800827 } else {
Charles Chan5b9df8d2016-03-28 22:21:40 -0700828 if (ipv4Dst.prefixLength() > 0) {
829 filteredSelector.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipv4Dst);
830 } else {
831 filteredSelector.matchEthType(Ethernet.TYPE_IPV4)
832 .matchIPDst(IpPrefix.valueOf("0.0.0.0/1"));
833 complementarySelector.matchEthType(Ethernet.TYPE_IPV4)
834 .matchIPDst(IpPrefix.valueOf("128.0.0.0/1"));
835 defaultRule = true;
836 }
837 forTableId = UNICAST_ROUTING_TABLE;
838 log.debug("processing IPv4 unicast specific forwarding objective {} -> next:{}"
839 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
Flavio Castroe10fa242016-01-15 12:43:51 -0800840 }
Charles Chan14967c22015-12-07 11:11:50 -0800841
842 if (fwd.treatment() != null) {
843 for (Instruction instr : fwd.treatment().allInstructions()) {
844 if (instr instanceof L3ModificationInstruction &&
845 ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
846 tb.deferred().add(instr);
847 }
848 }
849 }
850
Saurav Das8a0732e2015-11-20 15:27:53 -0800851 } else {
852 filteredSelector
853 .matchEthType(Ethernet.MPLS_UNICAST)
854 .matchMplsLabel(((MplsCriterion)
855 selector.getCriterion(Criterion.Type.MPLS_LABEL)).label());
856 MplsBosCriterion bos = (MplsBosCriterion) selector
857 .getCriterion(Criterion.Type.MPLS_BOS);
858 if (bos != null) {
859 filteredSelector.matchMplsBos(bos.mplsBos());
860 }
861 forTableId = MPLS_TABLE_1;
Saurav Das4ce45962015-11-24 23:21:05 -0800862 log.debug("processing MPLS specific forwarding objective {} -> next:{}"
863 + " in dev {}", fwd.id(), fwd.nextId(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700864
Charles Chan14967c22015-12-07 11:11:50 -0800865 if (fwd.treatment() != null) {
866 for (Instruction instr : fwd.treatment().allInstructions()) {
867 if (instr instanceof L2ModificationInstruction &&
868 ((L2ModificationInstruction) instr).subtype() == L2SubType.MPLS_POP) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800869 popMpls = true;
Charles Chan14967c22015-12-07 11:11:50 -0800870 tb.immediate().add(instr);
871 }
872 if (instr instanceof L3ModificationInstruction &&
873 ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
874 // FIXME Should modify the app to send the correct DEC_MPLS_TTL instruction
875 tb.immediate().decMplsTtl();
876 }
877 if (instr instanceof L3ModificationInstruction &&
878 ((L3ModificationInstruction) instr).subtype() == L3SubType.TTL_IN) {
879 tb.immediate().add(instr);
880 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800881 }
882 }
883 }
Saurav Das822c4e22015-10-23 10:51:11 -0700884
885 if (fwd.nextId() != null) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800886 if (forTableId == MPLS_TABLE_1 && !popMpls) {
887 log.warn("SR CONTINUE case cannot be handled as MPLS ECMP "
888 + "is not implemented in OF-DPA yet. Aborting this flow "
889 + "in this device {}", deviceId);
890 // XXX We could convert to forwarding to a single-port, via a
891 // MPLS interface, or a MPLS SWAP (with-same) but that would
892 // have to be handled in the next-objective. Also the pop-mpls
893 // logic used here won't work in non-BoS case.
Saurav Das4ce45962015-11-24 23:21:05 -0800894 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
Saurav Das8a0732e2015-11-20 15:27:53 -0800895 return Collections.emptySet();
896 }
897
Saurav Das423fe2b2015-12-04 10:52:59 -0800898 NextGroup next = getGroupForNextObjective(fwd.nextId());
899 if (next != null) {
900 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
901 // we only need the top level group's key to point the flow to it
902 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
903 if (group == null) {
904 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
905 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
906 fail(fwd, ObjectiveError.GROUPMISSING);
907 return Collections.emptySet();
908 }
909 tb.deferred().group(group.id());
Saurav Das822c4e22015-10-23 10:51:11 -0700910 }
Saurav Das822c4e22015-10-23 10:51:11 -0700911 }
912 tb.transition(ACL_TABLE);
913 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
914 .fromApp(fwd.appId())
915 .withPriority(fwd.priority())
916 .forDevice(deviceId)
Saurav Das8a0732e2015-11-20 15:27:53 -0800917 .withSelector(filteredSelector.build())
918 .withTreatment(tb.build())
919 .forTable(forTableId);
Saurav Das822c4e22015-10-23 10:51:11 -0700920
921 if (fwd.permanent()) {
922 ruleBuilder.makePermanent();
923 } else {
924 ruleBuilder.makeTemporary(fwd.timeout());
925 }
Flavio Castroe10fa242016-01-15 12:43:51 -0800926 Collection<FlowRule> flowRuleCollection = new ArrayList<>();
927 flowRuleCollection.add(ruleBuilder.build());
928 if (defaultRule) {
929 FlowRule.Builder rule = DefaultFlowRule.builder()
930 .fromApp(fwd.appId())
931 .withPriority(fwd.priority())
932 .forDevice(deviceId)
933 .withSelector(complementarySelector.build())
934 .withTreatment(tb.build())
935 .forTable(forTableId);
936 if (fwd.permanent()) {
937 rule.makePermanent();
938 } else {
939 rule.makeTemporary(fwd.timeout());
940 }
941 flowRuleCollection.add(rule.build());
942 log.debug("Default rule 0.0.0.0/0 is being installed two rules");
943 }
Saurav Das822c4e22015-10-23 10:51:11 -0700944
Flavio Castroe10fa242016-01-15 12:43:51 -0800945 return flowRuleCollection;
Saurav Das822c4e22015-10-23 10:51:11 -0700946 }
947
Saurav Das4ce45962015-11-24 23:21:05 -0800948 /**
949 * Handles forwarding rules to the L2 bridging table. Flow actions are not
950 * allowed in the bridging table - instead we use L2 Interface group or
951 * L2 flood group
952 *
953 * @param fwd the forwarding objective
954 * @return A collection of flow rules, or an empty set
955 */
956 protected Collection<FlowRule> processEthDstSpecific(ForwardingObjective fwd) {
957 List<FlowRule> rules = new ArrayList<>();
958
959 // Build filtered selector
960 TrafficSelector selector = fwd.selector();
961 EthCriterion ethCriterion = (EthCriterion) selector
962 .getCriterion(Criterion.Type.ETH_DST);
963 VlanIdCriterion vlanIdCriterion = (VlanIdCriterion) selector
964 .getCriterion(Criterion.Type.VLAN_VID);
965
966 if (vlanIdCriterion == null) {
967 log.warn("Forwarding objective for bridging requires vlan. Not "
968 + "installing fwd:{} in dev:{}", fwd.id(), deviceId);
969 fail(fwd, ObjectiveError.BADPARAMS);
970 return Collections.emptySet();
971 }
972
973 TrafficSelector.Builder filteredSelectorBuilder =
974 DefaultTrafficSelector.builder();
975 // Do not match MacAddress for subnet broadcast entry
976 if (!ethCriterion.mac().equals(MacAddress.NONE)) {
977 filteredSelectorBuilder.matchEthDst(ethCriterion.mac());
978 log.debug("processing L2 forwarding objective:{} -> next:{} in dev:{}",
979 fwd.id(), fwd.nextId(), deviceId);
980 } else {
981 log.debug("processing L2 Broadcast forwarding objective:{} -> next:{} "
982 + "in dev:{} for vlan:{}",
983 fwd.id(), fwd.nextId(), deviceId, vlanIdCriterion.vlanId());
984 }
Charles Chan14967c22015-12-07 11:11:50 -0800985 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vlanIdCriterion.vlanId());
986 filteredSelectorBuilder.extension(ofdpaMatchVlanVid, deviceId);
Saurav Das4ce45962015-11-24 23:21:05 -0800987 TrafficSelector filteredSelector = filteredSelectorBuilder.build();
988
989 if (fwd.treatment() != null) {
990 log.warn("Ignoring traffic treatment in fwd rule {} meant for L2 table"
991 + "for dev:{}. Expecting only nextId", fwd.id(), deviceId);
992 }
993
994 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
995 if (fwd.nextId() != null) {
Saurav Das423fe2b2015-12-04 10:52:59 -0800996 NextGroup next = getGroupForNextObjective(fwd.nextId());
Saurav Das4ce45962015-11-24 23:21:05 -0800997 if (next != null) {
998 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
999 // we only need the top level group's key to point the flow to it
1000 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
1001 if (group != null) {
1002 treatmentBuilder.deferred().group(group.id());
1003 } else {
1004 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
1005 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
1006 fail(fwd, ObjectiveError.GROUPMISSING);
1007 return Collections.emptySet();
1008 }
1009 }
1010 }
1011 treatmentBuilder.immediate().transition(ACL_TABLE);
1012 TrafficTreatment filteredTreatment = treatmentBuilder.build();
1013
1014 // Build bridging table entries
1015 FlowRule.Builder flowRuleBuilder = DefaultFlowRule.builder();
1016 flowRuleBuilder.fromApp(fwd.appId())
1017 .withPriority(fwd.priority())
1018 .forDevice(deviceId)
1019 .withSelector(filteredSelector)
1020 .withTreatment(filteredTreatment)
1021 .forTable(BRIDGING_TABLE);
1022 if (fwd.permanent()) {
1023 flowRuleBuilder.makePermanent();
1024 } else {
1025 flowRuleBuilder.makeTemporary(fwd.timeout());
1026 }
1027 rules.add(flowRuleBuilder.build());
1028 return rules;
1029 }
1030
Saurav Das423fe2b2015-12-04 10:52:59 -08001031 protected NextGroup getGroupForNextObjective(Integer nextId) {
1032 NextGroup next = flowObjectiveStore.getNextGroup(nextId);
1033 if (next != null) {
1034 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1035 if (gkeys != null && !gkeys.isEmpty()) {
1036 return next;
1037 } else {
1038 log.warn("Empty next group found in FlowObjective store for "
1039 + "next-id:{} in dev:{}", nextId, deviceId);
1040 }
1041 } else {
1042 log.warn("next-id {} not found in Flow objective store for dev:{}",
1043 nextId, deviceId);
1044 }
1045 return null;
1046 }
1047
Charles Chan188ebf52015-12-23 00:15:11 -08001048 protected static void pass(Objective obj) {
Sho SHIMIZUef7e2902016-02-12 18:38:29 -08001049 obj.context().ifPresent(context -> context.onSuccess(obj));
Saurav Das822c4e22015-10-23 10:51:11 -07001050 }
1051
Charles Chan188ebf52015-12-23 00:15:11 -08001052 protected static void fail(Objective obj, ObjectiveError error) {
Sho SHIMIZUef7e2902016-02-12 18:38:29 -08001053 obj.context().ifPresent(context -> context.onError(obj, error));
Saurav Das822c4e22015-10-23 10:51:11 -07001054 }
Saurav Das24431192016-03-07 19:13:00 -08001055
Saurav Das24431192016-03-07 19:13:00 -08001056 @Override
1057 public List<String> getNextMappings(NextGroup nextGroup) {
1058 List<String> mappings = new ArrayList<>();
1059 List<Deque<GroupKey>> gkeys = appKryo.deserialize(nextGroup.data());
1060 for (Deque<GroupKey> gkd : gkeys) {
1061 Group lastGroup = null;
Saurav Das8be4e3a2016-03-11 17:19:07 -08001062 StringBuffer gchain = new StringBuffer();
Saurav Das24431192016-03-07 19:13:00 -08001063 for (GroupKey gk : gkd) {
1064 Group g = groupService.getGroup(deviceId, gk);
Saurav Das8be4e3a2016-03-11 17:19:07 -08001065 if (g == null) {
1066 gchain.append(" ERROR").append(" -->");
1067 continue;
1068 }
1069 gchain.append(" 0x").append(Integer.toHexString(g.id().id()))
1070 .append(" -->");
Saurav Das24431192016-03-07 19:13:00 -08001071 lastGroup = g;
1072 }
1073 // add port information for last group in group-chain
1074 for (Instruction i: lastGroup.buckets().buckets().get(0).treatment().allInstructions()) {
1075 if (i instanceof OutputInstruction) {
Saurav Das8be4e3a2016-03-11 17:19:07 -08001076 gchain.append(" port:").append(((OutputInstruction) i).port());
Saurav Das24431192016-03-07 19:13:00 -08001077 }
1078 }
Saurav Das8be4e3a2016-03-11 17:19:07 -08001079 mappings.add(gchain.toString());
Saurav Das24431192016-03-07 19:13:00 -08001080 }
1081 return mappings;
1082 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001083
1084 protected static VlanId readVlanFromSelector(TrafficSelector selector) {
Saurav Das59232cf2016-04-27 18:35:50 -07001085 if (selector == null) {
1086 return null;
1087 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001088 Criterion criterion = selector.getCriterion(Criterion.Type.VLAN_VID);
1089 return (criterion == null)
1090 ? null : ((VlanIdCriterion) criterion).vlanId();
1091 }
1092
1093 protected static IpPrefix readIpDstFromSelector(TrafficSelector selector) {
Saurav Das59232cf2016-04-27 18:35:50 -07001094 if (selector == null) {
1095 return null;
1096 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001097 Criterion criterion = selector.getCriterion(Criterion.Type.IPV4_DST);
1098 return (criterion == null) ? null : ((IPCriterion) criterion).ip();
1099 }
Charles Chand55e84d2016-03-30 17:54:24 -07001100
1101 private static VlanId readVlanFromTreatment(TrafficTreatment treatment) {
Saurav Das59232cf2016-04-27 18:35:50 -07001102 if (treatment == null) {
1103 return null;
1104 }
Charles Chand55e84d2016-03-30 17:54:24 -07001105 for (Instruction i : treatment.allInstructions()) {
1106 if (i instanceof ModVlanIdInstruction) {
1107 return ((ModVlanIdInstruction) i).vlanId();
1108 }
1109 }
1110 return null;
1111 }
Saurav Das822c4e22015-10-23 10:51:11 -07001112}