blob: e32f5d55c2e98f052b8ba8d7b91fe16208e02832 [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
Saurav Das822c4e22015-10-23 10:51:11 -0700132 private final Logger log = getLogger(getClass());
Charles Chan425854b2016-04-11 15:32:12 -0700133 protected ServiceDirectory serviceDirectory;
Saurav Das822c4e22015-10-23 10:51:11 -0700134 protected FlowRuleService flowRuleService;
Charles Chan425854b2016-04-11 15:32:12 -0700135 protected CoreService coreService;
Saurav Das8a0732e2015-11-20 15:27:53 -0800136 protected GroupService groupService;
137 protected FlowObjectiveStore flowObjectiveStore;
Saurav Das822c4e22015-10-23 10:51:11 -0700138 protected DeviceId deviceId;
139 protected ApplicationId driverId;
Saurav Das822c4e22015-10-23 10:51:11 -0700140 protected DeviceService deviceService;
Charles Chan188ebf52015-12-23 00:15:11 -0800141 protected static KryoNamespace appKryo = new KryoNamespace.Builder()
Saurav Das822c4e22015-10-23 10:51:11 -0700142 .register(KryoNamespaces.API)
143 .register(GroupKey.class)
144 .register(DefaultGroupKey.class)
Charles Chan361154b2016-03-24 10:23:39 -0700145 .register(Ofdpa2GroupHandler.OfdpaNextGroup.class)
Saurav Das8a0732e2015-11-20 15:27:53 -0800146 .register(ArrayDeque.class)
Charles Chaneefdedf2016-05-23 16:45:45 -0700147 .build("Ofdpa2Pipeline");
Saurav Das822c4e22015-10-23 10:51:11 -0700148
Charles Chan425854b2016-04-11 15:32:12 -0700149 protected Ofdpa2GroupHandler groupHandler;
Saurav Das822c4e22015-10-23 10:51:11 -0700150
Saurav Das52025962016-01-28 22:30:01 -0800151 protected Set<IPCriterion> sentIpFilters = Collections.newSetFromMap(
Charles Chan188ebf52015-12-23 00:15:11 -0800152 new ConcurrentHashMap<>());
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700153 // flows installations to be retried
154 protected ScheduledExecutorService executorService
155 = newScheduledThreadPool(5, groupedThreads("OfdpaPipeliner", "retry-%d", log));
156 protected static final int MAX_RETRY_ATTEMPTS = 10;
157 protected static final int RETRY_MS = 1000;
Saurav Das4f980082015-11-05 13:39:15 -0800158
Saurav Das822c4e22015-10-23 10:51:11 -0700159 @Override
160 public void init(DeviceId deviceId, PipelinerContext context) {
Saurav Das822c4e22015-10-23 10:51:11 -0700161 this.deviceId = deviceId;
162
Charles Chan425854b2016-04-11 15:32:12 -0700163 serviceDirectory = context.directory();
Saurav Das822c4e22015-10-23 10:51:11 -0700164 coreService = serviceDirectory.get(CoreService.class);
165 flowRuleService = serviceDirectory.get(FlowRuleService.class);
166 groupService = serviceDirectory.get(GroupService.class);
167 flowObjectiveStore = context.store();
Saurav Das822c4e22015-10-23 10:51:11 -0700168 deviceService = serviceDirectory.get(DeviceService.class);
Saurav Das822c4e22015-10-23 10:51:11 -0700169
Charles Chan40132b32017-01-22 00:19:37 -0800170 initDriverId();
171 initGroupHander(context);
Saurav Das822c4e22015-10-23 10:51:11 -0700172
Saurav Das822c4e22015-10-23 10:51:11 -0700173 initializePipeline();
Saurav Das822c4e22015-10-23 10:51:11 -0700174 }
175
Charles Chan40132b32017-01-22 00:19:37 -0800176 protected void initDriverId() {
177 driverId = coreService.registerApplication(
178 "org.onosproject.driver.Ofdpa2Pipeline");
179 }
180
181 protected void initGroupHander(PipelinerContext context) {
182 groupHandler = new Ofdpa2GroupHandler();
183 groupHandler.init(deviceId, context);
184 }
185
Saurav Das822c4e22015-10-23 10:51:11 -0700186 protected void initializePipeline() {
Charles Chan188ebf52015-12-23 00:15:11 -0800187 // OF-DPA does not require initializing the pipeline as it puts default
188 // rules automatically in the hardware. However emulation of OFDPA in
189 // software switches does require table-miss-entries.
Saurav Das822c4e22015-10-23 10:51:11 -0700190 }
191
192 //////////////////////////////////////
193 // Flow Objectives
194 //////////////////////////////////////
195
196 @Override
197 public void filter(FilteringObjective filteringObjective) {
198 if (filteringObjective.type() == FilteringObjective.Type.PERMIT) {
199 processFilter(filteringObjective,
200 filteringObjective.op() == Objective.Operation.ADD,
201 filteringObjective.appId());
202 } else {
203 // Note that packets that don't match the PERMIT filter are
204 // automatically denied. The DENY filter is used to deny packets
205 // that are otherwise permitted by the PERMIT filter.
206 // Use ACL table flow rules here for DENY filtering objectives
207 log.debug("filter objective other than PERMIT currently not supported");
208 fail(filteringObjective, ObjectiveError.UNSUPPORTED);
209 }
210 }
211
212 @Override
213 public void forward(ForwardingObjective fwd) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700214 Collection<FlowRule> rules = processForward(fwd);
Saurav Das25190812016-05-27 13:54:07 -0700215 if (rules == null || rules.isEmpty()) {
216 // Assumes fail message has already been generated to the objective
217 // context. Returning here prevents spurious pass message to be
218 // generated by FlowRule service for empty flowOps.
219 return;
220 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700221 sendForward(fwd, rules);
222 }
223
224 protected void sendForward(ForwardingObjective fwd, Collection<FlowRule> rules) {
225 FlowRuleOperations.Builder flowOpsBuilder = FlowRuleOperations.builder();
Saurav Das822c4e22015-10-23 10:51:11 -0700226 switch (fwd.op()) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700227 case ADD:
228 rules.stream()
229 .filter(Objects::nonNull)
230 .forEach(flowOpsBuilder::add);
Saurav Dasd2fded02016-12-02 15:43:47 -0800231 log.debug("Applying a add fwd-obj {} to sw:{}", fwd.id(), deviceId);
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700232 break;
233 case REMOVE:
234 rules.stream()
235 .filter(Objects::nonNull)
236 .forEach(flowOpsBuilder::remove);
Pier Ventre42287df2016-11-09 14:17:26 -0800237 log.debug("Deleting a flow rule to sw:{}", deviceId);
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700238 break;
239 default:
240 fail(fwd, ObjectiveError.UNKNOWN);
241 log.warn("Unknown forwarding type {}", fwd.op());
Saurav Das822c4e22015-10-23 10:51:11 -0700242 }
243
Saurav Das822c4e22015-10-23 10:51:11 -0700244 flowRuleService.apply(flowOpsBuilder.build(new FlowRuleOperationsContext() {
245 @Override
246 public void onSuccess(FlowRuleOperations ops) {
247 pass(fwd);
248 }
249
250 @Override
251 public void onError(FlowRuleOperations ops) {
252 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
253 }
254 }));
Saurav Das822c4e22015-10-23 10:51:11 -0700255 }
256
257 @Override
258 public void next(NextObjective nextObjective) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800259 NextGroup nextGroup = flowObjectiveStore.getNextGroup(nextObjective.id());
260 switch (nextObjective.op()) {
261 case ADD:
Saurav Das4f980082015-11-05 13:39:15 -0800262 if (nextGroup != null) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800263 log.warn("Cannot add next {} that already exists in device {}",
264 nextObjective.id(), deviceId);
265 return;
266 }
267 log.debug("Processing NextObjective id{} in dev{} - add group",
268 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700269 groupHandler.addGroup(nextObjective);
Saurav Das8a0732e2015-11-20 15:27:53 -0800270 break;
271 case ADD_TO_EXISTING:
272 if (nextGroup != null) {
273 log.debug("Processing NextObjective id{} in dev{} - add bucket",
274 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700275 groupHandler.addBucketToGroup(nextObjective, nextGroup);
Saurav Das4f980082015-11-05 13:39:15 -0800276 } else {
Saurav Das8a0732e2015-11-20 15:27:53 -0800277 // it is possible that group-chain has not been fully created yet
Saurav Das423fe2b2015-12-04 10:52:59 -0800278 log.debug("Waiting to add bucket to group for next-id:{} in dev:{}",
279 nextObjective.id(), deviceId);
280 // by design only one pending bucket is allowed for the group
Charles Chan425854b2016-04-11 15:32:12 -0700281 groupHandler.pendingBuckets.put(nextObjective.id(), nextObjective);
Saurav Das4f980082015-11-05 13:39:15 -0800282 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800283 break;
284 case REMOVE:
285 if (nextGroup == null) {
286 log.warn("Cannot remove next {} that does not exist in device {}",
287 nextObjective.id(), deviceId);
288 return;
289 }
290 log.debug("Processing NextObjective id{} in dev{} - remove group",
291 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700292 groupHandler.removeGroup(nextObjective, nextGroup);
Saurav Das8a0732e2015-11-20 15:27:53 -0800293 break;
294 case REMOVE_FROM_EXISTING:
295 if (nextGroup == null) {
296 log.warn("Cannot remove from next {} that does not exist in device {}",
297 nextObjective.id(), deviceId);
298 return;
299 }
300 log.debug("Processing NextObjective id{} in dev{} - remove bucket",
301 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700302 groupHandler.removeBucketFromGroup(nextObjective, nextGroup);
Saurav Das8a0732e2015-11-20 15:27:53 -0800303 break;
304 default:
Saurav Das4f980082015-11-05 13:39:15 -0800305 log.warn("Unsupported operation {}", nextObjective.op());
Saurav Das822c4e22015-10-23 10:51:11 -0700306 }
307 }
308
309 //////////////////////////////////////
310 // Flow handling
311 //////////////////////////////////////
312
313 /**
314 * As per OFDPA 2.0 TTP, filtering of VLAN ids, MAC addresses (for routing)
315 * and IP addresses configured on switch ports happen in different tables.
316 * Note that IP filtering rules need to be added to the ACL table, as there
317 * is no mechanism to send to controller via IP table.
318 *
319 * @param filt the filtering objective
320 * @param install indicates whether to add or remove the objective
321 * @param applicationId the application that sent this objective
322 */
Saurav Das52025962016-01-28 22:30:01 -0800323 protected void processFilter(FilteringObjective filt,
324 boolean install, ApplicationId applicationId) {
Saurav Das822c4e22015-10-23 10:51:11 -0700325 // This driver only processes filtering criteria defined with switch
326 // ports as the key
327 PortCriterion portCriterion = null;
328 EthCriterion ethCriterion = null;
329 VlanIdCriterion vidCriterion = null;
330 Collection<IPCriterion> ips = new ArrayList<IPCriterion>();
331 if (!filt.key().equals(Criteria.dummy()) &&
332 filt.key().type() == Criterion.Type.IN_PORT) {
333 portCriterion = (PortCriterion) filt.key();
334 } else {
335 log.warn("No key defined in filtering objective from app: {}. Not"
336 + "processing filtering objective", applicationId);
Saurav Das59232cf2016-04-27 18:35:50 -0700337 fail(filt, ObjectiveError.BADPARAMS);
Saurav Das822c4e22015-10-23 10:51:11 -0700338 return;
339 }
Saurav Das59232cf2016-04-27 18:35:50 -0700340 log.debug("Received filtering objective for dev/port: {}/{}", deviceId,
341 portCriterion.port());
Saurav Das822c4e22015-10-23 10:51:11 -0700342 // convert filtering conditions for switch-intfs into flowrules
343 FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
344 for (Criterion criterion : filt.conditions()) {
Charles Chan5b9df8d2016-03-28 22:21:40 -0700345 if (criterion.type() == Criterion.Type.ETH_DST ||
346 criterion.type() == Criterion.Type.ETH_DST_MASKED) {
Saurav Das822c4e22015-10-23 10:51:11 -0700347 ethCriterion = (EthCriterion) criterion;
348 } else if (criterion.type() == Criterion.Type.VLAN_VID) {
349 vidCriterion = (VlanIdCriterion) criterion;
350 } else if (criterion.type() == Criterion.Type.IPV4_DST) {
351 ips.add((IPCriterion) criterion);
352 } else {
353 log.error("Unsupported filter {}", criterion);
354 fail(filt, ObjectiveError.UNSUPPORTED);
355 return;
356 }
357 }
358
Saurav Das0e99e2b2015-10-28 12:39:42 -0700359 VlanId assignedVlan = null;
Charles Chane849c192016-01-11 18:28:54 -0800360 if (vidCriterion != null) {
Charles Chand55e84d2016-03-30 17:54:24 -0700361 // Use the VLAN in metadata whenever a metadata is provided
362 if (filt.meta() != null) {
363 assignedVlan = readVlanFromTreatment(filt.meta());
364 // Use the VLAN in criterion if metadata is not present and the traffic is tagged
365 } else if (!vidCriterion.vlanId().equals(VlanId.NONE)) {
Charles Chane849c192016-01-11 18:28:54 -0800366 assignedVlan = vidCriterion.vlanId();
Charles Chand55e84d2016-03-30 17:54:24 -0700367 }
Charles Chane849c192016-01-11 18:28:54 -0800368
Charles Chand55e84d2016-03-30 17:54:24 -0700369 if (assignedVlan == null) {
370 log.error("Driver fails to extract VLAN information. "
371 + "Not proccessing VLAN filters on device {}.", deviceId);
372 log.debug("VLAN ID in criterion={}, metadata={}",
373 readVlanFromTreatment(filt.meta()), vidCriterion.vlanId());
374 fail(filt, ObjectiveError.BADPARAMS);
375 return;
Saurav Das0e99e2b2015-10-28 12:39:42 -0700376 }
377 }
378
Yi Tseng3a77b4f2017-02-06 15:02:17 -0800379 if (ethCriterion == null || ethCriterion.mac().equals(NONE)) {
Charles Chan985b12e2016-05-11 19:47:22 -0700380 // NOTE: it is possible that a filtering objective only has vidCriterion
381 log.debug("filtering objective missing dstMac, cannot program TMAC table");
Saurav Das822c4e22015-10-23 10:51:11 -0700382 } else {
383 for (FlowRule tmacRule : processEthDstFilter(portCriterion, ethCriterion,
Saurav Das0e99e2b2015-10-28 12:39:42 -0700384 vidCriterion, assignedVlan,
385 applicationId)) {
Saurav Das822c4e22015-10-23 10:51:11 -0700386 log.debug("adding MAC filtering rules in TMAC table: {} for dev: {}",
387 tmacRule, deviceId);
388 ops = install ? ops.add(tmacRule) : ops.remove(tmacRule);
389 }
390 }
391
Charles Chan985b12e2016-05-11 19:47:22 -0700392 if (vidCriterion == null) {
393 // NOTE: it is possible that a filtering objective only has ethCriterion
394 log.debug("filtering objective missing dstMac or VLAN, "
Charles Chane849c192016-01-11 18:28:54 -0800395 + "cannot program VLAN Table");
Saurav Das822c4e22015-10-23 10:51:11 -0700396 } else {
Charles Chan14967c22015-12-07 11:11:50 -0800397 /*
398 * NOTE: Separate vlan filtering rules and assignment rules
399 * into different stage in order to guarantee that filtering rules
Saurav Das52025962016-01-28 22:30:01 -0800400 * always go first, as required by ofdpa.
Charles Chan14967c22015-12-07 11:11:50 -0800401 */
402 List<FlowRule> allRules = processVlanIdFilter(
403 portCriterion, vidCriterion, assignedVlan, applicationId);
404 List<FlowRule> filteringRules = new ArrayList<>();
405 List<FlowRule> assignmentRules = new ArrayList<>();
406
407 allRules.forEach(flowRule -> {
408 ExtensionCriterion extCriterion =
409 (ExtensionCriterion) flowRule.selector().getCriterion(Criterion.Type.EXTENSION);
410 VlanId vlanId = ((OfdpaMatchVlanVid) extCriterion.extensionSelector()).vlanId();
Charles Chanbe8aea42016-02-24 12:04:47 -0800411 if (!vlanId.equals(VlanId.NONE)) {
Charles Chan14967c22015-12-07 11:11:50 -0800412 filteringRules.add(flowRule);
413 } else {
414 assignmentRules.add(flowRule);
415 }
416 });
417
418 for (FlowRule filteringRule : filteringRules) {
Saurav Das822c4e22015-10-23 10:51:11 -0700419 log.debug("adding VLAN filtering rule in VLAN table: {} for dev: {}",
Charles Chan14967c22015-12-07 11:11:50 -0800420 filteringRule, deviceId);
421 ops = install ? ops.add(filteringRule) : ops.remove(filteringRule);
422 }
423
424 ops.newStage();
425
426 for (FlowRule assignmentRule : assignmentRules) {
427 log.debug("adding VLAN assignment rule in VLAN table: {} for dev: {}",
428 assignmentRule, deviceId);
429 ops = install ? ops.add(assignmentRule) : ops.remove(assignmentRule);
Saurav Das822c4e22015-10-23 10:51:11 -0700430 }
431 }
432
433 for (IPCriterion ipaddr : ips) {
434 // since we ignore port information for IP rules, and the same (gateway) IP
435 // can be configured on multiple ports, we make sure that we send
436 // only a single rule to the switch.
437 if (!sentIpFilters.contains(ipaddr)) {
438 sentIpFilters.add(ipaddr);
439 log.debug("adding IP filtering rules in ACL table {} for dev: {}",
440 ipaddr, deviceId);
441 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
442 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
443 selector.matchEthType(Ethernet.TYPE_IPV4);
444 selector.matchIPDst(ipaddr.ip());
445 treatment.setOutput(PortNumber.CONTROLLER);
446 FlowRule rule = DefaultFlowRule.builder()
447 .forDevice(deviceId)
448 .withSelector(selector.build())
449 .withTreatment(treatment.build())
450 .withPriority(HIGHEST_PRIORITY)
451 .fromApp(applicationId)
452 .makePermanent()
453 .forTable(ACL_TABLE).build();
454 ops = install ? ops.add(rule) : ops.remove(rule);
455 }
456 }
457
458 // apply filtering flow rules
459 flowRuleService.apply(ops.build(new FlowRuleOperationsContext() {
460 @Override
461 public void onSuccess(FlowRuleOperations ops) {
462 log.info("Applied {} filtering rules in device {}",
463 ops.stages().get(0).size(), deviceId);
464 pass(filt);
465 }
466
467 @Override
468 public void onError(FlowRuleOperations ops) {
469 log.info("Failed to apply all filtering rules in dev {}", deviceId);
470 fail(filt, ObjectiveError.FLOWINSTALLATIONFAILED);
471 }
472 }));
473
474 }
475
476 /**
477 * Allows untagged packets into pipeline by assigning a vlan id.
Saurav Das0e99e2b2015-10-28 12:39:42 -0700478 * Vlan assignment is done by the application.
Saurav Das822c4e22015-10-23 10:51:11 -0700479 * Allows tagged packets into pipeline as per configured port-vlan info.
Saurav Das0e99e2b2015-10-28 12:39:42 -0700480 *
Charles Chanf9e98652016-09-07 16:54:23 -0700481 * @param portCriterion port on device for which this filter is programmed
482 * @param vidCriterion vlan assigned to port, or NONE for untagged
483 * @param assignedVlan assigned vlan-id for untagged packets
484 * @param applicationId for application programming this filter
Saurav Das822c4e22015-10-23 10:51:11 -0700485 * @return list of FlowRule for port-vlan filters
486 */
487 protected List<FlowRule> processVlanIdFilter(PortCriterion portCriterion,
488 VlanIdCriterion vidCriterion,
Saurav Das0e99e2b2015-10-28 12:39:42 -0700489 VlanId assignedVlan,
Saurav Das822c4e22015-10-23 10:51:11 -0700490 ApplicationId applicationId) {
Charles Chanf9e98652016-09-07 16:54:23 -0700491 return processVlanIdFilterInternal(portCriterion, vidCriterion, assignedVlan,
492 applicationId, true);
493 }
494
495 /**
496 * Internal implementation of processVlanIdFilter.
497 * <p>
498 * The is_present bit in set_vlan_vid action is required to be 0 in OFDPA i12.
499 * Since it is non-OF spec, we need an extension treatment for that.
500 * The useSetVlanExtension must be set to false for OFDPA i12.
501 * </p>
502 *
503 * @param portCriterion port on device for which this filter is programmed
504 * @param vidCriterion vlan assigned to port, or NONE for untagged
505 * @param assignedVlan assigned vlan-id for untagged packets
506 * @param applicationId for application programming this filter
507 * @param useSetVlanExtension use the setVlanVid extension that has is_present bit set to 0.
508 * @return list of FlowRule for port-vlan filters
509 */
510 protected List<FlowRule> processVlanIdFilterInternal(PortCriterion portCriterion,
511 VlanIdCriterion vidCriterion,
512 VlanId assignedVlan,
513 ApplicationId applicationId,
514 boolean useSetVlanExtension) {
Charles Chan14967c22015-12-07 11:11:50 -0800515 List<FlowRule> rules = new ArrayList<>();
Saurav Das822c4e22015-10-23 10:51:11 -0700516 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
517 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
Charles Chan14967c22015-12-07 11:11:50 -0800518 TrafficSelector.Builder preSelector = null;
519 TrafficTreatment.Builder preTreatment = null;
520
Saurav Das4f980082015-11-05 13:39:15 -0800521 treatment.transition(TMAC_TABLE);
522
Saurav Das822c4e22015-10-23 10:51:11 -0700523 if (vidCriterion.vlanId() == VlanId.NONE) {
524 // untagged packets are assigned vlans
Charles Chanbe8aea42016-02-24 12:04:47 -0800525 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(VlanId.NONE);
Charles Chan14967c22015-12-07 11:11:50 -0800526 selector.extension(ofdpaMatchVlanVid, deviceId);
Charles Chanf9e98652016-09-07 16:54:23 -0700527 if (useSetVlanExtension) {
528 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(assignedVlan);
529 treatment.extension(ofdpaSetVlanVid, deviceId);
530 } else {
531 treatment.setVlanId(assignedVlan);
532 }
Charles Chan14967c22015-12-07 11:11:50 -0800533
534 preSelector = DefaultTrafficSelector.builder();
535 OfdpaMatchVlanVid preOfdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
536 preSelector.extension(preOfdpaMatchVlanVid, deviceId);
537 preTreatment = DefaultTrafficTreatment.builder().transition(TMAC_TABLE);
538
Saurav Das4f980082015-11-05 13:39:15 -0800539 } else {
Charles Chan14967c22015-12-07 11:11:50 -0800540 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vidCriterion.vlanId());
541 selector.extension(ofdpaMatchVlanVid, deviceId);
Charles Chand55e84d2016-03-30 17:54:24 -0700542
543 if (!assignedVlan.equals(vidCriterion.vlanId())) {
Charles Chanc03782d2017-01-31 13:57:55 -0800544 if (useSetVlanExtension) {
545 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(assignedVlan);
546 treatment.extension(ofdpaSetVlanVid, deviceId);
547 } else {
548 treatment.setVlanId(assignedVlan);
549 }
Charles Chand55e84d2016-03-30 17:54:24 -0700550 }
Saurav Das822c4e22015-10-23 10:51:11 -0700551 }
Saurav Das822c4e22015-10-23 10:51:11 -0700552
553 // ofdpa cannot match on ALL portnumber, so we need to use separate
554 // rules for each port.
Charles Chan14967c22015-12-07 11:11:50 -0800555 List<PortNumber> portnums = new ArrayList<>();
Saurav Das822c4e22015-10-23 10:51:11 -0700556 if (portCriterion.port() == PortNumber.ALL) {
557 for (Port port : deviceService.getPorts(deviceId)) {
558 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
559 portnums.add(port.number());
560 }
561 }
562 } else {
563 portnums.add(portCriterion.port());
564 }
Saurav Das4f980082015-11-05 13:39:15 -0800565
Saurav Das822c4e22015-10-23 10:51:11 -0700566 for (PortNumber pnum : portnums) {
Saurav Das4f980082015-11-05 13:39:15 -0800567 // create rest of flowrule
Saurav Das822c4e22015-10-23 10:51:11 -0700568 selector.matchInPort(pnum);
569 FlowRule rule = DefaultFlowRule.builder()
570 .forDevice(deviceId)
571 .withSelector(selector.build())
572 .withTreatment(treatment.build())
573 .withPriority(DEFAULT_PRIORITY)
574 .fromApp(applicationId)
575 .makePermanent()
576 .forTable(VLAN_TABLE).build();
Charles Chan14967c22015-12-07 11:11:50 -0800577
578 if (preSelector != null) {
579 preSelector.matchInPort(pnum);
580 FlowRule preRule = DefaultFlowRule.builder()
581 .forDevice(deviceId)
582 .withSelector(preSelector.build())
583 .withTreatment(preTreatment.build())
584 .withPriority(DEFAULT_PRIORITY)
585 .fromApp(applicationId)
586 .makePermanent()
587 .forTable(VLAN_TABLE).build();
588 rules.add(preRule);
589 }
590
Saurav Das822c4e22015-10-23 10:51:11 -0700591 rules.add(rule);
592 }
593 return rules;
594 }
595
596 /**
597 * Allows routed packets with correct destination MAC to be directed
598 * to unicast-IP routing table or MPLS forwarding table.
Saurav Das822c4e22015-10-23 10:51:11 -0700599 *
600 * @param portCriterion port on device for which this filter is programmed
601 * @param ethCriterion dstMac of device for which is filter is programmed
602 * @param vidCriterion vlan assigned to port, or NONE for untagged
Saurav Das0e99e2b2015-10-28 12:39:42 -0700603 * @param assignedVlan assigned vlan-id for untagged packets
Saurav Das822c4e22015-10-23 10:51:11 -0700604 * @param applicationId for application programming this filter
605 * @return list of FlowRule for port-vlan filters
606
607 */
608 protected List<FlowRule> processEthDstFilter(PortCriterion portCriterion,
609 EthCriterion ethCriterion,
610 VlanIdCriterion vidCriterion,
Saurav Das0e99e2b2015-10-28 12:39:42 -0700611 VlanId assignedVlan,
Saurav Das822c4e22015-10-23 10:51:11 -0700612 ApplicationId applicationId) {
Charles Chan5270ed02016-01-30 23:22:37 -0800613 // Consider PortNumber.ANY as wildcard. Match ETH_DST only
614 if (portCriterion != null && portCriterion.port() == PortNumber.ANY) {
615 return processEthDstOnlyFilter(ethCriterion, applicationId);
616 }
617
Charles Chan5b9df8d2016-03-28 22:21:40 -0700618 // Multicast MAC
619 if (ethCriterion.mask() != null) {
620 return processMcastEthDstFilter(ethCriterion, applicationId);
621 }
622
Saurav Das822c4e22015-10-23 10:51:11 -0700623 //handling untagged packets via assigned VLAN
624 if (vidCriterion.vlanId() == VlanId.NONE) {
Saurav Das0e99e2b2015-10-28 12:39:42 -0700625 vidCriterion = (VlanIdCriterion) Criteria.matchVlanId(assignedVlan);
Saurav Das822c4e22015-10-23 10:51:11 -0700626 }
627 // ofdpa cannot match on ALL portnumber, so we need to use separate
628 // rules for each port.
629 List<PortNumber> portnums = new ArrayList<PortNumber>();
630 if (portCriterion.port() == PortNumber.ALL) {
631 for (Port port : deviceService.getPorts(deviceId)) {
632 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
633 portnums.add(port.number());
634 }
635 }
636 } else {
637 portnums.add(portCriterion.port());
638 }
639
640 List<FlowRule> rules = new ArrayList<FlowRule>();
641 for (PortNumber pnum : portnums) {
Charles Chan14967c22015-12-07 11:11:50 -0800642 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vidCriterion.vlanId());
Saurav Das822c4e22015-10-23 10:51:11 -0700643 // for unicast IP packets
644 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
645 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
646 selector.matchInPort(pnum);
Charles Chan14967c22015-12-07 11:11:50 -0800647 selector.extension(ofdpaMatchVlanVid, deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700648 selector.matchEthType(Ethernet.TYPE_IPV4);
649 selector.matchEthDst(ethCriterion.mac());
650 treatment.transition(UNICAST_ROUTING_TABLE);
651 FlowRule rule = DefaultFlowRule.builder()
652 .forDevice(deviceId)
653 .withSelector(selector.build())
654 .withTreatment(treatment.build())
655 .withPriority(DEFAULT_PRIORITY)
656 .fromApp(applicationId)
657 .makePermanent()
658 .forTable(TMAC_TABLE).build();
659 rules.add(rule);
660 //for MPLS packets
661 selector = DefaultTrafficSelector.builder();
662 treatment = DefaultTrafficTreatment.builder();
663 selector.matchInPort(pnum);
Charles Chan14967c22015-12-07 11:11:50 -0800664 selector.extension(ofdpaMatchVlanVid, deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700665 selector.matchEthType(Ethernet.MPLS_UNICAST);
666 selector.matchEthDst(ethCriterion.mac());
667 treatment.transition(MPLS_TABLE_0);
668 rule = DefaultFlowRule.builder()
669 .forDevice(deviceId)
670 .withSelector(selector.build())
671 .withTreatment(treatment.build())
672 .withPriority(DEFAULT_PRIORITY)
673 .fromApp(applicationId)
674 .makePermanent()
675 .forTable(TMAC_TABLE).build();
676 rules.add(rule);
Pier Ventree0ae7a32016-11-23 09:57:42 -0800677 /*
678 * TMAC rules for IPv6 packets
679 */
680 selector = DefaultTrafficSelector.builder();
681 treatment = DefaultTrafficTreatment.builder();
682 selector.matchInPort(pnum);
683 selector.extension(ofdpaMatchVlanVid, deviceId);
684 selector.matchEthType(Ethernet.TYPE_IPV6);
685 selector.matchEthDst(ethCriterion.mac());
686 treatment.transition(UNICAST_ROUTING_TABLE);
687 rule = DefaultFlowRule.builder()
688 .forDevice(deviceId)
689 .withSelector(selector.build())
690 .withTreatment(treatment.build())
691 .withPriority(DEFAULT_PRIORITY)
692 .fromApp(applicationId)
693 .makePermanent()
694 .forTable(TMAC_TABLE).build();
695 rules.add(rule);
Saurav Das822c4e22015-10-23 10:51:11 -0700696 }
697 return rules;
698 }
699
Charles Chan5270ed02016-01-30 23:22:37 -0800700 protected List<FlowRule> processEthDstOnlyFilter(EthCriterion ethCriterion,
701 ApplicationId applicationId) {
Pier Luigi0e358632017-01-31 09:35:05 -0800702 ImmutableList.Builder<FlowRule> builder = ImmutableList.builder();
703
Charles Chan5270ed02016-01-30 23:22:37 -0800704 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
705 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
706 selector.matchEthType(Ethernet.TYPE_IPV4);
707 selector.matchEthDst(ethCriterion.mac());
708 treatment.transition(UNICAST_ROUTING_TABLE);
709 FlowRule rule = DefaultFlowRule.builder()
710 .forDevice(deviceId)
711 .withSelector(selector.build())
712 .withTreatment(treatment.build())
713 .withPriority(DEFAULT_PRIORITY)
714 .fromApp(applicationId)
715 .makePermanent()
716 .forTable(TMAC_TABLE).build();
Pier Luigi0e358632017-01-31 09:35:05 -0800717 builder.add(rule);
718
719 selector = DefaultTrafficSelector.builder();
720 treatment = DefaultTrafficTreatment.builder();
721 selector.matchEthType(Ethernet.TYPE_IPV6);
722 selector.matchEthDst(ethCriterion.mac());
723 treatment.transition(UNICAST_ROUTING_TABLE);
724 rule = DefaultFlowRule.builder()
725 .forDevice(deviceId)
726 .withSelector(selector.build())
727 .withTreatment(treatment.build())
728 .withPriority(DEFAULT_PRIORITY)
729 .fromApp(applicationId)
730 .makePermanent()
731 .forTable(TMAC_TABLE).build();
732 return builder.add(rule).build();
Charles Chan5270ed02016-01-30 23:22:37 -0800733 }
734
Charles Chan5b9df8d2016-03-28 22:21:40 -0700735 protected List<FlowRule> processMcastEthDstFilter(EthCriterion ethCriterion,
736 ApplicationId applicationId) {
737 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
738 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
739 selector.matchEthType(Ethernet.TYPE_IPV4);
740 selector.matchEthDstMasked(ethCriterion.mac(), ethCriterion.mask());
741 treatment.transition(MULTICAST_ROUTING_TABLE);
742 FlowRule rule = DefaultFlowRule.builder()
743 .forDevice(deviceId)
744 .withSelector(selector.build())
745 .withTreatment(treatment.build())
746 .withPriority(DEFAULT_PRIORITY)
747 .fromApp(applicationId)
748 .makePermanent()
749 .forTable(TMAC_TABLE).build();
750 return ImmutableList.<FlowRule>builder().add(rule).build();
751 }
752
Saurav Das822c4e22015-10-23 10:51:11 -0700753 private Collection<FlowRule> processForward(ForwardingObjective fwd) {
754 switch (fwd.flag()) {
755 case SPECIFIC:
756 return processSpecific(fwd);
757 case VERSATILE:
758 return processVersatile(fwd);
759 default:
760 fail(fwd, ObjectiveError.UNKNOWN);
761 log.warn("Unknown forwarding flag {}", fwd.flag());
762 }
763 return Collections.emptySet();
764 }
765
766 /**
767 * In the OF-DPA 2.0 pipeline, versatile forwarding objectives go to the
768 * ACL table.
769 * @param fwd the forwarding objective of type 'versatile'
770 * @return a collection of flow rules to be sent to the switch. An empty
771 * collection may be returned if there is a problem in processing
772 * the flow rule
773 */
Saurav Das52025962016-01-28 22:30:01 -0800774 protected Collection<FlowRule> processVersatile(ForwardingObjective fwd) {
Saurav Dasd2fded02016-12-02 15:43:47 -0800775 log.info("Processing versatile forwarding objective:{} in dev:{}",
776 fwd.id(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700777
778 EthTypeCriterion ethType =
Saurav Das77b5e902016-01-27 17:01:59 -0800779 (EthTypeCriterion) fwd.selector().getCriterion(Criterion.Type.ETH_TYPE);
Saurav Das822c4e22015-10-23 10:51:11 -0700780 if (ethType == null) {
Saurav Dasd2fded02016-12-02 15:43:47 -0800781 log.error("Versatile forwarding objective:{} must include ethType",
782 fwd.id());
Saurav Das822c4e22015-10-23 10:51:11 -0700783 fail(fwd, ObjectiveError.BADPARAMS);
784 return Collections.emptySet();
785 }
786 if (fwd.nextId() == null && fwd.treatment() == null) {
787 log.error("Forwarding objective {} from {} must contain "
788 + "nextId or Treatment", fwd.selector(), fwd.appId());
Zsolt Haraszti9faab752016-02-17 15:55:20 -0800789 fail(fwd, ObjectiveError.BADPARAMS);
Saurav Das822c4e22015-10-23 10:51:11 -0700790 return Collections.emptySet();
791 }
Saurav Das49cb5a12016-01-16 22:54:07 -0800792
Saurav Das77b5e902016-01-27 17:01:59 -0800793 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
794 fwd.selector().criteria().forEach(criterion -> {
795 if (criterion instanceof VlanIdCriterion) {
796 VlanId vlanId = ((VlanIdCriterion) criterion).vlanId();
797 // ensure that match does not include vlan = NONE as OF-DPA does not
798 // match untagged packets this way in the ACL table.
799 if (vlanId.equals(VlanId.NONE)) {
800 return;
801 }
802 OfdpaMatchVlanVid ofdpaMatchVlanVid =
803 new OfdpaMatchVlanVid(vlanId);
804 sbuilder.extension(ofdpaMatchVlanVid, deviceId);
Pier Ventree0ae7a32016-11-23 09:57:42 -0800805 } else if (criterion instanceof Icmpv6TypeCriterion ||
806 criterion instanceof Icmpv6CodeCriterion) {
807 /*
808 * We silenty discard these criterions, our current
809 * OFDPA platform does not support these matches on
810 * the ACL table.
811 */
812 log.warn("ICMPv6 Type and ICMPv6 Code are not supported");
Saurav Das77b5e902016-01-27 17:01:59 -0800813 } else {
814 sbuilder.add(criterion);
815 }
816 });
817
Saurav Das822c4e22015-10-23 10:51:11 -0700818 // XXX driver does not currently do type checking as per Tables 65-67 in
819 // OFDPA 2.0 spec. The only allowed treatment is a punt to the controller.
Saurav Das49cb5a12016-01-16 22:54:07 -0800820 TrafficTreatment.Builder ttBuilder = DefaultTrafficTreatment.builder();
821 if (fwd.treatment() != null) {
822 for (Instruction ins : fwd.treatment().allInstructions()) {
823 if (ins instanceof OutputInstruction) {
824 OutputInstruction o = (OutputInstruction) ins;
825 if (o.port() == PortNumber.CONTROLLER) {
826 ttBuilder.add(o);
827 } else {
828 log.warn("Only allowed treatments in versatile forwarding "
829 + "objectives are punts to the controller");
830 }
831 } else {
832 log.warn("Cannot process instruction in versatile fwd {}", ins);
833 }
Saurav Das822c4e22015-10-23 10:51:11 -0700834 }
Charles Chan2df0e8a2017-01-09 11:45:08 -0800835 if (fwd.treatment().clearedDeferred()) {
836 ttBuilder.wipeDeferred();
837 }
Saurav Das822c4e22015-10-23 10:51:11 -0700838 }
Saurav Das822c4e22015-10-23 10:51:11 -0700839 if (fwd.nextId() != null) {
Saurav Das49cb5a12016-01-16 22:54:07 -0800840 // overide case
841 NextGroup next = getGroupForNextObjective(fwd.nextId());
842 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
843 // we only need the top level group's key to point the flow to it
844 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
845 if (group == null) {
846 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
847 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
848 fail(fwd, ObjectiveError.GROUPMISSING);
849 return Collections.emptySet();
850 }
851 ttBuilder.deferred().group(group.id());
Saurav Das822c4e22015-10-23 10:51:11 -0700852 }
Saurav Das49cb5a12016-01-16 22:54:07 -0800853
854 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
855 .fromApp(fwd.appId())
856 .withPriority(fwd.priority())
857 .forDevice(deviceId)
Saurav Das77b5e902016-01-27 17:01:59 -0800858 .withSelector(sbuilder.build())
Saurav Das49cb5a12016-01-16 22:54:07 -0800859 .withTreatment(ttBuilder.build())
860 .makePermanent()
861 .forTable(ACL_TABLE);
862 return Collections.singletonList(ruleBuilder.build());
Saurav Das822c4e22015-10-23 10:51:11 -0700863 }
864
865 /**
866 * In the OF-DPA 2.0 pipeline, specific forwarding refers to the IP table
Saurav Das8a0732e2015-11-20 15:27:53 -0800867 * (unicast or multicast) or the L2 table (mac + vlan) or the MPLS table.
Saurav Das822c4e22015-10-23 10:51:11 -0700868 *
869 * @param fwd the forwarding objective of type 'specific'
870 * @return a collection of flow rules. Typically there will be only one
871 * for this type of forwarding objective. An empty set may be
872 * returned if there is an issue in processing the objective.
873 */
Saurav Das8a0732e2015-11-20 15:27:53 -0800874 protected Collection<FlowRule> processSpecific(ForwardingObjective fwd) {
Saurav Das25190812016-05-27 13:54:07 -0700875 log.debug("Processing specific fwd objective:{} in dev:{} with next:{}",
Saurav Das4ce45962015-11-24 23:21:05 -0800876 fwd.id(), deviceId, fwd.nextId());
877 boolean isEthTypeObj = isSupportedEthTypeObjective(fwd);
878 boolean isEthDstObj = isSupportedEthDstObjective(fwd);
879
880 if (isEthTypeObj) {
881 return processEthTypeSpecific(fwd);
882 } else if (isEthDstObj) {
883 return processEthDstSpecific(fwd);
884 } else {
885 log.warn("processSpecific: Unsupported forwarding objective "
886 + "criteria fwd:{} in dev:{}", fwd.nextId(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700887 fail(fwd, ObjectiveError.UNSUPPORTED);
888 return Collections.emptySet();
889 }
Saurav Das4ce45962015-11-24 23:21:05 -0800890 }
891
Saurav Das4ce45962015-11-24 23:21:05 -0800892 /**
893 * Handles forwarding rules to the IP and MPLS tables.
894 *
895 * @param fwd the forwarding objective
896 * @return A collection of flow rules, or an empty set
897 */
898 protected Collection<FlowRule> processEthTypeSpecific(ForwardingObjective fwd) {
Charles Chancad338a2016-09-16 18:03:11 -0700899 return processEthTypeSpecificInternal(fwd, false, ACL_TABLE);
Charles Chanf9e98652016-09-07 16:54:23 -0700900 }
901
902 /**
Pier Ventree0ae7a32016-11-23 09:57:42 -0800903 * Helper method to build Ipv6 selector using the selector provided by
904 * a forwarding objective.
905 *
906 * @param builderToUpdate the builder to update
907 * @param fwd the selector to read
908 * @return 0 if the update ends correctly. -1 if the matches
909 * are not yet supported
910 */
911 protected int buildIpv6Selector(TrafficSelector.Builder builderToUpdate,
912 ForwardingObjective fwd) {
913
914 TrafficSelector selector = fwd.selector();
915
916 IpPrefix ipv6Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV6_DST)).ip();
917 if (ipv6Dst.isMulticast()) {
918 log.warn("IPv6 Multicast is currently not supported");
919 fail(fwd, ObjectiveError.BADPARAMS);
920 return -1;
Pier Ventree0ae7a32016-11-23 09:57:42 -0800921 }
Pier Luigi69fd4312017-01-28 17:58:58 -0800922 if (ipv6Dst.prefixLength() != 0) {
923 builderToUpdate.matchIPv6Dst(ipv6Dst);
924 }
925 builderToUpdate.matchEthType(Ethernet.TYPE_IPV6);
926 log.debug("processing IPv6 unicast specific forwarding objective {} -> next:{}"
927 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
Pier Ventree0ae7a32016-11-23 09:57:42 -0800928 return 0;
929 }
930
931 /**
Charles Chanf9e98652016-09-07 16:54:23 -0700932 * Internal implementation of processEthTypeSpecific.
933 * <p>
934 * Wildcarded IPv4_DST is not supported in OFDPA i12. Therefore, we break
935 * the rule into 0.0.0.0/1 and 128.0.0.0/1.
936 * The allowDefaultRoute must be set to false for OFDPA i12.
937 * </p>
938 *
939 * @param fwd the forwarding objective
940 * @param allowDefaultRoute allow wildcarded IPv4_DST or not
Ray Milkey0bb1e102016-11-10 14:51:27 -0800941 * @param mplsNextTable next MPLS table
Charles Chanf9e98652016-09-07 16:54:23 -0700942 * @return A collection of flow rules, or an empty set
943 */
944 protected Collection<FlowRule> processEthTypeSpecificInternal(ForwardingObjective fwd,
Charles Chancad338a2016-09-16 18:03:11 -0700945 boolean allowDefaultRoute,
946 int mplsNextTable) {
Saurav Das4ce45962015-11-24 23:21:05 -0800947 TrafficSelector selector = fwd.selector();
948 EthTypeCriterion ethType =
949 (EthTypeCriterion) selector.getCriterion(Criterion.Type.ETH_TYPE);
Flavio Castroe10fa242016-01-15 12:43:51 -0800950 boolean defaultRule = false;
951 boolean popMpls = false;
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700952 boolean emptyGroup = false;
Charles Chan188ebf52015-12-23 00:15:11 -0800953 int forTableId;
Saurav Das8a0732e2015-11-20 15:27:53 -0800954 TrafficSelector.Builder filteredSelector = DefaultTrafficSelector.builder();
Charles Chan14967c22015-12-07 11:11:50 -0800955 TrafficTreatment.Builder tb = DefaultTrafficTreatment.builder();
Flavio Castroe10fa242016-01-15 12:43:51 -0800956 TrafficSelector.Builder complementarySelector = DefaultTrafficSelector.builder();
Charles Chan14967c22015-12-07 11:11:50 -0800957
Saurav Das8a0732e2015-11-20 15:27:53 -0800958 if (ethType.ethType().toShort() == Ethernet.TYPE_IPV4) {
Flavio Castroe10fa242016-01-15 12:43:51 -0800959 IpPrefix ipv4Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV4_DST)).ip();
Charles Chan5b9df8d2016-03-28 22:21:40 -0700960 if (ipv4Dst.isMulticast()) {
961 if (ipv4Dst.prefixLength() != 32) {
962 log.warn("Multicast specific forwarding objective can only be /32");
963 fail(fwd, ObjectiveError.BADPARAMS);
964 return ImmutableSet.of();
965 }
966 VlanId assignedVlan = readVlanFromSelector(fwd.meta());
967 if (assignedVlan == null) {
968 log.warn("VLAN ID required by multicast specific fwd obj is missing. Abort.");
969 fail(fwd, ObjectiveError.BADPARAMS);
970 return ImmutableSet.of();
971 }
972 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
973 filteredSelector.extension(ofdpaMatchVlanVid, deviceId);
974 filteredSelector.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipv4Dst);
975 forTableId = MULTICAST_ROUTING_TABLE;
976 log.debug("processing IPv4 multicast specific forwarding objective {} -> next:{}"
977 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
Flavio Castroe10fa242016-01-15 12:43:51 -0800978 } else {
Charles Chanf9e98652016-09-07 16:54:23 -0700979 if (ipv4Dst.prefixLength() == 0) {
980 if (allowDefaultRoute) {
981 // The entire IPV4_DST field is wildcarded intentionally
982 filteredSelector.matchEthType(Ethernet.TYPE_IPV4);
983 } else {
Pier Ventree0ae7a32016-11-23 09:57:42 -0800984 // NOTE: The switch does not support matching 0.0.0.0/0
985 // Split it into 0.0.0.0/1 and 128.0.0.0/1
Charles Chanf9e98652016-09-07 16:54:23 -0700986 filteredSelector.matchEthType(Ethernet.TYPE_IPV4)
987 .matchIPDst(IpPrefix.valueOf("0.0.0.0/1"));
988 complementarySelector.matchEthType(Ethernet.TYPE_IPV4)
989 .matchIPDst(IpPrefix.valueOf("128.0.0.0/1"));
990 defaultRule = true;
991 }
Charles Chan5b9df8d2016-03-28 22:21:40 -0700992 } else {
Charles Chanf9e98652016-09-07 16:54:23 -0700993 filteredSelector.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipv4Dst);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700994 }
995 forTableId = UNICAST_ROUTING_TABLE;
996 log.debug("processing IPv4 unicast specific forwarding objective {} -> next:{}"
997 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
Flavio Castroe10fa242016-01-15 12:43:51 -0800998 }
Charles Chan14967c22015-12-07 11:11:50 -0800999
1000 if (fwd.treatment() != null) {
1001 for (Instruction instr : fwd.treatment().allInstructions()) {
1002 if (instr instanceof L3ModificationInstruction &&
1003 ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
Saurav Das9c705342017-01-06 11:36:01 -08001004 // XXX decrementing IP ttl is done automatically for routing, this
1005 // action is ignored or rejected in ofdpa as it is not fully implemented
1006 //tb.deferred().add(instr);
Charles Chan14967c22015-12-07 11:11:50 -08001007 }
1008 }
1009 }
1010
Pier Ventree0ae7a32016-11-23 09:57:42 -08001011 } else if (ethType.ethType().toShort() == Ethernet.TYPE_IPV6) {
1012 if (buildIpv6Selector(filteredSelector, fwd) < 0) {
1013 return Collections.emptyList();
1014 }
1015 forTableId = UNICAST_ROUTING_TABLE;
1016 if (fwd.treatment() != null) {
1017 for (Instruction instr : fwd.treatment().allInstructions()) {
1018 if (instr instanceof L3ModificationInstruction &&
1019 ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
1020 // XXX decrementing IP ttl is done automatically for routing, this
1021 // action is ignored or rejected in ofdpa as it is not fully implemented
1022 //tb.deferred().add(instr);
1023 }
1024 }
1025 }
Saurav Das8a0732e2015-11-20 15:27:53 -08001026 } else {
1027 filteredSelector
1028 .matchEthType(Ethernet.MPLS_UNICAST)
1029 .matchMplsLabel(((MplsCriterion)
1030 selector.getCriterion(Criterion.Type.MPLS_LABEL)).label());
1031 MplsBosCriterion bos = (MplsBosCriterion) selector
Pier Ventre140a8942016-11-02 07:26:38 -07001032 .getCriterion(MPLS_BOS);
Saurav Das8a0732e2015-11-20 15:27:53 -08001033 if (bos != null) {
1034 filteredSelector.matchMplsBos(bos.mplsBos());
1035 }
1036 forTableId = MPLS_TABLE_1;
Saurav Das4ce45962015-11-24 23:21:05 -08001037 log.debug("processing MPLS specific forwarding objective {} -> next:{}"
1038 + " in dev {}", fwd.id(), fwd.nextId(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -07001039
Charles Chan14967c22015-12-07 11:11:50 -08001040 if (fwd.treatment() != null) {
1041 for (Instruction instr : fwd.treatment().allInstructions()) {
1042 if (instr instanceof L2ModificationInstruction &&
1043 ((L2ModificationInstruction) instr).subtype() == L2SubType.MPLS_POP) {
Saurav Das8a0732e2015-11-20 15:27:53 -08001044 popMpls = true;
Saurav Das9c705342017-01-06 11:36:01 -08001045 // OF-DPA does not pop in MPLS table. Instead it requires
1046 // setting the MPLS_TYPE so pop can happen down the pipeline
Charles Chan14967c22015-12-07 11:11:50 -08001047 }
1048 if (instr instanceof L3ModificationInstruction &&
1049 ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
1050 // FIXME Should modify the app to send the correct DEC_MPLS_TTL instruction
1051 tb.immediate().decMplsTtl();
1052 }
1053 if (instr instanceof L3ModificationInstruction &&
1054 ((L3ModificationInstruction) instr).subtype() == L3SubType.TTL_IN) {
1055 tb.immediate().add(instr);
1056 }
Saurav Das8a0732e2015-11-20 15:27:53 -08001057 }
1058 }
1059 }
Saurav Das822c4e22015-10-23 10:51:11 -07001060
1061 if (fwd.nextId() != null) {
Saurav Das8a0732e2015-11-20 15:27:53 -08001062 if (forTableId == MPLS_TABLE_1 && !popMpls) {
1063 log.warn("SR CONTINUE case cannot be handled as MPLS ECMP "
Saurav Das25190812016-05-27 13:54:07 -07001064 + "is not implemented in OF-DPA yet. Aborting this flow {} -> next:{}"
1065 + "in this device {}", fwd.id(), fwd.nextId(), deviceId);
Pier Ventree0ae7a32016-11-23 09:57:42 -08001066 // XXX We could convert to forwarding to a single-port, via a MPLS interface,
1067 // or a MPLS SWAP (with-same) but that would have to be handled in the next-objective.
1068 // Also the pop-mpls logic used here won't work in non-BoS case.
Saurav Das4ce45962015-11-24 23:21:05 -08001069 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
Saurav Das8a0732e2015-11-20 15:27:53 -08001070 return Collections.emptySet();
1071 }
1072
Saurav Das423fe2b2015-12-04 10:52:59 -08001073 NextGroup next = getGroupForNextObjective(fwd.nextId());
1074 if (next != null) {
1075 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1076 // we only need the top level group's key to point the flow to it
1077 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
Pier Ventre140a8942016-11-02 07:26:38 -07001078 if (isNotMplsBos(selector) && group.type().equals(HASHED)) {
1079 log.warn("SR CONTINUE case cannot be handled as MPLS ECMP "
1080 + "is not implemented in OF-DPA yet. Aborting this flow {} -> next:{}"
1081 + "in this device {}", fwd.id(), fwd.nextId(), deviceId);
1082 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
1083 return Collections.emptySet();
1084 }
Saurav Das423fe2b2015-12-04 10:52:59 -08001085 if (group == null) {
1086 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
1087 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
1088 fail(fwd, ObjectiveError.GROUPMISSING);
1089 return Collections.emptySet();
1090 }
1091 tb.deferred().group(group.id());
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001092 // check if group is empty
1093 if (gkeys.size() == 1 && gkeys.get(0).size() == 1) {
1094 log.warn("Found empty group 0x{} in dev:{} .. will retry fwd:{}",
1095 Integer.toHexString(group.id().id()), deviceId, fwd.id());
1096 emptyGroup = true;
1097 }
Saurav Das25190812016-05-27 13:54:07 -07001098 } else {
1099 log.warn("Cannot find group for nextId:{} in dev:{}. Aborting fwd:{}",
1100 fwd.nextId(), deviceId, fwd.id());
1101 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
1102 return Collections.emptySet();
Saurav Das822c4e22015-10-23 10:51:11 -07001103 }
Saurav Das822c4e22015-10-23 10:51:11 -07001104 }
Charles Chancad338a2016-09-16 18:03:11 -07001105
1106 if (forTableId == MPLS_TABLE_1) {
Pier Ventre140a8942016-11-02 07:26:38 -07001107 if (mplsNextTable == MPLS_L3_TYPE_TABLE) {
Charles Chancad338a2016-09-16 18:03:11 -07001108 Ofdpa3SetMplsType setMplsType = new Ofdpa3SetMplsType(Ofdpa3MplsType.L3_PHP);
Saurav Das9c705342017-01-06 11:36:01 -08001109 // set mpls type as apply_action
1110 tb.immediate().extension(setMplsType, deviceId);
Charles Chancad338a2016-09-16 18:03:11 -07001111 }
1112 tb.transition(mplsNextTable);
1113 } else {
1114 tb.transition(ACL_TABLE);
1115 }
1116
Saurav Das822c4e22015-10-23 10:51:11 -07001117 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
1118 .fromApp(fwd.appId())
1119 .withPriority(fwd.priority())
1120 .forDevice(deviceId)
Saurav Das8a0732e2015-11-20 15:27:53 -08001121 .withSelector(filteredSelector.build())
1122 .withTreatment(tb.build())
1123 .forTable(forTableId);
Saurav Das822c4e22015-10-23 10:51:11 -07001124
1125 if (fwd.permanent()) {
1126 ruleBuilder.makePermanent();
1127 } else {
1128 ruleBuilder.makeTemporary(fwd.timeout());
1129 }
Flavio Castroe10fa242016-01-15 12:43:51 -08001130 Collection<FlowRule> flowRuleCollection = new ArrayList<>();
1131 flowRuleCollection.add(ruleBuilder.build());
1132 if (defaultRule) {
Pier Ventree0ae7a32016-11-23 09:57:42 -08001133 flowRuleCollection.add(
1134 defaultRoute(fwd, complementarySelector, forTableId, tb)
1135 );
Flavio Castroe10fa242016-01-15 12:43:51 -08001136 log.debug("Default rule 0.0.0.0/0 is being installed two rules");
1137 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001138 // XXX retrying flows may be necessary due to bug CORD-554
1139 if (emptyGroup) {
1140 executorService.schedule(new RetryFlows(fwd, flowRuleCollection),
1141 RETRY_MS, TimeUnit.MILLISECONDS);
1142 }
Flavio Castroe10fa242016-01-15 12:43:51 -08001143 return flowRuleCollection;
Saurav Das822c4e22015-10-23 10:51:11 -07001144 }
1145
Pier Ventree0ae7a32016-11-23 09:57:42 -08001146 protected FlowRule defaultRoute(ForwardingObjective fwd,
1147 TrafficSelector.Builder complementarySelector,
1148 int forTableId,
1149 TrafficTreatment.Builder tb) {
1150 FlowRule.Builder rule = DefaultFlowRule.builder()
1151 .fromApp(fwd.appId())
1152 .withPriority(fwd.priority())
1153 .forDevice(deviceId)
1154 .withSelector(complementarySelector.build())
1155 .withTreatment(tb.build())
1156 .forTable(forTableId);
1157 if (fwd.permanent()) {
1158 rule.makePermanent();
1159 } else {
1160 rule.makeTemporary(fwd.timeout());
1161 }
1162 return rule.build();
1163 }
1164
Saurav Das4ce45962015-11-24 23:21:05 -08001165 /**
1166 * Handles forwarding rules to the L2 bridging table. Flow actions are not
1167 * allowed in the bridging table - instead we use L2 Interface group or
1168 * L2 flood group
1169 *
1170 * @param fwd the forwarding objective
1171 * @return A collection of flow rules, or an empty set
1172 */
1173 protected Collection<FlowRule> processEthDstSpecific(ForwardingObjective fwd) {
1174 List<FlowRule> rules = new ArrayList<>();
1175
1176 // Build filtered selector
1177 TrafficSelector selector = fwd.selector();
1178 EthCriterion ethCriterion = (EthCriterion) selector
1179 .getCriterion(Criterion.Type.ETH_DST);
1180 VlanIdCriterion vlanIdCriterion = (VlanIdCriterion) selector
1181 .getCriterion(Criterion.Type.VLAN_VID);
1182
1183 if (vlanIdCriterion == null) {
1184 log.warn("Forwarding objective for bridging requires vlan. Not "
1185 + "installing fwd:{} in dev:{}", fwd.id(), deviceId);
1186 fail(fwd, ObjectiveError.BADPARAMS);
1187 return Collections.emptySet();
1188 }
1189
1190 TrafficSelector.Builder filteredSelectorBuilder =
1191 DefaultTrafficSelector.builder();
Yi Tseng3a77b4f2017-02-06 15:02:17 -08001192
1193 if (!ethCriterion.mac().equals(NONE) &&
1194 !ethCriterion.mac().equals(BROADCAST)) {
Saurav Das4ce45962015-11-24 23:21:05 -08001195 filteredSelectorBuilder.matchEthDst(ethCriterion.mac());
1196 log.debug("processing L2 forwarding objective:{} -> next:{} in dev:{}",
1197 fwd.id(), fwd.nextId(), deviceId);
1198 } else {
Yi Tseng3a77b4f2017-02-06 15:02:17 -08001199 // Use wildcard DST_MAC if the MacAddress is None or Broadcast
Saurav Das4ce45962015-11-24 23:21:05 -08001200 log.debug("processing L2 Broadcast forwarding objective:{} -> next:{} "
1201 + "in dev:{} for vlan:{}",
1202 fwd.id(), fwd.nextId(), deviceId, vlanIdCriterion.vlanId());
1203 }
Charles Chan14967c22015-12-07 11:11:50 -08001204 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vlanIdCriterion.vlanId());
1205 filteredSelectorBuilder.extension(ofdpaMatchVlanVid, deviceId);
Saurav Das4ce45962015-11-24 23:21:05 -08001206 TrafficSelector filteredSelector = filteredSelectorBuilder.build();
1207
1208 if (fwd.treatment() != null) {
1209 log.warn("Ignoring traffic treatment in fwd rule {} meant for L2 table"
1210 + "for dev:{}. Expecting only nextId", fwd.id(), deviceId);
1211 }
1212
1213 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
1214 if (fwd.nextId() != null) {
Saurav Das423fe2b2015-12-04 10:52:59 -08001215 NextGroup next = getGroupForNextObjective(fwd.nextId());
Saurav Das4ce45962015-11-24 23:21:05 -08001216 if (next != null) {
1217 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1218 // we only need the top level group's key to point the flow to it
1219 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
1220 if (group != null) {
1221 treatmentBuilder.deferred().group(group.id());
1222 } else {
1223 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
1224 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
1225 fail(fwd, ObjectiveError.GROUPMISSING);
1226 return Collections.emptySet();
1227 }
1228 }
1229 }
1230 treatmentBuilder.immediate().transition(ACL_TABLE);
1231 TrafficTreatment filteredTreatment = treatmentBuilder.build();
1232
1233 // Build bridging table entries
1234 FlowRule.Builder flowRuleBuilder = DefaultFlowRule.builder();
1235 flowRuleBuilder.fromApp(fwd.appId())
1236 .withPriority(fwd.priority())
1237 .forDevice(deviceId)
1238 .withSelector(filteredSelector)
1239 .withTreatment(filteredTreatment)
1240 .forTable(BRIDGING_TABLE);
1241 if (fwd.permanent()) {
1242 flowRuleBuilder.makePermanent();
1243 } else {
1244 flowRuleBuilder.makeTemporary(fwd.timeout());
1245 }
1246 rules.add(flowRuleBuilder.build());
1247 return rules;
1248 }
1249
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001250 //////////////////////////////////////
1251 // Helper Methods and Classes
1252 //////////////////////////////////////
1253
1254 private boolean isSupportedEthTypeObjective(ForwardingObjective fwd) {
1255 TrafficSelector selector = fwd.selector();
1256 EthTypeCriterion ethType = (EthTypeCriterion) selector
1257 .getCriterion(Criterion.Type.ETH_TYPE);
1258 return !((ethType == null) ||
1259 ((ethType.ethType().toShort() != Ethernet.TYPE_IPV4) &&
Pier Ventree0ae7a32016-11-23 09:57:42 -08001260 (ethType.ethType().toShort() != Ethernet.MPLS_UNICAST)) &&
1261 (ethType.ethType().toShort() != Ethernet.TYPE_IPV6));
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001262 }
1263
1264 private boolean isSupportedEthDstObjective(ForwardingObjective fwd) {
1265 TrafficSelector selector = fwd.selector();
1266 EthCriterion ethDst = (EthCriterion) selector
1267 .getCriterion(Criterion.Type.ETH_DST);
1268 VlanIdCriterion vlanId = (VlanIdCriterion) selector
1269 .getCriterion(Criterion.Type.VLAN_VID);
1270 return !(ethDst == null && vlanId == null);
1271 }
1272
Saurav Das423fe2b2015-12-04 10:52:59 -08001273 protected NextGroup getGroupForNextObjective(Integer nextId) {
1274 NextGroup next = flowObjectiveStore.getNextGroup(nextId);
1275 if (next != null) {
1276 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1277 if (gkeys != null && !gkeys.isEmpty()) {
1278 return next;
1279 } else {
1280 log.warn("Empty next group found in FlowObjective store for "
1281 + "next-id:{} in dev:{}", nextId, deviceId);
1282 }
1283 } else {
1284 log.warn("next-id {} not found in Flow objective store for dev:{}",
1285 nextId, deviceId);
1286 }
1287 return null;
1288 }
1289
Charles Chan188ebf52015-12-23 00:15:11 -08001290 protected static void pass(Objective obj) {
Sho SHIMIZUef7e2902016-02-12 18:38:29 -08001291 obj.context().ifPresent(context -> context.onSuccess(obj));
Saurav Das822c4e22015-10-23 10:51:11 -07001292 }
1293
Charles Chan188ebf52015-12-23 00:15:11 -08001294 protected static void fail(Objective obj, ObjectiveError error) {
Sho SHIMIZUef7e2902016-02-12 18:38:29 -08001295 obj.context().ifPresent(context -> context.onError(obj, error));
Saurav Das822c4e22015-10-23 10:51:11 -07001296 }
Saurav Das24431192016-03-07 19:13:00 -08001297
Saurav Das24431192016-03-07 19:13:00 -08001298 @Override
1299 public List<String> getNextMappings(NextGroup nextGroup) {
1300 List<String> mappings = new ArrayList<>();
1301 List<Deque<GroupKey>> gkeys = appKryo.deserialize(nextGroup.data());
1302 for (Deque<GroupKey> gkd : gkeys) {
1303 Group lastGroup = null;
Saurav Das8be4e3a2016-03-11 17:19:07 -08001304 StringBuffer gchain = new StringBuffer();
Saurav Das24431192016-03-07 19:13:00 -08001305 for (GroupKey gk : gkd) {
1306 Group g = groupService.getGroup(deviceId, gk);
Saurav Das8be4e3a2016-03-11 17:19:07 -08001307 if (g == null) {
Saurav Das25190812016-05-27 13:54:07 -07001308 gchain.append(" NoGrp").append(" -->");
Saurav Das8be4e3a2016-03-11 17:19:07 -08001309 continue;
1310 }
1311 gchain.append(" 0x").append(Integer.toHexString(g.id().id()))
1312 .append(" -->");
Saurav Das24431192016-03-07 19:13:00 -08001313 lastGroup = g;
1314 }
1315 // add port information for last group in group-chain
Saurav Das25190812016-05-27 13:54:07 -07001316 List<Instruction> lastGroupIns = new ArrayList<Instruction>();
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001317 if (lastGroup != null && !lastGroup.buckets().buckets().isEmpty()) {
Saurav Das25190812016-05-27 13:54:07 -07001318 lastGroupIns = lastGroup.buckets().buckets().get(0)
1319 .treatment().allInstructions();
1320 }
1321 for (Instruction i: lastGroupIns) {
Saurav Das24431192016-03-07 19:13:00 -08001322 if (i instanceof OutputInstruction) {
Saurav Das8be4e3a2016-03-11 17:19:07 -08001323 gchain.append(" port:").append(((OutputInstruction) i).port());
Saurav Das24431192016-03-07 19:13:00 -08001324 }
1325 }
Saurav Das8be4e3a2016-03-11 17:19:07 -08001326 mappings.add(gchain.toString());
Saurav Das24431192016-03-07 19:13:00 -08001327 }
1328 return mappings;
1329 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001330
Pier Ventre140a8942016-11-02 07:26:38 -07001331 static boolean isMplsBos(TrafficSelector selector) {
1332 MplsBosCriterion bosCriterion = (MplsBosCriterion) selector.getCriterion(MPLS_BOS);
1333 return bosCriterion != null && bosCriterion.mplsBos();
1334 }
1335
1336 static boolean isNotMplsBos(TrafficSelector selector) {
1337 MplsBosCriterion bosCriterion = (MplsBosCriterion) selector.getCriterion(MPLS_BOS);
1338 return bosCriterion != null && !bosCriterion.mplsBos();
1339 }
1340
Charles Chan5b9df8d2016-03-28 22:21:40 -07001341 protected static VlanId readVlanFromSelector(TrafficSelector selector) {
Saurav Das59232cf2016-04-27 18:35:50 -07001342 if (selector == null) {
1343 return null;
1344 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001345 Criterion criterion = selector.getCriterion(Criterion.Type.VLAN_VID);
1346 return (criterion == null)
1347 ? null : ((VlanIdCriterion) criterion).vlanId();
1348 }
1349
1350 protected static IpPrefix readIpDstFromSelector(TrafficSelector selector) {
Saurav Das59232cf2016-04-27 18:35:50 -07001351 if (selector == null) {
1352 return null;
1353 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001354 Criterion criterion = selector.getCriterion(Criterion.Type.IPV4_DST);
1355 return (criterion == null) ? null : ((IPCriterion) criterion).ip();
1356 }
Charles Chand55e84d2016-03-30 17:54:24 -07001357
1358 private static VlanId readVlanFromTreatment(TrafficTreatment treatment) {
Saurav Das59232cf2016-04-27 18:35:50 -07001359 if (treatment == null) {
1360 return null;
1361 }
Charles Chand55e84d2016-03-30 17:54:24 -07001362 for (Instruction i : treatment.allInstructions()) {
1363 if (i instanceof ModVlanIdInstruction) {
1364 return ((ModVlanIdInstruction) i).vlanId();
1365 }
1366 }
1367 return null;
1368 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001369
1370 /**
1371 * Utility class that retries sending flows a fixed number of times, even if
1372 * some of the attempts are successful. Used only for forwarding objectives.
1373 */
1374 protected final class RetryFlows implements Runnable {
1375 int attempts = MAX_RETRY_ATTEMPTS;
1376 private Collection<FlowRule> retryFlows;
1377 private ForwardingObjective fwd;
1378
1379 RetryFlows(ForwardingObjective fwd, Collection<FlowRule> retryFlows) {
1380 this.fwd = fwd;
1381 this.retryFlows = retryFlows;
1382 }
1383
1384 @Override
1385 public void run() {
1386 log.info("RETRY FLOWS ATTEMPT# {} for fwd:{} rules:{}",
1387 MAX_RETRY_ATTEMPTS - attempts, fwd.id(), retryFlows.size());
1388 sendForward(fwd, retryFlows);
1389 if (--attempts > 0) {
1390 executorService.schedule(this, RETRY_MS, TimeUnit.MILLISECONDS);
1391 }
1392 }
1393 }
1394
Saurav Das822c4e22015-10-23 10:51:11 -07001395}