blob: a49e4fa1b26a0db97b7d427f0059f3f9af207a05 [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 {
Saurav Das822c4e22015-10-23 10:51:11 -0700105 protected static final int PORT_TABLE = 0;
106 protected static final int VLAN_TABLE = 10;
107 protected static final int TMAC_TABLE = 20;
108 protected static final int UNICAST_ROUTING_TABLE = 30;
109 protected static final int MULTICAST_ROUTING_TABLE = 40;
110 protected static final int MPLS_TABLE_0 = 23;
111 protected static final int MPLS_TABLE_1 = 24;
Pier Ventre140a8942016-11-02 07:26:38 -0700112 protected static final int MPLS_L3_TYPE_TABLE = 27;
113 protected static final int MPLS_TYPE_TABLE = 29;
Saurav Das822c4e22015-10-23 10:51:11 -0700114 protected static final int BRIDGING_TABLE = 50;
115 protected static final int ACL_TABLE = 60;
116 protected static final int MAC_LEARNING_TABLE = 254;
117 protected static final long OFPP_MAX = 0xffffff00L;
118
Saurav Das52025962016-01-28 22:30:01 -0800119 protected static final int HIGHEST_PRIORITY = 0xffff;
Saurav Das2857f382015-11-03 14:39:27 -0800120 protected static final int DEFAULT_PRIORITY = 0x8000;
Saurav Das822c4e22015-10-23 10:51:11 -0700121 protected static final int LOWEST_PRIORITY = 0x0;
122
Saurav Das822c4e22015-10-23 10:51:11 -0700123 private final Logger log = getLogger(getClass());
Charles Chan425854b2016-04-11 15:32:12 -0700124 protected ServiceDirectory serviceDirectory;
Saurav Das822c4e22015-10-23 10:51:11 -0700125 protected FlowRuleService flowRuleService;
Charles Chan425854b2016-04-11 15:32:12 -0700126 protected CoreService coreService;
Saurav Das8a0732e2015-11-20 15:27:53 -0800127 protected GroupService groupService;
128 protected FlowObjectiveStore flowObjectiveStore;
Saurav Das822c4e22015-10-23 10:51:11 -0700129 protected DeviceId deviceId;
130 protected ApplicationId driverId;
Saurav Das822c4e22015-10-23 10:51:11 -0700131 protected DeviceService deviceService;
Charles Chan188ebf52015-12-23 00:15:11 -0800132 protected static KryoNamespace appKryo = new KryoNamespace.Builder()
Saurav Das822c4e22015-10-23 10:51:11 -0700133 .register(KryoNamespaces.API)
134 .register(GroupKey.class)
135 .register(DefaultGroupKey.class)
Charles Chan361154b2016-03-24 10:23:39 -0700136 .register(Ofdpa2GroupHandler.OfdpaNextGroup.class)
Saurav Das8a0732e2015-11-20 15:27:53 -0800137 .register(ArrayDeque.class)
Charles Chaneefdedf2016-05-23 16:45:45 -0700138 .build("Ofdpa2Pipeline");
Saurav Das822c4e22015-10-23 10:51:11 -0700139
Charles Chan425854b2016-04-11 15:32:12 -0700140 protected Ofdpa2GroupHandler groupHandler;
Saurav Das822c4e22015-10-23 10:51:11 -0700141
Saurav Das52025962016-01-28 22:30:01 -0800142 protected Set<IPCriterion> sentIpFilters = Collections.newSetFromMap(
Charles Chan188ebf52015-12-23 00:15:11 -0800143 new ConcurrentHashMap<>());
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700144 // flows installations to be retried
145 protected ScheduledExecutorService executorService
146 = newScheduledThreadPool(5, groupedThreads("OfdpaPipeliner", "retry-%d", log));
147 protected static final int MAX_RETRY_ATTEMPTS = 10;
148 protected static final int RETRY_MS = 1000;
Saurav Das4f980082015-11-05 13:39:15 -0800149
Saurav Das822c4e22015-10-23 10:51:11 -0700150 @Override
151 public void init(DeviceId deviceId, PipelinerContext context) {
Saurav Das822c4e22015-10-23 10:51:11 -0700152 this.deviceId = deviceId;
153
Charles Chan425854b2016-04-11 15:32:12 -0700154 serviceDirectory = context.directory();
Saurav Das822c4e22015-10-23 10:51:11 -0700155 coreService = serviceDirectory.get(CoreService.class);
156 flowRuleService = serviceDirectory.get(FlowRuleService.class);
157 groupService = serviceDirectory.get(GroupService.class);
158 flowObjectiveStore = context.store();
Saurav Das822c4e22015-10-23 10:51:11 -0700159 deviceService = serviceDirectory.get(DeviceService.class);
Saurav Das822c4e22015-10-23 10:51:11 -0700160
Charles Chan40132b32017-01-22 00:19:37 -0800161 initDriverId();
162 initGroupHander(context);
Saurav Das822c4e22015-10-23 10:51:11 -0700163
Saurav Das822c4e22015-10-23 10:51:11 -0700164 initializePipeline();
Saurav Das822c4e22015-10-23 10:51:11 -0700165 }
166
Charles Chan40132b32017-01-22 00:19:37 -0800167 protected void initDriverId() {
168 driverId = coreService.registerApplication(
169 "org.onosproject.driver.Ofdpa2Pipeline");
170 }
171
172 protected void initGroupHander(PipelinerContext context) {
173 groupHandler = new Ofdpa2GroupHandler();
174 groupHandler.init(deviceId, context);
175 }
176
Saurav Das822c4e22015-10-23 10:51:11 -0700177 protected void initializePipeline() {
Charles Chan188ebf52015-12-23 00:15:11 -0800178 // OF-DPA does not require initializing the pipeline as it puts default
179 // rules automatically in the hardware. However emulation of OFDPA in
180 // software switches does require table-miss-entries.
Saurav Das822c4e22015-10-23 10:51:11 -0700181 }
182
183 //////////////////////////////////////
184 // Flow Objectives
185 //////////////////////////////////////
186
187 @Override
188 public void filter(FilteringObjective filteringObjective) {
189 if (filteringObjective.type() == FilteringObjective.Type.PERMIT) {
190 processFilter(filteringObjective,
191 filteringObjective.op() == Objective.Operation.ADD,
192 filteringObjective.appId());
193 } else {
194 // Note that packets that don't match the PERMIT filter are
195 // automatically denied. The DENY filter is used to deny packets
196 // that are otherwise permitted by the PERMIT filter.
197 // Use ACL table flow rules here for DENY filtering objectives
198 log.debug("filter objective other than PERMIT currently not supported");
199 fail(filteringObjective, ObjectiveError.UNSUPPORTED);
200 }
201 }
202
203 @Override
204 public void forward(ForwardingObjective fwd) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700205 Collection<FlowRule> rules = processForward(fwd);
Saurav Das25190812016-05-27 13:54:07 -0700206 if (rules == null || rules.isEmpty()) {
207 // Assumes fail message has already been generated to the objective
208 // context. Returning here prevents spurious pass message to be
209 // generated by FlowRule service for empty flowOps.
210 return;
211 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700212 sendForward(fwd, rules);
213 }
214
215 protected void sendForward(ForwardingObjective fwd, Collection<FlowRule> rules) {
216 FlowRuleOperations.Builder flowOpsBuilder = FlowRuleOperations.builder();
Saurav Das822c4e22015-10-23 10:51:11 -0700217 switch (fwd.op()) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700218 case ADD:
219 rules.stream()
220 .filter(Objects::nonNull)
221 .forEach(flowOpsBuilder::add);
Saurav Dasd2fded02016-12-02 15:43:47 -0800222 log.debug("Applying a add fwd-obj {} to sw:{}", fwd.id(), deviceId);
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700223 break;
224 case REMOVE:
225 rules.stream()
226 .filter(Objects::nonNull)
227 .forEach(flowOpsBuilder::remove);
228 break;
229 default:
230 fail(fwd, ObjectiveError.UNKNOWN);
231 log.warn("Unknown forwarding type {}", fwd.op());
Saurav Das822c4e22015-10-23 10:51:11 -0700232 }
233
Saurav Das822c4e22015-10-23 10:51:11 -0700234 flowRuleService.apply(flowOpsBuilder.build(new FlowRuleOperationsContext() {
235 @Override
236 public void onSuccess(FlowRuleOperations ops) {
237 pass(fwd);
238 }
239
240 @Override
241 public void onError(FlowRuleOperations ops) {
242 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
243 }
244 }));
Saurav Das822c4e22015-10-23 10:51:11 -0700245 }
246
247 @Override
248 public void next(NextObjective nextObjective) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800249 NextGroup nextGroup = flowObjectiveStore.getNextGroup(nextObjective.id());
250 switch (nextObjective.op()) {
251 case ADD:
Saurav Das4f980082015-11-05 13:39:15 -0800252 if (nextGroup != null) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800253 log.warn("Cannot add next {} that already exists in device {}",
254 nextObjective.id(), deviceId);
255 return;
256 }
257 log.debug("Processing NextObjective id{} in dev{} - add group",
258 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700259 groupHandler.addGroup(nextObjective);
Saurav Das8a0732e2015-11-20 15:27:53 -0800260 break;
261 case ADD_TO_EXISTING:
262 if (nextGroup != null) {
263 log.debug("Processing NextObjective id{} in dev{} - add bucket",
264 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700265 groupHandler.addBucketToGroup(nextObjective, nextGroup);
Saurav Das4f980082015-11-05 13:39:15 -0800266 } else {
Saurav Das8a0732e2015-11-20 15:27:53 -0800267 // it is possible that group-chain has not been fully created yet
Saurav Das423fe2b2015-12-04 10:52:59 -0800268 log.debug("Waiting to add bucket to group for next-id:{} in dev:{}",
269 nextObjective.id(), deviceId);
270 // by design only one pending bucket is allowed for the group
Charles Chan425854b2016-04-11 15:32:12 -0700271 groupHandler.pendingBuckets.put(nextObjective.id(), nextObjective);
Saurav Das4f980082015-11-05 13:39:15 -0800272 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800273 break;
274 case REMOVE:
275 if (nextGroup == null) {
276 log.warn("Cannot remove next {} that does not exist in device {}",
277 nextObjective.id(), deviceId);
278 return;
279 }
280 log.debug("Processing NextObjective id{} in dev{} - remove group",
281 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700282 groupHandler.removeGroup(nextObjective, nextGroup);
Saurav Das8a0732e2015-11-20 15:27:53 -0800283 break;
284 case REMOVE_FROM_EXISTING:
285 if (nextGroup == null) {
286 log.warn("Cannot remove from next {} that does not exist in device {}",
287 nextObjective.id(), deviceId);
288 return;
289 }
290 log.debug("Processing NextObjective id{} in dev{} - remove bucket",
291 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700292 groupHandler.removeBucketFromGroup(nextObjective, nextGroup);
Saurav Das8a0732e2015-11-20 15:27:53 -0800293 break;
294 default:
Saurav Das4f980082015-11-05 13:39:15 -0800295 log.warn("Unsupported operation {}", nextObjective.op());
Saurav Das822c4e22015-10-23 10:51:11 -0700296 }
297 }
298
299 //////////////////////////////////////
300 // Flow handling
301 //////////////////////////////////////
302
303 /**
304 * As per OFDPA 2.0 TTP, filtering of VLAN ids, MAC addresses (for routing)
305 * and IP addresses configured on switch ports happen in different tables.
306 * Note that IP filtering rules need to be added to the ACL table, as there
307 * is no mechanism to send to controller via IP table.
308 *
309 * @param filt the filtering objective
310 * @param install indicates whether to add or remove the objective
311 * @param applicationId the application that sent this objective
312 */
Saurav Das52025962016-01-28 22:30:01 -0800313 protected void processFilter(FilteringObjective filt,
314 boolean install, ApplicationId applicationId) {
Saurav Das822c4e22015-10-23 10:51:11 -0700315 // This driver only processes filtering criteria defined with switch
316 // ports as the key
317 PortCriterion portCriterion = null;
318 EthCriterion ethCriterion = null;
319 VlanIdCriterion vidCriterion = null;
320 Collection<IPCriterion> ips = new ArrayList<IPCriterion>();
321 if (!filt.key().equals(Criteria.dummy()) &&
322 filt.key().type() == Criterion.Type.IN_PORT) {
323 portCriterion = (PortCriterion) filt.key();
324 } else {
325 log.warn("No key defined in filtering objective from app: {}. Not"
326 + "processing filtering objective", applicationId);
Saurav Das59232cf2016-04-27 18:35:50 -0700327 fail(filt, ObjectiveError.BADPARAMS);
Saurav Das822c4e22015-10-23 10:51:11 -0700328 return;
329 }
Saurav Das59232cf2016-04-27 18:35:50 -0700330 log.debug("Received filtering objective for dev/port: {}/{}", deviceId,
331 portCriterion.port());
Saurav Das822c4e22015-10-23 10:51:11 -0700332 // convert filtering conditions for switch-intfs into flowrules
333 FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
334 for (Criterion criterion : filt.conditions()) {
Charles Chan5b9df8d2016-03-28 22:21:40 -0700335 if (criterion.type() == Criterion.Type.ETH_DST ||
336 criterion.type() == Criterion.Type.ETH_DST_MASKED) {
Saurav Das822c4e22015-10-23 10:51:11 -0700337 ethCriterion = (EthCriterion) criterion;
338 } else if (criterion.type() == Criterion.Type.VLAN_VID) {
339 vidCriterion = (VlanIdCriterion) criterion;
340 } else if (criterion.type() == Criterion.Type.IPV4_DST) {
341 ips.add((IPCriterion) criterion);
342 } else {
343 log.error("Unsupported filter {}", criterion);
344 fail(filt, ObjectiveError.UNSUPPORTED);
345 return;
346 }
347 }
348
Saurav Das0e99e2b2015-10-28 12:39:42 -0700349 VlanId assignedVlan = null;
Charles Chane849c192016-01-11 18:28:54 -0800350 if (vidCriterion != null) {
Charles Chand55e84d2016-03-30 17:54:24 -0700351 // Use the VLAN in metadata whenever a metadata is provided
352 if (filt.meta() != null) {
353 assignedVlan = readVlanFromTreatment(filt.meta());
354 // Use the VLAN in criterion if metadata is not present and the traffic is tagged
355 } else if (!vidCriterion.vlanId().equals(VlanId.NONE)) {
Charles Chane849c192016-01-11 18:28:54 -0800356 assignedVlan = vidCriterion.vlanId();
Charles Chand55e84d2016-03-30 17:54:24 -0700357 }
Charles Chane849c192016-01-11 18:28:54 -0800358
Charles Chand55e84d2016-03-30 17:54:24 -0700359 if (assignedVlan == null) {
360 log.error("Driver fails to extract VLAN information. "
361 + "Not proccessing VLAN filters on device {}.", deviceId);
362 log.debug("VLAN ID in criterion={}, metadata={}",
363 readVlanFromTreatment(filt.meta()), vidCriterion.vlanId());
364 fail(filt, ObjectiveError.BADPARAMS);
365 return;
Saurav Das0e99e2b2015-10-28 12:39:42 -0700366 }
367 }
368
Yi Tseng3a77b4f2017-02-06 15:02:17 -0800369 if (ethCriterion == null || ethCriterion.mac().equals(NONE)) {
Charles Chan985b12e2016-05-11 19:47:22 -0700370 // NOTE: it is possible that a filtering objective only has vidCriterion
371 log.debug("filtering objective missing dstMac, cannot program TMAC table");
Saurav Das822c4e22015-10-23 10:51:11 -0700372 } else {
373 for (FlowRule tmacRule : processEthDstFilter(portCriterion, ethCriterion,
Saurav Das0e99e2b2015-10-28 12:39:42 -0700374 vidCriterion, assignedVlan,
375 applicationId)) {
Saurav Das822c4e22015-10-23 10:51:11 -0700376 log.debug("adding MAC filtering rules in TMAC table: {} for dev: {}",
377 tmacRule, deviceId);
378 ops = install ? ops.add(tmacRule) : ops.remove(tmacRule);
379 }
380 }
381
Charles Chan985b12e2016-05-11 19:47:22 -0700382 if (vidCriterion == null) {
383 // NOTE: it is possible that a filtering objective only has ethCriterion
384 log.debug("filtering objective missing dstMac or VLAN, "
Charles Chane849c192016-01-11 18:28:54 -0800385 + "cannot program VLAN Table");
Saurav Das822c4e22015-10-23 10:51:11 -0700386 } else {
Charles Chan14967c22015-12-07 11:11:50 -0800387 /*
388 * NOTE: Separate vlan filtering rules and assignment rules
389 * into different stage in order to guarantee that filtering rules
Saurav Das52025962016-01-28 22:30:01 -0800390 * always go first, as required by ofdpa.
Charles Chan14967c22015-12-07 11:11:50 -0800391 */
392 List<FlowRule> allRules = processVlanIdFilter(
393 portCriterion, vidCriterion, assignedVlan, applicationId);
394 List<FlowRule> filteringRules = new ArrayList<>();
395 List<FlowRule> assignmentRules = new ArrayList<>();
396
397 allRules.forEach(flowRule -> {
398 ExtensionCriterion extCriterion =
399 (ExtensionCriterion) flowRule.selector().getCriterion(Criterion.Type.EXTENSION);
400 VlanId vlanId = ((OfdpaMatchVlanVid) extCriterion.extensionSelector()).vlanId();
Charles Chanbe8aea42016-02-24 12:04:47 -0800401 if (!vlanId.equals(VlanId.NONE)) {
Charles Chan14967c22015-12-07 11:11:50 -0800402 filteringRules.add(flowRule);
403 } else {
404 assignmentRules.add(flowRule);
405 }
406 });
407
408 for (FlowRule filteringRule : filteringRules) {
Saurav Das822c4e22015-10-23 10:51:11 -0700409 log.debug("adding VLAN filtering rule in VLAN table: {} for dev: {}",
Charles Chan14967c22015-12-07 11:11:50 -0800410 filteringRule, deviceId);
411 ops = install ? ops.add(filteringRule) : ops.remove(filteringRule);
412 }
413
414 ops.newStage();
415
416 for (FlowRule assignmentRule : assignmentRules) {
417 log.debug("adding VLAN assignment rule in VLAN table: {} for dev: {}",
418 assignmentRule, deviceId);
419 ops = install ? ops.add(assignmentRule) : ops.remove(assignmentRule);
Saurav Das822c4e22015-10-23 10:51:11 -0700420 }
421 }
422
423 for (IPCriterion ipaddr : ips) {
424 // since we ignore port information for IP rules, and the same (gateway) IP
425 // can be configured on multiple ports, we make sure that we send
426 // only a single rule to the switch.
427 if (!sentIpFilters.contains(ipaddr)) {
428 sentIpFilters.add(ipaddr);
429 log.debug("adding IP filtering rules in ACL table {} for dev: {}",
430 ipaddr, deviceId);
431 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
432 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
433 selector.matchEthType(Ethernet.TYPE_IPV4);
434 selector.matchIPDst(ipaddr.ip());
435 treatment.setOutput(PortNumber.CONTROLLER);
436 FlowRule rule = DefaultFlowRule.builder()
437 .forDevice(deviceId)
438 .withSelector(selector.build())
439 .withTreatment(treatment.build())
440 .withPriority(HIGHEST_PRIORITY)
441 .fromApp(applicationId)
442 .makePermanent()
443 .forTable(ACL_TABLE).build();
444 ops = install ? ops.add(rule) : ops.remove(rule);
445 }
446 }
447
448 // apply filtering flow rules
449 flowRuleService.apply(ops.build(new FlowRuleOperationsContext() {
450 @Override
451 public void onSuccess(FlowRuleOperations ops) {
452 log.info("Applied {} filtering rules in device {}",
453 ops.stages().get(0).size(), deviceId);
454 pass(filt);
455 }
456
457 @Override
458 public void onError(FlowRuleOperations ops) {
459 log.info("Failed to apply all filtering rules in dev {}", deviceId);
460 fail(filt, ObjectiveError.FLOWINSTALLATIONFAILED);
461 }
462 }));
463
464 }
465
466 /**
467 * Allows untagged packets into pipeline by assigning a vlan id.
Saurav Das0e99e2b2015-10-28 12:39:42 -0700468 * Vlan assignment is done by the application.
Saurav Das822c4e22015-10-23 10:51:11 -0700469 * Allows tagged packets into pipeline as per configured port-vlan info.
Saurav Das0e99e2b2015-10-28 12:39:42 -0700470 *
Charles Chanf9e98652016-09-07 16:54:23 -0700471 * @param portCriterion port on device for which this filter is programmed
472 * @param vidCriterion vlan assigned to port, or NONE for untagged
473 * @param assignedVlan assigned vlan-id for untagged packets
474 * @param applicationId for application programming this filter
Saurav Das822c4e22015-10-23 10:51:11 -0700475 * @return list of FlowRule for port-vlan filters
476 */
477 protected List<FlowRule> processVlanIdFilter(PortCriterion portCriterion,
478 VlanIdCriterion vidCriterion,
Saurav Das0e99e2b2015-10-28 12:39:42 -0700479 VlanId assignedVlan,
Saurav Das822c4e22015-10-23 10:51:11 -0700480 ApplicationId applicationId) {
Charles Chanf9e98652016-09-07 16:54:23 -0700481 return processVlanIdFilterInternal(portCriterion, vidCriterion, assignedVlan,
482 applicationId, true);
483 }
484
485 /**
486 * Internal implementation of processVlanIdFilter.
487 * <p>
488 * The is_present bit in set_vlan_vid action is required to be 0 in OFDPA i12.
489 * Since it is non-OF spec, we need an extension treatment for that.
490 * The useSetVlanExtension must be set to false for OFDPA i12.
491 * </p>
492 *
493 * @param portCriterion port on device for which this filter is programmed
494 * @param vidCriterion vlan assigned to port, or NONE for untagged
495 * @param assignedVlan assigned vlan-id for untagged packets
496 * @param applicationId for application programming this filter
497 * @param useSetVlanExtension use the setVlanVid extension that has is_present bit set to 0.
498 * @return list of FlowRule for port-vlan filters
499 */
500 protected List<FlowRule> processVlanIdFilterInternal(PortCriterion portCriterion,
501 VlanIdCriterion vidCriterion,
502 VlanId assignedVlan,
503 ApplicationId applicationId,
504 boolean useSetVlanExtension) {
Charles Chan14967c22015-12-07 11:11:50 -0800505 List<FlowRule> rules = new ArrayList<>();
Saurav Das822c4e22015-10-23 10:51:11 -0700506 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
507 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
Charles Chan14967c22015-12-07 11:11:50 -0800508 TrafficSelector.Builder preSelector = null;
509 TrafficTreatment.Builder preTreatment = null;
510
Saurav Das4f980082015-11-05 13:39:15 -0800511 treatment.transition(TMAC_TABLE);
512
Saurav Das822c4e22015-10-23 10:51:11 -0700513 if (vidCriterion.vlanId() == VlanId.NONE) {
514 // untagged packets are assigned vlans
Charles Chanbe8aea42016-02-24 12:04:47 -0800515 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(VlanId.NONE);
Charles Chan14967c22015-12-07 11:11:50 -0800516 selector.extension(ofdpaMatchVlanVid, deviceId);
Charles Chanf9e98652016-09-07 16:54:23 -0700517 if (useSetVlanExtension) {
518 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(assignedVlan);
519 treatment.extension(ofdpaSetVlanVid, deviceId);
520 } else {
521 treatment.setVlanId(assignedVlan);
522 }
Charles Chan14967c22015-12-07 11:11:50 -0800523
524 preSelector = DefaultTrafficSelector.builder();
525 OfdpaMatchVlanVid preOfdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
526 preSelector.extension(preOfdpaMatchVlanVid, deviceId);
527 preTreatment = DefaultTrafficTreatment.builder().transition(TMAC_TABLE);
528
Saurav Das4f980082015-11-05 13:39:15 -0800529 } else {
Charles Chan14967c22015-12-07 11:11:50 -0800530 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vidCriterion.vlanId());
531 selector.extension(ofdpaMatchVlanVid, deviceId);
Charles Chand55e84d2016-03-30 17:54:24 -0700532
533 if (!assignedVlan.equals(vidCriterion.vlanId())) {
Charles Chanc03782d2017-01-31 13:57:55 -0800534 if (useSetVlanExtension) {
535 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(assignedVlan);
536 treatment.extension(ofdpaSetVlanVid, deviceId);
537 } else {
538 treatment.setVlanId(assignedVlan);
539 }
Charles Chand55e84d2016-03-30 17:54:24 -0700540 }
Saurav Das822c4e22015-10-23 10:51:11 -0700541 }
Saurav Das822c4e22015-10-23 10:51:11 -0700542
543 // ofdpa cannot match on ALL portnumber, so we need to use separate
544 // rules for each port.
Charles Chan14967c22015-12-07 11:11:50 -0800545 List<PortNumber> portnums = new ArrayList<>();
Saurav Das822c4e22015-10-23 10:51:11 -0700546 if (portCriterion.port() == PortNumber.ALL) {
547 for (Port port : deviceService.getPorts(deviceId)) {
548 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
549 portnums.add(port.number());
550 }
551 }
552 } else {
553 portnums.add(portCriterion.port());
554 }
Saurav Das4f980082015-11-05 13:39:15 -0800555
Saurav Das822c4e22015-10-23 10:51:11 -0700556 for (PortNumber pnum : portnums) {
Saurav Das4f980082015-11-05 13:39:15 -0800557 // create rest of flowrule
Saurav Das822c4e22015-10-23 10:51:11 -0700558 selector.matchInPort(pnum);
559 FlowRule rule = DefaultFlowRule.builder()
560 .forDevice(deviceId)
561 .withSelector(selector.build())
562 .withTreatment(treatment.build())
563 .withPriority(DEFAULT_PRIORITY)
564 .fromApp(applicationId)
565 .makePermanent()
566 .forTable(VLAN_TABLE).build();
Charles Chan14967c22015-12-07 11:11:50 -0800567
568 if (preSelector != null) {
569 preSelector.matchInPort(pnum);
570 FlowRule preRule = DefaultFlowRule.builder()
571 .forDevice(deviceId)
572 .withSelector(preSelector.build())
573 .withTreatment(preTreatment.build())
574 .withPriority(DEFAULT_PRIORITY)
575 .fromApp(applicationId)
576 .makePermanent()
577 .forTable(VLAN_TABLE).build();
578 rules.add(preRule);
579 }
580
Saurav Das822c4e22015-10-23 10:51:11 -0700581 rules.add(rule);
582 }
583 return rules;
584 }
585
586 /**
587 * Allows routed packets with correct destination MAC to be directed
588 * to unicast-IP routing table or MPLS forwarding table.
Saurav Das822c4e22015-10-23 10:51:11 -0700589 *
590 * @param portCriterion port on device for which this filter is programmed
591 * @param ethCriterion dstMac of device for which is filter is programmed
592 * @param vidCriterion vlan assigned to port, or NONE for untagged
Saurav Das0e99e2b2015-10-28 12:39:42 -0700593 * @param assignedVlan assigned vlan-id for untagged packets
Saurav Das822c4e22015-10-23 10:51:11 -0700594 * @param applicationId for application programming this filter
595 * @return list of FlowRule for port-vlan filters
596
597 */
598 protected List<FlowRule> processEthDstFilter(PortCriterion portCriterion,
599 EthCriterion ethCriterion,
600 VlanIdCriterion vidCriterion,
Saurav Das0e99e2b2015-10-28 12:39:42 -0700601 VlanId assignedVlan,
Saurav Das822c4e22015-10-23 10:51:11 -0700602 ApplicationId applicationId) {
Charles Chan5270ed02016-01-30 23:22:37 -0800603 // Consider PortNumber.ANY as wildcard. Match ETH_DST only
604 if (portCriterion != null && portCriterion.port() == PortNumber.ANY) {
605 return processEthDstOnlyFilter(ethCriterion, applicationId);
606 }
607
Charles Chan5b9df8d2016-03-28 22:21:40 -0700608 // Multicast MAC
609 if (ethCriterion.mask() != null) {
610 return processMcastEthDstFilter(ethCriterion, applicationId);
611 }
612
Saurav Das822c4e22015-10-23 10:51:11 -0700613 //handling untagged packets via assigned VLAN
614 if (vidCriterion.vlanId() == VlanId.NONE) {
Saurav Das0e99e2b2015-10-28 12:39:42 -0700615 vidCriterion = (VlanIdCriterion) Criteria.matchVlanId(assignedVlan);
Saurav Das822c4e22015-10-23 10:51:11 -0700616 }
617 // ofdpa cannot match on ALL portnumber, so we need to use separate
618 // rules for each port.
619 List<PortNumber> portnums = new ArrayList<PortNumber>();
620 if (portCriterion.port() == PortNumber.ALL) {
621 for (Port port : deviceService.getPorts(deviceId)) {
622 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
623 portnums.add(port.number());
624 }
625 }
626 } else {
627 portnums.add(portCriterion.port());
628 }
629
630 List<FlowRule> rules = new ArrayList<FlowRule>();
631 for (PortNumber pnum : portnums) {
Charles Chan14967c22015-12-07 11:11:50 -0800632 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vidCriterion.vlanId());
Saurav Das822c4e22015-10-23 10:51:11 -0700633 // for unicast IP packets
634 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
635 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
636 selector.matchInPort(pnum);
Charles Chan14967c22015-12-07 11:11:50 -0800637 selector.extension(ofdpaMatchVlanVid, deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700638 selector.matchEthType(Ethernet.TYPE_IPV4);
639 selector.matchEthDst(ethCriterion.mac());
640 treatment.transition(UNICAST_ROUTING_TABLE);
641 FlowRule rule = DefaultFlowRule.builder()
642 .forDevice(deviceId)
643 .withSelector(selector.build())
644 .withTreatment(treatment.build())
645 .withPriority(DEFAULT_PRIORITY)
646 .fromApp(applicationId)
647 .makePermanent()
648 .forTable(TMAC_TABLE).build();
649 rules.add(rule);
650 //for MPLS packets
651 selector = DefaultTrafficSelector.builder();
652 treatment = DefaultTrafficTreatment.builder();
653 selector.matchInPort(pnum);
Charles Chan14967c22015-12-07 11:11:50 -0800654 selector.extension(ofdpaMatchVlanVid, deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700655 selector.matchEthType(Ethernet.MPLS_UNICAST);
656 selector.matchEthDst(ethCriterion.mac());
657 treatment.transition(MPLS_TABLE_0);
658 rule = DefaultFlowRule.builder()
659 .forDevice(deviceId)
660 .withSelector(selector.build())
661 .withTreatment(treatment.build())
662 .withPriority(DEFAULT_PRIORITY)
663 .fromApp(applicationId)
664 .makePermanent()
665 .forTable(TMAC_TABLE).build();
666 rules.add(rule);
Pier Ventree0ae7a32016-11-23 09:57:42 -0800667 /*
668 * TMAC rules for IPv6 packets
669 */
670 selector = DefaultTrafficSelector.builder();
671 treatment = DefaultTrafficTreatment.builder();
672 selector.matchInPort(pnum);
673 selector.extension(ofdpaMatchVlanVid, deviceId);
674 selector.matchEthType(Ethernet.TYPE_IPV6);
675 selector.matchEthDst(ethCriterion.mac());
676 treatment.transition(UNICAST_ROUTING_TABLE);
677 rule = DefaultFlowRule.builder()
678 .forDevice(deviceId)
679 .withSelector(selector.build())
680 .withTreatment(treatment.build())
681 .withPriority(DEFAULT_PRIORITY)
682 .fromApp(applicationId)
683 .makePermanent()
684 .forTable(TMAC_TABLE).build();
685 rules.add(rule);
Saurav Das822c4e22015-10-23 10:51:11 -0700686 }
687 return rules;
688 }
689
Charles Chan5270ed02016-01-30 23:22:37 -0800690 protected List<FlowRule> processEthDstOnlyFilter(EthCriterion ethCriterion,
691 ApplicationId applicationId) {
Pier Luigi0e358632017-01-31 09:35:05 -0800692 ImmutableList.Builder<FlowRule> builder = ImmutableList.builder();
693
Charles Chan5270ed02016-01-30 23:22:37 -0800694 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
695 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
696 selector.matchEthType(Ethernet.TYPE_IPV4);
697 selector.matchEthDst(ethCriterion.mac());
698 treatment.transition(UNICAST_ROUTING_TABLE);
699 FlowRule rule = DefaultFlowRule.builder()
700 .forDevice(deviceId)
701 .withSelector(selector.build())
702 .withTreatment(treatment.build())
703 .withPriority(DEFAULT_PRIORITY)
704 .fromApp(applicationId)
705 .makePermanent()
706 .forTable(TMAC_TABLE).build();
Pier Luigi0e358632017-01-31 09:35:05 -0800707 builder.add(rule);
708
709 selector = DefaultTrafficSelector.builder();
710 treatment = DefaultTrafficTreatment.builder();
711 selector.matchEthType(Ethernet.TYPE_IPV6);
712 selector.matchEthDst(ethCriterion.mac());
713 treatment.transition(UNICAST_ROUTING_TABLE);
714 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();
722 return builder.add(rule).build();
Charles Chan5270ed02016-01-30 23:22:37 -0800723 }
724
Charles Chan5b9df8d2016-03-28 22:21:40 -0700725 protected List<FlowRule> processMcastEthDstFilter(EthCriterion ethCriterion,
726 ApplicationId applicationId) {
727 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
728 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
729 selector.matchEthType(Ethernet.TYPE_IPV4);
730 selector.matchEthDstMasked(ethCriterion.mac(), ethCriterion.mask());
731 treatment.transition(MULTICAST_ROUTING_TABLE);
732 FlowRule rule = DefaultFlowRule.builder()
733 .forDevice(deviceId)
734 .withSelector(selector.build())
735 .withTreatment(treatment.build())
736 .withPriority(DEFAULT_PRIORITY)
737 .fromApp(applicationId)
738 .makePermanent()
739 .forTable(TMAC_TABLE).build();
740 return ImmutableList.<FlowRule>builder().add(rule).build();
741 }
742
Saurav Das822c4e22015-10-23 10:51:11 -0700743 private Collection<FlowRule> processForward(ForwardingObjective fwd) {
744 switch (fwd.flag()) {
745 case SPECIFIC:
746 return processSpecific(fwd);
747 case VERSATILE:
748 return processVersatile(fwd);
749 default:
750 fail(fwd, ObjectiveError.UNKNOWN);
751 log.warn("Unknown forwarding flag {}", fwd.flag());
752 }
753 return Collections.emptySet();
754 }
755
756 /**
757 * In the OF-DPA 2.0 pipeline, versatile forwarding objectives go to the
758 * ACL table.
759 * @param fwd the forwarding objective of type 'versatile'
760 * @return a collection of flow rules to be sent to the switch. An empty
761 * collection may be returned if there is a problem in processing
762 * the flow rule
763 */
Saurav Das52025962016-01-28 22:30:01 -0800764 protected Collection<FlowRule> processVersatile(ForwardingObjective fwd) {
Saurav Dasd2fded02016-12-02 15:43:47 -0800765 log.info("Processing versatile forwarding objective:{} in dev:{}",
766 fwd.id(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700767
768 EthTypeCriterion ethType =
Saurav Das77b5e902016-01-27 17:01:59 -0800769 (EthTypeCriterion) fwd.selector().getCriterion(Criterion.Type.ETH_TYPE);
Saurav Das822c4e22015-10-23 10:51:11 -0700770 if (ethType == null) {
Saurav Dasd2fded02016-12-02 15:43:47 -0800771 log.error("Versatile forwarding objective:{} must include ethType",
772 fwd.id());
Saurav Das822c4e22015-10-23 10:51:11 -0700773 fail(fwd, ObjectiveError.BADPARAMS);
774 return Collections.emptySet();
775 }
776 if (fwd.nextId() == null && fwd.treatment() == null) {
777 log.error("Forwarding objective {} from {} must contain "
778 + "nextId or Treatment", fwd.selector(), fwd.appId());
Zsolt Haraszti9faab752016-02-17 15:55:20 -0800779 fail(fwd, ObjectiveError.BADPARAMS);
Saurav Das822c4e22015-10-23 10:51:11 -0700780 return Collections.emptySet();
781 }
Saurav Das49cb5a12016-01-16 22:54:07 -0800782
Saurav Das77b5e902016-01-27 17:01:59 -0800783 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
784 fwd.selector().criteria().forEach(criterion -> {
785 if (criterion instanceof VlanIdCriterion) {
786 VlanId vlanId = ((VlanIdCriterion) criterion).vlanId();
787 // ensure that match does not include vlan = NONE as OF-DPA does not
788 // match untagged packets this way in the ACL table.
789 if (vlanId.equals(VlanId.NONE)) {
790 return;
791 }
792 OfdpaMatchVlanVid ofdpaMatchVlanVid =
793 new OfdpaMatchVlanVid(vlanId);
794 sbuilder.extension(ofdpaMatchVlanVid, deviceId);
Pier Ventree0ae7a32016-11-23 09:57:42 -0800795 } else if (criterion instanceof Icmpv6TypeCriterion ||
796 criterion instanceof Icmpv6CodeCriterion) {
797 /*
798 * We silenty discard these criterions, our current
799 * OFDPA platform does not support these matches on
800 * the ACL table.
801 */
802 log.warn("ICMPv6 Type and ICMPv6 Code are not supported");
Saurav Das77b5e902016-01-27 17:01:59 -0800803 } else {
804 sbuilder.add(criterion);
805 }
806 });
807
Saurav Das822c4e22015-10-23 10:51:11 -0700808 // XXX driver does not currently do type checking as per Tables 65-67 in
809 // OFDPA 2.0 spec. The only allowed treatment is a punt to the controller.
Saurav Das49cb5a12016-01-16 22:54:07 -0800810 TrafficTreatment.Builder ttBuilder = DefaultTrafficTreatment.builder();
811 if (fwd.treatment() != null) {
812 for (Instruction ins : fwd.treatment().allInstructions()) {
813 if (ins instanceof OutputInstruction) {
814 OutputInstruction o = (OutputInstruction) ins;
815 if (o.port() == PortNumber.CONTROLLER) {
816 ttBuilder.add(o);
817 } else {
818 log.warn("Only allowed treatments in versatile forwarding "
819 + "objectives are punts to the controller");
820 }
821 } else {
822 log.warn("Cannot process instruction in versatile fwd {}", ins);
823 }
Saurav Das822c4e22015-10-23 10:51:11 -0700824 }
Charles Chan2df0e8a2017-01-09 11:45:08 -0800825 if (fwd.treatment().clearedDeferred()) {
826 ttBuilder.wipeDeferred();
827 }
Saurav Das822c4e22015-10-23 10:51:11 -0700828 }
Saurav Das822c4e22015-10-23 10:51:11 -0700829 if (fwd.nextId() != null) {
Saurav Das49cb5a12016-01-16 22:54:07 -0800830 // overide case
831 NextGroup next = getGroupForNextObjective(fwd.nextId());
832 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
833 // we only need the top level group's key to point the flow to it
834 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
835 if (group == null) {
836 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
837 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
838 fail(fwd, ObjectiveError.GROUPMISSING);
839 return Collections.emptySet();
840 }
841 ttBuilder.deferred().group(group.id());
Saurav Das822c4e22015-10-23 10:51:11 -0700842 }
Saurav Das49cb5a12016-01-16 22:54:07 -0800843
844 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
845 .fromApp(fwd.appId())
846 .withPriority(fwd.priority())
847 .forDevice(deviceId)
Saurav Das77b5e902016-01-27 17:01:59 -0800848 .withSelector(sbuilder.build())
Saurav Das49cb5a12016-01-16 22:54:07 -0800849 .withTreatment(ttBuilder.build())
850 .makePermanent()
851 .forTable(ACL_TABLE);
852 return Collections.singletonList(ruleBuilder.build());
Saurav Das822c4e22015-10-23 10:51:11 -0700853 }
854
855 /**
856 * In the OF-DPA 2.0 pipeline, specific forwarding refers to the IP table
Saurav Das8a0732e2015-11-20 15:27:53 -0800857 * (unicast or multicast) or the L2 table (mac + vlan) or the MPLS table.
Saurav Das822c4e22015-10-23 10:51:11 -0700858 *
859 * @param fwd the forwarding objective of type 'specific'
860 * @return a collection of flow rules. Typically there will be only one
861 * for this type of forwarding objective. An empty set may be
862 * returned if there is an issue in processing the objective.
863 */
Saurav Das8a0732e2015-11-20 15:27:53 -0800864 protected Collection<FlowRule> processSpecific(ForwardingObjective fwd) {
Saurav Das25190812016-05-27 13:54:07 -0700865 log.debug("Processing specific fwd objective:{} in dev:{} with next:{}",
Saurav Das4ce45962015-11-24 23:21:05 -0800866 fwd.id(), deviceId, fwd.nextId());
867 boolean isEthTypeObj = isSupportedEthTypeObjective(fwd);
868 boolean isEthDstObj = isSupportedEthDstObjective(fwd);
869
870 if (isEthTypeObj) {
871 return processEthTypeSpecific(fwd);
872 } else if (isEthDstObj) {
873 return processEthDstSpecific(fwd);
874 } else {
875 log.warn("processSpecific: Unsupported forwarding objective "
876 + "criteria fwd:{} in dev:{}", fwd.nextId(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700877 fail(fwd, ObjectiveError.UNSUPPORTED);
878 return Collections.emptySet();
879 }
Saurav Das4ce45962015-11-24 23:21:05 -0800880 }
881
Saurav Das4ce45962015-11-24 23:21:05 -0800882 /**
883 * Handles forwarding rules to the IP and MPLS tables.
884 *
885 * @param fwd the forwarding objective
886 * @return A collection of flow rules, or an empty set
887 */
888 protected Collection<FlowRule> processEthTypeSpecific(ForwardingObjective fwd) {
Charles Chancad338a2016-09-16 18:03:11 -0700889 return processEthTypeSpecificInternal(fwd, false, ACL_TABLE);
Charles Chanf9e98652016-09-07 16:54:23 -0700890 }
891
892 /**
Pier Ventree0ae7a32016-11-23 09:57:42 -0800893 * Helper method to build Ipv6 selector using the selector provided by
894 * a forwarding objective.
895 *
896 * @param builderToUpdate the builder to update
897 * @param fwd the selector to read
898 * @return 0 if the update ends correctly. -1 if the matches
899 * are not yet supported
900 */
901 protected int buildIpv6Selector(TrafficSelector.Builder builderToUpdate,
902 ForwardingObjective fwd) {
903
904 TrafficSelector selector = fwd.selector();
905
906 IpPrefix ipv6Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV6_DST)).ip();
907 if (ipv6Dst.isMulticast()) {
908 log.warn("IPv6 Multicast is currently not supported");
909 fail(fwd, ObjectiveError.BADPARAMS);
910 return -1;
Pier Ventree0ae7a32016-11-23 09:57:42 -0800911 }
Pier Luigi69fd4312017-01-28 17:58:58 -0800912 if (ipv6Dst.prefixLength() != 0) {
913 builderToUpdate.matchIPv6Dst(ipv6Dst);
914 }
915 builderToUpdate.matchEthType(Ethernet.TYPE_IPV6);
916 log.debug("processing IPv6 unicast specific forwarding objective {} -> next:{}"
917 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
Pier Ventree0ae7a32016-11-23 09:57:42 -0800918 return 0;
919 }
920
921 /**
Charles Chanf9e98652016-09-07 16:54:23 -0700922 * Internal implementation of processEthTypeSpecific.
923 * <p>
924 * Wildcarded IPv4_DST is not supported in OFDPA i12. Therefore, we break
925 * the rule into 0.0.0.0/1 and 128.0.0.0/1.
926 * The allowDefaultRoute must be set to false for OFDPA i12.
927 * </p>
928 *
929 * @param fwd the forwarding objective
930 * @param allowDefaultRoute allow wildcarded IPv4_DST or not
Ray Milkey0bb1e102016-11-10 14:51:27 -0800931 * @param mplsNextTable next MPLS table
Charles Chanf9e98652016-09-07 16:54:23 -0700932 * @return A collection of flow rules, or an empty set
933 */
934 protected Collection<FlowRule> processEthTypeSpecificInternal(ForwardingObjective fwd,
Charles Chancad338a2016-09-16 18:03:11 -0700935 boolean allowDefaultRoute,
936 int mplsNextTable) {
Saurav Das4ce45962015-11-24 23:21:05 -0800937 TrafficSelector selector = fwd.selector();
938 EthTypeCriterion ethType =
939 (EthTypeCriterion) selector.getCriterion(Criterion.Type.ETH_TYPE);
Flavio Castroe10fa242016-01-15 12:43:51 -0800940 boolean defaultRule = false;
941 boolean popMpls = false;
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700942 boolean emptyGroup = false;
Charles Chan188ebf52015-12-23 00:15:11 -0800943 int forTableId;
Saurav Das8a0732e2015-11-20 15:27:53 -0800944 TrafficSelector.Builder filteredSelector = DefaultTrafficSelector.builder();
Charles Chan14967c22015-12-07 11:11:50 -0800945 TrafficTreatment.Builder tb = DefaultTrafficTreatment.builder();
Flavio Castroe10fa242016-01-15 12:43:51 -0800946 TrafficSelector.Builder complementarySelector = DefaultTrafficSelector.builder();
Charles Chan14967c22015-12-07 11:11:50 -0800947
Saurav Das8a0732e2015-11-20 15:27:53 -0800948 if (ethType.ethType().toShort() == Ethernet.TYPE_IPV4) {
Flavio Castroe10fa242016-01-15 12:43:51 -0800949 IpPrefix ipv4Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV4_DST)).ip();
Charles Chan5b9df8d2016-03-28 22:21:40 -0700950 if (ipv4Dst.isMulticast()) {
951 if (ipv4Dst.prefixLength() != 32) {
952 log.warn("Multicast specific forwarding objective can only be /32");
953 fail(fwd, ObjectiveError.BADPARAMS);
954 return ImmutableSet.of();
955 }
956 VlanId assignedVlan = readVlanFromSelector(fwd.meta());
957 if (assignedVlan == null) {
958 log.warn("VLAN ID required by multicast specific fwd obj is missing. Abort.");
959 fail(fwd, ObjectiveError.BADPARAMS);
960 return ImmutableSet.of();
961 }
962 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
963 filteredSelector.extension(ofdpaMatchVlanVid, deviceId);
964 filteredSelector.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipv4Dst);
965 forTableId = MULTICAST_ROUTING_TABLE;
966 log.debug("processing IPv4 multicast specific forwarding objective {} -> next:{}"
967 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
Flavio Castroe10fa242016-01-15 12:43:51 -0800968 } else {
Charles Chanf9e98652016-09-07 16:54:23 -0700969 if (ipv4Dst.prefixLength() == 0) {
970 if (allowDefaultRoute) {
971 // The entire IPV4_DST field is wildcarded intentionally
972 filteredSelector.matchEthType(Ethernet.TYPE_IPV4);
973 } else {
Pier Ventree0ae7a32016-11-23 09:57:42 -0800974 // NOTE: The switch does not support matching 0.0.0.0/0
975 // Split it into 0.0.0.0/1 and 128.0.0.0/1
Charles Chanf9e98652016-09-07 16:54:23 -0700976 filteredSelector.matchEthType(Ethernet.TYPE_IPV4)
977 .matchIPDst(IpPrefix.valueOf("0.0.0.0/1"));
978 complementarySelector.matchEthType(Ethernet.TYPE_IPV4)
979 .matchIPDst(IpPrefix.valueOf("128.0.0.0/1"));
980 defaultRule = true;
981 }
Charles Chan5b9df8d2016-03-28 22:21:40 -0700982 } else {
Charles Chanf9e98652016-09-07 16:54:23 -0700983 filteredSelector.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipv4Dst);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700984 }
985 forTableId = UNICAST_ROUTING_TABLE;
986 log.debug("processing IPv4 unicast specific forwarding objective {} -> next:{}"
987 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
Flavio Castroe10fa242016-01-15 12:43:51 -0800988 }
Charles Chan14967c22015-12-07 11:11:50 -0800989
990 if (fwd.treatment() != null) {
991 for (Instruction instr : fwd.treatment().allInstructions()) {
992 if (instr instanceof L3ModificationInstruction &&
993 ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
Saurav Das9c705342017-01-06 11:36:01 -0800994 // XXX decrementing IP ttl is done automatically for routing, this
995 // action is ignored or rejected in ofdpa as it is not fully implemented
996 //tb.deferred().add(instr);
Charles Chan14967c22015-12-07 11:11:50 -0800997 }
998 }
999 }
1000
Pier Ventree0ae7a32016-11-23 09:57:42 -08001001 } else if (ethType.ethType().toShort() == Ethernet.TYPE_IPV6) {
1002 if (buildIpv6Selector(filteredSelector, fwd) < 0) {
1003 return Collections.emptyList();
1004 }
1005 forTableId = UNICAST_ROUTING_TABLE;
1006 if (fwd.treatment() != null) {
1007 for (Instruction instr : fwd.treatment().allInstructions()) {
1008 if (instr instanceof L3ModificationInstruction &&
1009 ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
1010 // XXX decrementing IP ttl is done automatically for routing, this
1011 // action is ignored or rejected in ofdpa as it is not fully implemented
1012 //tb.deferred().add(instr);
1013 }
1014 }
1015 }
Saurav Das8a0732e2015-11-20 15:27:53 -08001016 } else {
1017 filteredSelector
1018 .matchEthType(Ethernet.MPLS_UNICAST)
1019 .matchMplsLabel(((MplsCriterion)
1020 selector.getCriterion(Criterion.Type.MPLS_LABEL)).label());
1021 MplsBosCriterion bos = (MplsBosCriterion) selector
Pier Ventre140a8942016-11-02 07:26:38 -07001022 .getCriterion(MPLS_BOS);
Saurav Das8a0732e2015-11-20 15:27:53 -08001023 if (bos != null) {
1024 filteredSelector.matchMplsBos(bos.mplsBos());
1025 }
1026 forTableId = MPLS_TABLE_1;
Saurav Das4ce45962015-11-24 23:21:05 -08001027 log.debug("processing MPLS specific forwarding objective {} -> next:{}"
1028 + " in dev {}", fwd.id(), fwd.nextId(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -07001029
Charles Chan14967c22015-12-07 11:11:50 -08001030 if (fwd.treatment() != null) {
1031 for (Instruction instr : fwd.treatment().allInstructions()) {
1032 if (instr instanceof L2ModificationInstruction &&
1033 ((L2ModificationInstruction) instr).subtype() == L2SubType.MPLS_POP) {
Saurav Das8a0732e2015-11-20 15:27:53 -08001034 popMpls = true;
Saurav Das9c705342017-01-06 11:36:01 -08001035 // OF-DPA does not pop in MPLS table. Instead it requires
1036 // setting the MPLS_TYPE so pop can happen down the pipeline
Charles Chan14967c22015-12-07 11:11:50 -08001037 }
1038 if (instr instanceof L3ModificationInstruction &&
1039 ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
1040 // FIXME Should modify the app to send the correct DEC_MPLS_TTL instruction
1041 tb.immediate().decMplsTtl();
1042 }
1043 if (instr instanceof L3ModificationInstruction &&
1044 ((L3ModificationInstruction) instr).subtype() == L3SubType.TTL_IN) {
1045 tb.immediate().add(instr);
1046 }
Saurav Das8a0732e2015-11-20 15:27:53 -08001047 }
1048 }
1049 }
Saurav Das822c4e22015-10-23 10:51:11 -07001050
1051 if (fwd.nextId() != null) {
Saurav Das8a0732e2015-11-20 15:27:53 -08001052 if (forTableId == MPLS_TABLE_1 && !popMpls) {
1053 log.warn("SR CONTINUE case cannot be handled as MPLS ECMP "
Saurav Das25190812016-05-27 13:54:07 -07001054 + "is not implemented in OF-DPA yet. Aborting this flow {} -> next:{}"
1055 + "in this device {}", fwd.id(), fwd.nextId(), deviceId);
Pier Ventree0ae7a32016-11-23 09:57:42 -08001056 // XXX We could convert to forwarding to a single-port, via a MPLS interface,
1057 // or a MPLS SWAP (with-same) but that would have to be handled in the next-objective.
1058 // Also the pop-mpls logic used here won't work in non-BoS case.
Saurav Das4ce45962015-11-24 23:21:05 -08001059 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
Saurav Das8a0732e2015-11-20 15:27:53 -08001060 return Collections.emptySet();
1061 }
1062
Saurav Das423fe2b2015-12-04 10:52:59 -08001063 NextGroup next = getGroupForNextObjective(fwd.nextId());
1064 if (next != null) {
1065 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1066 // we only need the top level group's key to point the flow to it
1067 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
Pier Ventre140a8942016-11-02 07:26:38 -07001068 if (isNotMplsBos(selector) && group.type().equals(HASHED)) {
1069 log.warn("SR CONTINUE case cannot be handled as MPLS ECMP "
1070 + "is not implemented in OF-DPA yet. Aborting this flow {} -> next:{}"
1071 + "in this device {}", fwd.id(), fwd.nextId(), deviceId);
1072 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
1073 return Collections.emptySet();
1074 }
Saurav Das423fe2b2015-12-04 10:52:59 -08001075 if (group == null) {
1076 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
1077 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
1078 fail(fwd, ObjectiveError.GROUPMISSING);
1079 return Collections.emptySet();
1080 }
1081 tb.deferred().group(group.id());
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001082 // check if group is empty
1083 if (gkeys.size() == 1 && gkeys.get(0).size() == 1) {
1084 log.warn("Found empty group 0x{} in dev:{} .. will retry fwd:{}",
1085 Integer.toHexString(group.id().id()), deviceId, fwd.id());
1086 emptyGroup = true;
1087 }
Saurav Das25190812016-05-27 13:54:07 -07001088 } else {
1089 log.warn("Cannot find group for nextId:{} in dev:{}. Aborting fwd:{}",
1090 fwd.nextId(), deviceId, fwd.id());
1091 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
1092 return Collections.emptySet();
Saurav Das822c4e22015-10-23 10:51:11 -07001093 }
Saurav Das822c4e22015-10-23 10:51:11 -07001094 }
Charles Chancad338a2016-09-16 18:03:11 -07001095
1096 if (forTableId == MPLS_TABLE_1) {
Pier Ventre140a8942016-11-02 07:26:38 -07001097 if (mplsNextTable == MPLS_L3_TYPE_TABLE) {
Charles Chancad338a2016-09-16 18:03:11 -07001098 Ofdpa3SetMplsType setMplsType = new Ofdpa3SetMplsType(Ofdpa3MplsType.L3_PHP);
Saurav Das9c705342017-01-06 11:36:01 -08001099 // set mpls type as apply_action
1100 tb.immediate().extension(setMplsType, deviceId);
Charles Chancad338a2016-09-16 18:03:11 -07001101 }
1102 tb.transition(mplsNextTable);
1103 } else {
1104 tb.transition(ACL_TABLE);
1105 }
1106
Saurav Das822c4e22015-10-23 10:51:11 -07001107 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
1108 .fromApp(fwd.appId())
1109 .withPriority(fwd.priority())
1110 .forDevice(deviceId)
Saurav Das8a0732e2015-11-20 15:27:53 -08001111 .withSelector(filteredSelector.build())
1112 .withTreatment(tb.build())
1113 .forTable(forTableId);
Saurav Das822c4e22015-10-23 10:51:11 -07001114
1115 if (fwd.permanent()) {
1116 ruleBuilder.makePermanent();
1117 } else {
1118 ruleBuilder.makeTemporary(fwd.timeout());
1119 }
Flavio Castroe10fa242016-01-15 12:43:51 -08001120 Collection<FlowRule> flowRuleCollection = new ArrayList<>();
1121 flowRuleCollection.add(ruleBuilder.build());
1122 if (defaultRule) {
Pier Ventree0ae7a32016-11-23 09:57:42 -08001123 flowRuleCollection.add(
1124 defaultRoute(fwd, complementarySelector, forTableId, tb)
1125 );
Flavio Castroe10fa242016-01-15 12:43:51 -08001126 log.debug("Default rule 0.0.0.0/0 is being installed two rules");
1127 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001128 // XXX retrying flows may be necessary due to bug CORD-554
1129 if (emptyGroup) {
1130 executorService.schedule(new RetryFlows(fwd, flowRuleCollection),
1131 RETRY_MS, TimeUnit.MILLISECONDS);
1132 }
Flavio Castroe10fa242016-01-15 12:43:51 -08001133 return flowRuleCollection;
Saurav Das822c4e22015-10-23 10:51:11 -07001134 }
1135
Pier Ventree0ae7a32016-11-23 09:57:42 -08001136 protected FlowRule defaultRoute(ForwardingObjective fwd,
1137 TrafficSelector.Builder complementarySelector,
1138 int forTableId,
1139 TrafficTreatment.Builder tb) {
1140 FlowRule.Builder rule = DefaultFlowRule.builder()
1141 .fromApp(fwd.appId())
1142 .withPriority(fwd.priority())
1143 .forDevice(deviceId)
1144 .withSelector(complementarySelector.build())
1145 .withTreatment(tb.build())
1146 .forTable(forTableId);
1147 if (fwd.permanent()) {
1148 rule.makePermanent();
1149 } else {
1150 rule.makeTemporary(fwd.timeout());
1151 }
1152 return rule.build();
1153 }
1154
Saurav Das4ce45962015-11-24 23:21:05 -08001155 /**
1156 * Handles forwarding rules to the L2 bridging table. Flow actions are not
1157 * allowed in the bridging table - instead we use L2 Interface group or
1158 * L2 flood group
1159 *
1160 * @param fwd the forwarding objective
1161 * @return A collection of flow rules, or an empty set
1162 */
1163 protected Collection<FlowRule> processEthDstSpecific(ForwardingObjective fwd) {
1164 List<FlowRule> rules = new ArrayList<>();
1165
1166 // Build filtered selector
1167 TrafficSelector selector = fwd.selector();
1168 EthCriterion ethCriterion = (EthCriterion) selector
1169 .getCriterion(Criterion.Type.ETH_DST);
1170 VlanIdCriterion vlanIdCriterion = (VlanIdCriterion) selector
1171 .getCriterion(Criterion.Type.VLAN_VID);
1172
1173 if (vlanIdCriterion == null) {
1174 log.warn("Forwarding objective for bridging requires vlan. Not "
1175 + "installing fwd:{} in dev:{}", fwd.id(), deviceId);
1176 fail(fwd, ObjectiveError.BADPARAMS);
1177 return Collections.emptySet();
1178 }
1179
1180 TrafficSelector.Builder filteredSelectorBuilder =
1181 DefaultTrafficSelector.builder();
Yi Tseng3a77b4f2017-02-06 15:02:17 -08001182
1183 if (!ethCriterion.mac().equals(NONE) &&
1184 !ethCriterion.mac().equals(BROADCAST)) {
Saurav Das4ce45962015-11-24 23:21:05 -08001185 filteredSelectorBuilder.matchEthDst(ethCriterion.mac());
1186 log.debug("processing L2 forwarding objective:{} -> next:{} in dev:{}",
1187 fwd.id(), fwd.nextId(), deviceId);
1188 } else {
Yi Tseng3a77b4f2017-02-06 15:02:17 -08001189 // Use wildcard DST_MAC if the MacAddress is None or Broadcast
Saurav Das4ce45962015-11-24 23:21:05 -08001190 log.debug("processing L2 Broadcast forwarding objective:{} -> next:{} "
1191 + "in dev:{} for vlan:{}",
1192 fwd.id(), fwd.nextId(), deviceId, vlanIdCriterion.vlanId());
1193 }
Charles Chan14967c22015-12-07 11:11:50 -08001194 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vlanIdCriterion.vlanId());
1195 filteredSelectorBuilder.extension(ofdpaMatchVlanVid, deviceId);
Saurav Das4ce45962015-11-24 23:21:05 -08001196 TrafficSelector filteredSelector = filteredSelectorBuilder.build();
1197
1198 if (fwd.treatment() != null) {
1199 log.warn("Ignoring traffic treatment in fwd rule {} meant for L2 table"
1200 + "for dev:{}. Expecting only nextId", fwd.id(), deviceId);
1201 }
1202
1203 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
1204 if (fwd.nextId() != null) {
Saurav Das423fe2b2015-12-04 10:52:59 -08001205 NextGroup next = getGroupForNextObjective(fwd.nextId());
Saurav Das4ce45962015-11-24 23:21:05 -08001206 if (next != null) {
1207 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1208 // we only need the top level group's key to point the flow to it
1209 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
1210 if (group != null) {
1211 treatmentBuilder.deferred().group(group.id());
1212 } else {
1213 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
1214 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
1215 fail(fwd, ObjectiveError.GROUPMISSING);
1216 return Collections.emptySet();
1217 }
1218 }
1219 }
1220 treatmentBuilder.immediate().transition(ACL_TABLE);
1221 TrafficTreatment filteredTreatment = treatmentBuilder.build();
1222
1223 // Build bridging table entries
1224 FlowRule.Builder flowRuleBuilder = DefaultFlowRule.builder();
1225 flowRuleBuilder.fromApp(fwd.appId())
1226 .withPriority(fwd.priority())
1227 .forDevice(deviceId)
1228 .withSelector(filteredSelector)
1229 .withTreatment(filteredTreatment)
1230 .forTable(BRIDGING_TABLE);
1231 if (fwd.permanent()) {
1232 flowRuleBuilder.makePermanent();
1233 } else {
1234 flowRuleBuilder.makeTemporary(fwd.timeout());
1235 }
1236 rules.add(flowRuleBuilder.build());
1237 return rules;
1238 }
1239
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001240 //////////////////////////////////////
1241 // Helper Methods and Classes
1242 //////////////////////////////////////
1243
1244 private boolean isSupportedEthTypeObjective(ForwardingObjective fwd) {
1245 TrafficSelector selector = fwd.selector();
1246 EthTypeCriterion ethType = (EthTypeCriterion) selector
1247 .getCriterion(Criterion.Type.ETH_TYPE);
1248 return !((ethType == null) ||
1249 ((ethType.ethType().toShort() != Ethernet.TYPE_IPV4) &&
Pier Ventree0ae7a32016-11-23 09:57:42 -08001250 (ethType.ethType().toShort() != Ethernet.MPLS_UNICAST)) &&
1251 (ethType.ethType().toShort() != Ethernet.TYPE_IPV6));
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001252 }
1253
1254 private boolean isSupportedEthDstObjective(ForwardingObjective fwd) {
1255 TrafficSelector selector = fwd.selector();
1256 EthCriterion ethDst = (EthCriterion) selector
1257 .getCriterion(Criterion.Type.ETH_DST);
1258 VlanIdCriterion vlanId = (VlanIdCriterion) selector
1259 .getCriterion(Criterion.Type.VLAN_VID);
1260 return !(ethDst == null && vlanId == null);
1261 }
1262
Saurav Das423fe2b2015-12-04 10:52:59 -08001263 protected NextGroup getGroupForNextObjective(Integer nextId) {
1264 NextGroup next = flowObjectiveStore.getNextGroup(nextId);
1265 if (next != null) {
1266 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1267 if (gkeys != null && !gkeys.isEmpty()) {
1268 return next;
1269 } else {
1270 log.warn("Empty next group found in FlowObjective store for "
1271 + "next-id:{} in dev:{}", nextId, deviceId);
1272 }
1273 } else {
1274 log.warn("next-id {} not found in Flow objective store for dev:{}",
1275 nextId, deviceId);
1276 }
1277 return null;
1278 }
1279
Charles Chan188ebf52015-12-23 00:15:11 -08001280 protected static void pass(Objective obj) {
Sho SHIMIZUef7e2902016-02-12 18:38:29 -08001281 obj.context().ifPresent(context -> context.onSuccess(obj));
Saurav Das822c4e22015-10-23 10:51:11 -07001282 }
1283
Charles Chan188ebf52015-12-23 00:15:11 -08001284 protected static void fail(Objective obj, ObjectiveError error) {
Sho SHIMIZUef7e2902016-02-12 18:38:29 -08001285 obj.context().ifPresent(context -> context.onError(obj, error));
Saurav Das822c4e22015-10-23 10:51:11 -07001286 }
Saurav Das24431192016-03-07 19:13:00 -08001287
Saurav Das24431192016-03-07 19:13:00 -08001288 @Override
1289 public List<String> getNextMappings(NextGroup nextGroup) {
1290 List<String> mappings = new ArrayList<>();
1291 List<Deque<GroupKey>> gkeys = appKryo.deserialize(nextGroup.data());
1292 for (Deque<GroupKey> gkd : gkeys) {
1293 Group lastGroup = null;
Saurav Das8be4e3a2016-03-11 17:19:07 -08001294 StringBuffer gchain = new StringBuffer();
Saurav Das24431192016-03-07 19:13:00 -08001295 for (GroupKey gk : gkd) {
1296 Group g = groupService.getGroup(deviceId, gk);
Saurav Das8be4e3a2016-03-11 17:19:07 -08001297 if (g == null) {
Saurav Das25190812016-05-27 13:54:07 -07001298 gchain.append(" NoGrp").append(" -->");
Saurav Das8be4e3a2016-03-11 17:19:07 -08001299 continue;
1300 }
1301 gchain.append(" 0x").append(Integer.toHexString(g.id().id()))
1302 .append(" -->");
Saurav Das24431192016-03-07 19:13:00 -08001303 lastGroup = g;
1304 }
1305 // add port information for last group in group-chain
Saurav Das25190812016-05-27 13:54:07 -07001306 List<Instruction> lastGroupIns = new ArrayList<Instruction>();
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001307 if (lastGroup != null && !lastGroup.buckets().buckets().isEmpty()) {
Saurav Das25190812016-05-27 13:54:07 -07001308 lastGroupIns = lastGroup.buckets().buckets().get(0)
1309 .treatment().allInstructions();
1310 }
1311 for (Instruction i: lastGroupIns) {
Saurav Das24431192016-03-07 19:13:00 -08001312 if (i instanceof OutputInstruction) {
Saurav Das8be4e3a2016-03-11 17:19:07 -08001313 gchain.append(" port:").append(((OutputInstruction) i).port());
Saurav Das24431192016-03-07 19:13:00 -08001314 }
1315 }
Saurav Das8be4e3a2016-03-11 17:19:07 -08001316 mappings.add(gchain.toString());
Saurav Das24431192016-03-07 19:13:00 -08001317 }
1318 return mappings;
1319 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001320
Pier Ventre140a8942016-11-02 07:26:38 -07001321 static boolean isMplsBos(TrafficSelector selector) {
1322 MplsBosCriterion bosCriterion = (MplsBosCriterion) selector.getCriterion(MPLS_BOS);
1323 return bosCriterion != null && bosCriterion.mplsBos();
1324 }
1325
1326 static boolean isNotMplsBos(TrafficSelector selector) {
1327 MplsBosCriterion bosCriterion = (MplsBosCriterion) selector.getCriterion(MPLS_BOS);
1328 return bosCriterion != null && !bosCriterion.mplsBos();
1329 }
1330
Charles Chan5b9df8d2016-03-28 22:21:40 -07001331 protected static VlanId readVlanFromSelector(TrafficSelector selector) {
Saurav Das59232cf2016-04-27 18:35:50 -07001332 if (selector == null) {
1333 return null;
1334 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001335 Criterion criterion = selector.getCriterion(Criterion.Type.VLAN_VID);
1336 return (criterion == null)
1337 ? null : ((VlanIdCriterion) criterion).vlanId();
1338 }
1339
1340 protected static IpPrefix readIpDstFromSelector(TrafficSelector selector) {
Saurav Das59232cf2016-04-27 18:35:50 -07001341 if (selector == null) {
1342 return null;
1343 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001344 Criterion criterion = selector.getCriterion(Criterion.Type.IPV4_DST);
1345 return (criterion == null) ? null : ((IPCriterion) criterion).ip();
1346 }
Charles Chand55e84d2016-03-30 17:54:24 -07001347
1348 private static VlanId readVlanFromTreatment(TrafficTreatment treatment) {
Saurav Das59232cf2016-04-27 18:35:50 -07001349 if (treatment == null) {
1350 return null;
1351 }
Charles Chand55e84d2016-03-30 17:54:24 -07001352 for (Instruction i : treatment.allInstructions()) {
1353 if (i instanceof ModVlanIdInstruction) {
1354 return ((ModVlanIdInstruction) i).vlanId();
1355 }
1356 }
1357 return null;
1358 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001359
1360 /**
1361 * Utility class that retries sending flows a fixed number of times, even if
1362 * some of the attempts are successful. Used only for forwarding objectives.
1363 */
1364 protected final class RetryFlows implements Runnable {
1365 int attempts = MAX_RETRY_ATTEMPTS;
1366 private Collection<FlowRule> retryFlows;
1367 private ForwardingObjective fwd;
1368
1369 RetryFlows(ForwardingObjective fwd, Collection<FlowRule> retryFlows) {
1370 this.fwd = fwd;
1371 this.retryFlows = retryFlows;
1372 }
1373
1374 @Override
1375 public void run() {
1376 log.info("RETRY FLOWS ATTEMPT# {} for fwd:{} rules:{}",
1377 MAX_RETRY_ATTEMPTS - attempts, fwd.id(), retryFlows.size());
1378 sendForward(fwd, retryFlows);
1379 if (--attempts > 0) {
1380 executorService.schedule(this, RETRY_MS, TimeUnit.MILLISECONDS);
1381 }
1382 }
1383 }
1384
Saurav Das822c4e22015-10-23 10:51:11 -07001385}