blob: 9da1e27ab7c960c75fc6a22c023273e155e56f1a [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
Charles Chan5270ed02016-01-30 23:22:37 -080018import com.google.common.collect.ImmutableList;
Charles Chan5b9df8d2016-03-28 22:21:40 -070019import com.google.common.collect.ImmutableSet;
Saurav Das822c4e22015-10-23 10:51:11 -070020import org.onlab.osgi.ServiceDirectory;
Saurav Das822c4e22015-10-23 10:51:11 -070021import org.onlab.packet.Ethernet;
Charles Chan14967c22015-12-07 11:11:50 -080022import org.onlab.packet.IpPrefix;
Saurav Das822c4e22015-10-23 10:51:11 -070023import org.onlab.packet.VlanId;
24import org.onlab.util.KryoNamespace;
25import org.onosproject.core.ApplicationId;
26import org.onosproject.core.CoreService;
Charles Chancad338a2016-09-16 18:03:11 -070027import org.onosproject.driver.extensions.Ofdpa3MplsType;
28import org.onosproject.driver.extensions.Ofdpa3SetMplsType;
Charles Chan14967c22015-12-07 11:11:50 -080029import org.onosproject.driver.extensions.OfdpaMatchVlanVid;
30import org.onosproject.driver.extensions.OfdpaSetVlanVid;
Saurav Das822c4e22015-10-23 10:51:11 -070031import org.onosproject.net.DeviceId;
32import org.onosproject.net.Port;
33import org.onosproject.net.PortNumber;
34import org.onosproject.net.behaviour.NextGroup;
35import org.onosproject.net.behaviour.Pipeliner;
36import org.onosproject.net.behaviour.PipelinerContext;
37import org.onosproject.net.device.DeviceService;
38import org.onosproject.net.driver.AbstractHandlerBehaviour;
39import org.onosproject.net.flow.DefaultFlowRule;
40import org.onosproject.net.flow.DefaultTrafficSelector;
41import org.onosproject.net.flow.DefaultTrafficTreatment;
42import org.onosproject.net.flow.FlowRule;
43import org.onosproject.net.flow.FlowRuleOperations;
44import org.onosproject.net.flow.FlowRuleOperationsContext;
45import org.onosproject.net.flow.FlowRuleService;
46import org.onosproject.net.flow.TrafficSelector;
47import org.onosproject.net.flow.TrafficTreatment;
48import org.onosproject.net.flow.criteria.Criteria;
49import org.onosproject.net.flow.criteria.Criterion;
50import org.onosproject.net.flow.criteria.EthCriterion;
51import org.onosproject.net.flow.criteria.EthTypeCriterion;
Charles Chan14967c22015-12-07 11:11:50 -080052import org.onosproject.net.flow.criteria.ExtensionCriterion;
Saurav Das822c4e22015-10-23 10:51:11 -070053import org.onosproject.net.flow.criteria.IPCriterion;
Pier Ventree0ae7a32016-11-23 09:57:42 -080054import org.onosproject.net.flow.criteria.Icmpv6CodeCriterion;
55import org.onosproject.net.flow.criteria.Icmpv6TypeCriterion;
Saurav Das8a0732e2015-11-20 15:27:53 -080056import org.onosproject.net.flow.criteria.MplsBosCriterion;
57import org.onosproject.net.flow.criteria.MplsCriterion;
Saurav Das822c4e22015-10-23 10:51:11 -070058import org.onosproject.net.flow.criteria.PortCriterion;
59import org.onosproject.net.flow.criteria.VlanIdCriterion;
60import org.onosproject.net.flow.instructions.Instruction;
61import org.onosproject.net.flow.instructions.Instructions.OutputInstruction;
62import org.onosproject.net.flow.instructions.L2ModificationInstruction;
Saurav Das8a0732e2015-11-20 15:27:53 -080063import org.onosproject.net.flow.instructions.L2ModificationInstruction.L2SubType;
Saurav Das822c4e22015-10-23 10:51:11 -070064import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModVlanIdInstruction;
Charles Chan14967c22015-12-07 11:11:50 -080065import org.onosproject.net.flow.instructions.L3ModificationInstruction;
66import org.onosproject.net.flow.instructions.L3ModificationInstruction.L3SubType;
Saurav Das822c4e22015-10-23 10:51:11 -070067import org.onosproject.net.flowobjective.FilteringObjective;
68import org.onosproject.net.flowobjective.FlowObjectiveStore;
69import org.onosproject.net.flowobjective.ForwardingObjective;
70import org.onosproject.net.flowobjective.NextObjective;
71import org.onosproject.net.flowobjective.Objective;
72import org.onosproject.net.flowobjective.ObjectiveError;
Saurav Das822c4e22015-10-23 10:51:11 -070073import org.onosproject.net.group.DefaultGroupKey;
74import org.onosproject.net.group.Group;
Saurav Das822c4e22015-10-23 10:51:11 -070075import org.onosproject.net.group.GroupKey;
Saurav Das822c4e22015-10-23 10:51:11 -070076import org.onosproject.net.group.GroupService;
Saurav Das822c4e22015-10-23 10:51:11 -070077import org.onosproject.store.serializers.KryoNamespaces;
78import org.slf4j.Logger;
79
Pier Ventree0ae7a32016-11-23 09:57:42 -080080import java.util.ArrayDeque;
81import java.util.ArrayList;
82import java.util.Collection;
83import java.util.Collections;
84import java.util.Deque;
85import java.util.List;
86import java.util.Objects;
87import java.util.Set;
88import java.util.concurrent.ConcurrentHashMap;
89import java.util.concurrent.ScheduledExecutorService;
90import java.util.concurrent.TimeUnit;
91
92import static java.util.concurrent.Executors.newScheduledThreadPool;
Yi Tseng3a77b4f2017-02-06 15:02:17 -080093import static org.onlab.packet.MacAddress.BROADCAST;
94import static org.onlab.packet.MacAddress.NONE;
Pier Ventree0ae7a32016-11-23 09:57:42 -080095import static org.onlab.util.Tools.groupedThreads;
96import static org.slf4j.LoggerFactory.getLogger;
Pier Ventre140a8942016-11-02 07:26:38 -070097import static org.onosproject.net.flow.criteria.Criterion.Type.MPLS_BOS;
98import static org.onosproject.net.flowobjective.NextObjective.Type.HASHED;
Pier Ventree0ae7a32016-11-23 09:57:42 -080099
Saurav Das822c4e22015-10-23 10:51:11 -0700100/**
101 * Driver for Broadcom's OF-DPA v2.0 TTP.
102 *
103 */
Charles Chan361154b2016-03-24 10:23:39 -0700104public class Ofdpa2Pipeline extends AbstractHandlerBehaviour implements Pipeliner {
Pier Ventre42287df2016-11-09 14:17:26 -0800105
Saurav Das822c4e22015-10-23 10:51:11 -0700106 protected static final int PORT_TABLE = 0;
107 protected static final int VLAN_TABLE = 10;
Pier Ventre42287df2016-11-09 14:17:26 -0800108 protected static final int VLAN_1_TABLE = 11;
109 protected static final int MPLS_L2_PORT_FLOW_TABLE = 13;
110 protected static final int MPLS_L2_PORT_PCP_TRUST_FLOW_TABLE = 16;
Saurav Das822c4e22015-10-23 10:51:11 -0700111 protected static final int TMAC_TABLE = 20;
112 protected static final int UNICAST_ROUTING_TABLE = 30;
113 protected static final int MULTICAST_ROUTING_TABLE = 40;
114 protected static final int MPLS_TABLE_0 = 23;
115 protected static final int MPLS_TABLE_1 = 24;
Pier Ventre140a8942016-11-02 07:26:38 -0700116 protected static final int MPLS_L3_TYPE_TABLE = 27;
117 protected static final int MPLS_TYPE_TABLE = 29;
Saurav Das822c4e22015-10-23 10:51:11 -0700118 protected static final int BRIDGING_TABLE = 50;
119 protected static final int ACL_TABLE = 60;
120 protected static final int MAC_LEARNING_TABLE = 254;
121 protected static final long OFPP_MAX = 0xffffff00L;
122
Saurav Das52025962016-01-28 22:30:01 -0800123 protected static final int HIGHEST_PRIORITY = 0xffff;
Saurav Das2857f382015-11-03 14:39:27 -0800124 protected static final int DEFAULT_PRIORITY = 0x8000;
Saurav Das822c4e22015-10-23 10:51:11 -0700125 protected static final int LOWEST_PRIORITY = 0x0;
126
Pier Ventre42287df2016-11-09 14:17:26 -0800127 protected static final int MPLS_L2_PORT_PRIORITY = 2;
128
129 protected static final int MPLS_TUNNEL_ID_BASE = 0x10000;
130 protected static final int MPLS_TUNNEL_ID_MAX = 0x1FFFF;
131
Pier Ventre70d53ba2016-11-17 22:26:29 -0800132 protected static final int MPLS_UNI_PORT_MAX = 0x0000FFFF;
133
134 protected static final int MPLS_NNI_PORT_BASE = 0x00020000;
135 protected static final int MPLS_NNI_PORT_MAX = 0x0002FFFF;
136
Saurav Das822c4e22015-10-23 10:51:11 -0700137 private final Logger log = getLogger(getClass());
Charles Chan425854b2016-04-11 15:32:12 -0700138 protected ServiceDirectory serviceDirectory;
Saurav Das822c4e22015-10-23 10:51:11 -0700139 protected FlowRuleService flowRuleService;
Charles Chan425854b2016-04-11 15:32:12 -0700140 protected CoreService coreService;
Saurav Das8a0732e2015-11-20 15:27:53 -0800141 protected GroupService groupService;
142 protected FlowObjectiveStore flowObjectiveStore;
Saurav Das822c4e22015-10-23 10:51:11 -0700143 protected DeviceId deviceId;
144 protected ApplicationId driverId;
Saurav Das822c4e22015-10-23 10:51:11 -0700145 protected DeviceService deviceService;
Charles Chan188ebf52015-12-23 00:15:11 -0800146 protected static KryoNamespace appKryo = new KryoNamespace.Builder()
Saurav Das822c4e22015-10-23 10:51:11 -0700147 .register(KryoNamespaces.API)
148 .register(GroupKey.class)
149 .register(DefaultGroupKey.class)
Charles Chan361154b2016-03-24 10:23:39 -0700150 .register(Ofdpa2GroupHandler.OfdpaNextGroup.class)
Saurav Das8a0732e2015-11-20 15:27:53 -0800151 .register(ArrayDeque.class)
Charles Chaneefdedf2016-05-23 16:45:45 -0700152 .build("Ofdpa2Pipeline");
Saurav Das822c4e22015-10-23 10:51:11 -0700153
Charles Chan425854b2016-04-11 15:32:12 -0700154 protected Ofdpa2GroupHandler groupHandler;
Saurav Das822c4e22015-10-23 10:51:11 -0700155
Saurav Das52025962016-01-28 22:30:01 -0800156 protected Set<IPCriterion> sentIpFilters = Collections.newSetFromMap(
Charles Chan188ebf52015-12-23 00:15:11 -0800157 new ConcurrentHashMap<>());
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700158 // flows installations to be retried
159 protected ScheduledExecutorService executorService
160 = newScheduledThreadPool(5, groupedThreads("OfdpaPipeliner", "retry-%d", log));
161 protected static final int MAX_RETRY_ATTEMPTS = 10;
162 protected static final int RETRY_MS = 1000;
Saurav Das4f980082015-11-05 13:39:15 -0800163
Saurav Das822c4e22015-10-23 10:51:11 -0700164 @Override
165 public void init(DeviceId deviceId, PipelinerContext context) {
Saurav Das822c4e22015-10-23 10:51:11 -0700166 this.deviceId = deviceId;
167
Charles Chan425854b2016-04-11 15:32:12 -0700168 serviceDirectory = context.directory();
Saurav Das822c4e22015-10-23 10:51:11 -0700169 coreService = serviceDirectory.get(CoreService.class);
170 flowRuleService = serviceDirectory.get(FlowRuleService.class);
171 groupService = serviceDirectory.get(GroupService.class);
172 flowObjectiveStore = context.store();
Saurav Das822c4e22015-10-23 10:51:11 -0700173 deviceService = serviceDirectory.get(DeviceService.class);
Saurav Das822c4e22015-10-23 10:51:11 -0700174
Charles Chan40132b32017-01-22 00:19:37 -0800175 initDriverId();
176 initGroupHander(context);
Saurav Das822c4e22015-10-23 10:51:11 -0700177
Saurav Das822c4e22015-10-23 10:51:11 -0700178 initializePipeline();
Saurav Das822c4e22015-10-23 10:51:11 -0700179 }
180
Charles Chan40132b32017-01-22 00:19:37 -0800181 protected void initDriverId() {
182 driverId = coreService.registerApplication(
183 "org.onosproject.driver.Ofdpa2Pipeline");
184 }
185
186 protected void initGroupHander(PipelinerContext context) {
187 groupHandler = new Ofdpa2GroupHandler();
188 groupHandler.init(deviceId, context);
189 }
190
Saurav Das822c4e22015-10-23 10:51:11 -0700191 protected void initializePipeline() {
Charles Chan188ebf52015-12-23 00:15:11 -0800192 // OF-DPA does not require initializing the pipeline as it puts default
193 // rules automatically in the hardware. However emulation of OFDPA in
194 // software switches does require table-miss-entries.
Saurav Das822c4e22015-10-23 10:51:11 -0700195 }
196
197 //////////////////////////////////////
198 // Flow Objectives
199 //////////////////////////////////////
200
201 @Override
202 public void filter(FilteringObjective filteringObjective) {
203 if (filteringObjective.type() == FilteringObjective.Type.PERMIT) {
204 processFilter(filteringObjective,
205 filteringObjective.op() == Objective.Operation.ADD,
206 filteringObjective.appId());
207 } else {
208 // Note that packets that don't match the PERMIT filter are
209 // automatically denied. The DENY filter is used to deny packets
210 // that are otherwise permitted by the PERMIT filter.
211 // Use ACL table flow rules here for DENY filtering objectives
212 log.debug("filter objective other than PERMIT currently not supported");
213 fail(filteringObjective, ObjectiveError.UNSUPPORTED);
214 }
215 }
216
217 @Override
218 public void forward(ForwardingObjective fwd) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700219 Collection<FlowRule> rules = processForward(fwd);
Saurav Das25190812016-05-27 13:54:07 -0700220 if (rules == null || rules.isEmpty()) {
221 // Assumes fail message has already been generated to the objective
222 // context. Returning here prevents spurious pass message to be
223 // generated by FlowRule service for empty flowOps.
224 return;
225 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700226 sendForward(fwd, rules);
227 }
228
229 protected void sendForward(ForwardingObjective fwd, Collection<FlowRule> rules) {
230 FlowRuleOperations.Builder flowOpsBuilder = FlowRuleOperations.builder();
Saurav Das822c4e22015-10-23 10:51:11 -0700231 switch (fwd.op()) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700232 case ADD:
233 rules.stream()
234 .filter(Objects::nonNull)
235 .forEach(flowOpsBuilder::add);
Saurav Dasd2fded02016-12-02 15:43:47 -0800236 log.debug("Applying a add fwd-obj {} to sw:{}", fwd.id(), deviceId);
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700237 break;
238 case REMOVE:
239 rules.stream()
240 .filter(Objects::nonNull)
241 .forEach(flowOpsBuilder::remove);
Pier Ventre42287df2016-11-09 14:17:26 -0800242 log.debug("Deleting a flow rule to sw:{}", deviceId);
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700243 break;
244 default:
245 fail(fwd, ObjectiveError.UNKNOWN);
246 log.warn("Unknown forwarding type {}", fwd.op());
Saurav Das822c4e22015-10-23 10:51:11 -0700247 }
248
Saurav Das822c4e22015-10-23 10:51:11 -0700249 flowRuleService.apply(flowOpsBuilder.build(new FlowRuleOperationsContext() {
250 @Override
251 public void onSuccess(FlowRuleOperations ops) {
252 pass(fwd);
253 }
254
255 @Override
256 public void onError(FlowRuleOperations ops) {
257 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
258 }
259 }));
Saurav Das822c4e22015-10-23 10:51:11 -0700260 }
261
262 @Override
263 public void next(NextObjective nextObjective) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800264 NextGroup nextGroup = flowObjectiveStore.getNextGroup(nextObjective.id());
265 switch (nextObjective.op()) {
266 case ADD:
Saurav Das4f980082015-11-05 13:39:15 -0800267 if (nextGroup != null) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800268 log.warn("Cannot add next {} that already exists in device {}",
269 nextObjective.id(), deviceId);
270 return;
271 }
272 log.debug("Processing NextObjective id{} in dev{} - add group",
273 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700274 groupHandler.addGroup(nextObjective);
Saurav Das8a0732e2015-11-20 15:27:53 -0800275 break;
276 case ADD_TO_EXISTING:
277 if (nextGroup != null) {
278 log.debug("Processing NextObjective id{} in dev{} - add bucket",
279 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700280 groupHandler.addBucketToGroup(nextObjective, nextGroup);
Saurav Das4f980082015-11-05 13:39:15 -0800281 } else {
Saurav Das8a0732e2015-11-20 15:27:53 -0800282 // it is possible that group-chain has not been fully created yet
Saurav Das423fe2b2015-12-04 10:52:59 -0800283 log.debug("Waiting to add bucket to group for next-id:{} in dev:{}",
284 nextObjective.id(), deviceId);
285 // by design only one pending bucket is allowed for the group
Charles Chan425854b2016-04-11 15:32:12 -0700286 groupHandler.pendingBuckets.put(nextObjective.id(), nextObjective);
Saurav Das4f980082015-11-05 13:39:15 -0800287 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800288 break;
289 case REMOVE:
290 if (nextGroup == null) {
291 log.warn("Cannot remove next {} that does not exist in device {}",
292 nextObjective.id(), deviceId);
293 return;
294 }
295 log.debug("Processing NextObjective id{} in dev{} - remove group",
296 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700297 groupHandler.removeGroup(nextObjective, nextGroup);
Saurav Das8a0732e2015-11-20 15:27:53 -0800298 break;
299 case REMOVE_FROM_EXISTING:
300 if (nextGroup == null) {
301 log.warn("Cannot remove from next {} that does not exist in device {}",
302 nextObjective.id(), deviceId);
303 return;
304 }
305 log.debug("Processing NextObjective id{} in dev{} - remove bucket",
306 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700307 groupHandler.removeBucketFromGroup(nextObjective, nextGroup);
Saurav Das8a0732e2015-11-20 15:27:53 -0800308 break;
309 default:
Saurav Das4f980082015-11-05 13:39:15 -0800310 log.warn("Unsupported operation {}", nextObjective.op());
Saurav Das822c4e22015-10-23 10:51:11 -0700311 }
312 }
313
314 //////////////////////////////////////
315 // Flow handling
316 //////////////////////////////////////
317
318 /**
319 * As per OFDPA 2.0 TTP, filtering of VLAN ids, MAC addresses (for routing)
320 * and IP addresses configured on switch ports happen in different tables.
321 * Note that IP filtering rules need to be added to the ACL table, as there
322 * is no mechanism to send to controller via IP table.
323 *
324 * @param filt the filtering objective
325 * @param install indicates whether to add or remove the objective
326 * @param applicationId the application that sent this objective
327 */
Saurav Das52025962016-01-28 22:30:01 -0800328 protected void processFilter(FilteringObjective filt,
329 boolean install, ApplicationId applicationId) {
Saurav Das822c4e22015-10-23 10:51:11 -0700330 // This driver only processes filtering criteria defined with switch
331 // ports as the key
332 PortCriterion portCriterion = null;
333 EthCriterion ethCriterion = null;
334 VlanIdCriterion vidCriterion = null;
335 Collection<IPCriterion> ips = new ArrayList<IPCriterion>();
336 if (!filt.key().equals(Criteria.dummy()) &&
337 filt.key().type() == Criterion.Type.IN_PORT) {
338 portCriterion = (PortCriterion) filt.key();
339 } else {
340 log.warn("No key defined in filtering objective from app: {}. Not"
341 + "processing filtering objective", applicationId);
Saurav Das59232cf2016-04-27 18:35:50 -0700342 fail(filt, ObjectiveError.BADPARAMS);
Saurav Das822c4e22015-10-23 10:51:11 -0700343 return;
344 }
Saurav Das59232cf2016-04-27 18:35:50 -0700345 log.debug("Received filtering objective for dev/port: {}/{}", deviceId,
346 portCriterion.port());
Saurav Das822c4e22015-10-23 10:51:11 -0700347 // convert filtering conditions for switch-intfs into flowrules
348 FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
349 for (Criterion criterion : filt.conditions()) {
Charles Chan5b9df8d2016-03-28 22:21:40 -0700350 if (criterion.type() == Criterion.Type.ETH_DST ||
351 criterion.type() == Criterion.Type.ETH_DST_MASKED) {
Saurav Das822c4e22015-10-23 10:51:11 -0700352 ethCriterion = (EthCriterion) criterion;
353 } else if (criterion.type() == Criterion.Type.VLAN_VID) {
354 vidCriterion = (VlanIdCriterion) criterion;
355 } else if (criterion.type() == Criterion.Type.IPV4_DST) {
356 ips.add((IPCriterion) criterion);
357 } else {
358 log.error("Unsupported filter {}", criterion);
359 fail(filt, ObjectiveError.UNSUPPORTED);
360 return;
361 }
362 }
363
Saurav Das0e99e2b2015-10-28 12:39:42 -0700364 VlanId assignedVlan = null;
Charles Chane849c192016-01-11 18:28:54 -0800365 if (vidCriterion != null) {
Charles Chand55e84d2016-03-30 17:54:24 -0700366 // Use the VLAN in metadata whenever a metadata is provided
367 if (filt.meta() != null) {
368 assignedVlan = readVlanFromTreatment(filt.meta());
369 // Use the VLAN in criterion if metadata is not present and the traffic is tagged
370 } else if (!vidCriterion.vlanId().equals(VlanId.NONE)) {
Charles Chane849c192016-01-11 18:28:54 -0800371 assignedVlan = vidCriterion.vlanId();
Charles Chand55e84d2016-03-30 17:54:24 -0700372 }
Charles Chane849c192016-01-11 18:28:54 -0800373
Charles Chand55e84d2016-03-30 17:54:24 -0700374 if (assignedVlan == null) {
375 log.error("Driver fails to extract VLAN information. "
376 + "Not proccessing VLAN filters on device {}.", deviceId);
377 log.debug("VLAN ID in criterion={}, metadata={}",
378 readVlanFromTreatment(filt.meta()), vidCriterion.vlanId());
379 fail(filt, ObjectiveError.BADPARAMS);
380 return;
Saurav Das0e99e2b2015-10-28 12:39:42 -0700381 }
382 }
383
Yi Tseng3a77b4f2017-02-06 15:02:17 -0800384 if (ethCriterion == null || ethCriterion.mac().equals(NONE)) {
Charles Chan985b12e2016-05-11 19:47:22 -0700385 // NOTE: it is possible that a filtering objective only has vidCriterion
386 log.debug("filtering objective missing dstMac, cannot program TMAC table");
Saurav Das822c4e22015-10-23 10:51:11 -0700387 } else {
388 for (FlowRule tmacRule : processEthDstFilter(portCriterion, ethCriterion,
Saurav Das0e99e2b2015-10-28 12:39:42 -0700389 vidCriterion, assignedVlan,
390 applicationId)) {
Saurav Das822c4e22015-10-23 10:51:11 -0700391 log.debug("adding MAC filtering rules in TMAC table: {} for dev: {}",
392 tmacRule, deviceId);
393 ops = install ? ops.add(tmacRule) : ops.remove(tmacRule);
394 }
395 }
396
Charles Chan985b12e2016-05-11 19:47:22 -0700397 if (vidCriterion == null) {
398 // NOTE: it is possible that a filtering objective only has ethCriterion
399 log.debug("filtering objective missing dstMac or VLAN, "
Charles Chane849c192016-01-11 18:28:54 -0800400 + "cannot program VLAN Table");
Saurav Das822c4e22015-10-23 10:51:11 -0700401 } else {
Charles Chan14967c22015-12-07 11:11:50 -0800402 /*
403 * NOTE: Separate vlan filtering rules and assignment rules
404 * into different stage in order to guarantee that filtering rules
Saurav Das52025962016-01-28 22:30:01 -0800405 * always go first, as required by ofdpa.
Charles Chan14967c22015-12-07 11:11:50 -0800406 */
407 List<FlowRule> allRules = processVlanIdFilter(
408 portCriterion, vidCriterion, assignedVlan, applicationId);
409 List<FlowRule> filteringRules = new ArrayList<>();
410 List<FlowRule> assignmentRules = new ArrayList<>();
411
412 allRules.forEach(flowRule -> {
413 ExtensionCriterion extCriterion =
414 (ExtensionCriterion) flowRule.selector().getCriterion(Criterion.Type.EXTENSION);
415 VlanId vlanId = ((OfdpaMatchVlanVid) extCriterion.extensionSelector()).vlanId();
Charles Chanbe8aea42016-02-24 12:04:47 -0800416 if (!vlanId.equals(VlanId.NONE)) {
Charles Chan14967c22015-12-07 11:11:50 -0800417 filteringRules.add(flowRule);
418 } else {
419 assignmentRules.add(flowRule);
420 }
421 });
422
423 for (FlowRule filteringRule : filteringRules) {
Saurav Das822c4e22015-10-23 10:51:11 -0700424 log.debug("adding VLAN filtering rule in VLAN table: {} for dev: {}",
Charles Chan14967c22015-12-07 11:11:50 -0800425 filteringRule, deviceId);
426 ops = install ? ops.add(filteringRule) : ops.remove(filteringRule);
427 }
428
429 ops.newStage();
430
431 for (FlowRule assignmentRule : assignmentRules) {
432 log.debug("adding VLAN assignment rule in VLAN table: {} for dev: {}",
433 assignmentRule, deviceId);
434 ops = install ? ops.add(assignmentRule) : ops.remove(assignmentRule);
Saurav Das822c4e22015-10-23 10:51:11 -0700435 }
436 }
437
438 for (IPCriterion ipaddr : ips) {
439 // since we ignore port information for IP rules, and the same (gateway) IP
440 // can be configured on multiple ports, we make sure that we send
441 // only a single rule to the switch.
442 if (!sentIpFilters.contains(ipaddr)) {
443 sentIpFilters.add(ipaddr);
444 log.debug("adding IP filtering rules in ACL table {} for dev: {}",
445 ipaddr, deviceId);
446 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
447 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
448 selector.matchEthType(Ethernet.TYPE_IPV4);
449 selector.matchIPDst(ipaddr.ip());
450 treatment.setOutput(PortNumber.CONTROLLER);
451 FlowRule rule = DefaultFlowRule.builder()
452 .forDevice(deviceId)
453 .withSelector(selector.build())
454 .withTreatment(treatment.build())
455 .withPriority(HIGHEST_PRIORITY)
456 .fromApp(applicationId)
457 .makePermanent()
458 .forTable(ACL_TABLE).build();
459 ops = install ? ops.add(rule) : ops.remove(rule);
460 }
461 }
462
463 // apply filtering flow rules
464 flowRuleService.apply(ops.build(new FlowRuleOperationsContext() {
465 @Override
466 public void onSuccess(FlowRuleOperations ops) {
467 log.info("Applied {} filtering rules in device {}",
468 ops.stages().get(0).size(), deviceId);
469 pass(filt);
470 }
471
472 @Override
473 public void onError(FlowRuleOperations ops) {
474 log.info("Failed to apply all filtering rules in dev {}", deviceId);
475 fail(filt, ObjectiveError.FLOWINSTALLATIONFAILED);
476 }
477 }));
478
479 }
480
481 /**
482 * Allows untagged packets into pipeline by assigning a vlan id.
Saurav Das0e99e2b2015-10-28 12:39:42 -0700483 * Vlan assignment is done by the application.
Saurav Das822c4e22015-10-23 10:51:11 -0700484 * Allows tagged packets into pipeline as per configured port-vlan info.
Saurav Das0e99e2b2015-10-28 12:39:42 -0700485 *
Charles Chanf9e98652016-09-07 16:54:23 -0700486 * @param portCriterion port on device for which this filter is programmed
487 * @param vidCriterion vlan assigned to port, or NONE for untagged
488 * @param assignedVlan assigned vlan-id for untagged packets
489 * @param applicationId for application programming this filter
Saurav Das822c4e22015-10-23 10:51:11 -0700490 * @return list of FlowRule for port-vlan filters
491 */
492 protected List<FlowRule> processVlanIdFilter(PortCriterion portCriterion,
493 VlanIdCriterion vidCriterion,
Saurav Das0e99e2b2015-10-28 12:39:42 -0700494 VlanId assignedVlan,
Saurav Das822c4e22015-10-23 10:51:11 -0700495 ApplicationId applicationId) {
Charles Chanf9e98652016-09-07 16:54:23 -0700496 return processVlanIdFilterInternal(portCriterion, vidCriterion, assignedVlan,
497 applicationId, true);
498 }
499
500 /**
501 * Internal implementation of processVlanIdFilter.
502 * <p>
503 * The is_present bit in set_vlan_vid action is required to be 0 in OFDPA i12.
504 * Since it is non-OF spec, we need an extension treatment for that.
505 * The useSetVlanExtension must be set to false for OFDPA i12.
506 * </p>
507 *
508 * @param portCriterion port on device for which this filter is programmed
509 * @param vidCriterion vlan assigned to port, or NONE for untagged
510 * @param assignedVlan assigned vlan-id for untagged packets
511 * @param applicationId for application programming this filter
512 * @param useSetVlanExtension use the setVlanVid extension that has is_present bit set to 0.
513 * @return list of FlowRule for port-vlan filters
514 */
515 protected List<FlowRule> processVlanIdFilterInternal(PortCriterion portCriterion,
516 VlanIdCriterion vidCriterion,
517 VlanId assignedVlan,
518 ApplicationId applicationId,
519 boolean useSetVlanExtension) {
Charles Chan14967c22015-12-07 11:11:50 -0800520 List<FlowRule> rules = new ArrayList<>();
Saurav Das822c4e22015-10-23 10:51:11 -0700521 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
522 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
Charles Chan14967c22015-12-07 11:11:50 -0800523 TrafficSelector.Builder preSelector = null;
524 TrafficTreatment.Builder preTreatment = null;
525
Saurav Das4f980082015-11-05 13:39:15 -0800526 treatment.transition(TMAC_TABLE);
527
Saurav Das822c4e22015-10-23 10:51:11 -0700528 if (vidCriterion.vlanId() == VlanId.NONE) {
529 // untagged packets are assigned vlans
Charles Chanbe8aea42016-02-24 12:04:47 -0800530 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(VlanId.NONE);
Charles Chan14967c22015-12-07 11:11:50 -0800531 selector.extension(ofdpaMatchVlanVid, deviceId);
Charles Chanf9e98652016-09-07 16:54:23 -0700532 if (useSetVlanExtension) {
533 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(assignedVlan);
534 treatment.extension(ofdpaSetVlanVid, deviceId);
535 } else {
536 treatment.setVlanId(assignedVlan);
537 }
Charles Chan14967c22015-12-07 11:11:50 -0800538
539 preSelector = DefaultTrafficSelector.builder();
540 OfdpaMatchVlanVid preOfdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
541 preSelector.extension(preOfdpaMatchVlanVid, deviceId);
542 preTreatment = DefaultTrafficTreatment.builder().transition(TMAC_TABLE);
543
Saurav Das4f980082015-11-05 13:39:15 -0800544 } else {
Charles Chan14967c22015-12-07 11:11:50 -0800545 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vidCriterion.vlanId());
546 selector.extension(ofdpaMatchVlanVid, deviceId);
Charles Chand55e84d2016-03-30 17:54:24 -0700547
548 if (!assignedVlan.equals(vidCriterion.vlanId())) {
Charles Chanc03782d2017-01-31 13:57:55 -0800549 if (useSetVlanExtension) {
550 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(assignedVlan);
551 treatment.extension(ofdpaSetVlanVid, deviceId);
552 } else {
553 treatment.setVlanId(assignedVlan);
554 }
Charles Chand55e84d2016-03-30 17:54:24 -0700555 }
Saurav Das822c4e22015-10-23 10:51:11 -0700556 }
Saurav Das822c4e22015-10-23 10:51:11 -0700557
558 // ofdpa cannot match on ALL portnumber, so we need to use separate
559 // rules for each port.
Charles Chan14967c22015-12-07 11:11:50 -0800560 List<PortNumber> portnums = new ArrayList<>();
Saurav Das822c4e22015-10-23 10:51:11 -0700561 if (portCriterion.port() == PortNumber.ALL) {
562 for (Port port : deviceService.getPorts(deviceId)) {
563 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
564 portnums.add(port.number());
565 }
566 }
567 } else {
568 portnums.add(portCriterion.port());
569 }
Saurav Das4f980082015-11-05 13:39:15 -0800570
Saurav Das822c4e22015-10-23 10:51:11 -0700571 for (PortNumber pnum : portnums) {
Saurav Das4f980082015-11-05 13:39:15 -0800572 // create rest of flowrule
Saurav Das822c4e22015-10-23 10:51:11 -0700573 selector.matchInPort(pnum);
574 FlowRule rule = DefaultFlowRule.builder()
575 .forDevice(deviceId)
576 .withSelector(selector.build())
577 .withTreatment(treatment.build())
578 .withPriority(DEFAULT_PRIORITY)
579 .fromApp(applicationId)
580 .makePermanent()
581 .forTable(VLAN_TABLE).build();
Charles Chan14967c22015-12-07 11:11:50 -0800582
583 if (preSelector != null) {
584 preSelector.matchInPort(pnum);
585 FlowRule preRule = DefaultFlowRule.builder()
586 .forDevice(deviceId)
587 .withSelector(preSelector.build())
588 .withTreatment(preTreatment.build())
589 .withPriority(DEFAULT_PRIORITY)
590 .fromApp(applicationId)
591 .makePermanent()
592 .forTable(VLAN_TABLE).build();
593 rules.add(preRule);
594 }
595
Saurav Das822c4e22015-10-23 10:51:11 -0700596 rules.add(rule);
597 }
598 return rules;
599 }
600
601 /**
602 * Allows routed packets with correct destination MAC to be directed
603 * to unicast-IP routing table or MPLS forwarding table.
Saurav Das822c4e22015-10-23 10:51:11 -0700604 *
605 * @param portCriterion port on device for which this filter is programmed
606 * @param ethCriterion dstMac of device for which is filter is programmed
607 * @param vidCriterion vlan assigned to port, or NONE for untagged
Saurav Das0e99e2b2015-10-28 12:39:42 -0700608 * @param assignedVlan assigned vlan-id for untagged packets
Saurav Das822c4e22015-10-23 10:51:11 -0700609 * @param applicationId for application programming this filter
610 * @return list of FlowRule for port-vlan filters
611
612 */
613 protected List<FlowRule> processEthDstFilter(PortCriterion portCriterion,
614 EthCriterion ethCriterion,
615 VlanIdCriterion vidCriterion,
Saurav Das0e99e2b2015-10-28 12:39:42 -0700616 VlanId assignedVlan,
Saurav Das822c4e22015-10-23 10:51:11 -0700617 ApplicationId applicationId) {
Charles Chan5270ed02016-01-30 23:22:37 -0800618 // Consider PortNumber.ANY as wildcard. Match ETH_DST only
619 if (portCriterion != null && portCriterion.port() == PortNumber.ANY) {
620 return processEthDstOnlyFilter(ethCriterion, applicationId);
621 }
622
Charles Chan5b9df8d2016-03-28 22:21:40 -0700623 // Multicast MAC
624 if (ethCriterion.mask() != null) {
625 return processMcastEthDstFilter(ethCriterion, applicationId);
626 }
627
Saurav Das822c4e22015-10-23 10:51:11 -0700628 //handling untagged packets via assigned VLAN
629 if (vidCriterion.vlanId() == VlanId.NONE) {
Saurav Das0e99e2b2015-10-28 12:39:42 -0700630 vidCriterion = (VlanIdCriterion) Criteria.matchVlanId(assignedVlan);
Saurav Das822c4e22015-10-23 10:51:11 -0700631 }
632 // ofdpa cannot match on ALL portnumber, so we need to use separate
633 // rules for each port.
634 List<PortNumber> portnums = new ArrayList<PortNumber>();
635 if (portCriterion.port() == PortNumber.ALL) {
636 for (Port port : deviceService.getPorts(deviceId)) {
637 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
638 portnums.add(port.number());
639 }
640 }
641 } else {
642 portnums.add(portCriterion.port());
643 }
644
645 List<FlowRule> rules = new ArrayList<FlowRule>();
646 for (PortNumber pnum : portnums) {
Charles Chan14967c22015-12-07 11:11:50 -0800647 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vidCriterion.vlanId());
Saurav Das822c4e22015-10-23 10:51:11 -0700648 // for unicast IP packets
649 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
650 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
651 selector.matchInPort(pnum);
Charles Chan14967c22015-12-07 11:11:50 -0800652 selector.extension(ofdpaMatchVlanVid, deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700653 selector.matchEthType(Ethernet.TYPE_IPV4);
654 selector.matchEthDst(ethCriterion.mac());
655 treatment.transition(UNICAST_ROUTING_TABLE);
656 FlowRule rule = DefaultFlowRule.builder()
657 .forDevice(deviceId)
658 .withSelector(selector.build())
659 .withTreatment(treatment.build())
660 .withPriority(DEFAULT_PRIORITY)
661 .fromApp(applicationId)
662 .makePermanent()
663 .forTable(TMAC_TABLE).build();
664 rules.add(rule);
665 //for MPLS packets
666 selector = DefaultTrafficSelector.builder();
667 treatment = DefaultTrafficTreatment.builder();
668 selector.matchInPort(pnum);
Charles Chan14967c22015-12-07 11:11:50 -0800669 selector.extension(ofdpaMatchVlanVid, deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700670 selector.matchEthType(Ethernet.MPLS_UNICAST);
671 selector.matchEthDst(ethCriterion.mac());
672 treatment.transition(MPLS_TABLE_0);
673 rule = DefaultFlowRule.builder()
674 .forDevice(deviceId)
675 .withSelector(selector.build())
676 .withTreatment(treatment.build())
677 .withPriority(DEFAULT_PRIORITY)
678 .fromApp(applicationId)
679 .makePermanent()
680 .forTable(TMAC_TABLE).build();
681 rules.add(rule);
Pier Ventree0ae7a32016-11-23 09:57:42 -0800682 /*
683 * TMAC rules for IPv6 packets
684 */
685 selector = DefaultTrafficSelector.builder();
686 treatment = DefaultTrafficTreatment.builder();
687 selector.matchInPort(pnum);
688 selector.extension(ofdpaMatchVlanVid, deviceId);
689 selector.matchEthType(Ethernet.TYPE_IPV6);
690 selector.matchEthDst(ethCriterion.mac());
691 treatment.transition(UNICAST_ROUTING_TABLE);
692 rule = DefaultFlowRule.builder()
693 .forDevice(deviceId)
694 .withSelector(selector.build())
695 .withTreatment(treatment.build())
696 .withPriority(DEFAULT_PRIORITY)
697 .fromApp(applicationId)
698 .makePermanent()
699 .forTable(TMAC_TABLE).build();
700 rules.add(rule);
Saurav Das822c4e22015-10-23 10:51:11 -0700701 }
702 return rules;
703 }
704
Charles Chan5270ed02016-01-30 23:22:37 -0800705 protected List<FlowRule> processEthDstOnlyFilter(EthCriterion ethCriterion,
706 ApplicationId applicationId) {
Pier Luigi0e358632017-01-31 09:35:05 -0800707 ImmutableList.Builder<FlowRule> builder = ImmutableList.builder();
708
Charles Chan5270ed02016-01-30 23:22:37 -0800709 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
710 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
711 selector.matchEthType(Ethernet.TYPE_IPV4);
712 selector.matchEthDst(ethCriterion.mac());
713 treatment.transition(UNICAST_ROUTING_TABLE);
714 FlowRule rule = DefaultFlowRule.builder()
715 .forDevice(deviceId)
716 .withSelector(selector.build())
717 .withTreatment(treatment.build())
718 .withPriority(DEFAULT_PRIORITY)
719 .fromApp(applicationId)
720 .makePermanent()
721 .forTable(TMAC_TABLE).build();
Pier Luigi0e358632017-01-31 09:35:05 -0800722 builder.add(rule);
723
724 selector = DefaultTrafficSelector.builder();
725 treatment = DefaultTrafficTreatment.builder();
726 selector.matchEthType(Ethernet.TYPE_IPV6);
727 selector.matchEthDst(ethCriterion.mac());
728 treatment.transition(UNICAST_ROUTING_TABLE);
729 rule = DefaultFlowRule.builder()
730 .forDevice(deviceId)
731 .withSelector(selector.build())
732 .withTreatment(treatment.build())
733 .withPriority(DEFAULT_PRIORITY)
734 .fromApp(applicationId)
735 .makePermanent()
736 .forTable(TMAC_TABLE).build();
737 return builder.add(rule).build();
Charles Chan5270ed02016-01-30 23:22:37 -0800738 }
739
Charles Chan5b9df8d2016-03-28 22:21:40 -0700740 protected List<FlowRule> processMcastEthDstFilter(EthCriterion ethCriterion,
741 ApplicationId applicationId) {
742 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
743 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
744 selector.matchEthType(Ethernet.TYPE_IPV4);
745 selector.matchEthDstMasked(ethCriterion.mac(), ethCriterion.mask());
746 treatment.transition(MULTICAST_ROUTING_TABLE);
747 FlowRule rule = DefaultFlowRule.builder()
748 .forDevice(deviceId)
749 .withSelector(selector.build())
750 .withTreatment(treatment.build())
751 .withPriority(DEFAULT_PRIORITY)
752 .fromApp(applicationId)
753 .makePermanent()
754 .forTable(TMAC_TABLE).build();
755 return ImmutableList.<FlowRule>builder().add(rule).build();
756 }
757
Saurav Das822c4e22015-10-23 10:51:11 -0700758 private Collection<FlowRule> processForward(ForwardingObjective fwd) {
759 switch (fwd.flag()) {
760 case SPECIFIC:
761 return processSpecific(fwd);
762 case VERSATILE:
763 return processVersatile(fwd);
764 default:
765 fail(fwd, ObjectiveError.UNKNOWN);
766 log.warn("Unknown forwarding flag {}", fwd.flag());
767 }
768 return Collections.emptySet();
769 }
770
771 /**
772 * In the OF-DPA 2.0 pipeline, versatile forwarding objectives go to the
773 * ACL table.
774 * @param fwd the forwarding objective of type 'versatile'
775 * @return a collection of flow rules to be sent to the switch. An empty
776 * collection may be returned if there is a problem in processing
777 * the flow rule
778 */
Saurav Das52025962016-01-28 22:30:01 -0800779 protected Collection<FlowRule> processVersatile(ForwardingObjective fwd) {
Saurav Dasd2fded02016-12-02 15:43:47 -0800780 log.info("Processing versatile forwarding objective:{} in dev:{}",
781 fwd.id(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700782
783 EthTypeCriterion ethType =
Saurav Das77b5e902016-01-27 17:01:59 -0800784 (EthTypeCriterion) fwd.selector().getCriterion(Criterion.Type.ETH_TYPE);
Saurav Das822c4e22015-10-23 10:51:11 -0700785 if (ethType == null) {
Saurav Dasd2fded02016-12-02 15:43:47 -0800786 log.error("Versatile forwarding objective:{} must include ethType",
787 fwd.id());
Saurav Das822c4e22015-10-23 10:51:11 -0700788 fail(fwd, ObjectiveError.BADPARAMS);
789 return Collections.emptySet();
790 }
791 if (fwd.nextId() == null && fwd.treatment() == null) {
792 log.error("Forwarding objective {} from {} must contain "
793 + "nextId or Treatment", fwd.selector(), fwd.appId());
Zsolt Haraszti9faab752016-02-17 15:55:20 -0800794 fail(fwd, ObjectiveError.BADPARAMS);
Saurav Das822c4e22015-10-23 10:51:11 -0700795 return Collections.emptySet();
796 }
Saurav Das49cb5a12016-01-16 22:54:07 -0800797
Saurav Das77b5e902016-01-27 17:01:59 -0800798 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
799 fwd.selector().criteria().forEach(criterion -> {
800 if (criterion instanceof VlanIdCriterion) {
801 VlanId vlanId = ((VlanIdCriterion) criterion).vlanId();
802 // ensure that match does not include vlan = NONE as OF-DPA does not
803 // match untagged packets this way in the ACL table.
804 if (vlanId.equals(VlanId.NONE)) {
805 return;
806 }
807 OfdpaMatchVlanVid ofdpaMatchVlanVid =
808 new OfdpaMatchVlanVid(vlanId);
809 sbuilder.extension(ofdpaMatchVlanVid, deviceId);
Pier Ventree0ae7a32016-11-23 09:57:42 -0800810 } else if (criterion instanceof Icmpv6TypeCriterion ||
811 criterion instanceof Icmpv6CodeCriterion) {
812 /*
813 * We silenty discard these criterions, our current
814 * OFDPA platform does not support these matches on
815 * the ACL table.
816 */
817 log.warn("ICMPv6 Type and ICMPv6 Code are not supported");
Saurav Das77b5e902016-01-27 17:01:59 -0800818 } else {
819 sbuilder.add(criterion);
820 }
821 });
822
Saurav Das822c4e22015-10-23 10:51:11 -0700823 // XXX driver does not currently do type checking as per Tables 65-67 in
824 // OFDPA 2.0 spec. The only allowed treatment is a punt to the controller.
Saurav Das49cb5a12016-01-16 22:54:07 -0800825 TrafficTreatment.Builder ttBuilder = DefaultTrafficTreatment.builder();
826 if (fwd.treatment() != null) {
827 for (Instruction ins : fwd.treatment().allInstructions()) {
828 if (ins instanceof OutputInstruction) {
829 OutputInstruction o = (OutputInstruction) ins;
830 if (o.port() == PortNumber.CONTROLLER) {
831 ttBuilder.add(o);
832 } else {
833 log.warn("Only allowed treatments in versatile forwarding "
834 + "objectives are punts to the controller");
835 }
836 } else {
837 log.warn("Cannot process instruction in versatile fwd {}", ins);
838 }
Saurav Das822c4e22015-10-23 10:51:11 -0700839 }
Charles Chan2df0e8a2017-01-09 11:45:08 -0800840 if (fwd.treatment().clearedDeferred()) {
841 ttBuilder.wipeDeferred();
842 }
Saurav Das822c4e22015-10-23 10:51:11 -0700843 }
Saurav Das822c4e22015-10-23 10:51:11 -0700844 if (fwd.nextId() != null) {
Saurav Das49cb5a12016-01-16 22:54:07 -0800845 // overide case
846 NextGroup next = getGroupForNextObjective(fwd.nextId());
847 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
848 // we only need the top level group's key to point the flow to it
849 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
850 if (group == null) {
851 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
852 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
853 fail(fwd, ObjectiveError.GROUPMISSING);
854 return Collections.emptySet();
855 }
856 ttBuilder.deferred().group(group.id());
Saurav Das822c4e22015-10-23 10:51:11 -0700857 }
Saurav Das49cb5a12016-01-16 22:54:07 -0800858
859 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
860 .fromApp(fwd.appId())
861 .withPriority(fwd.priority())
862 .forDevice(deviceId)
Saurav Das77b5e902016-01-27 17:01:59 -0800863 .withSelector(sbuilder.build())
Saurav Das49cb5a12016-01-16 22:54:07 -0800864 .withTreatment(ttBuilder.build())
865 .makePermanent()
866 .forTable(ACL_TABLE);
867 return Collections.singletonList(ruleBuilder.build());
Saurav Das822c4e22015-10-23 10:51:11 -0700868 }
869
870 /**
871 * In the OF-DPA 2.0 pipeline, specific forwarding refers to the IP table
Saurav Das8a0732e2015-11-20 15:27:53 -0800872 * (unicast or multicast) or the L2 table (mac + vlan) or the MPLS table.
Saurav Das822c4e22015-10-23 10:51:11 -0700873 *
874 * @param fwd the forwarding objective of type 'specific'
875 * @return a collection of flow rules. Typically there will be only one
876 * for this type of forwarding objective. An empty set may be
877 * returned if there is an issue in processing the objective.
878 */
Saurav Das8a0732e2015-11-20 15:27:53 -0800879 protected Collection<FlowRule> processSpecific(ForwardingObjective fwd) {
Saurav Das25190812016-05-27 13:54:07 -0700880 log.debug("Processing specific fwd objective:{} in dev:{} with next:{}",
Saurav Das4ce45962015-11-24 23:21:05 -0800881 fwd.id(), deviceId, fwd.nextId());
882 boolean isEthTypeObj = isSupportedEthTypeObjective(fwd);
883 boolean isEthDstObj = isSupportedEthDstObjective(fwd);
884
885 if (isEthTypeObj) {
886 return processEthTypeSpecific(fwd);
887 } else if (isEthDstObj) {
888 return processEthDstSpecific(fwd);
889 } else {
890 log.warn("processSpecific: Unsupported forwarding objective "
891 + "criteria fwd:{} in dev:{}", fwd.nextId(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700892 fail(fwd, ObjectiveError.UNSUPPORTED);
893 return Collections.emptySet();
894 }
Saurav Das4ce45962015-11-24 23:21:05 -0800895 }
896
Saurav Das4ce45962015-11-24 23:21:05 -0800897 /**
898 * Handles forwarding rules to the IP and MPLS tables.
899 *
900 * @param fwd the forwarding objective
901 * @return A collection of flow rules, or an empty set
902 */
903 protected Collection<FlowRule> processEthTypeSpecific(ForwardingObjective fwd) {
Charles Chancad338a2016-09-16 18:03:11 -0700904 return processEthTypeSpecificInternal(fwd, false, ACL_TABLE);
Charles Chanf9e98652016-09-07 16:54:23 -0700905 }
906
907 /**
Pier Ventree0ae7a32016-11-23 09:57:42 -0800908 * Helper method to build Ipv6 selector using the selector provided by
909 * a forwarding objective.
910 *
911 * @param builderToUpdate the builder to update
912 * @param fwd the selector to read
913 * @return 0 if the update ends correctly. -1 if the matches
914 * are not yet supported
915 */
916 protected int buildIpv6Selector(TrafficSelector.Builder builderToUpdate,
917 ForwardingObjective fwd) {
918
919 TrafficSelector selector = fwd.selector();
920
921 IpPrefix ipv6Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV6_DST)).ip();
922 if (ipv6Dst.isMulticast()) {
923 log.warn("IPv6 Multicast is currently not supported");
924 fail(fwd, ObjectiveError.BADPARAMS);
925 return -1;
Pier Ventree0ae7a32016-11-23 09:57:42 -0800926 }
Pier Luigi69fd4312017-01-28 17:58:58 -0800927 if (ipv6Dst.prefixLength() != 0) {
928 builderToUpdate.matchIPv6Dst(ipv6Dst);
929 }
930 builderToUpdate.matchEthType(Ethernet.TYPE_IPV6);
931 log.debug("processing IPv6 unicast specific forwarding objective {} -> next:{}"
932 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
Pier Ventree0ae7a32016-11-23 09:57:42 -0800933 return 0;
934 }
935
936 /**
Charles Chanf9e98652016-09-07 16:54:23 -0700937 * Internal implementation of processEthTypeSpecific.
938 * <p>
939 * Wildcarded IPv4_DST is not supported in OFDPA i12. Therefore, we break
940 * the rule into 0.0.0.0/1 and 128.0.0.0/1.
941 * The allowDefaultRoute must be set to false for OFDPA i12.
942 * </p>
943 *
944 * @param fwd the forwarding objective
945 * @param allowDefaultRoute allow wildcarded IPv4_DST or not
Ray Milkey0bb1e102016-11-10 14:51:27 -0800946 * @param mplsNextTable next MPLS table
Charles Chanf9e98652016-09-07 16:54:23 -0700947 * @return A collection of flow rules, or an empty set
948 */
949 protected Collection<FlowRule> processEthTypeSpecificInternal(ForwardingObjective fwd,
Charles Chancad338a2016-09-16 18:03:11 -0700950 boolean allowDefaultRoute,
951 int mplsNextTable) {
Saurav Das4ce45962015-11-24 23:21:05 -0800952 TrafficSelector selector = fwd.selector();
953 EthTypeCriterion ethType =
954 (EthTypeCriterion) selector.getCriterion(Criterion.Type.ETH_TYPE);
Flavio Castroe10fa242016-01-15 12:43:51 -0800955 boolean defaultRule = false;
956 boolean popMpls = false;
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700957 boolean emptyGroup = false;
Charles Chan188ebf52015-12-23 00:15:11 -0800958 int forTableId;
Saurav Das8a0732e2015-11-20 15:27:53 -0800959 TrafficSelector.Builder filteredSelector = DefaultTrafficSelector.builder();
Charles Chan14967c22015-12-07 11:11:50 -0800960 TrafficTreatment.Builder tb = DefaultTrafficTreatment.builder();
Flavio Castroe10fa242016-01-15 12:43:51 -0800961 TrafficSelector.Builder complementarySelector = DefaultTrafficSelector.builder();
Charles Chan14967c22015-12-07 11:11:50 -0800962
Saurav Das8a0732e2015-11-20 15:27:53 -0800963 if (ethType.ethType().toShort() == Ethernet.TYPE_IPV4) {
Flavio Castroe10fa242016-01-15 12:43:51 -0800964 IpPrefix ipv4Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV4_DST)).ip();
Charles Chan5b9df8d2016-03-28 22:21:40 -0700965 if (ipv4Dst.isMulticast()) {
966 if (ipv4Dst.prefixLength() != 32) {
967 log.warn("Multicast specific forwarding objective can only be /32");
968 fail(fwd, ObjectiveError.BADPARAMS);
969 return ImmutableSet.of();
970 }
971 VlanId assignedVlan = readVlanFromSelector(fwd.meta());
972 if (assignedVlan == null) {
973 log.warn("VLAN ID required by multicast specific fwd obj is missing. Abort.");
974 fail(fwd, ObjectiveError.BADPARAMS);
975 return ImmutableSet.of();
976 }
977 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
978 filteredSelector.extension(ofdpaMatchVlanVid, deviceId);
979 filteredSelector.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipv4Dst);
980 forTableId = MULTICAST_ROUTING_TABLE;
981 log.debug("processing IPv4 multicast specific forwarding objective {} -> next:{}"
982 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
Flavio Castroe10fa242016-01-15 12:43:51 -0800983 } else {
Charles Chanf9e98652016-09-07 16:54:23 -0700984 if (ipv4Dst.prefixLength() == 0) {
985 if (allowDefaultRoute) {
986 // The entire IPV4_DST field is wildcarded intentionally
987 filteredSelector.matchEthType(Ethernet.TYPE_IPV4);
988 } else {
Pier Ventree0ae7a32016-11-23 09:57:42 -0800989 // NOTE: The switch does not support matching 0.0.0.0/0
990 // Split it into 0.0.0.0/1 and 128.0.0.0/1
Charles Chanf9e98652016-09-07 16:54:23 -0700991 filteredSelector.matchEthType(Ethernet.TYPE_IPV4)
992 .matchIPDst(IpPrefix.valueOf("0.0.0.0/1"));
993 complementarySelector.matchEthType(Ethernet.TYPE_IPV4)
994 .matchIPDst(IpPrefix.valueOf("128.0.0.0/1"));
995 defaultRule = true;
996 }
Charles Chan5b9df8d2016-03-28 22:21:40 -0700997 } else {
Charles Chanf9e98652016-09-07 16:54:23 -0700998 filteredSelector.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipv4Dst);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700999 }
1000 forTableId = UNICAST_ROUTING_TABLE;
1001 log.debug("processing IPv4 unicast specific forwarding objective {} -> next:{}"
1002 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
Flavio Castroe10fa242016-01-15 12:43:51 -08001003 }
Charles Chan14967c22015-12-07 11:11:50 -08001004
1005 if (fwd.treatment() != null) {
1006 for (Instruction instr : fwd.treatment().allInstructions()) {
1007 if (instr instanceof L3ModificationInstruction &&
1008 ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
Saurav Das9c705342017-01-06 11:36:01 -08001009 // XXX decrementing IP ttl is done automatically for routing, this
1010 // action is ignored or rejected in ofdpa as it is not fully implemented
1011 //tb.deferred().add(instr);
Charles Chan14967c22015-12-07 11:11:50 -08001012 }
1013 }
1014 }
1015
Pier Ventree0ae7a32016-11-23 09:57:42 -08001016 } else if (ethType.ethType().toShort() == Ethernet.TYPE_IPV6) {
1017 if (buildIpv6Selector(filteredSelector, fwd) < 0) {
1018 return Collections.emptyList();
1019 }
1020 forTableId = UNICAST_ROUTING_TABLE;
1021 if (fwd.treatment() != null) {
1022 for (Instruction instr : fwd.treatment().allInstructions()) {
1023 if (instr instanceof L3ModificationInstruction &&
1024 ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
1025 // XXX decrementing IP ttl is done automatically for routing, this
1026 // action is ignored or rejected in ofdpa as it is not fully implemented
1027 //tb.deferred().add(instr);
1028 }
1029 }
1030 }
Saurav Das8a0732e2015-11-20 15:27:53 -08001031 } else {
1032 filteredSelector
1033 .matchEthType(Ethernet.MPLS_UNICAST)
1034 .matchMplsLabel(((MplsCriterion)
1035 selector.getCriterion(Criterion.Type.MPLS_LABEL)).label());
1036 MplsBosCriterion bos = (MplsBosCriterion) selector
Pier Ventre140a8942016-11-02 07:26:38 -07001037 .getCriterion(MPLS_BOS);
Saurav Das8a0732e2015-11-20 15:27:53 -08001038 if (bos != null) {
1039 filteredSelector.matchMplsBos(bos.mplsBos());
1040 }
1041 forTableId = MPLS_TABLE_1;
Saurav Das4ce45962015-11-24 23:21:05 -08001042 log.debug("processing MPLS specific forwarding objective {} -> next:{}"
1043 + " in dev {}", fwd.id(), fwd.nextId(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -07001044
Charles Chan14967c22015-12-07 11:11:50 -08001045 if (fwd.treatment() != null) {
1046 for (Instruction instr : fwd.treatment().allInstructions()) {
1047 if (instr instanceof L2ModificationInstruction &&
1048 ((L2ModificationInstruction) instr).subtype() == L2SubType.MPLS_POP) {
Saurav Das8a0732e2015-11-20 15:27:53 -08001049 popMpls = true;
Saurav Das9c705342017-01-06 11:36:01 -08001050 // OF-DPA does not pop in MPLS table. Instead it requires
1051 // setting the MPLS_TYPE so pop can happen down the pipeline
Charles Chan14967c22015-12-07 11:11:50 -08001052 }
1053 if (instr instanceof L3ModificationInstruction &&
1054 ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
1055 // FIXME Should modify the app to send the correct DEC_MPLS_TTL instruction
1056 tb.immediate().decMplsTtl();
1057 }
1058 if (instr instanceof L3ModificationInstruction &&
1059 ((L3ModificationInstruction) instr).subtype() == L3SubType.TTL_IN) {
1060 tb.immediate().add(instr);
1061 }
Saurav Das8a0732e2015-11-20 15:27:53 -08001062 }
1063 }
1064 }
Saurav Das822c4e22015-10-23 10:51:11 -07001065
1066 if (fwd.nextId() != null) {
Saurav Das8a0732e2015-11-20 15:27:53 -08001067 if (forTableId == MPLS_TABLE_1 && !popMpls) {
1068 log.warn("SR CONTINUE case cannot be handled as MPLS ECMP "
Saurav Das25190812016-05-27 13:54:07 -07001069 + "is not implemented in OF-DPA yet. Aborting this flow {} -> next:{}"
1070 + "in this device {}", fwd.id(), fwd.nextId(), deviceId);
Pier Ventree0ae7a32016-11-23 09:57:42 -08001071 // XXX We could convert to forwarding to a single-port, via a MPLS interface,
1072 // or a MPLS SWAP (with-same) but that would have to be handled in the next-objective.
1073 // Also the pop-mpls logic used here won't work in non-BoS case.
Saurav Das4ce45962015-11-24 23:21:05 -08001074 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
Saurav Das8a0732e2015-11-20 15:27:53 -08001075 return Collections.emptySet();
1076 }
1077
Saurav Das423fe2b2015-12-04 10:52:59 -08001078 NextGroup next = getGroupForNextObjective(fwd.nextId());
1079 if (next != null) {
1080 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1081 // we only need the top level group's key to point the flow to it
1082 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
Pier Ventre140a8942016-11-02 07:26:38 -07001083 if (isNotMplsBos(selector) && group.type().equals(HASHED)) {
1084 log.warn("SR CONTINUE case cannot be handled as MPLS ECMP "
1085 + "is not implemented in OF-DPA yet. Aborting this flow {} -> next:{}"
1086 + "in this device {}", fwd.id(), fwd.nextId(), deviceId);
1087 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
1088 return Collections.emptySet();
1089 }
Saurav Das423fe2b2015-12-04 10:52:59 -08001090 if (group == null) {
1091 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
1092 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
1093 fail(fwd, ObjectiveError.GROUPMISSING);
1094 return Collections.emptySet();
1095 }
1096 tb.deferred().group(group.id());
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001097 // check if group is empty
1098 if (gkeys.size() == 1 && gkeys.get(0).size() == 1) {
1099 log.warn("Found empty group 0x{} in dev:{} .. will retry fwd:{}",
1100 Integer.toHexString(group.id().id()), deviceId, fwd.id());
1101 emptyGroup = true;
1102 }
Saurav Das25190812016-05-27 13:54:07 -07001103 } else {
1104 log.warn("Cannot find group for nextId:{} in dev:{}. Aborting fwd:{}",
1105 fwd.nextId(), deviceId, fwd.id());
1106 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
1107 return Collections.emptySet();
Saurav Das822c4e22015-10-23 10:51:11 -07001108 }
Saurav Das822c4e22015-10-23 10:51:11 -07001109 }
Charles Chancad338a2016-09-16 18:03:11 -07001110
1111 if (forTableId == MPLS_TABLE_1) {
Pier Ventre140a8942016-11-02 07:26:38 -07001112 if (mplsNextTable == MPLS_L3_TYPE_TABLE) {
Charles Chancad338a2016-09-16 18:03:11 -07001113 Ofdpa3SetMplsType setMplsType = new Ofdpa3SetMplsType(Ofdpa3MplsType.L3_PHP);
Saurav Das9c705342017-01-06 11:36:01 -08001114 // set mpls type as apply_action
1115 tb.immediate().extension(setMplsType, deviceId);
Charles Chancad338a2016-09-16 18:03:11 -07001116 }
1117 tb.transition(mplsNextTable);
1118 } else {
1119 tb.transition(ACL_TABLE);
1120 }
1121
Saurav Das822c4e22015-10-23 10:51:11 -07001122 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
1123 .fromApp(fwd.appId())
1124 .withPriority(fwd.priority())
1125 .forDevice(deviceId)
Saurav Das8a0732e2015-11-20 15:27:53 -08001126 .withSelector(filteredSelector.build())
1127 .withTreatment(tb.build())
1128 .forTable(forTableId);
Saurav Das822c4e22015-10-23 10:51:11 -07001129
1130 if (fwd.permanent()) {
1131 ruleBuilder.makePermanent();
1132 } else {
1133 ruleBuilder.makeTemporary(fwd.timeout());
1134 }
Flavio Castroe10fa242016-01-15 12:43:51 -08001135 Collection<FlowRule> flowRuleCollection = new ArrayList<>();
1136 flowRuleCollection.add(ruleBuilder.build());
1137 if (defaultRule) {
Pier Ventree0ae7a32016-11-23 09:57:42 -08001138 flowRuleCollection.add(
1139 defaultRoute(fwd, complementarySelector, forTableId, tb)
1140 );
Flavio Castroe10fa242016-01-15 12:43:51 -08001141 log.debug("Default rule 0.0.0.0/0 is being installed two rules");
1142 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001143 // XXX retrying flows may be necessary due to bug CORD-554
1144 if (emptyGroup) {
1145 executorService.schedule(new RetryFlows(fwd, flowRuleCollection),
1146 RETRY_MS, TimeUnit.MILLISECONDS);
1147 }
Flavio Castroe10fa242016-01-15 12:43:51 -08001148 return flowRuleCollection;
Saurav Das822c4e22015-10-23 10:51:11 -07001149 }
1150
Pier Ventree0ae7a32016-11-23 09:57:42 -08001151 protected FlowRule defaultRoute(ForwardingObjective fwd,
1152 TrafficSelector.Builder complementarySelector,
1153 int forTableId,
1154 TrafficTreatment.Builder tb) {
1155 FlowRule.Builder rule = DefaultFlowRule.builder()
1156 .fromApp(fwd.appId())
1157 .withPriority(fwd.priority())
1158 .forDevice(deviceId)
1159 .withSelector(complementarySelector.build())
1160 .withTreatment(tb.build())
1161 .forTable(forTableId);
1162 if (fwd.permanent()) {
1163 rule.makePermanent();
1164 } else {
1165 rule.makeTemporary(fwd.timeout());
1166 }
1167 return rule.build();
1168 }
1169
Saurav Das4ce45962015-11-24 23:21:05 -08001170 /**
1171 * Handles forwarding rules to the L2 bridging table. Flow actions are not
1172 * allowed in the bridging table - instead we use L2 Interface group or
1173 * L2 flood group
1174 *
1175 * @param fwd the forwarding objective
1176 * @return A collection of flow rules, or an empty set
1177 */
1178 protected Collection<FlowRule> processEthDstSpecific(ForwardingObjective fwd) {
1179 List<FlowRule> rules = new ArrayList<>();
1180
1181 // Build filtered selector
1182 TrafficSelector selector = fwd.selector();
1183 EthCriterion ethCriterion = (EthCriterion) selector
1184 .getCriterion(Criterion.Type.ETH_DST);
1185 VlanIdCriterion vlanIdCriterion = (VlanIdCriterion) selector
1186 .getCriterion(Criterion.Type.VLAN_VID);
1187
1188 if (vlanIdCriterion == null) {
1189 log.warn("Forwarding objective for bridging requires vlan. Not "
1190 + "installing fwd:{} in dev:{}", fwd.id(), deviceId);
1191 fail(fwd, ObjectiveError.BADPARAMS);
1192 return Collections.emptySet();
1193 }
1194
1195 TrafficSelector.Builder filteredSelectorBuilder =
1196 DefaultTrafficSelector.builder();
Yi Tseng3a77b4f2017-02-06 15:02:17 -08001197
1198 if (!ethCriterion.mac().equals(NONE) &&
1199 !ethCriterion.mac().equals(BROADCAST)) {
Saurav Das4ce45962015-11-24 23:21:05 -08001200 filteredSelectorBuilder.matchEthDst(ethCriterion.mac());
1201 log.debug("processing L2 forwarding objective:{} -> next:{} in dev:{}",
1202 fwd.id(), fwd.nextId(), deviceId);
1203 } else {
Yi Tseng3a77b4f2017-02-06 15:02:17 -08001204 // Use wildcard DST_MAC if the MacAddress is None or Broadcast
Saurav Das4ce45962015-11-24 23:21:05 -08001205 log.debug("processing L2 Broadcast forwarding objective:{} -> next:{} "
1206 + "in dev:{} for vlan:{}",
1207 fwd.id(), fwd.nextId(), deviceId, vlanIdCriterion.vlanId());
1208 }
Charles Chan14967c22015-12-07 11:11:50 -08001209 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vlanIdCriterion.vlanId());
1210 filteredSelectorBuilder.extension(ofdpaMatchVlanVid, deviceId);
Saurav Das4ce45962015-11-24 23:21:05 -08001211 TrafficSelector filteredSelector = filteredSelectorBuilder.build();
1212
1213 if (fwd.treatment() != null) {
1214 log.warn("Ignoring traffic treatment in fwd rule {} meant for L2 table"
1215 + "for dev:{}. Expecting only nextId", fwd.id(), deviceId);
1216 }
1217
1218 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
1219 if (fwd.nextId() != null) {
Saurav Das423fe2b2015-12-04 10:52:59 -08001220 NextGroup next = getGroupForNextObjective(fwd.nextId());
Saurav Das4ce45962015-11-24 23:21:05 -08001221 if (next != null) {
1222 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1223 // we only need the top level group's key to point the flow to it
1224 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
1225 if (group != null) {
1226 treatmentBuilder.deferred().group(group.id());
1227 } else {
1228 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
1229 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
1230 fail(fwd, ObjectiveError.GROUPMISSING);
1231 return Collections.emptySet();
1232 }
1233 }
1234 }
1235 treatmentBuilder.immediate().transition(ACL_TABLE);
1236 TrafficTreatment filteredTreatment = treatmentBuilder.build();
1237
1238 // Build bridging table entries
1239 FlowRule.Builder flowRuleBuilder = DefaultFlowRule.builder();
1240 flowRuleBuilder.fromApp(fwd.appId())
1241 .withPriority(fwd.priority())
1242 .forDevice(deviceId)
1243 .withSelector(filteredSelector)
1244 .withTreatment(filteredTreatment)
1245 .forTable(BRIDGING_TABLE);
1246 if (fwd.permanent()) {
1247 flowRuleBuilder.makePermanent();
1248 } else {
1249 flowRuleBuilder.makeTemporary(fwd.timeout());
1250 }
1251 rules.add(flowRuleBuilder.build());
1252 return rules;
1253 }
1254
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001255 //////////////////////////////////////
1256 // Helper Methods and Classes
1257 //////////////////////////////////////
1258
1259 private boolean isSupportedEthTypeObjective(ForwardingObjective fwd) {
1260 TrafficSelector selector = fwd.selector();
1261 EthTypeCriterion ethType = (EthTypeCriterion) selector
1262 .getCriterion(Criterion.Type.ETH_TYPE);
1263 return !((ethType == null) ||
1264 ((ethType.ethType().toShort() != Ethernet.TYPE_IPV4) &&
Pier Ventree0ae7a32016-11-23 09:57:42 -08001265 (ethType.ethType().toShort() != Ethernet.MPLS_UNICAST)) &&
1266 (ethType.ethType().toShort() != Ethernet.TYPE_IPV6));
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001267 }
1268
1269 private boolean isSupportedEthDstObjective(ForwardingObjective fwd) {
1270 TrafficSelector selector = fwd.selector();
1271 EthCriterion ethDst = (EthCriterion) selector
1272 .getCriterion(Criterion.Type.ETH_DST);
1273 VlanIdCriterion vlanId = (VlanIdCriterion) selector
1274 .getCriterion(Criterion.Type.VLAN_VID);
1275 return !(ethDst == null && vlanId == null);
1276 }
1277
Saurav Das423fe2b2015-12-04 10:52:59 -08001278 protected NextGroup getGroupForNextObjective(Integer nextId) {
1279 NextGroup next = flowObjectiveStore.getNextGroup(nextId);
1280 if (next != null) {
1281 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1282 if (gkeys != null && !gkeys.isEmpty()) {
1283 return next;
1284 } else {
1285 log.warn("Empty next group found in FlowObjective store for "
1286 + "next-id:{} in dev:{}", nextId, deviceId);
1287 }
1288 } else {
1289 log.warn("next-id {} not found in Flow objective store for dev:{}",
1290 nextId, deviceId);
1291 }
1292 return null;
1293 }
1294
Charles Chan188ebf52015-12-23 00:15:11 -08001295 protected static void pass(Objective obj) {
Sho SHIMIZUef7e2902016-02-12 18:38:29 -08001296 obj.context().ifPresent(context -> context.onSuccess(obj));
Saurav Das822c4e22015-10-23 10:51:11 -07001297 }
1298
Charles Chan188ebf52015-12-23 00:15:11 -08001299 protected static void fail(Objective obj, ObjectiveError error) {
Sho SHIMIZUef7e2902016-02-12 18:38:29 -08001300 obj.context().ifPresent(context -> context.onError(obj, error));
Saurav Das822c4e22015-10-23 10:51:11 -07001301 }
Saurav Das24431192016-03-07 19:13:00 -08001302
Saurav Das24431192016-03-07 19:13:00 -08001303 @Override
1304 public List<String> getNextMappings(NextGroup nextGroup) {
1305 List<String> mappings = new ArrayList<>();
1306 List<Deque<GroupKey>> gkeys = appKryo.deserialize(nextGroup.data());
1307 for (Deque<GroupKey> gkd : gkeys) {
1308 Group lastGroup = null;
Saurav Das8be4e3a2016-03-11 17:19:07 -08001309 StringBuffer gchain = new StringBuffer();
Saurav Das24431192016-03-07 19:13:00 -08001310 for (GroupKey gk : gkd) {
1311 Group g = groupService.getGroup(deviceId, gk);
Saurav Das8be4e3a2016-03-11 17:19:07 -08001312 if (g == null) {
Saurav Das25190812016-05-27 13:54:07 -07001313 gchain.append(" NoGrp").append(" -->");
Saurav Das8be4e3a2016-03-11 17:19:07 -08001314 continue;
1315 }
1316 gchain.append(" 0x").append(Integer.toHexString(g.id().id()))
1317 .append(" -->");
Saurav Das24431192016-03-07 19:13:00 -08001318 lastGroup = g;
1319 }
1320 // add port information for last group in group-chain
Saurav Das25190812016-05-27 13:54:07 -07001321 List<Instruction> lastGroupIns = new ArrayList<Instruction>();
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001322 if (lastGroup != null && !lastGroup.buckets().buckets().isEmpty()) {
Saurav Das25190812016-05-27 13:54:07 -07001323 lastGroupIns = lastGroup.buckets().buckets().get(0)
1324 .treatment().allInstructions();
1325 }
1326 for (Instruction i: lastGroupIns) {
Saurav Das24431192016-03-07 19:13:00 -08001327 if (i instanceof OutputInstruction) {
Saurav Das8be4e3a2016-03-11 17:19:07 -08001328 gchain.append(" port:").append(((OutputInstruction) i).port());
Saurav Das24431192016-03-07 19:13:00 -08001329 }
1330 }
Saurav Das8be4e3a2016-03-11 17:19:07 -08001331 mappings.add(gchain.toString());
Saurav Das24431192016-03-07 19:13:00 -08001332 }
1333 return mappings;
1334 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001335
Pier Ventre140a8942016-11-02 07:26:38 -07001336 static boolean isMplsBos(TrafficSelector selector) {
1337 MplsBosCriterion bosCriterion = (MplsBosCriterion) selector.getCriterion(MPLS_BOS);
1338 return bosCriterion != null && bosCriterion.mplsBos();
1339 }
1340
1341 static boolean isNotMplsBos(TrafficSelector selector) {
1342 MplsBosCriterion bosCriterion = (MplsBosCriterion) selector.getCriterion(MPLS_BOS);
1343 return bosCriterion != null && !bosCriterion.mplsBos();
1344 }
1345
Charles Chan5b9df8d2016-03-28 22:21:40 -07001346 protected static VlanId readVlanFromSelector(TrafficSelector selector) {
Saurav Das59232cf2016-04-27 18:35:50 -07001347 if (selector == null) {
1348 return null;
1349 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001350 Criterion criterion = selector.getCriterion(Criterion.Type.VLAN_VID);
1351 return (criterion == null)
1352 ? null : ((VlanIdCriterion) criterion).vlanId();
1353 }
1354
1355 protected static IpPrefix readIpDstFromSelector(TrafficSelector selector) {
Saurav Das59232cf2016-04-27 18:35:50 -07001356 if (selector == null) {
1357 return null;
1358 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001359 Criterion criterion = selector.getCriterion(Criterion.Type.IPV4_DST);
1360 return (criterion == null) ? null : ((IPCriterion) criterion).ip();
1361 }
Charles Chand55e84d2016-03-30 17:54:24 -07001362
1363 private static VlanId readVlanFromTreatment(TrafficTreatment treatment) {
Saurav Das59232cf2016-04-27 18:35:50 -07001364 if (treatment == null) {
1365 return null;
1366 }
Charles Chand55e84d2016-03-30 17:54:24 -07001367 for (Instruction i : treatment.allInstructions()) {
1368 if (i instanceof ModVlanIdInstruction) {
1369 return ((ModVlanIdInstruction) i).vlanId();
1370 }
1371 }
1372 return null;
1373 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001374
1375 /**
1376 * Utility class that retries sending flows a fixed number of times, even if
1377 * some of the attempts are successful. Used only for forwarding objectives.
1378 */
1379 protected final class RetryFlows implements Runnable {
1380 int attempts = MAX_RETRY_ATTEMPTS;
1381 private Collection<FlowRule> retryFlows;
1382 private ForwardingObjective fwd;
1383
1384 RetryFlows(ForwardingObjective fwd, Collection<FlowRule> retryFlows) {
1385 this.fwd = fwd;
1386 this.retryFlows = retryFlows;
1387 }
1388
1389 @Override
1390 public void run() {
1391 log.info("RETRY FLOWS ATTEMPT# {} for fwd:{} rules:{}",
1392 MAX_RETRY_ATTEMPTS - attempts, fwd.id(), retryFlows.size());
1393 sendForward(fwd, retryFlows);
1394 if (--attempts > 0) {
1395 executorService.schedule(this, RETRY_MS, TimeUnit.MILLISECONDS);
1396 }
1397 }
1398 }
1399
Saurav Das822c4e22015-10-23 10:51:11 -07001400}