blob: a698a81024c22b22fdd7fa32a14bad9ca93243b8 [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;
Saurav Das822c4e22015-10-23 10:51:11 -070019import org.onlab.osgi.ServiceDirectory;
Saurav Das822c4e22015-10-23 10:51:11 -070020import org.onlab.packet.Ethernet;
Charles Chan14967c22015-12-07 11:11:50 -080021import org.onlab.packet.IpPrefix;
Saurav Das822c4e22015-10-23 10:51:11 -070022import org.onlab.packet.VlanId;
23import org.onlab.util.KryoNamespace;
24import org.onosproject.core.ApplicationId;
25import org.onosproject.core.CoreService;
Charles Chancad338a2016-09-16 18:03:11 -070026import org.onosproject.driver.extensions.Ofdpa3MplsType;
27import org.onosproject.driver.extensions.Ofdpa3SetMplsType;
Charles Chan14967c22015-12-07 11:11:50 -080028import org.onosproject.driver.extensions.OfdpaMatchVlanVid;
29import org.onosproject.driver.extensions.OfdpaSetVlanVid;
Saurav Das822c4e22015-10-23 10:51:11 -070030import org.onosproject.net.DeviceId;
31import org.onosproject.net.Port;
32import org.onosproject.net.PortNumber;
33import org.onosproject.net.behaviour.NextGroup;
34import org.onosproject.net.behaviour.Pipeliner;
35import org.onosproject.net.behaviour.PipelinerContext;
36import org.onosproject.net.device.DeviceService;
37import org.onosproject.net.driver.AbstractHandlerBehaviour;
38import org.onosproject.net.flow.DefaultFlowRule;
39import org.onosproject.net.flow.DefaultTrafficSelector;
40import org.onosproject.net.flow.DefaultTrafficTreatment;
41import org.onosproject.net.flow.FlowRule;
42import org.onosproject.net.flow.FlowRuleOperations;
43import org.onosproject.net.flow.FlowRuleOperationsContext;
44import org.onosproject.net.flow.FlowRuleService;
45import org.onosproject.net.flow.TrafficSelector;
46import org.onosproject.net.flow.TrafficTreatment;
47import org.onosproject.net.flow.criteria.Criteria;
48import org.onosproject.net.flow.criteria.Criterion;
49import org.onosproject.net.flow.criteria.EthCriterion;
50import org.onosproject.net.flow.criteria.EthTypeCriterion;
Charles Chan14967c22015-12-07 11:11:50 -080051import org.onosproject.net.flow.criteria.ExtensionCriterion;
Saurav Das822c4e22015-10-23 10:51:11 -070052import org.onosproject.net.flow.criteria.IPCriterion;
Pier Ventree0ae7a32016-11-23 09:57:42 -080053import org.onosproject.net.flow.criteria.Icmpv6CodeCriterion;
54import org.onosproject.net.flow.criteria.Icmpv6TypeCriterion;
Saurav Das8a0732e2015-11-20 15:27:53 -080055import org.onosproject.net.flow.criteria.MplsBosCriterion;
56import org.onosproject.net.flow.criteria.MplsCriterion;
Saurav Das822c4e22015-10-23 10:51:11 -070057import org.onosproject.net.flow.criteria.PortCriterion;
58import org.onosproject.net.flow.criteria.VlanIdCriterion;
59import org.onosproject.net.flow.instructions.Instruction;
60import org.onosproject.net.flow.instructions.Instructions.OutputInstruction;
61import org.onosproject.net.flow.instructions.L2ModificationInstruction;
Saurav Das8a0732e2015-11-20 15:27:53 -080062import org.onosproject.net.flow.instructions.L2ModificationInstruction.L2SubType;
Saurav Das822c4e22015-10-23 10:51:11 -070063import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModVlanIdInstruction;
Charles Chan14967c22015-12-07 11:11:50 -080064import org.onosproject.net.flow.instructions.L3ModificationInstruction;
65import org.onosproject.net.flow.instructions.L3ModificationInstruction.L3SubType;
Saurav Das822c4e22015-10-23 10:51:11 -070066import org.onosproject.net.flowobjective.FilteringObjective;
67import org.onosproject.net.flowobjective.FlowObjectiveStore;
68import org.onosproject.net.flowobjective.ForwardingObjective;
69import org.onosproject.net.flowobjective.NextObjective;
70import org.onosproject.net.flowobjective.Objective;
71import org.onosproject.net.flowobjective.ObjectiveError;
Saurav Das822c4e22015-10-23 10:51:11 -070072import org.onosproject.net.group.DefaultGroupKey;
73import org.onosproject.net.group.Group;
Saurav Das822c4e22015-10-23 10:51:11 -070074import org.onosproject.net.group.GroupKey;
Saurav Das822c4e22015-10-23 10:51:11 -070075import org.onosproject.net.group.GroupService;
Saurav Das822c4e22015-10-23 10:51:11 -070076import org.onosproject.store.serializers.KryoNamespaces;
77import org.slf4j.Logger;
78
Pier Ventree0ae7a32016-11-23 09:57:42 -080079import java.util.ArrayDeque;
80import java.util.ArrayList;
81import java.util.Collection;
82import java.util.Collections;
83import java.util.Deque;
84import java.util.List;
85import java.util.Objects;
86import java.util.Set;
87import java.util.concurrent.ConcurrentHashMap;
88import java.util.concurrent.ScheduledExecutorService;
89import java.util.concurrent.TimeUnit;
90
91import static java.util.concurrent.Executors.newScheduledThreadPool;
Yi Tseng3a77b4f2017-02-06 15:02:17 -080092import static org.onlab.packet.MacAddress.BROADCAST;
93import static org.onlab.packet.MacAddress.NONE;
Pier Ventree0ae7a32016-11-23 09:57:42 -080094import static org.onlab.util.Tools.groupedThreads;
95import static org.slf4j.LoggerFactory.getLogger;
Pier Ventre140a8942016-11-02 07:26:38 -070096import static org.onosproject.net.flow.criteria.Criterion.Type.MPLS_BOS;
97import static org.onosproject.net.flowobjective.NextObjective.Type.HASHED;
Pier Ventree0ae7a32016-11-23 09:57:42 -080098
Saurav Das822c4e22015-10-23 10:51:11 -070099/**
100 * Driver for Broadcom's OF-DPA v2.0 TTP.
101 *
102 */
Charles Chan361154b2016-03-24 10:23:39 -0700103public class Ofdpa2Pipeline extends AbstractHandlerBehaviour implements Pipeliner {
Pier Ventre42287df2016-11-09 14:17:26 -0800104
Saurav Das822c4e22015-10-23 10:51:11 -0700105 protected static final int PORT_TABLE = 0;
106 protected static final int VLAN_TABLE = 10;
Pier Ventre42287df2016-11-09 14:17:26 -0800107 protected static final int VLAN_1_TABLE = 11;
108 protected static final int MPLS_L2_PORT_FLOW_TABLE = 13;
109 protected static final int MPLS_L2_PORT_PCP_TRUST_FLOW_TABLE = 16;
Saurav Das822c4e22015-10-23 10:51:11 -0700110 protected static final int TMAC_TABLE = 20;
111 protected static final int UNICAST_ROUTING_TABLE = 30;
112 protected static final int MULTICAST_ROUTING_TABLE = 40;
113 protected static final int MPLS_TABLE_0 = 23;
114 protected static final int MPLS_TABLE_1 = 24;
Pier Ventre140a8942016-11-02 07:26:38 -0700115 protected static final int MPLS_L3_TYPE_TABLE = 27;
116 protected static final int MPLS_TYPE_TABLE = 29;
Saurav Das822c4e22015-10-23 10:51:11 -0700117 protected static final int BRIDGING_TABLE = 50;
118 protected static final int ACL_TABLE = 60;
119 protected static final int MAC_LEARNING_TABLE = 254;
120 protected static final long OFPP_MAX = 0xffffff00L;
121
Saurav Das52025962016-01-28 22:30:01 -0800122 protected static final int HIGHEST_PRIORITY = 0xffff;
Saurav Das2857f382015-11-03 14:39:27 -0800123 protected static final int DEFAULT_PRIORITY = 0x8000;
Saurav Das822c4e22015-10-23 10:51:11 -0700124 protected static final int LOWEST_PRIORITY = 0x0;
125
Pier Ventre42287df2016-11-09 14:17:26 -0800126 protected static final int MPLS_L2_PORT_PRIORITY = 2;
127
128 protected static final int MPLS_TUNNEL_ID_BASE = 0x10000;
129 protected static final int MPLS_TUNNEL_ID_MAX = 0x1FFFF;
130
Pier Ventre70d53ba2016-11-17 22:26:29 -0800131 protected static final int MPLS_UNI_PORT_MAX = 0x0000FFFF;
132
133 protected static final int MPLS_NNI_PORT_BASE = 0x00020000;
134 protected static final int MPLS_NNI_PORT_MAX = 0x0002FFFF;
135
Saurav Das822c4e22015-10-23 10:51:11 -0700136 private final Logger log = getLogger(getClass());
Charles Chan425854b2016-04-11 15:32:12 -0700137 protected ServiceDirectory serviceDirectory;
Saurav Das822c4e22015-10-23 10:51:11 -0700138 protected FlowRuleService flowRuleService;
Charles Chan425854b2016-04-11 15:32:12 -0700139 protected CoreService coreService;
Saurav Das8a0732e2015-11-20 15:27:53 -0800140 protected GroupService groupService;
141 protected FlowObjectiveStore flowObjectiveStore;
Saurav Das822c4e22015-10-23 10:51:11 -0700142 protected DeviceId deviceId;
143 protected ApplicationId driverId;
Saurav Das822c4e22015-10-23 10:51:11 -0700144 protected DeviceService deviceService;
Charles Chan188ebf52015-12-23 00:15:11 -0800145 protected static KryoNamespace appKryo = new KryoNamespace.Builder()
Saurav Das822c4e22015-10-23 10:51:11 -0700146 .register(KryoNamespaces.API)
147 .register(GroupKey.class)
148 .register(DefaultGroupKey.class)
Charles Chan361154b2016-03-24 10:23:39 -0700149 .register(Ofdpa2GroupHandler.OfdpaNextGroup.class)
Saurav Das8a0732e2015-11-20 15:27:53 -0800150 .register(ArrayDeque.class)
Charles Chaneefdedf2016-05-23 16:45:45 -0700151 .build("Ofdpa2Pipeline");
Saurav Das822c4e22015-10-23 10:51:11 -0700152
Charles Chan425854b2016-04-11 15:32:12 -0700153 protected Ofdpa2GroupHandler groupHandler;
Saurav Das822c4e22015-10-23 10:51:11 -0700154
Saurav Das52025962016-01-28 22:30:01 -0800155 protected Set<IPCriterion> sentIpFilters = Collections.newSetFromMap(
Charles Chan188ebf52015-12-23 00:15:11 -0800156 new ConcurrentHashMap<>());
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700157 // flows installations to be retried
158 protected ScheduledExecutorService executorService
159 = newScheduledThreadPool(5, groupedThreads("OfdpaPipeliner", "retry-%d", log));
160 protected static final int MAX_RETRY_ATTEMPTS = 10;
161 protected static final int RETRY_MS = 1000;
Saurav Das4f980082015-11-05 13:39:15 -0800162
Saurav Das822c4e22015-10-23 10:51:11 -0700163 @Override
164 public void init(DeviceId deviceId, PipelinerContext context) {
Saurav Das822c4e22015-10-23 10:51:11 -0700165 this.deviceId = deviceId;
166
Charles Chan425854b2016-04-11 15:32:12 -0700167 serviceDirectory = context.directory();
Saurav Das822c4e22015-10-23 10:51:11 -0700168 coreService = serviceDirectory.get(CoreService.class);
169 flowRuleService = serviceDirectory.get(FlowRuleService.class);
170 groupService = serviceDirectory.get(GroupService.class);
171 flowObjectiveStore = context.store();
Saurav Das822c4e22015-10-23 10:51:11 -0700172 deviceService = serviceDirectory.get(DeviceService.class);
Saurav Das822c4e22015-10-23 10:51:11 -0700173
Charles Chan40132b32017-01-22 00:19:37 -0800174 initDriverId();
175 initGroupHander(context);
Saurav Das822c4e22015-10-23 10:51:11 -0700176
Saurav Das822c4e22015-10-23 10:51:11 -0700177 initializePipeline();
Saurav Das822c4e22015-10-23 10:51:11 -0700178 }
179
Charles Chan40132b32017-01-22 00:19:37 -0800180 protected void initDriverId() {
181 driverId = coreService.registerApplication(
182 "org.onosproject.driver.Ofdpa2Pipeline");
183 }
184
185 protected void initGroupHander(PipelinerContext context) {
186 groupHandler = new Ofdpa2GroupHandler();
187 groupHandler.init(deviceId, context);
188 }
189
Saurav Das822c4e22015-10-23 10:51:11 -0700190 protected void initializePipeline() {
Charles Chan188ebf52015-12-23 00:15:11 -0800191 // OF-DPA does not require initializing the pipeline as it puts default
192 // rules automatically in the hardware. However emulation of OFDPA in
193 // software switches does require table-miss-entries.
Saurav Das822c4e22015-10-23 10:51:11 -0700194 }
195
Charles Chand1172632017-03-15 17:33:09 -0700196 /**
197 * Determines whether this pipeline requires OFDPA match and set VLAN extensions.
198 *
199 * @return true to use the extensions
200 */
201 protected boolean requireVlanExtensions() {
202 return true;
203 }
204
Saurav Das822c4e22015-10-23 10:51:11 -0700205 //////////////////////////////////////
206 // Flow Objectives
207 //////////////////////////////////////
208
209 @Override
210 public void filter(FilteringObjective filteringObjective) {
211 if (filteringObjective.type() == FilteringObjective.Type.PERMIT) {
212 processFilter(filteringObjective,
213 filteringObjective.op() == Objective.Operation.ADD,
214 filteringObjective.appId());
215 } else {
216 // Note that packets that don't match the PERMIT filter are
217 // automatically denied. The DENY filter is used to deny packets
218 // that are otherwise permitted by the PERMIT filter.
219 // Use ACL table flow rules here for DENY filtering objectives
220 log.debug("filter objective other than PERMIT currently not supported");
221 fail(filteringObjective, ObjectiveError.UNSUPPORTED);
222 }
223 }
224
225 @Override
226 public void forward(ForwardingObjective fwd) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700227 Collection<FlowRule> rules = processForward(fwd);
Saurav Das25190812016-05-27 13:54:07 -0700228 if (rules == null || rules.isEmpty()) {
229 // Assumes fail message has already been generated to the objective
230 // context. Returning here prevents spurious pass message to be
231 // generated by FlowRule service for empty flowOps.
232 return;
233 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700234 sendForward(fwd, rules);
235 }
236
237 protected void sendForward(ForwardingObjective fwd, Collection<FlowRule> rules) {
238 FlowRuleOperations.Builder flowOpsBuilder = FlowRuleOperations.builder();
Saurav Das822c4e22015-10-23 10:51:11 -0700239 switch (fwd.op()) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700240 case ADD:
241 rules.stream()
242 .filter(Objects::nonNull)
243 .forEach(flowOpsBuilder::add);
Saurav Dasd2fded02016-12-02 15:43:47 -0800244 log.debug("Applying a add fwd-obj {} to sw:{}", fwd.id(), deviceId);
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700245 break;
246 case REMOVE:
247 rules.stream()
248 .filter(Objects::nonNull)
249 .forEach(flowOpsBuilder::remove);
Pier Ventre42287df2016-11-09 14:17:26 -0800250 log.debug("Deleting a flow rule to sw:{}", deviceId);
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700251 break;
252 default:
253 fail(fwd, ObjectiveError.UNKNOWN);
254 log.warn("Unknown forwarding type {}", fwd.op());
Saurav Das822c4e22015-10-23 10:51:11 -0700255 }
256
Saurav Das822c4e22015-10-23 10:51:11 -0700257 flowRuleService.apply(flowOpsBuilder.build(new FlowRuleOperationsContext() {
258 @Override
259 public void onSuccess(FlowRuleOperations ops) {
260 pass(fwd);
261 }
262
263 @Override
264 public void onError(FlowRuleOperations ops) {
265 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
266 }
267 }));
Saurav Das822c4e22015-10-23 10:51:11 -0700268 }
269
270 @Override
271 public void next(NextObjective nextObjective) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800272 NextGroup nextGroup = flowObjectiveStore.getNextGroup(nextObjective.id());
273 switch (nextObjective.op()) {
274 case ADD:
Saurav Das4f980082015-11-05 13:39:15 -0800275 if (nextGroup != null) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800276 log.warn("Cannot add next {} that already exists in device {}",
277 nextObjective.id(), deviceId);
278 return;
279 }
280 log.debug("Processing NextObjective id{} in dev{} - add group",
281 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700282 groupHandler.addGroup(nextObjective);
Saurav Das8a0732e2015-11-20 15:27:53 -0800283 break;
284 case ADD_TO_EXISTING:
285 if (nextGroup != null) {
286 log.debug("Processing NextObjective id{} in dev{} - add bucket",
287 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700288 groupHandler.addBucketToGroup(nextObjective, nextGroup);
Saurav Das4f980082015-11-05 13:39:15 -0800289 } else {
Saurav Das8a0732e2015-11-20 15:27:53 -0800290 // it is possible that group-chain has not been fully created yet
Saurav Das423fe2b2015-12-04 10:52:59 -0800291 log.debug("Waiting to add bucket to group for next-id:{} in dev:{}",
292 nextObjective.id(), deviceId);
293 // by design only one pending bucket is allowed for the group
Charles Chan425854b2016-04-11 15:32:12 -0700294 groupHandler.pendingBuckets.put(nextObjective.id(), nextObjective);
Saurav Das4f980082015-11-05 13:39:15 -0800295 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800296 break;
297 case REMOVE:
298 if (nextGroup == null) {
299 log.warn("Cannot remove next {} that does not exist in device {}",
300 nextObjective.id(), deviceId);
301 return;
302 }
303 log.debug("Processing NextObjective id{} in dev{} - remove group",
304 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700305 groupHandler.removeGroup(nextObjective, nextGroup);
Saurav Das8a0732e2015-11-20 15:27:53 -0800306 break;
307 case REMOVE_FROM_EXISTING:
308 if (nextGroup == null) {
309 log.warn("Cannot remove from next {} that does not exist in device {}",
310 nextObjective.id(), deviceId);
311 return;
312 }
313 log.debug("Processing NextObjective id{} in dev{} - remove bucket",
314 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700315 groupHandler.removeBucketFromGroup(nextObjective, nextGroup);
Saurav Das8a0732e2015-11-20 15:27:53 -0800316 break;
317 default:
Saurav Das4f980082015-11-05 13:39:15 -0800318 log.warn("Unsupported operation {}", nextObjective.op());
Saurav Das822c4e22015-10-23 10:51:11 -0700319 }
320 }
321
322 //////////////////////////////////////
323 // Flow handling
324 //////////////////////////////////////
325
326 /**
327 * As per OFDPA 2.0 TTP, filtering of VLAN ids, MAC addresses (for routing)
328 * and IP addresses configured on switch ports happen in different tables.
329 * Note that IP filtering rules need to be added to the ACL table, as there
330 * is no mechanism to send to controller via IP table.
331 *
332 * @param filt the filtering objective
333 * @param install indicates whether to add or remove the objective
334 * @param applicationId the application that sent this objective
335 */
Saurav Das52025962016-01-28 22:30:01 -0800336 protected void processFilter(FilteringObjective filt,
337 boolean install, ApplicationId applicationId) {
Saurav Das822c4e22015-10-23 10:51:11 -0700338 // This driver only processes filtering criteria defined with switch
339 // ports as the key
340 PortCriterion portCriterion = null;
341 EthCriterion ethCriterion = null;
342 VlanIdCriterion vidCriterion = null;
343 Collection<IPCriterion> ips = new ArrayList<IPCriterion>();
344 if (!filt.key().equals(Criteria.dummy()) &&
345 filt.key().type() == Criterion.Type.IN_PORT) {
346 portCriterion = (PortCriterion) filt.key();
347 } else {
348 log.warn("No key defined in filtering objective from app: {}. Not"
349 + "processing filtering objective", applicationId);
Saurav Das59232cf2016-04-27 18:35:50 -0700350 fail(filt, ObjectiveError.BADPARAMS);
Saurav Das822c4e22015-10-23 10:51:11 -0700351 return;
352 }
Saurav Das59232cf2016-04-27 18:35:50 -0700353 log.debug("Received filtering objective for dev/port: {}/{}", deviceId,
354 portCriterion.port());
Saurav Das822c4e22015-10-23 10:51:11 -0700355 // convert filtering conditions for switch-intfs into flowrules
356 FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
357 for (Criterion criterion : filt.conditions()) {
Charles Chan5b9df8d2016-03-28 22:21:40 -0700358 if (criterion.type() == Criterion.Type.ETH_DST ||
359 criterion.type() == Criterion.Type.ETH_DST_MASKED) {
Saurav Das822c4e22015-10-23 10:51:11 -0700360 ethCriterion = (EthCriterion) criterion;
361 } else if (criterion.type() == Criterion.Type.VLAN_VID) {
362 vidCriterion = (VlanIdCriterion) criterion;
363 } else if (criterion.type() == Criterion.Type.IPV4_DST) {
364 ips.add((IPCriterion) criterion);
365 } else {
366 log.error("Unsupported filter {}", criterion);
367 fail(filt, ObjectiveError.UNSUPPORTED);
368 return;
369 }
370 }
371
Saurav Das0e99e2b2015-10-28 12:39:42 -0700372 VlanId assignedVlan = null;
Charles Chane849c192016-01-11 18:28:54 -0800373 if (vidCriterion != null) {
Charles Chand55e84d2016-03-30 17:54:24 -0700374 // Use the VLAN in metadata whenever a metadata is provided
375 if (filt.meta() != null) {
376 assignedVlan = readVlanFromTreatment(filt.meta());
377 // Use the VLAN in criterion if metadata is not present and the traffic is tagged
378 } else if (!vidCriterion.vlanId().equals(VlanId.NONE)) {
Charles Chane849c192016-01-11 18:28:54 -0800379 assignedVlan = vidCriterion.vlanId();
Charles Chand55e84d2016-03-30 17:54:24 -0700380 }
Charles Chane849c192016-01-11 18:28:54 -0800381
Charles Chand55e84d2016-03-30 17:54:24 -0700382 if (assignedVlan == null) {
383 log.error("Driver fails to extract VLAN information. "
384 + "Not proccessing VLAN filters on device {}.", deviceId);
385 log.debug("VLAN ID in criterion={}, metadata={}",
386 readVlanFromTreatment(filt.meta()), vidCriterion.vlanId());
387 fail(filt, ObjectiveError.BADPARAMS);
388 return;
Saurav Das0e99e2b2015-10-28 12:39:42 -0700389 }
390 }
391
Yi Tseng3a77b4f2017-02-06 15:02:17 -0800392 if (ethCriterion == null || ethCriterion.mac().equals(NONE)) {
Charles Chan985b12e2016-05-11 19:47:22 -0700393 // NOTE: it is possible that a filtering objective only has vidCriterion
394 log.debug("filtering objective missing dstMac, cannot program TMAC table");
Saurav Das822c4e22015-10-23 10:51:11 -0700395 } else {
396 for (FlowRule tmacRule : processEthDstFilter(portCriterion, ethCriterion,
Saurav Das0e99e2b2015-10-28 12:39:42 -0700397 vidCriterion, assignedVlan,
398 applicationId)) {
Saurav Das018605f2017-02-18 14:05:44 -0800399 log.debug("{} MAC filtering rules in TMAC table: {} for dev: {}",
400 (install) ? "adding" : "removing", tmacRule, deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700401 ops = install ? ops.add(tmacRule) : ops.remove(tmacRule);
402 }
403 }
404
Charles Chan985b12e2016-05-11 19:47:22 -0700405 if (vidCriterion == null) {
406 // NOTE: it is possible that a filtering objective only has ethCriterion
407 log.debug("filtering objective missing dstMac or VLAN, "
Charles Chane849c192016-01-11 18:28:54 -0800408 + "cannot program VLAN Table");
Saurav Das822c4e22015-10-23 10:51:11 -0700409 } else {
Charles Chan14967c22015-12-07 11:11:50 -0800410 /*
411 * NOTE: Separate vlan filtering rules and assignment rules
412 * into different stage in order to guarantee that filtering rules
Saurav Das52025962016-01-28 22:30:01 -0800413 * always go first, as required by ofdpa.
Charles Chan14967c22015-12-07 11:11:50 -0800414 */
415 List<FlowRule> allRules = processVlanIdFilter(
416 portCriterion, vidCriterion, assignedVlan, applicationId);
417 List<FlowRule> filteringRules = new ArrayList<>();
418 List<FlowRule> assignmentRules = new ArrayList<>();
419
420 allRules.forEach(flowRule -> {
Charles Chand1172632017-03-15 17:33:09 -0700421 VlanId vlanId;
422 if (requireVlanExtensions()) {
423 ExtensionCriterion extCriterion =
424 (ExtensionCriterion) flowRule.selector().getCriterion(Criterion.Type.EXTENSION);
425 vlanId = ((OfdpaMatchVlanVid) extCriterion.extensionSelector()).vlanId();
426 } else {
427 VlanIdCriterion vlanIdCriterion =
428 (VlanIdCriterion) flowRule.selector().getCriterion(Criterion.Type.VLAN_VID);
429 vlanId = vlanIdCriterion.vlanId();
430 }
Charles Chanbe8aea42016-02-24 12:04:47 -0800431 if (!vlanId.equals(VlanId.NONE)) {
Charles Chan14967c22015-12-07 11:11:50 -0800432 filteringRules.add(flowRule);
433 } else {
434 assignmentRules.add(flowRule);
435 }
436 });
437
438 for (FlowRule filteringRule : filteringRules) {
Saurav Das018605f2017-02-18 14:05:44 -0800439 log.debug("{} VLAN filtering rule in VLAN table: {} for dev: {}",
440 (install) ? "adding" : "removing", filteringRule, deviceId);
Charles Chan14967c22015-12-07 11:11:50 -0800441 ops = install ? ops.add(filteringRule) : ops.remove(filteringRule);
442 }
443
444 ops.newStage();
445
446 for (FlowRule assignmentRule : assignmentRules) {
Saurav Das018605f2017-02-18 14:05:44 -0800447 log.debug("{} VLAN assignment rule in VLAN table: {} for dev: {}",
448 (install) ? "adding" : "removing", assignmentRule, deviceId);
Charles Chan14967c22015-12-07 11:11:50 -0800449 ops = install ? ops.add(assignmentRule) : ops.remove(assignmentRule);
Saurav Das822c4e22015-10-23 10:51:11 -0700450 }
451 }
452
Saurav Das822c4e22015-10-23 10:51:11 -0700453 // apply filtering flow rules
454 flowRuleService.apply(ops.build(new FlowRuleOperationsContext() {
455 @Override
456 public void onSuccess(FlowRuleOperations ops) {
Saurav Das018605f2017-02-18 14:05:44 -0800457 log.debug("Applied {} filtering rules in device {}",
Saurav Das822c4e22015-10-23 10:51:11 -0700458 ops.stages().get(0).size(), deviceId);
459 pass(filt);
460 }
461
462 @Override
463 public void onError(FlowRuleOperations ops) {
464 log.info("Failed to apply all filtering rules in dev {}", deviceId);
465 fail(filt, ObjectiveError.FLOWINSTALLATIONFAILED);
466 }
467 }));
468
469 }
470
471 /**
Charles Chand1172632017-03-15 17:33:09 -0700472 * Internal implementation of processVlanIdFilter.
473 * <p>
474 * The is_present bit in set_vlan_vid action is required to be 0 in OFDPA i12.
475 * Since it is non-OF spec, we need an extension treatment for that.
476 * The useVlanExtension must be set to false for OFDPA i12.
477 * </p>
Saurav Das0e99e2b2015-10-28 12:39:42 -0700478 *
Charles Chanf9e98652016-09-07 16:54:23 -0700479 * @param portCriterion port on device for which this filter is programmed
480 * @param vidCriterion vlan assigned to port, or NONE for untagged
481 * @param assignedVlan assigned vlan-id for untagged packets
482 * @param applicationId for application programming this filter
Saurav Das822c4e22015-10-23 10:51:11 -0700483 * @return list of FlowRule for port-vlan filters
484 */
485 protected List<FlowRule> processVlanIdFilter(PortCriterion portCriterion,
Charles Chanf9e98652016-09-07 16:54:23 -0700486 VlanIdCriterion vidCriterion,
487 VlanId assignedVlan,
Charles Chand1172632017-03-15 17:33:09 -0700488 ApplicationId applicationId) {
Charles Chan14967c22015-12-07 11:11:50 -0800489 List<FlowRule> rules = new ArrayList<>();
Saurav Das822c4e22015-10-23 10:51:11 -0700490 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
491 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
Charles Chan14967c22015-12-07 11:11:50 -0800492 TrafficSelector.Builder preSelector = null;
493 TrafficTreatment.Builder preTreatment = null;
494
Saurav Das4f980082015-11-05 13:39:15 -0800495 treatment.transition(TMAC_TABLE);
496
Saurav Das822c4e22015-10-23 10:51:11 -0700497 if (vidCriterion.vlanId() == VlanId.NONE) {
498 // untagged packets are assigned vlans
Charles Chand1172632017-03-15 17:33:09 -0700499 preSelector = DefaultTrafficSelector.builder();
500 if (requireVlanExtensions()) {
501 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(VlanId.NONE);
502 selector.extension(ofdpaMatchVlanVid, deviceId);
Charles Chanf9e98652016-09-07 16:54:23 -0700503 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(assignedVlan);
504 treatment.extension(ofdpaSetVlanVid, deviceId);
Charles Chand1172632017-03-15 17:33:09 -0700505
506 OfdpaMatchVlanVid preOfdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
507 preSelector.extension(preOfdpaMatchVlanVid, deviceId);
Charles Chanf9e98652016-09-07 16:54:23 -0700508 } else {
Charles Chand1172632017-03-15 17:33:09 -0700509 selector.matchVlanId(VlanId.NONE);
Charles Chanf9e98652016-09-07 16:54:23 -0700510 treatment.setVlanId(assignedVlan);
Charles Chand1172632017-03-15 17:33:09 -0700511
512 preSelector.matchVlanId(assignedVlan);
513 }
514 preTreatment = DefaultTrafficTreatment.builder().transition(TMAC_TABLE);
515 } else {
516 if (requireVlanExtensions()) {
517 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vidCriterion.vlanId());
518 selector.extension(ofdpaMatchVlanVid, deviceId);
519 } else {
520 selector.matchVlanId(vidCriterion.vlanId());
Charles Chanf9e98652016-09-07 16:54:23 -0700521 }
Charles Chan14967c22015-12-07 11:11:50 -0800522
Charles Chand55e84d2016-03-30 17:54:24 -0700523 if (!assignedVlan.equals(vidCriterion.vlanId())) {
Charles Chand1172632017-03-15 17:33:09 -0700524 if (requireVlanExtensions()) {
Charles Chanc03782d2017-01-31 13:57:55 -0800525 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(assignedVlan);
526 treatment.extension(ofdpaSetVlanVid, deviceId);
527 } else {
528 treatment.setVlanId(assignedVlan);
529 }
Charles Chand55e84d2016-03-30 17:54:24 -0700530 }
Saurav Das822c4e22015-10-23 10:51:11 -0700531 }
Saurav Das822c4e22015-10-23 10:51:11 -0700532
533 // ofdpa cannot match on ALL portnumber, so we need to use separate
534 // rules for each port.
Charles Chan14967c22015-12-07 11:11:50 -0800535 List<PortNumber> portnums = new ArrayList<>();
Saurav Das822c4e22015-10-23 10:51:11 -0700536 if (portCriterion.port() == PortNumber.ALL) {
537 for (Port port : deviceService.getPorts(deviceId)) {
538 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
539 portnums.add(port.number());
540 }
541 }
542 } else {
543 portnums.add(portCriterion.port());
544 }
Saurav Das4f980082015-11-05 13:39:15 -0800545
Saurav Das822c4e22015-10-23 10:51:11 -0700546 for (PortNumber pnum : portnums) {
Saurav Das4f980082015-11-05 13:39:15 -0800547 // create rest of flowrule
Saurav Das822c4e22015-10-23 10:51:11 -0700548 selector.matchInPort(pnum);
549 FlowRule rule = DefaultFlowRule.builder()
550 .forDevice(deviceId)
551 .withSelector(selector.build())
552 .withTreatment(treatment.build())
553 .withPriority(DEFAULT_PRIORITY)
554 .fromApp(applicationId)
555 .makePermanent()
556 .forTable(VLAN_TABLE).build();
Charles Chan14967c22015-12-07 11:11:50 -0800557
558 if (preSelector != null) {
559 preSelector.matchInPort(pnum);
560 FlowRule preRule = DefaultFlowRule.builder()
561 .forDevice(deviceId)
562 .withSelector(preSelector.build())
563 .withTreatment(preTreatment.build())
564 .withPriority(DEFAULT_PRIORITY)
565 .fromApp(applicationId)
566 .makePermanent()
567 .forTable(VLAN_TABLE).build();
568 rules.add(preRule);
569 }
570
Saurav Das822c4e22015-10-23 10:51:11 -0700571 rules.add(rule);
572 }
573 return rules;
574 }
575
576 /**
577 * Allows routed packets with correct destination MAC to be directed
578 * to unicast-IP routing table or MPLS forwarding table.
Saurav Das822c4e22015-10-23 10:51:11 -0700579 *
580 * @param portCriterion port on device for which this filter is programmed
581 * @param ethCriterion dstMac of device for which is filter is programmed
582 * @param vidCriterion vlan assigned to port, or NONE for untagged
Saurav Das0e99e2b2015-10-28 12:39:42 -0700583 * @param assignedVlan assigned vlan-id for untagged packets
Saurav Das822c4e22015-10-23 10:51:11 -0700584 * @param applicationId for application programming this filter
585 * @return list of FlowRule for port-vlan filters
586
587 */
588 protected List<FlowRule> processEthDstFilter(PortCriterion portCriterion,
589 EthCriterion ethCriterion,
590 VlanIdCriterion vidCriterion,
Saurav Das0e99e2b2015-10-28 12:39:42 -0700591 VlanId assignedVlan,
Saurav Das822c4e22015-10-23 10:51:11 -0700592 ApplicationId applicationId) {
Charles Chan5270ed02016-01-30 23:22:37 -0800593 // Consider PortNumber.ANY as wildcard. Match ETH_DST only
594 if (portCriterion != null && portCriterion.port() == PortNumber.ANY) {
595 return processEthDstOnlyFilter(ethCriterion, applicationId);
596 }
597
Charles Chan5b9df8d2016-03-28 22:21:40 -0700598 // Multicast MAC
599 if (ethCriterion.mask() != null) {
600 return processMcastEthDstFilter(ethCriterion, applicationId);
601 }
602
Saurav Das822c4e22015-10-23 10:51:11 -0700603 //handling untagged packets via assigned VLAN
604 if (vidCriterion.vlanId() == VlanId.NONE) {
Saurav Das0e99e2b2015-10-28 12:39:42 -0700605 vidCriterion = (VlanIdCriterion) Criteria.matchVlanId(assignedVlan);
Saurav Das822c4e22015-10-23 10:51:11 -0700606 }
607 // ofdpa cannot match on ALL portnumber, so we need to use separate
608 // rules for each port.
609 List<PortNumber> portnums = new ArrayList<PortNumber>();
610 if (portCriterion.port() == PortNumber.ALL) {
611 for (Port port : deviceService.getPorts(deviceId)) {
612 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
613 portnums.add(port.number());
614 }
615 }
616 } else {
617 portnums.add(portCriterion.port());
618 }
619
620 List<FlowRule> rules = new ArrayList<FlowRule>();
621 for (PortNumber pnum : portnums) {
Charles Chan14967c22015-12-07 11:11:50 -0800622 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vidCriterion.vlanId());
Saurav Das822c4e22015-10-23 10:51:11 -0700623 // for unicast IP packets
624 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
625 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
626 selector.matchInPort(pnum);
Charles Chand1172632017-03-15 17:33:09 -0700627 if (requireVlanExtensions()) {
628 selector.extension(ofdpaMatchVlanVid, deviceId);
629 } else {
630 selector.matchVlanId(vidCriterion.vlanId());
631 }
Saurav Das822c4e22015-10-23 10:51:11 -0700632 selector.matchEthType(Ethernet.TYPE_IPV4);
633 selector.matchEthDst(ethCriterion.mac());
634 treatment.transition(UNICAST_ROUTING_TABLE);
635 FlowRule rule = DefaultFlowRule.builder()
636 .forDevice(deviceId)
637 .withSelector(selector.build())
638 .withTreatment(treatment.build())
639 .withPriority(DEFAULT_PRIORITY)
640 .fromApp(applicationId)
641 .makePermanent()
642 .forTable(TMAC_TABLE).build();
643 rules.add(rule);
644 //for MPLS packets
645 selector = DefaultTrafficSelector.builder();
646 treatment = DefaultTrafficTreatment.builder();
647 selector.matchInPort(pnum);
Charles Chand1172632017-03-15 17:33:09 -0700648 if (requireVlanExtensions()) {
649 selector.extension(ofdpaMatchVlanVid, deviceId);
650 } else {
651 selector.matchVlanId(vidCriterion.vlanId());
652 }
Saurav Das822c4e22015-10-23 10:51:11 -0700653 selector.matchEthType(Ethernet.MPLS_UNICAST);
654 selector.matchEthDst(ethCriterion.mac());
655 treatment.transition(MPLS_TABLE_0);
656 rule = DefaultFlowRule.builder()
657 .forDevice(deviceId)
658 .withSelector(selector.build())
659 .withTreatment(treatment.build())
660 .withPriority(DEFAULT_PRIORITY)
661 .fromApp(applicationId)
662 .makePermanent()
663 .forTable(TMAC_TABLE).build();
664 rules.add(rule);
Pier Ventree0ae7a32016-11-23 09:57:42 -0800665 /*
666 * TMAC rules for IPv6 packets
667 */
668 selector = DefaultTrafficSelector.builder();
669 treatment = DefaultTrafficTreatment.builder();
670 selector.matchInPort(pnum);
Charles Chand1172632017-03-15 17:33:09 -0700671 if (requireVlanExtensions()) {
672 selector.extension(ofdpaMatchVlanVid, deviceId);
673 } else {
674 selector.matchVlanId(vidCriterion.vlanId());
675 }
Pier Ventree0ae7a32016-11-23 09:57:42 -0800676 selector.matchEthType(Ethernet.TYPE_IPV6);
677 selector.matchEthDst(ethCriterion.mac());
678 treatment.transition(UNICAST_ROUTING_TABLE);
679 rule = DefaultFlowRule.builder()
680 .forDevice(deviceId)
681 .withSelector(selector.build())
682 .withTreatment(treatment.build())
683 .withPriority(DEFAULT_PRIORITY)
684 .fromApp(applicationId)
685 .makePermanent()
686 .forTable(TMAC_TABLE).build();
687 rules.add(rule);
Saurav Das822c4e22015-10-23 10:51:11 -0700688 }
689 return rules;
690 }
691
Charles Chan5270ed02016-01-30 23:22:37 -0800692 protected List<FlowRule> processEthDstOnlyFilter(EthCriterion ethCriterion,
693 ApplicationId applicationId) {
Pier Luigi0e358632017-01-31 09:35:05 -0800694 ImmutableList.Builder<FlowRule> builder = ImmutableList.builder();
695
Charles Chan5270ed02016-01-30 23:22:37 -0800696 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
697 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
698 selector.matchEthType(Ethernet.TYPE_IPV4);
699 selector.matchEthDst(ethCriterion.mac());
700 treatment.transition(UNICAST_ROUTING_TABLE);
701 FlowRule rule = DefaultFlowRule.builder()
702 .forDevice(deviceId)
703 .withSelector(selector.build())
704 .withTreatment(treatment.build())
705 .withPriority(DEFAULT_PRIORITY)
706 .fromApp(applicationId)
707 .makePermanent()
708 .forTable(TMAC_TABLE).build();
Pier Luigi0e358632017-01-31 09:35:05 -0800709 builder.add(rule);
710
711 selector = DefaultTrafficSelector.builder();
712 treatment = DefaultTrafficTreatment.builder();
713 selector.matchEthType(Ethernet.TYPE_IPV6);
714 selector.matchEthDst(ethCriterion.mac());
715 treatment.transition(UNICAST_ROUTING_TABLE);
716 rule = DefaultFlowRule.builder()
717 .forDevice(deviceId)
718 .withSelector(selector.build())
719 .withTreatment(treatment.build())
720 .withPriority(DEFAULT_PRIORITY)
721 .fromApp(applicationId)
722 .makePermanent()
723 .forTable(TMAC_TABLE).build();
724 return builder.add(rule).build();
Charles Chan5270ed02016-01-30 23:22:37 -0800725 }
726
Charles Chan5b9df8d2016-03-28 22:21:40 -0700727 protected List<FlowRule> processMcastEthDstFilter(EthCriterion ethCriterion,
728 ApplicationId applicationId) {
729 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
730 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
731 selector.matchEthType(Ethernet.TYPE_IPV4);
732 selector.matchEthDstMasked(ethCriterion.mac(), ethCriterion.mask());
733 treatment.transition(MULTICAST_ROUTING_TABLE);
734 FlowRule rule = DefaultFlowRule.builder()
735 .forDevice(deviceId)
736 .withSelector(selector.build())
737 .withTreatment(treatment.build())
738 .withPriority(DEFAULT_PRIORITY)
739 .fromApp(applicationId)
740 .makePermanent()
741 .forTable(TMAC_TABLE).build();
742 return ImmutableList.<FlowRule>builder().add(rule).build();
743 }
744
Saurav Das822c4e22015-10-23 10:51:11 -0700745 private Collection<FlowRule> processForward(ForwardingObjective fwd) {
746 switch (fwd.flag()) {
747 case SPECIFIC:
748 return processSpecific(fwd);
749 case VERSATILE:
750 return processVersatile(fwd);
751 default:
752 fail(fwd, ObjectiveError.UNKNOWN);
753 log.warn("Unknown forwarding flag {}", fwd.flag());
754 }
755 return Collections.emptySet();
756 }
757
758 /**
759 * In the OF-DPA 2.0 pipeline, versatile forwarding objectives go to the
760 * ACL table.
761 * @param fwd the forwarding objective of type 'versatile'
762 * @return a collection of flow rules to be sent to the switch. An empty
763 * collection may be returned if there is a problem in processing
764 * the flow rule
765 */
Saurav Das52025962016-01-28 22:30:01 -0800766 protected Collection<FlowRule> processVersatile(ForwardingObjective fwd) {
Saurav Das018605f2017-02-18 14:05:44 -0800767 log.debug("Processing versatile forwarding objective:{} in dev:{}",
Saurav Dasd2fded02016-12-02 15:43:47 -0800768 fwd.id(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700769
770 EthTypeCriterion ethType =
Saurav Das77b5e902016-01-27 17:01:59 -0800771 (EthTypeCriterion) fwd.selector().getCriterion(Criterion.Type.ETH_TYPE);
Saurav Das822c4e22015-10-23 10:51:11 -0700772 if (ethType == null) {
Saurav Dasd2fded02016-12-02 15:43:47 -0800773 log.error("Versatile forwarding objective:{} must include ethType",
774 fwd.id());
Saurav Das822c4e22015-10-23 10:51:11 -0700775 fail(fwd, ObjectiveError.BADPARAMS);
776 return Collections.emptySet();
777 }
778 if (fwd.nextId() == null && fwd.treatment() == null) {
779 log.error("Forwarding objective {} from {} must contain "
780 + "nextId or Treatment", fwd.selector(), fwd.appId());
Zsolt Haraszti9faab752016-02-17 15:55:20 -0800781 fail(fwd, ObjectiveError.BADPARAMS);
Saurav Das822c4e22015-10-23 10:51:11 -0700782 return Collections.emptySet();
783 }
Saurav Das49cb5a12016-01-16 22:54:07 -0800784
Saurav Das77b5e902016-01-27 17:01:59 -0800785 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
786 fwd.selector().criteria().forEach(criterion -> {
787 if (criterion instanceof VlanIdCriterion) {
788 VlanId vlanId = ((VlanIdCriterion) criterion).vlanId();
789 // ensure that match does not include vlan = NONE as OF-DPA does not
790 // match untagged packets this way in the ACL table.
791 if (vlanId.equals(VlanId.NONE)) {
792 return;
793 }
Charles Chand1172632017-03-15 17:33:09 -0700794 if (requireVlanExtensions()) {
795 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vlanId);
796 sbuilder.extension(ofdpaMatchVlanVid, deviceId);
797 } else {
798 sbuilder.matchVlanId(vlanId);
799 }
Pier Ventree0ae7a32016-11-23 09:57:42 -0800800 } else if (criterion instanceof Icmpv6TypeCriterion ||
801 criterion instanceof Icmpv6CodeCriterion) {
802 /*
803 * We silenty discard these criterions, our current
804 * OFDPA platform does not support these matches on
805 * the ACL table.
806 */
807 log.warn("ICMPv6 Type and ICMPv6 Code are not supported");
Saurav Das77b5e902016-01-27 17:01:59 -0800808 } else {
809 sbuilder.add(criterion);
810 }
811 });
812
Saurav Das822c4e22015-10-23 10:51:11 -0700813 // XXX driver does not currently do type checking as per Tables 65-67 in
814 // OFDPA 2.0 spec. The only allowed treatment is a punt to the controller.
Saurav Das49cb5a12016-01-16 22:54:07 -0800815 TrafficTreatment.Builder ttBuilder = DefaultTrafficTreatment.builder();
816 if (fwd.treatment() != null) {
817 for (Instruction ins : fwd.treatment().allInstructions()) {
818 if (ins instanceof OutputInstruction) {
819 OutputInstruction o = (OutputInstruction) ins;
820 if (o.port() == PortNumber.CONTROLLER) {
821 ttBuilder.add(o);
822 } else {
823 log.warn("Only allowed treatments in versatile forwarding "
824 + "objectives are punts to the controller");
825 }
826 } else {
827 log.warn("Cannot process instruction in versatile fwd {}", ins);
828 }
Saurav Das822c4e22015-10-23 10:51:11 -0700829 }
Charles Chan2df0e8a2017-01-09 11:45:08 -0800830 if (fwd.treatment().clearedDeferred()) {
831 ttBuilder.wipeDeferred();
832 }
Saurav Das822c4e22015-10-23 10:51:11 -0700833 }
Saurav Das822c4e22015-10-23 10:51:11 -0700834 if (fwd.nextId() != null) {
Saurav Das49cb5a12016-01-16 22:54:07 -0800835 // overide case
836 NextGroup next = getGroupForNextObjective(fwd.nextId());
837 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
838 // we only need the top level group's key to point the flow to it
839 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
840 if (group == null) {
841 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
842 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
843 fail(fwd, ObjectiveError.GROUPMISSING);
844 return Collections.emptySet();
845 }
846 ttBuilder.deferred().group(group.id());
Saurav Das822c4e22015-10-23 10:51:11 -0700847 }
Saurav Das49cb5a12016-01-16 22:54:07 -0800848
849 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
850 .fromApp(fwd.appId())
851 .withPriority(fwd.priority())
852 .forDevice(deviceId)
Saurav Das77b5e902016-01-27 17:01:59 -0800853 .withSelector(sbuilder.build())
Saurav Das49cb5a12016-01-16 22:54:07 -0800854 .withTreatment(ttBuilder.build())
855 .makePermanent()
856 .forTable(ACL_TABLE);
857 return Collections.singletonList(ruleBuilder.build());
Saurav Das822c4e22015-10-23 10:51:11 -0700858 }
859
860 /**
861 * In the OF-DPA 2.0 pipeline, specific forwarding refers to the IP table
Saurav Das8a0732e2015-11-20 15:27:53 -0800862 * (unicast or multicast) or the L2 table (mac + vlan) or the MPLS table.
Saurav Das822c4e22015-10-23 10:51:11 -0700863 *
864 * @param fwd the forwarding objective of type 'specific'
865 * @return a collection of flow rules. Typically there will be only one
866 * for this type of forwarding objective. An empty set may be
867 * returned if there is an issue in processing the objective.
868 */
Saurav Das8a0732e2015-11-20 15:27:53 -0800869 protected Collection<FlowRule> processSpecific(ForwardingObjective fwd) {
Saurav Das25190812016-05-27 13:54:07 -0700870 log.debug("Processing specific fwd objective:{} in dev:{} with next:{}",
Saurav Das4ce45962015-11-24 23:21:05 -0800871 fwd.id(), deviceId, fwd.nextId());
872 boolean isEthTypeObj = isSupportedEthTypeObjective(fwd);
873 boolean isEthDstObj = isSupportedEthDstObjective(fwd);
874
875 if (isEthTypeObj) {
876 return processEthTypeSpecific(fwd);
877 } else if (isEthDstObj) {
878 return processEthDstSpecific(fwd);
879 } else {
880 log.warn("processSpecific: Unsupported forwarding objective "
881 + "criteria fwd:{} in dev:{}", fwd.nextId(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700882 fail(fwd, ObjectiveError.UNSUPPORTED);
883 return Collections.emptySet();
884 }
Saurav Das4ce45962015-11-24 23:21:05 -0800885 }
886
Saurav Das4ce45962015-11-24 23:21:05 -0800887 /**
888 * Handles forwarding rules to the IP and MPLS tables.
889 *
890 * @param fwd the forwarding objective
891 * @return A collection of flow rules, or an empty set
892 */
893 protected Collection<FlowRule> processEthTypeSpecific(ForwardingObjective fwd) {
Charles Chancad338a2016-09-16 18:03:11 -0700894 return processEthTypeSpecificInternal(fwd, false, ACL_TABLE);
Charles Chanf9e98652016-09-07 16:54:23 -0700895 }
896
897 /**
898 * Internal implementation of processEthTypeSpecific.
899 * <p>
900 * Wildcarded IPv4_DST is not supported in OFDPA i12. Therefore, we break
901 * the rule into 0.0.0.0/1 and 128.0.0.0/1.
902 * The allowDefaultRoute must be set to false for OFDPA i12.
903 * </p>
904 *
905 * @param fwd the forwarding objective
906 * @param allowDefaultRoute allow wildcarded IPv4_DST or not
Ray Milkey0bb1e102016-11-10 14:51:27 -0800907 * @param mplsNextTable next MPLS table
Charles Chanf9e98652016-09-07 16:54:23 -0700908 * @return A collection of flow rules, or an empty set
909 */
910 protected Collection<FlowRule> processEthTypeSpecificInternal(ForwardingObjective fwd,
Charles Chancad338a2016-09-16 18:03:11 -0700911 boolean allowDefaultRoute,
912 int mplsNextTable) {
Saurav Das4ce45962015-11-24 23:21:05 -0800913 TrafficSelector selector = fwd.selector();
914 EthTypeCriterion ethType =
915 (EthTypeCriterion) selector.getCriterion(Criterion.Type.ETH_TYPE);
Flavio Castroe10fa242016-01-15 12:43:51 -0800916 boolean popMpls = false;
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700917 boolean emptyGroup = false;
Charles Chan188ebf52015-12-23 00:15:11 -0800918 int forTableId;
Saurav Das8a0732e2015-11-20 15:27:53 -0800919 TrafficSelector.Builder filteredSelector = DefaultTrafficSelector.builder();
Charles Chan14967c22015-12-07 11:11:50 -0800920 TrafficTreatment.Builder tb = DefaultTrafficTreatment.builder();
Flavio Castroe10fa242016-01-15 12:43:51 -0800921 TrafficSelector.Builder complementarySelector = DefaultTrafficSelector.builder();
Charles Chan14967c22015-12-07 11:11:50 -0800922
Saurav Das8a0732e2015-11-20 15:27:53 -0800923 if (ethType.ethType().toShort() == Ethernet.TYPE_IPV4) {
Pier Luigi3bfe32c2017-01-30 09:47:36 -0800924 if (buildIpv4Selector(filteredSelector, complementarySelector, fwd, allowDefaultRoute) < 0) {
925 return Collections.emptyList();
926 }
927 // We need to set properly the next table
Flavio Castroe10fa242016-01-15 12:43:51 -0800928 IpPrefix ipv4Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV4_DST)).ip();
Charles Chan5b9df8d2016-03-28 22:21:40 -0700929 if (ipv4Dst.isMulticast()) {
Charles Chan5b9df8d2016-03-28 22:21:40 -0700930 forTableId = MULTICAST_ROUTING_TABLE;
Flavio Castroe10fa242016-01-15 12:43:51 -0800931 } else {
Charles Chan5b9df8d2016-03-28 22:21:40 -0700932 forTableId = UNICAST_ROUTING_TABLE;
Flavio Castroe10fa242016-01-15 12:43:51 -0800933 }
Charles Chan14967c22015-12-07 11:11:50 -0800934 if (fwd.treatment() != null) {
935 for (Instruction instr : fwd.treatment().allInstructions()) {
936 if (instr instanceof L3ModificationInstruction &&
937 ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
Saurav Das9c705342017-01-06 11:36:01 -0800938 // XXX decrementing IP ttl is done automatically for routing, this
939 // action is ignored or rejected in ofdpa as it is not fully implemented
940 //tb.deferred().add(instr);
Charles Chan14967c22015-12-07 11:11:50 -0800941 }
942 }
943 }
944
Pier Ventree0ae7a32016-11-23 09:57:42 -0800945 } else if (ethType.ethType().toShort() == Ethernet.TYPE_IPV6) {
946 if (buildIpv6Selector(filteredSelector, fwd) < 0) {
947 return Collections.emptyList();
948 }
949 forTableId = UNICAST_ROUTING_TABLE;
950 if (fwd.treatment() != null) {
951 for (Instruction instr : fwd.treatment().allInstructions()) {
952 if (instr instanceof L3ModificationInstruction &&
953 ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
954 // XXX decrementing IP ttl is done automatically for routing, this
955 // action is ignored or rejected in ofdpa as it is not fully implemented
956 //tb.deferred().add(instr);
957 }
958 }
959 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800960 } else {
961 filteredSelector
962 .matchEthType(Ethernet.MPLS_UNICAST)
963 .matchMplsLabel(((MplsCriterion)
964 selector.getCriterion(Criterion.Type.MPLS_LABEL)).label());
965 MplsBosCriterion bos = (MplsBosCriterion) selector
Pier Ventre140a8942016-11-02 07:26:38 -0700966 .getCriterion(MPLS_BOS);
Saurav Das8a0732e2015-11-20 15:27:53 -0800967 if (bos != null) {
968 filteredSelector.matchMplsBos(bos.mplsBos());
969 }
970 forTableId = MPLS_TABLE_1;
Saurav Das4ce45962015-11-24 23:21:05 -0800971 log.debug("processing MPLS specific forwarding objective {} -> next:{}"
972 + " in dev {}", fwd.id(), fwd.nextId(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700973
Charles Chan14967c22015-12-07 11:11:50 -0800974 if (fwd.treatment() != null) {
975 for (Instruction instr : fwd.treatment().allInstructions()) {
976 if (instr instanceof L2ModificationInstruction &&
977 ((L2ModificationInstruction) instr).subtype() == L2SubType.MPLS_POP) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800978 popMpls = true;
Pier Luigi3bfe32c2017-01-30 09:47:36 -0800979 // OF-DPA does not pop in MPLS table in some cases. For the L3 VPN, it requires
Saurav Das9c705342017-01-06 11:36:01 -0800980 // setting the MPLS_TYPE so pop can happen down the pipeline
Pier Luigi3bfe32c2017-01-30 09:47:36 -0800981 if (mplsNextTable == MPLS_TYPE_TABLE && isNotMplsBos(selector)) {
982 tb.immediate().popMpls();
983 }
Charles Chan14967c22015-12-07 11:11:50 -0800984 }
985 if (instr instanceof L3ModificationInstruction &&
986 ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
987 // FIXME Should modify the app to send the correct DEC_MPLS_TTL instruction
988 tb.immediate().decMplsTtl();
989 }
990 if (instr instanceof L3ModificationInstruction &&
991 ((L3ModificationInstruction) instr).subtype() == L3SubType.TTL_IN) {
992 tb.immediate().add(instr);
993 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800994 }
995 }
996 }
Saurav Das822c4e22015-10-23 10:51:11 -0700997
998 if (fwd.nextId() != null) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800999 if (forTableId == MPLS_TABLE_1 && !popMpls) {
1000 log.warn("SR CONTINUE case cannot be handled as MPLS ECMP "
Saurav Das25190812016-05-27 13:54:07 -07001001 + "is not implemented in OF-DPA yet. Aborting this flow {} -> next:{}"
1002 + "in this device {}", fwd.id(), fwd.nextId(), deviceId);
Pier Ventree0ae7a32016-11-23 09:57:42 -08001003 // XXX We could convert to forwarding to a single-port, via a MPLS interface,
1004 // or a MPLS SWAP (with-same) but that would have to be handled in the next-objective.
1005 // Also the pop-mpls logic used here won't work in non-BoS case.
Saurav Das4ce45962015-11-24 23:21:05 -08001006 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
Saurav Das8a0732e2015-11-20 15:27:53 -08001007 return Collections.emptySet();
1008 }
1009
Saurav Das423fe2b2015-12-04 10:52:59 -08001010 NextGroup next = getGroupForNextObjective(fwd.nextId());
1011 if (next != null) {
1012 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1013 // we only need the top level group's key to point the flow to it
1014 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
Pier Ventre140a8942016-11-02 07:26:38 -07001015 if (isNotMplsBos(selector) && group.type().equals(HASHED)) {
1016 log.warn("SR CONTINUE case cannot be handled as MPLS ECMP "
1017 + "is not implemented in OF-DPA yet. Aborting this flow {} -> next:{}"
1018 + "in this device {}", fwd.id(), fwd.nextId(), deviceId);
1019 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
1020 return Collections.emptySet();
1021 }
Saurav Das423fe2b2015-12-04 10:52:59 -08001022 if (group == null) {
1023 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
1024 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
1025 fail(fwd, ObjectiveError.GROUPMISSING);
1026 return Collections.emptySet();
1027 }
1028 tb.deferred().group(group.id());
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001029 // check if group is empty
1030 if (gkeys.size() == 1 && gkeys.get(0).size() == 1) {
1031 log.warn("Found empty group 0x{} in dev:{} .. will retry fwd:{}",
1032 Integer.toHexString(group.id().id()), deviceId, fwd.id());
1033 emptyGroup = true;
1034 }
Saurav Das25190812016-05-27 13:54:07 -07001035 } else {
1036 log.warn("Cannot find group for nextId:{} in dev:{}. Aborting fwd:{}",
1037 fwd.nextId(), deviceId, fwd.id());
1038 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
1039 return Collections.emptySet();
Saurav Das822c4e22015-10-23 10:51:11 -07001040 }
Saurav Das822c4e22015-10-23 10:51:11 -07001041 }
Charles Chancad338a2016-09-16 18:03:11 -07001042
1043 if (forTableId == MPLS_TABLE_1) {
Pier Ventre140a8942016-11-02 07:26:38 -07001044 if (mplsNextTable == MPLS_L3_TYPE_TABLE) {
Charles Chancad338a2016-09-16 18:03:11 -07001045 Ofdpa3SetMplsType setMplsType = new Ofdpa3SetMplsType(Ofdpa3MplsType.L3_PHP);
Saurav Das9c705342017-01-06 11:36:01 -08001046 // set mpls type as apply_action
1047 tb.immediate().extension(setMplsType, deviceId);
Charles Chancad338a2016-09-16 18:03:11 -07001048 }
1049 tb.transition(mplsNextTable);
1050 } else {
1051 tb.transition(ACL_TABLE);
1052 }
1053
Saurav Das822c4e22015-10-23 10:51:11 -07001054 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
1055 .fromApp(fwd.appId())
1056 .withPriority(fwd.priority())
1057 .forDevice(deviceId)
Saurav Das8a0732e2015-11-20 15:27:53 -08001058 .withSelector(filteredSelector.build())
1059 .withTreatment(tb.build())
1060 .forTable(forTableId);
Saurav Das822c4e22015-10-23 10:51:11 -07001061
1062 if (fwd.permanent()) {
1063 ruleBuilder.makePermanent();
1064 } else {
1065 ruleBuilder.makeTemporary(fwd.timeout());
1066 }
Flavio Castroe10fa242016-01-15 12:43:51 -08001067 Collection<FlowRule> flowRuleCollection = new ArrayList<>();
1068 flowRuleCollection.add(ruleBuilder.build());
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001069 if (!allowDefaultRoute) {
Pier Ventree0ae7a32016-11-23 09:57:42 -08001070 flowRuleCollection.add(
1071 defaultRoute(fwd, complementarySelector, forTableId, tb)
1072 );
Flavio Castroe10fa242016-01-15 12:43:51 -08001073 log.debug("Default rule 0.0.0.0/0 is being installed two rules");
1074 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001075 // XXX retrying flows may be necessary due to bug CORD-554
1076 if (emptyGroup) {
1077 executorService.schedule(new RetryFlows(fwd, flowRuleCollection),
1078 RETRY_MS, TimeUnit.MILLISECONDS);
1079 }
Flavio Castroe10fa242016-01-15 12:43:51 -08001080 return flowRuleCollection;
Saurav Das822c4e22015-10-23 10:51:11 -07001081 }
1082
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001083 protected int buildIpv4Selector(TrafficSelector.Builder builderToUpdate,
1084 TrafficSelector.Builder extBuilder,
1085 ForwardingObjective fwd,
1086 boolean allowDefaultRoute) {
1087 TrafficSelector selector = fwd.selector();
1088
1089 IpPrefix ipv4Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV4_DST)).ip();
1090 if (ipv4Dst.isMulticast()) {
1091 if (ipv4Dst.prefixLength() != 32) {
1092 log.warn("Multicast specific forwarding objective can only be /32");
1093 fail(fwd, ObjectiveError.BADPARAMS);
1094 return -1;
1095 }
1096 VlanId assignedVlan = readVlanFromSelector(fwd.meta());
1097 if (assignedVlan == null) {
1098 log.warn("VLAN ID required by multicast specific fwd obj is missing. Abort.");
1099 fail(fwd, ObjectiveError.BADPARAMS);
1100 return -1;
1101 }
Charles Chand1172632017-03-15 17:33:09 -07001102 if (requireVlanExtensions()) {
1103 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
1104 builderToUpdate.extension(ofdpaMatchVlanVid, deviceId);
1105 } else {
1106 builderToUpdate.matchVlanId(assignedVlan);
1107 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001108 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipv4Dst);
1109 log.debug("processing IPv4 multicast specific forwarding objective {} -> next:{}"
1110 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
1111 } else {
1112 if (ipv4Dst.prefixLength() == 0) {
1113 if (allowDefaultRoute) {
1114 // The entire IPV4_DST field is wildcarded intentionally
1115 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4);
1116 } else {
1117 // NOTE: The switch does not support matching 0.0.0.0/0
1118 // Split it into 0.0.0.0/1 and 128.0.0.0/1
1119 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4)
1120 .matchIPDst(IpPrefix.valueOf("0.0.0.0/1"));
1121 extBuilder.matchEthType(Ethernet.TYPE_IPV4)
1122 .matchIPDst(IpPrefix.valueOf("128.0.0.0/1"));
1123 }
1124 } else {
1125 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipv4Dst);
1126 }
1127 log.debug("processing IPv4 unicast specific forwarding objective {} -> next:{}"
1128 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
1129 }
1130 return 0;
1131 }
1132
1133 /**
1134 * Helper method to build Ipv6 selector using the selector provided by
1135 * a forwarding objective.
1136 *
1137 * @param builderToUpdate the builder to update
1138 * @param fwd the selector to read
1139 * @return 0 if the update ends correctly. -1 if the matches
1140 * are not yet supported
1141 */
1142 protected int buildIpv6Selector(TrafficSelector.Builder builderToUpdate,
1143 ForwardingObjective fwd) {
1144
1145 TrafficSelector selector = fwd.selector();
1146
1147 IpPrefix ipv6Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV6_DST)).ip();
1148 if (ipv6Dst.isMulticast()) {
1149 log.warn("IPv6 Multicast is currently not supported");
1150 fail(fwd, ObjectiveError.BADPARAMS);
1151 return -1;
1152 }
1153 if (ipv6Dst.prefixLength() != 0) {
1154 builderToUpdate.matchIPv6Dst(ipv6Dst);
1155 }
1156 builderToUpdate.matchEthType(Ethernet.TYPE_IPV6);
1157 log.debug("processing IPv6 unicast specific forwarding objective {} -> next:{}"
1158 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
1159 return 0;
1160 }
1161
Pier Ventree0ae7a32016-11-23 09:57:42 -08001162 protected FlowRule defaultRoute(ForwardingObjective fwd,
1163 TrafficSelector.Builder complementarySelector,
1164 int forTableId,
1165 TrafficTreatment.Builder tb) {
1166 FlowRule.Builder rule = DefaultFlowRule.builder()
1167 .fromApp(fwd.appId())
1168 .withPriority(fwd.priority())
1169 .forDevice(deviceId)
1170 .withSelector(complementarySelector.build())
1171 .withTreatment(tb.build())
1172 .forTable(forTableId);
1173 if (fwd.permanent()) {
1174 rule.makePermanent();
1175 } else {
1176 rule.makeTemporary(fwd.timeout());
1177 }
1178 return rule.build();
1179 }
1180
Saurav Das4ce45962015-11-24 23:21:05 -08001181 /**
1182 * Handles forwarding rules to the L2 bridging table. Flow actions are not
1183 * allowed in the bridging table - instead we use L2 Interface group or
1184 * L2 flood group
1185 *
1186 * @param fwd the forwarding objective
1187 * @return A collection of flow rules, or an empty set
1188 */
1189 protected Collection<FlowRule> processEthDstSpecific(ForwardingObjective fwd) {
1190 List<FlowRule> rules = new ArrayList<>();
1191
1192 // Build filtered selector
1193 TrafficSelector selector = fwd.selector();
1194 EthCriterion ethCriterion = (EthCriterion) selector
1195 .getCriterion(Criterion.Type.ETH_DST);
1196 VlanIdCriterion vlanIdCriterion = (VlanIdCriterion) selector
1197 .getCriterion(Criterion.Type.VLAN_VID);
1198
1199 if (vlanIdCriterion == null) {
1200 log.warn("Forwarding objective for bridging requires vlan. Not "
1201 + "installing fwd:{} in dev:{}", fwd.id(), deviceId);
1202 fail(fwd, ObjectiveError.BADPARAMS);
1203 return Collections.emptySet();
1204 }
1205
1206 TrafficSelector.Builder filteredSelectorBuilder =
1207 DefaultTrafficSelector.builder();
Yi Tseng3a77b4f2017-02-06 15:02:17 -08001208
1209 if (!ethCriterion.mac().equals(NONE) &&
1210 !ethCriterion.mac().equals(BROADCAST)) {
Saurav Das4ce45962015-11-24 23:21:05 -08001211 filteredSelectorBuilder.matchEthDst(ethCriterion.mac());
1212 log.debug("processing L2 forwarding objective:{} -> next:{} in dev:{}",
1213 fwd.id(), fwd.nextId(), deviceId);
1214 } else {
Yi Tseng3a77b4f2017-02-06 15:02:17 -08001215 // Use wildcard DST_MAC if the MacAddress is None or Broadcast
Saurav Das4ce45962015-11-24 23:21:05 -08001216 log.debug("processing L2 Broadcast forwarding objective:{} -> next:{} "
1217 + "in dev:{} for vlan:{}",
1218 fwd.id(), fwd.nextId(), deviceId, vlanIdCriterion.vlanId());
1219 }
Charles Chand1172632017-03-15 17:33:09 -07001220 if (requireVlanExtensions()) {
1221 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vlanIdCriterion.vlanId());
1222 filteredSelectorBuilder.extension(ofdpaMatchVlanVid, deviceId);
1223 } else {
1224 filteredSelectorBuilder.matchVlanId(vlanIdCriterion.vlanId());
1225 }
Saurav Das4ce45962015-11-24 23:21:05 -08001226 TrafficSelector filteredSelector = filteredSelectorBuilder.build();
1227
1228 if (fwd.treatment() != null) {
1229 log.warn("Ignoring traffic treatment in fwd rule {} meant for L2 table"
1230 + "for dev:{}. Expecting only nextId", fwd.id(), deviceId);
1231 }
1232
1233 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
1234 if (fwd.nextId() != null) {
Saurav Das423fe2b2015-12-04 10:52:59 -08001235 NextGroup next = getGroupForNextObjective(fwd.nextId());
Saurav Das4ce45962015-11-24 23:21:05 -08001236 if (next != null) {
1237 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1238 // we only need the top level group's key to point the flow to it
1239 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
1240 if (group != null) {
1241 treatmentBuilder.deferred().group(group.id());
1242 } else {
1243 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
1244 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
1245 fail(fwd, ObjectiveError.GROUPMISSING);
1246 return Collections.emptySet();
1247 }
1248 }
1249 }
1250 treatmentBuilder.immediate().transition(ACL_TABLE);
1251 TrafficTreatment filteredTreatment = treatmentBuilder.build();
1252
1253 // Build bridging table entries
1254 FlowRule.Builder flowRuleBuilder = DefaultFlowRule.builder();
1255 flowRuleBuilder.fromApp(fwd.appId())
1256 .withPriority(fwd.priority())
1257 .forDevice(deviceId)
1258 .withSelector(filteredSelector)
1259 .withTreatment(filteredTreatment)
1260 .forTable(BRIDGING_TABLE);
1261 if (fwd.permanent()) {
1262 flowRuleBuilder.makePermanent();
1263 } else {
1264 flowRuleBuilder.makeTemporary(fwd.timeout());
1265 }
1266 rules.add(flowRuleBuilder.build());
1267 return rules;
1268 }
1269
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001270 //////////////////////////////////////
1271 // Helper Methods and Classes
1272 //////////////////////////////////////
1273
1274 private boolean isSupportedEthTypeObjective(ForwardingObjective fwd) {
1275 TrafficSelector selector = fwd.selector();
1276 EthTypeCriterion ethType = (EthTypeCriterion) selector
1277 .getCriterion(Criterion.Type.ETH_TYPE);
1278 return !((ethType == null) ||
1279 ((ethType.ethType().toShort() != Ethernet.TYPE_IPV4) &&
Pier Ventree0ae7a32016-11-23 09:57:42 -08001280 (ethType.ethType().toShort() != Ethernet.MPLS_UNICAST)) &&
1281 (ethType.ethType().toShort() != Ethernet.TYPE_IPV6));
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001282 }
1283
1284 private boolean isSupportedEthDstObjective(ForwardingObjective fwd) {
1285 TrafficSelector selector = fwd.selector();
1286 EthCriterion ethDst = (EthCriterion) selector
1287 .getCriterion(Criterion.Type.ETH_DST);
1288 VlanIdCriterion vlanId = (VlanIdCriterion) selector
1289 .getCriterion(Criterion.Type.VLAN_VID);
1290 return !(ethDst == null && vlanId == null);
1291 }
1292
Saurav Das423fe2b2015-12-04 10:52:59 -08001293 protected NextGroup getGroupForNextObjective(Integer nextId) {
1294 NextGroup next = flowObjectiveStore.getNextGroup(nextId);
1295 if (next != null) {
1296 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1297 if (gkeys != null && !gkeys.isEmpty()) {
1298 return next;
1299 } else {
1300 log.warn("Empty next group found in FlowObjective store for "
1301 + "next-id:{} in dev:{}", nextId, deviceId);
1302 }
1303 } else {
1304 log.warn("next-id {} not found in Flow objective store for dev:{}",
1305 nextId, deviceId);
1306 }
1307 return null;
1308 }
1309
Charles Chan188ebf52015-12-23 00:15:11 -08001310 protected static void pass(Objective obj) {
Sho SHIMIZUef7e2902016-02-12 18:38:29 -08001311 obj.context().ifPresent(context -> context.onSuccess(obj));
Saurav Das822c4e22015-10-23 10:51:11 -07001312 }
1313
Charles Chan188ebf52015-12-23 00:15:11 -08001314 protected static void fail(Objective obj, ObjectiveError error) {
Sho SHIMIZUef7e2902016-02-12 18:38:29 -08001315 obj.context().ifPresent(context -> context.onError(obj, error));
Saurav Das822c4e22015-10-23 10:51:11 -07001316 }
Saurav Das24431192016-03-07 19:13:00 -08001317
Saurav Das24431192016-03-07 19:13:00 -08001318 @Override
1319 public List<String> getNextMappings(NextGroup nextGroup) {
1320 List<String> mappings = new ArrayList<>();
1321 List<Deque<GroupKey>> gkeys = appKryo.deserialize(nextGroup.data());
1322 for (Deque<GroupKey> gkd : gkeys) {
1323 Group lastGroup = null;
Saurav Das8be4e3a2016-03-11 17:19:07 -08001324 StringBuffer gchain = new StringBuffer();
Saurav Das24431192016-03-07 19:13:00 -08001325 for (GroupKey gk : gkd) {
1326 Group g = groupService.getGroup(deviceId, gk);
Saurav Das8be4e3a2016-03-11 17:19:07 -08001327 if (g == null) {
Saurav Das25190812016-05-27 13:54:07 -07001328 gchain.append(" NoGrp").append(" -->");
Saurav Das8be4e3a2016-03-11 17:19:07 -08001329 continue;
1330 }
1331 gchain.append(" 0x").append(Integer.toHexString(g.id().id()))
1332 .append(" -->");
Saurav Das24431192016-03-07 19:13:00 -08001333 lastGroup = g;
1334 }
1335 // add port information for last group in group-chain
Saurav Das25190812016-05-27 13:54:07 -07001336 List<Instruction> lastGroupIns = new ArrayList<Instruction>();
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001337 if (lastGroup != null && !lastGroup.buckets().buckets().isEmpty()) {
Saurav Das25190812016-05-27 13:54:07 -07001338 lastGroupIns = lastGroup.buckets().buckets().get(0)
1339 .treatment().allInstructions();
1340 }
1341 for (Instruction i: lastGroupIns) {
Saurav Das24431192016-03-07 19:13:00 -08001342 if (i instanceof OutputInstruction) {
Saurav Das8be4e3a2016-03-11 17:19:07 -08001343 gchain.append(" port:").append(((OutputInstruction) i).port());
Saurav Das24431192016-03-07 19:13:00 -08001344 }
1345 }
Saurav Das8be4e3a2016-03-11 17:19:07 -08001346 mappings.add(gchain.toString());
Saurav Das24431192016-03-07 19:13:00 -08001347 }
1348 return mappings;
1349 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001350
Pier Ventre140a8942016-11-02 07:26:38 -07001351 static boolean isMplsBos(TrafficSelector selector) {
1352 MplsBosCriterion bosCriterion = (MplsBosCriterion) selector.getCriterion(MPLS_BOS);
1353 return bosCriterion != null && bosCriterion.mplsBos();
1354 }
1355
1356 static boolean isNotMplsBos(TrafficSelector selector) {
1357 MplsBosCriterion bosCriterion = (MplsBosCriterion) selector.getCriterion(MPLS_BOS);
1358 return bosCriterion != null && !bosCriterion.mplsBos();
1359 }
1360
Charles Chan5b9df8d2016-03-28 22:21:40 -07001361 protected static VlanId readVlanFromSelector(TrafficSelector selector) {
Saurav Das59232cf2016-04-27 18:35:50 -07001362 if (selector == null) {
1363 return null;
1364 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001365 Criterion criterion = selector.getCriterion(Criterion.Type.VLAN_VID);
1366 return (criterion == null)
1367 ? null : ((VlanIdCriterion) criterion).vlanId();
1368 }
1369
1370 protected static IpPrefix readIpDstFromSelector(TrafficSelector selector) {
Saurav Das59232cf2016-04-27 18:35:50 -07001371 if (selector == null) {
1372 return null;
1373 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001374 Criterion criterion = selector.getCriterion(Criterion.Type.IPV4_DST);
1375 return (criterion == null) ? null : ((IPCriterion) criterion).ip();
1376 }
Charles Chand55e84d2016-03-30 17:54:24 -07001377
1378 private static VlanId readVlanFromTreatment(TrafficTreatment treatment) {
Saurav Das59232cf2016-04-27 18:35:50 -07001379 if (treatment == null) {
1380 return null;
1381 }
Charles Chand55e84d2016-03-30 17:54:24 -07001382 for (Instruction i : treatment.allInstructions()) {
1383 if (i instanceof ModVlanIdInstruction) {
1384 return ((ModVlanIdInstruction) i).vlanId();
1385 }
1386 }
1387 return null;
1388 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001389
1390 /**
1391 * Utility class that retries sending flows a fixed number of times, even if
1392 * some of the attempts are successful. Used only for forwarding objectives.
1393 */
1394 protected final class RetryFlows implements Runnable {
1395 int attempts = MAX_RETRY_ATTEMPTS;
1396 private Collection<FlowRule> retryFlows;
1397 private ForwardingObjective fwd;
1398
1399 RetryFlows(ForwardingObjective fwd, Collection<FlowRule> retryFlows) {
1400 this.fwd = fwd;
1401 this.retryFlows = retryFlows;
1402 }
1403
1404 @Override
1405 public void run() {
1406 log.info("RETRY FLOWS ATTEMPT# {} for fwd:{} rules:{}",
1407 MAX_RETRY_ATTEMPTS - attempts, fwd.id(), retryFlows.size());
1408 sendForward(fwd, retryFlows);
1409 if (--attempts > 0) {
1410 executorService.schedule(this, RETRY_MS, TimeUnit.MILLISECONDS);
1411 }
1412 }
1413 }
1414
Saurav Das822c4e22015-10-23 10:51:11 -07001415}