blob: 23249f771ee7438e6e92c1b995fcf778b470e54b [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
196 //////////////////////////////////////
197 // Flow Objectives
198 //////////////////////////////////////
199
200 @Override
201 public void filter(FilteringObjective filteringObjective) {
202 if (filteringObjective.type() == FilteringObjective.Type.PERMIT) {
203 processFilter(filteringObjective,
204 filteringObjective.op() == Objective.Operation.ADD,
205 filteringObjective.appId());
206 } else {
207 // Note that packets that don't match the PERMIT filter are
208 // automatically denied. The DENY filter is used to deny packets
209 // that are otherwise permitted by the PERMIT filter.
210 // Use ACL table flow rules here for DENY filtering objectives
211 log.debug("filter objective other than PERMIT currently not supported");
212 fail(filteringObjective, ObjectiveError.UNSUPPORTED);
213 }
214 }
215
216 @Override
217 public void forward(ForwardingObjective fwd) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700218 Collection<FlowRule> rules = processForward(fwd);
Saurav Das25190812016-05-27 13:54:07 -0700219 if (rules == null || rules.isEmpty()) {
220 // Assumes fail message has already been generated to the objective
221 // context. Returning here prevents spurious pass message to be
222 // generated by FlowRule service for empty flowOps.
223 return;
224 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700225 sendForward(fwd, rules);
226 }
227
228 protected void sendForward(ForwardingObjective fwd, Collection<FlowRule> rules) {
229 FlowRuleOperations.Builder flowOpsBuilder = FlowRuleOperations.builder();
Saurav Das822c4e22015-10-23 10:51:11 -0700230 switch (fwd.op()) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700231 case ADD:
232 rules.stream()
233 .filter(Objects::nonNull)
234 .forEach(flowOpsBuilder::add);
Saurav Dasd2fded02016-12-02 15:43:47 -0800235 log.debug("Applying a add fwd-obj {} to sw:{}", fwd.id(), deviceId);
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700236 break;
237 case REMOVE:
238 rules.stream()
239 .filter(Objects::nonNull)
240 .forEach(flowOpsBuilder::remove);
Pier Ventre42287df2016-11-09 14:17:26 -0800241 log.debug("Deleting a flow rule to sw:{}", deviceId);
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700242 break;
243 default:
244 fail(fwd, ObjectiveError.UNKNOWN);
245 log.warn("Unknown forwarding type {}", fwd.op());
Saurav Das822c4e22015-10-23 10:51:11 -0700246 }
247
Saurav Das822c4e22015-10-23 10:51:11 -0700248 flowRuleService.apply(flowOpsBuilder.build(new FlowRuleOperationsContext() {
249 @Override
250 public void onSuccess(FlowRuleOperations ops) {
251 pass(fwd);
252 }
253
254 @Override
255 public void onError(FlowRuleOperations ops) {
256 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
257 }
258 }));
Saurav Das822c4e22015-10-23 10:51:11 -0700259 }
260
261 @Override
262 public void next(NextObjective nextObjective) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800263 NextGroup nextGroup = flowObjectiveStore.getNextGroup(nextObjective.id());
264 switch (nextObjective.op()) {
265 case ADD:
Saurav Das4f980082015-11-05 13:39:15 -0800266 if (nextGroup != null) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800267 log.warn("Cannot add next {} that already exists in device {}",
268 nextObjective.id(), deviceId);
269 return;
270 }
271 log.debug("Processing NextObjective id{} in dev{} - add group",
272 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700273 groupHandler.addGroup(nextObjective);
Saurav Das8a0732e2015-11-20 15:27:53 -0800274 break;
275 case ADD_TO_EXISTING:
276 if (nextGroup != null) {
277 log.debug("Processing NextObjective id{} in dev{} - add bucket",
278 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700279 groupHandler.addBucketToGroup(nextObjective, nextGroup);
Saurav Das4f980082015-11-05 13:39:15 -0800280 } else {
Saurav Das8a0732e2015-11-20 15:27:53 -0800281 // it is possible that group-chain has not been fully created yet
Saurav Das423fe2b2015-12-04 10:52:59 -0800282 log.debug("Waiting to add bucket to group for next-id:{} in dev:{}",
283 nextObjective.id(), deviceId);
284 // by design only one pending bucket is allowed for the group
Charles Chan425854b2016-04-11 15:32:12 -0700285 groupHandler.pendingBuckets.put(nextObjective.id(), nextObjective);
Saurav Das4f980082015-11-05 13:39:15 -0800286 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800287 break;
288 case REMOVE:
289 if (nextGroup == null) {
290 log.warn("Cannot remove next {} that does not exist in device {}",
291 nextObjective.id(), deviceId);
292 return;
293 }
294 log.debug("Processing NextObjective id{} in dev{} - remove group",
295 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700296 groupHandler.removeGroup(nextObjective, nextGroup);
Saurav Das8a0732e2015-11-20 15:27:53 -0800297 break;
298 case REMOVE_FROM_EXISTING:
299 if (nextGroup == null) {
300 log.warn("Cannot remove from next {} that does not exist in device {}",
301 nextObjective.id(), deviceId);
302 return;
303 }
304 log.debug("Processing NextObjective id{} in dev{} - remove bucket",
305 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700306 groupHandler.removeBucketFromGroup(nextObjective, nextGroup);
Saurav Das8a0732e2015-11-20 15:27:53 -0800307 break;
308 default:
Saurav Das4f980082015-11-05 13:39:15 -0800309 log.warn("Unsupported operation {}", nextObjective.op());
Saurav Das822c4e22015-10-23 10:51:11 -0700310 }
311 }
312
313 //////////////////////////////////////
314 // Flow handling
315 //////////////////////////////////////
316
317 /**
318 * As per OFDPA 2.0 TTP, filtering of VLAN ids, MAC addresses (for routing)
319 * and IP addresses configured on switch ports happen in different tables.
320 * Note that IP filtering rules need to be added to the ACL table, as there
321 * is no mechanism to send to controller via IP table.
322 *
323 * @param filt the filtering objective
324 * @param install indicates whether to add or remove the objective
325 * @param applicationId the application that sent this objective
326 */
Saurav Das52025962016-01-28 22:30:01 -0800327 protected void processFilter(FilteringObjective filt,
328 boolean install, ApplicationId applicationId) {
Saurav Das822c4e22015-10-23 10:51:11 -0700329 // This driver only processes filtering criteria defined with switch
330 // ports as the key
331 PortCriterion portCriterion = null;
332 EthCriterion ethCriterion = null;
333 VlanIdCriterion vidCriterion = null;
334 Collection<IPCriterion> ips = new ArrayList<IPCriterion>();
335 if (!filt.key().equals(Criteria.dummy()) &&
336 filt.key().type() == Criterion.Type.IN_PORT) {
337 portCriterion = (PortCriterion) filt.key();
338 } else {
339 log.warn("No key defined in filtering objective from app: {}. Not"
340 + "processing filtering objective", applicationId);
Saurav Das59232cf2016-04-27 18:35:50 -0700341 fail(filt, ObjectiveError.BADPARAMS);
Saurav Das822c4e22015-10-23 10:51:11 -0700342 return;
343 }
Saurav Das59232cf2016-04-27 18:35:50 -0700344 log.debug("Received filtering objective for dev/port: {}/{}", deviceId,
345 portCriterion.port());
Saurav Das822c4e22015-10-23 10:51:11 -0700346 // convert filtering conditions for switch-intfs into flowrules
347 FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
348 for (Criterion criterion : filt.conditions()) {
Charles Chan5b9df8d2016-03-28 22:21:40 -0700349 if (criterion.type() == Criterion.Type.ETH_DST ||
350 criterion.type() == Criterion.Type.ETH_DST_MASKED) {
Saurav Das822c4e22015-10-23 10:51:11 -0700351 ethCriterion = (EthCriterion) criterion;
352 } else if (criterion.type() == Criterion.Type.VLAN_VID) {
353 vidCriterion = (VlanIdCriterion) criterion;
354 } else if (criterion.type() == Criterion.Type.IPV4_DST) {
355 ips.add((IPCriterion) criterion);
356 } else {
357 log.error("Unsupported filter {}", criterion);
358 fail(filt, ObjectiveError.UNSUPPORTED);
359 return;
360 }
361 }
362
Saurav Das0e99e2b2015-10-28 12:39:42 -0700363 VlanId assignedVlan = null;
Charles Chane849c192016-01-11 18:28:54 -0800364 if (vidCriterion != null) {
Charles Chand55e84d2016-03-30 17:54:24 -0700365 // Use the VLAN in metadata whenever a metadata is provided
366 if (filt.meta() != null) {
367 assignedVlan = readVlanFromTreatment(filt.meta());
368 // Use the VLAN in criterion if metadata is not present and the traffic is tagged
369 } else if (!vidCriterion.vlanId().equals(VlanId.NONE)) {
Charles Chane849c192016-01-11 18:28:54 -0800370 assignedVlan = vidCriterion.vlanId();
Charles Chand55e84d2016-03-30 17:54:24 -0700371 }
Charles Chane849c192016-01-11 18:28:54 -0800372
Charles Chand55e84d2016-03-30 17:54:24 -0700373 if (assignedVlan == null) {
374 log.error("Driver fails to extract VLAN information. "
375 + "Not proccessing VLAN filters on device {}.", deviceId);
376 log.debug("VLAN ID in criterion={}, metadata={}",
377 readVlanFromTreatment(filt.meta()), vidCriterion.vlanId());
378 fail(filt, ObjectiveError.BADPARAMS);
379 return;
Saurav Das0e99e2b2015-10-28 12:39:42 -0700380 }
381 }
382
Yi Tseng3a77b4f2017-02-06 15:02:17 -0800383 if (ethCriterion == null || ethCriterion.mac().equals(NONE)) {
Charles Chan985b12e2016-05-11 19:47:22 -0700384 // NOTE: it is possible that a filtering objective only has vidCriterion
385 log.debug("filtering objective missing dstMac, cannot program TMAC table");
Saurav Das822c4e22015-10-23 10:51:11 -0700386 } else {
387 for (FlowRule tmacRule : processEthDstFilter(portCriterion, ethCriterion,
Saurav Das0e99e2b2015-10-28 12:39:42 -0700388 vidCriterion, assignedVlan,
389 applicationId)) {
Saurav Das822c4e22015-10-23 10:51:11 -0700390 log.debug("adding MAC filtering rules in TMAC table: {} for dev: {}",
391 tmacRule, deviceId);
392 ops = install ? ops.add(tmacRule) : ops.remove(tmacRule);
393 }
394 }
395
Charles Chan985b12e2016-05-11 19:47:22 -0700396 if (vidCriterion == null) {
397 // NOTE: it is possible that a filtering objective only has ethCriterion
398 log.debug("filtering objective missing dstMac or VLAN, "
Charles Chane849c192016-01-11 18:28:54 -0800399 + "cannot program VLAN Table");
Saurav Das822c4e22015-10-23 10:51:11 -0700400 } else {
Charles Chan14967c22015-12-07 11:11:50 -0800401 /*
402 * NOTE: Separate vlan filtering rules and assignment rules
403 * into different stage in order to guarantee that filtering rules
Saurav Das52025962016-01-28 22:30:01 -0800404 * always go first, as required by ofdpa.
Charles Chan14967c22015-12-07 11:11:50 -0800405 */
406 List<FlowRule> allRules = processVlanIdFilter(
407 portCriterion, vidCriterion, assignedVlan, applicationId);
408 List<FlowRule> filteringRules = new ArrayList<>();
409 List<FlowRule> assignmentRules = new ArrayList<>();
410
411 allRules.forEach(flowRule -> {
412 ExtensionCriterion extCriterion =
413 (ExtensionCriterion) flowRule.selector().getCriterion(Criterion.Type.EXTENSION);
414 VlanId vlanId = ((OfdpaMatchVlanVid) extCriterion.extensionSelector()).vlanId();
Charles Chanbe8aea42016-02-24 12:04:47 -0800415 if (!vlanId.equals(VlanId.NONE)) {
Charles Chan14967c22015-12-07 11:11:50 -0800416 filteringRules.add(flowRule);
417 } else {
418 assignmentRules.add(flowRule);
419 }
420 });
421
422 for (FlowRule filteringRule : filteringRules) {
Saurav Das822c4e22015-10-23 10:51:11 -0700423 log.debug("adding VLAN filtering rule in VLAN table: {} for dev: {}",
Charles Chan14967c22015-12-07 11:11:50 -0800424 filteringRule, deviceId);
425 ops = install ? ops.add(filteringRule) : ops.remove(filteringRule);
426 }
427
428 ops.newStage();
429
430 for (FlowRule assignmentRule : assignmentRules) {
431 log.debug("adding VLAN assignment rule in VLAN table: {} for dev: {}",
432 assignmentRule, deviceId);
433 ops = install ? ops.add(assignmentRule) : ops.remove(assignmentRule);
Saurav Das822c4e22015-10-23 10:51:11 -0700434 }
435 }
436
437 for (IPCriterion ipaddr : ips) {
438 // since we ignore port information for IP rules, and the same (gateway) IP
439 // can be configured on multiple ports, we make sure that we send
440 // only a single rule to the switch.
441 if (!sentIpFilters.contains(ipaddr)) {
442 sentIpFilters.add(ipaddr);
443 log.debug("adding IP filtering rules in ACL table {} for dev: {}",
444 ipaddr, deviceId);
445 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
446 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
447 selector.matchEthType(Ethernet.TYPE_IPV4);
448 selector.matchIPDst(ipaddr.ip());
449 treatment.setOutput(PortNumber.CONTROLLER);
450 FlowRule rule = DefaultFlowRule.builder()
451 .forDevice(deviceId)
452 .withSelector(selector.build())
453 .withTreatment(treatment.build())
454 .withPriority(HIGHEST_PRIORITY)
455 .fromApp(applicationId)
456 .makePermanent()
457 .forTable(ACL_TABLE).build();
458 ops = install ? ops.add(rule) : ops.remove(rule);
459 }
460 }
461
462 // apply filtering flow rules
463 flowRuleService.apply(ops.build(new FlowRuleOperationsContext() {
464 @Override
465 public void onSuccess(FlowRuleOperations ops) {
466 log.info("Applied {} filtering rules in device {}",
467 ops.stages().get(0).size(), deviceId);
468 pass(filt);
469 }
470
471 @Override
472 public void onError(FlowRuleOperations ops) {
473 log.info("Failed to apply all filtering rules in dev {}", deviceId);
474 fail(filt, ObjectiveError.FLOWINSTALLATIONFAILED);
475 }
476 }));
477
478 }
479
480 /**
481 * Allows untagged packets into pipeline by assigning a vlan id.
Saurav Das0e99e2b2015-10-28 12:39:42 -0700482 * Vlan assignment is done by the application.
Saurav Das822c4e22015-10-23 10:51:11 -0700483 * Allows tagged packets into pipeline as per configured port-vlan info.
Saurav Das0e99e2b2015-10-28 12:39:42 -0700484 *
Charles Chanf9e98652016-09-07 16:54:23 -0700485 * @param portCriterion port on device for which this filter is programmed
486 * @param vidCriterion vlan assigned to port, or NONE for untagged
487 * @param assignedVlan assigned vlan-id for untagged packets
488 * @param applicationId for application programming this filter
Saurav Das822c4e22015-10-23 10:51:11 -0700489 * @return list of FlowRule for port-vlan filters
490 */
491 protected List<FlowRule> processVlanIdFilter(PortCriterion portCriterion,
492 VlanIdCriterion vidCriterion,
Saurav Das0e99e2b2015-10-28 12:39:42 -0700493 VlanId assignedVlan,
Saurav Das822c4e22015-10-23 10:51:11 -0700494 ApplicationId applicationId) {
Charles Chanf9e98652016-09-07 16:54:23 -0700495 return processVlanIdFilterInternal(portCriterion, vidCriterion, assignedVlan,
496 applicationId, true);
497 }
498
499 /**
500 * Internal implementation of processVlanIdFilter.
501 * <p>
502 * The is_present bit in set_vlan_vid action is required to be 0 in OFDPA i12.
503 * Since it is non-OF spec, we need an extension treatment for that.
504 * The useSetVlanExtension must be set to false for OFDPA i12.
505 * </p>
506 *
507 * @param portCriterion port on device for which this filter is programmed
508 * @param vidCriterion vlan assigned to port, or NONE for untagged
509 * @param assignedVlan assigned vlan-id for untagged packets
510 * @param applicationId for application programming this filter
511 * @param useSetVlanExtension use the setVlanVid extension that has is_present bit set to 0.
512 * @return list of FlowRule for port-vlan filters
513 */
514 protected List<FlowRule> processVlanIdFilterInternal(PortCriterion portCriterion,
515 VlanIdCriterion vidCriterion,
516 VlanId assignedVlan,
517 ApplicationId applicationId,
518 boolean useSetVlanExtension) {
Charles Chan14967c22015-12-07 11:11:50 -0800519 List<FlowRule> rules = new ArrayList<>();
Saurav Das822c4e22015-10-23 10:51:11 -0700520 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
521 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
Charles Chan14967c22015-12-07 11:11:50 -0800522 TrafficSelector.Builder preSelector = null;
523 TrafficTreatment.Builder preTreatment = null;
524
Saurav Das4f980082015-11-05 13:39:15 -0800525 treatment.transition(TMAC_TABLE);
526
Saurav Das822c4e22015-10-23 10:51:11 -0700527 if (vidCriterion.vlanId() == VlanId.NONE) {
528 // untagged packets are assigned vlans
Charles Chanbe8aea42016-02-24 12:04:47 -0800529 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(VlanId.NONE);
Charles Chan14967c22015-12-07 11:11:50 -0800530 selector.extension(ofdpaMatchVlanVid, deviceId);
Charles Chanf9e98652016-09-07 16:54:23 -0700531 if (useSetVlanExtension) {
532 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(assignedVlan);
533 treatment.extension(ofdpaSetVlanVid, deviceId);
534 } else {
535 treatment.setVlanId(assignedVlan);
536 }
Charles Chan14967c22015-12-07 11:11:50 -0800537
538 preSelector = DefaultTrafficSelector.builder();
539 OfdpaMatchVlanVid preOfdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
540 preSelector.extension(preOfdpaMatchVlanVid, deviceId);
541 preTreatment = DefaultTrafficTreatment.builder().transition(TMAC_TABLE);
542
Saurav Das4f980082015-11-05 13:39:15 -0800543 } else {
Charles Chan14967c22015-12-07 11:11:50 -0800544 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vidCriterion.vlanId());
545 selector.extension(ofdpaMatchVlanVid, deviceId);
Charles Chand55e84d2016-03-30 17:54:24 -0700546
547 if (!assignedVlan.equals(vidCriterion.vlanId())) {
Charles Chanc03782d2017-01-31 13:57:55 -0800548 if (useSetVlanExtension) {
549 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(assignedVlan);
550 treatment.extension(ofdpaSetVlanVid, deviceId);
551 } else {
552 treatment.setVlanId(assignedVlan);
553 }
Charles Chand55e84d2016-03-30 17:54:24 -0700554 }
Saurav Das822c4e22015-10-23 10:51:11 -0700555 }
Saurav Das822c4e22015-10-23 10:51:11 -0700556
557 // ofdpa cannot match on ALL portnumber, so we need to use separate
558 // rules for each port.
Charles Chan14967c22015-12-07 11:11:50 -0800559 List<PortNumber> portnums = new ArrayList<>();
Saurav Das822c4e22015-10-23 10:51:11 -0700560 if (portCriterion.port() == PortNumber.ALL) {
561 for (Port port : deviceService.getPorts(deviceId)) {
562 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
563 portnums.add(port.number());
564 }
565 }
566 } else {
567 portnums.add(portCriterion.port());
568 }
Saurav Das4f980082015-11-05 13:39:15 -0800569
Saurav Das822c4e22015-10-23 10:51:11 -0700570 for (PortNumber pnum : portnums) {
Saurav Das4f980082015-11-05 13:39:15 -0800571 // create rest of flowrule
Saurav Das822c4e22015-10-23 10:51:11 -0700572 selector.matchInPort(pnum);
573 FlowRule rule = DefaultFlowRule.builder()
574 .forDevice(deviceId)
575 .withSelector(selector.build())
576 .withTreatment(treatment.build())
577 .withPriority(DEFAULT_PRIORITY)
578 .fromApp(applicationId)
579 .makePermanent()
580 .forTable(VLAN_TABLE).build();
Charles Chan14967c22015-12-07 11:11:50 -0800581
582 if (preSelector != null) {
583 preSelector.matchInPort(pnum);
584 FlowRule preRule = DefaultFlowRule.builder()
585 .forDevice(deviceId)
586 .withSelector(preSelector.build())
587 .withTreatment(preTreatment.build())
588 .withPriority(DEFAULT_PRIORITY)
589 .fromApp(applicationId)
590 .makePermanent()
591 .forTable(VLAN_TABLE).build();
592 rules.add(preRule);
593 }
594
Saurav Das822c4e22015-10-23 10:51:11 -0700595 rules.add(rule);
596 }
597 return rules;
598 }
599
600 /**
601 * Allows routed packets with correct destination MAC to be directed
602 * to unicast-IP routing table or MPLS forwarding table.
Saurav Das822c4e22015-10-23 10:51:11 -0700603 *
604 * @param portCriterion port on device for which this filter is programmed
605 * @param ethCriterion dstMac of device for which is filter is programmed
606 * @param vidCriterion vlan assigned to port, or NONE for untagged
Saurav Das0e99e2b2015-10-28 12:39:42 -0700607 * @param assignedVlan assigned vlan-id for untagged packets
Saurav Das822c4e22015-10-23 10:51:11 -0700608 * @param applicationId for application programming this filter
609 * @return list of FlowRule for port-vlan filters
610
611 */
612 protected List<FlowRule> processEthDstFilter(PortCriterion portCriterion,
613 EthCriterion ethCriterion,
614 VlanIdCriterion vidCriterion,
Saurav Das0e99e2b2015-10-28 12:39:42 -0700615 VlanId assignedVlan,
Saurav Das822c4e22015-10-23 10:51:11 -0700616 ApplicationId applicationId) {
Charles Chan5270ed02016-01-30 23:22:37 -0800617 // Consider PortNumber.ANY as wildcard. Match ETH_DST only
618 if (portCriterion != null && portCriterion.port() == PortNumber.ANY) {
619 return processEthDstOnlyFilter(ethCriterion, applicationId);
620 }
621
Charles Chan5b9df8d2016-03-28 22:21:40 -0700622 // Multicast MAC
623 if (ethCriterion.mask() != null) {
624 return processMcastEthDstFilter(ethCriterion, applicationId);
625 }
626
Saurav Das822c4e22015-10-23 10:51:11 -0700627 //handling untagged packets via assigned VLAN
628 if (vidCriterion.vlanId() == VlanId.NONE) {
Saurav Das0e99e2b2015-10-28 12:39:42 -0700629 vidCriterion = (VlanIdCriterion) Criteria.matchVlanId(assignedVlan);
Saurav Das822c4e22015-10-23 10:51:11 -0700630 }
631 // ofdpa cannot match on ALL portnumber, so we need to use separate
632 // rules for each port.
633 List<PortNumber> portnums = new ArrayList<PortNumber>();
634 if (portCriterion.port() == PortNumber.ALL) {
635 for (Port port : deviceService.getPorts(deviceId)) {
636 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
637 portnums.add(port.number());
638 }
639 }
640 } else {
641 portnums.add(portCriterion.port());
642 }
643
644 List<FlowRule> rules = new ArrayList<FlowRule>();
645 for (PortNumber pnum : portnums) {
Charles Chan14967c22015-12-07 11:11:50 -0800646 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vidCriterion.vlanId());
Saurav Das822c4e22015-10-23 10:51:11 -0700647 // for unicast IP packets
648 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
649 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
650 selector.matchInPort(pnum);
Charles Chan14967c22015-12-07 11:11:50 -0800651 selector.extension(ofdpaMatchVlanVid, deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700652 selector.matchEthType(Ethernet.TYPE_IPV4);
653 selector.matchEthDst(ethCriterion.mac());
654 treatment.transition(UNICAST_ROUTING_TABLE);
655 FlowRule rule = DefaultFlowRule.builder()
656 .forDevice(deviceId)
657 .withSelector(selector.build())
658 .withTreatment(treatment.build())
659 .withPriority(DEFAULT_PRIORITY)
660 .fromApp(applicationId)
661 .makePermanent()
662 .forTable(TMAC_TABLE).build();
663 rules.add(rule);
664 //for MPLS packets
665 selector = DefaultTrafficSelector.builder();
666 treatment = DefaultTrafficTreatment.builder();
667 selector.matchInPort(pnum);
Charles Chan14967c22015-12-07 11:11:50 -0800668 selector.extension(ofdpaMatchVlanVid, deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700669 selector.matchEthType(Ethernet.MPLS_UNICAST);
670 selector.matchEthDst(ethCriterion.mac());
671 treatment.transition(MPLS_TABLE_0);
672 rule = DefaultFlowRule.builder()
673 .forDevice(deviceId)
674 .withSelector(selector.build())
675 .withTreatment(treatment.build())
676 .withPriority(DEFAULT_PRIORITY)
677 .fromApp(applicationId)
678 .makePermanent()
679 .forTable(TMAC_TABLE).build();
680 rules.add(rule);
Pier Ventree0ae7a32016-11-23 09:57:42 -0800681 /*
682 * TMAC rules for IPv6 packets
683 */
684 selector = DefaultTrafficSelector.builder();
685 treatment = DefaultTrafficTreatment.builder();
686 selector.matchInPort(pnum);
687 selector.extension(ofdpaMatchVlanVid, deviceId);
688 selector.matchEthType(Ethernet.TYPE_IPV6);
689 selector.matchEthDst(ethCriterion.mac());
690 treatment.transition(UNICAST_ROUTING_TABLE);
691 rule = DefaultFlowRule.builder()
692 .forDevice(deviceId)
693 .withSelector(selector.build())
694 .withTreatment(treatment.build())
695 .withPriority(DEFAULT_PRIORITY)
696 .fromApp(applicationId)
697 .makePermanent()
698 .forTable(TMAC_TABLE).build();
699 rules.add(rule);
Saurav Das822c4e22015-10-23 10:51:11 -0700700 }
701 return rules;
702 }
703
Charles Chan5270ed02016-01-30 23:22:37 -0800704 protected List<FlowRule> processEthDstOnlyFilter(EthCriterion ethCriterion,
705 ApplicationId applicationId) {
Pier Luigi0e358632017-01-31 09:35:05 -0800706 ImmutableList.Builder<FlowRule> builder = ImmutableList.builder();
707
Charles Chan5270ed02016-01-30 23:22:37 -0800708 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
709 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
710 selector.matchEthType(Ethernet.TYPE_IPV4);
711 selector.matchEthDst(ethCriterion.mac());
712 treatment.transition(UNICAST_ROUTING_TABLE);
713 FlowRule rule = DefaultFlowRule.builder()
714 .forDevice(deviceId)
715 .withSelector(selector.build())
716 .withTreatment(treatment.build())
717 .withPriority(DEFAULT_PRIORITY)
718 .fromApp(applicationId)
719 .makePermanent()
720 .forTable(TMAC_TABLE).build();
Pier Luigi0e358632017-01-31 09:35:05 -0800721 builder.add(rule);
722
723 selector = DefaultTrafficSelector.builder();
724 treatment = DefaultTrafficTreatment.builder();
725 selector.matchEthType(Ethernet.TYPE_IPV6);
726 selector.matchEthDst(ethCriterion.mac());
727 treatment.transition(UNICAST_ROUTING_TABLE);
728 rule = DefaultFlowRule.builder()
729 .forDevice(deviceId)
730 .withSelector(selector.build())
731 .withTreatment(treatment.build())
732 .withPriority(DEFAULT_PRIORITY)
733 .fromApp(applicationId)
734 .makePermanent()
735 .forTable(TMAC_TABLE).build();
736 return builder.add(rule).build();
Charles Chan5270ed02016-01-30 23:22:37 -0800737 }
738
Charles Chan5b9df8d2016-03-28 22:21:40 -0700739 protected List<FlowRule> processMcastEthDstFilter(EthCriterion ethCriterion,
740 ApplicationId applicationId) {
741 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
742 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
743 selector.matchEthType(Ethernet.TYPE_IPV4);
744 selector.matchEthDstMasked(ethCriterion.mac(), ethCriterion.mask());
745 treatment.transition(MULTICAST_ROUTING_TABLE);
746 FlowRule rule = DefaultFlowRule.builder()
747 .forDevice(deviceId)
748 .withSelector(selector.build())
749 .withTreatment(treatment.build())
750 .withPriority(DEFAULT_PRIORITY)
751 .fromApp(applicationId)
752 .makePermanent()
753 .forTable(TMAC_TABLE).build();
754 return ImmutableList.<FlowRule>builder().add(rule).build();
755 }
756
Saurav Das822c4e22015-10-23 10:51:11 -0700757 private Collection<FlowRule> processForward(ForwardingObjective fwd) {
758 switch (fwd.flag()) {
759 case SPECIFIC:
760 return processSpecific(fwd);
761 case VERSATILE:
762 return processVersatile(fwd);
763 default:
764 fail(fwd, ObjectiveError.UNKNOWN);
765 log.warn("Unknown forwarding flag {}", fwd.flag());
766 }
767 return Collections.emptySet();
768 }
769
770 /**
771 * In the OF-DPA 2.0 pipeline, versatile forwarding objectives go to the
772 * ACL table.
773 * @param fwd the forwarding objective of type 'versatile'
774 * @return a collection of flow rules to be sent to the switch. An empty
775 * collection may be returned if there is a problem in processing
776 * the flow rule
777 */
Saurav Das52025962016-01-28 22:30:01 -0800778 protected Collection<FlowRule> processVersatile(ForwardingObjective fwd) {
Saurav Dasd2fded02016-12-02 15:43:47 -0800779 log.info("Processing versatile forwarding objective:{} in dev:{}",
780 fwd.id(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700781
782 EthTypeCriterion ethType =
Saurav Das77b5e902016-01-27 17:01:59 -0800783 (EthTypeCriterion) fwd.selector().getCriterion(Criterion.Type.ETH_TYPE);
Saurav Das822c4e22015-10-23 10:51:11 -0700784 if (ethType == null) {
Saurav Dasd2fded02016-12-02 15:43:47 -0800785 log.error("Versatile forwarding objective:{} must include ethType",
786 fwd.id());
Saurav Das822c4e22015-10-23 10:51:11 -0700787 fail(fwd, ObjectiveError.BADPARAMS);
788 return Collections.emptySet();
789 }
790 if (fwd.nextId() == null && fwd.treatment() == null) {
791 log.error("Forwarding objective {} from {} must contain "
792 + "nextId or Treatment", fwd.selector(), fwd.appId());
Zsolt Haraszti9faab752016-02-17 15:55:20 -0800793 fail(fwd, ObjectiveError.BADPARAMS);
Saurav Das822c4e22015-10-23 10:51:11 -0700794 return Collections.emptySet();
795 }
Saurav Das49cb5a12016-01-16 22:54:07 -0800796
Saurav Das77b5e902016-01-27 17:01:59 -0800797 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
798 fwd.selector().criteria().forEach(criterion -> {
799 if (criterion instanceof VlanIdCriterion) {
800 VlanId vlanId = ((VlanIdCriterion) criterion).vlanId();
801 // ensure that match does not include vlan = NONE as OF-DPA does not
802 // match untagged packets this way in the ACL table.
803 if (vlanId.equals(VlanId.NONE)) {
804 return;
805 }
806 OfdpaMatchVlanVid ofdpaMatchVlanVid =
807 new OfdpaMatchVlanVid(vlanId);
808 sbuilder.extension(ofdpaMatchVlanVid, deviceId);
Pier Ventree0ae7a32016-11-23 09:57:42 -0800809 } else if (criterion instanceof Icmpv6TypeCriterion ||
810 criterion instanceof Icmpv6CodeCriterion) {
811 /*
812 * We silenty discard these criterions, our current
813 * OFDPA platform does not support these matches on
814 * the ACL table.
815 */
816 log.warn("ICMPv6 Type and ICMPv6 Code are not supported");
Saurav Das77b5e902016-01-27 17:01:59 -0800817 } else {
818 sbuilder.add(criterion);
819 }
820 });
821
Saurav Das822c4e22015-10-23 10:51:11 -0700822 // XXX driver does not currently do type checking as per Tables 65-67 in
823 // OFDPA 2.0 spec. The only allowed treatment is a punt to the controller.
Saurav Das49cb5a12016-01-16 22:54:07 -0800824 TrafficTreatment.Builder ttBuilder = DefaultTrafficTreatment.builder();
825 if (fwd.treatment() != null) {
826 for (Instruction ins : fwd.treatment().allInstructions()) {
827 if (ins instanceof OutputInstruction) {
828 OutputInstruction o = (OutputInstruction) ins;
829 if (o.port() == PortNumber.CONTROLLER) {
830 ttBuilder.add(o);
831 } else {
832 log.warn("Only allowed treatments in versatile forwarding "
833 + "objectives are punts to the controller");
834 }
835 } else {
836 log.warn("Cannot process instruction in versatile fwd {}", ins);
837 }
Saurav Das822c4e22015-10-23 10:51:11 -0700838 }
Charles Chan2df0e8a2017-01-09 11:45:08 -0800839 if (fwd.treatment().clearedDeferred()) {
840 ttBuilder.wipeDeferred();
841 }
Saurav Das822c4e22015-10-23 10:51:11 -0700842 }
Saurav Das822c4e22015-10-23 10:51:11 -0700843 if (fwd.nextId() != null) {
Saurav Das49cb5a12016-01-16 22:54:07 -0800844 // overide case
845 NextGroup next = getGroupForNextObjective(fwd.nextId());
846 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
847 // we only need the top level group's key to point the flow to it
848 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
849 if (group == null) {
850 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
851 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
852 fail(fwd, ObjectiveError.GROUPMISSING);
853 return Collections.emptySet();
854 }
855 ttBuilder.deferred().group(group.id());
Saurav Das822c4e22015-10-23 10:51:11 -0700856 }
Saurav Das49cb5a12016-01-16 22:54:07 -0800857
858 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
859 .fromApp(fwd.appId())
860 .withPriority(fwd.priority())
861 .forDevice(deviceId)
Saurav Das77b5e902016-01-27 17:01:59 -0800862 .withSelector(sbuilder.build())
Saurav Das49cb5a12016-01-16 22:54:07 -0800863 .withTreatment(ttBuilder.build())
864 .makePermanent()
865 .forTable(ACL_TABLE);
866 return Collections.singletonList(ruleBuilder.build());
Saurav Das822c4e22015-10-23 10:51:11 -0700867 }
868
869 /**
870 * In the OF-DPA 2.0 pipeline, specific forwarding refers to the IP table
Saurav Das8a0732e2015-11-20 15:27:53 -0800871 * (unicast or multicast) or the L2 table (mac + vlan) or the MPLS table.
Saurav Das822c4e22015-10-23 10:51:11 -0700872 *
873 * @param fwd the forwarding objective of type 'specific'
874 * @return a collection of flow rules. Typically there will be only one
875 * for this type of forwarding objective. An empty set may be
876 * returned if there is an issue in processing the objective.
877 */
Saurav Das8a0732e2015-11-20 15:27:53 -0800878 protected Collection<FlowRule> processSpecific(ForwardingObjective fwd) {
Saurav Das25190812016-05-27 13:54:07 -0700879 log.debug("Processing specific fwd objective:{} in dev:{} with next:{}",
Saurav Das4ce45962015-11-24 23:21:05 -0800880 fwd.id(), deviceId, fwd.nextId());
881 boolean isEthTypeObj = isSupportedEthTypeObjective(fwd);
882 boolean isEthDstObj = isSupportedEthDstObjective(fwd);
883
884 if (isEthTypeObj) {
885 return processEthTypeSpecific(fwd);
886 } else if (isEthDstObj) {
887 return processEthDstSpecific(fwd);
888 } else {
889 log.warn("processSpecific: Unsupported forwarding objective "
890 + "criteria fwd:{} in dev:{}", fwd.nextId(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700891 fail(fwd, ObjectiveError.UNSUPPORTED);
892 return Collections.emptySet();
893 }
Saurav Das4ce45962015-11-24 23:21:05 -0800894 }
895
Saurav Das4ce45962015-11-24 23:21:05 -0800896 /**
897 * Handles forwarding rules to the IP and MPLS tables.
898 *
899 * @param fwd the forwarding objective
900 * @return A collection of flow rules, or an empty set
901 */
902 protected Collection<FlowRule> processEthTypeSpecific(ForwardingObjective fwd) {
Charles Chancad338a2016-09-16 18:03:11 -0700903 return processEthTypeSpecificInternal(fwd, false, ACL_TABLE);
Charles Chanf9e98652016-09-07 16:54:23 -0700904 }
905
906 /**
907 * Internal implementation of processEthTypeSpecific.
908 * <p>
909 * Wildcarded IPv4_DST is not supported in OFDPA i12. Therefore, we break
910 * the rule into 0.0.0.0/1 and 128.0.0.0/1.
911 * The allowDefaultRoute must be set to false for OFDPA i12.
912 * </p>
913 *
914 * @param fwd the forwarding objective
915 * @param allowDefaultRoute allow wildcarded IPv4_DST or not
Ray Milkey0bb1e102016-11-10 14:51:27 -0800916 * @param mplsNextTable next MPLS table
Charles Chanf9e98652016-09-07 16:54:23 -0700917 * @return A collection of flow rules, or an empty set
918 */
919 protected Collection<FlowRule> processEthTypeSpecificInternal(ForwardingObjective fwd,
Charles Chancad338a2016-09-16 18:03:11 -0700920 boolean allowDefaultRoute,
921 int mplsNextTable) {
Saurav Das4ce45962015-11-24 23:21:05 -0800922 TrafficSelector selector = fwd.selector();
923 EthTypeCriterion ethType =
924 (EthTypeCriterion) selector.getCriterion(Criterion.Type.ETH_TYPE);
Flavio Castroe10fa242016-01-15 12:43:51 -0800925 boolean popMpls = false;
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700926 boolean emptyGroup = false;
Charles Chan188ebf52015-12-23 00:15:11 -0800927 int forTableId;
Saurav Das8a0732e2015-11-20 15:27:53 -0800928 TrafficSelector.Builder filteredSelector = DefaultTrafficSelector.builder();
Charles Chan14967c22015-12-07 11:11:50 -0800929 TrafficTreatment.Builder tb = DefaultTrafficTreatment.builder();
Flavio Castroe10fa242016-01-15 12:43:51 -0800930 TrafficSelector.Builder complementarySelector = DefaultTrafficSelector.builder();
Charles Chan14967c22015-12-07 11:11:50 -0800931
Saurav Das8a0732e2015-11-20 15:27:53 -0800932 if (ethType.ethType().toShort() == Ethernet.TYPE_IPV4) {
Pier Luigi3bfe32c2017-01-30 09:47:36 -0800933 if (buildIpv4Selector(filteredSelector, complementarySelector, fwd, allowDefaultRoute) < 0) {
934 return Collections.emptyList();
935 }
936 // We need to set properly the next table
Flavio Castroe10fa242016-01-15 12:43:51 -0800937 IpPrefix ipv4Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV4_DST)).ip();
Charles Chan5b9df8d2016-03-28 22:21:40 -0700938 if (ipv4Dst.isMulticast()) {
Charles Chan5b9df8d2016-03-28 22:21:40 -0700939 forTableId = MULTICAST_ROUTING_TABLE;
Flavio Castroe10fa242016-01-15 12:43:51 -0800940 } else {
Charles Chan5b9df8d2016-03-28 22:21:40 -0700941 forTableId = UNICAST_ROUTING_TABLE;
Flavio Castroe10fa242016-01-15 12:43:51 -0800942 }
Charles Chan14967c22015-12-07 11:11:50 -0800943 if (fwd.treatment() != null) {
944 for (Instruction instr : fwd.treatment().allInstructions()) {
945 if (instr instanceof L3ModificationInstruction &&
946 ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
Saurav Das9c705342017-01-06 11:36:01 -0800947 // XXX decrementing IP ttl is done automatically for routing, this
948 // action is ignored or rejected in ofdpa as it is not fully implemented
949 //tb.deferred().add(instr);
Charles Chan14967c22015-12-07 11:11:50 -0800950 }
951 }
952 }
953
Pier Ventree0ae7a32016-11-23 09:57:42 -0800954 } else if (ethType.ethType().toShort() == Ethernet.TYPE_IPV6) {
955 if (buildIpv6Selector(filteredSelector, fwd) < 0) {
956 return Collections.emptyList();
957 }
958 forTableId = UNICAST_ROUTING_TABLE;
959 if (fwd.treatment() != null) {
960 for (Instruction instr : fwd.treatment().allInstructions()) {
961 if (instr instanceof L3ModificationInstruction &&
962 ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
963 // XXX decrementing IP ttl is done automatically for routing, this
964 // action is ignored or rejected in ofdpa as it is not fully implemented
965 //tb.deferred().add(instr);
966 }
967 }
968 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800969 } else {
970 filteredSelector
971 .matchEthType(Ethernet.MPLS_UNICAST)
972 .matchMplsLabel(((MplsCriterion)
973 selector.getCriterion(Criterion.Type.MPLS_LABEL)).label());
974 MplsBosCriterion bos = (MplsBosCriterion) selector
Pier Ventre140a8942016-11-02 07:26:38 -0700975 .getCriterion(MPLS_BOS);
Saurav Das8a0732e2015-11-20 15:27:53 -0800976 if (bos != null) {
977 filteredSelector.matchMplsBos(bos.mplsBos());
978 }
979 forTableId = MPLS_TABLE_1;
Saurav Das4ce45962015-11-24 23:21:05 -0800980 log.debug("processing MPLS specific forwarding objective {} -> next:{}"
981 + " in dev {}", fwd.id(), fwd.nextId(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700982
Charles Chan14967c22015-12-07 11:11:50 -0800983 if (fwd.treatment() != null) {
984 for (Instruction instr : fwd.treatment().allInstructions()) {
985 if (instr instanceof L2ModificationInstruction &&
986 ((L2ModificationInstruction) instr).subtype() == L2SubType.MPLS_POP) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800987 popMpls = true;
Pier Luigi3bfe32c2017-01-30 09:47:36 -0800988 // OF-DPA does not pop in MPLS table in some cases. For the L3 VPN, it requires
Saurav Das9c705342017-01-06 11:36:01 -0800989 // setting the MPLS_TYPE so pop can happen down the pipeline
Pier Luigi3bfe32c2017-01-30 09:47:36 -0800990 if (mplsNextTable == MPLS_TYPE_TABLE && isNotMplsBos(selector)) {
991 tb.immediate().popMpls();
992 }
Charles Chan14967c22015-12-07 11:11:50 -0800993 }
994 if (instr instanceof L3ModificationInstruction &&
995 ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
996 // FIXME Should modify the app to send the correct DEC_MPLS_TTL instruction
997 tb.immediate().decMplsTtl();
998 }
999 if (instr instanceof L3ModificationInstruction &&
1000 ((L3ModificationInstruction) instr).subtype() == L3SubType.TTL_IN) {
1001 tb.immediate().add(instr);
1002 }
Saurav Das8a0732e2015-11-20 15:27:53 -08001003 }
1004 }
1005 }
Saurav Das822c4e22015-10-23 10:51:11 -07001006
1007 if (fwd.nextId() != null) {
Saurav Das8a0732e2015-11-20 15:27:53 -08001008 if (forTableId == MPLS_TABLE_1 && !popMpls) {
1009 log.warn("SR CONTINUE case cannot be handled as MPLS ECMP "
Saurav Das25190812016-05-27 13:54:07 -07001010 + "is not implemented in OF-DPA yet. Aborting this flow {} -> next:{}"
1011 + "in this device {}", fwd.id(), fwd.nextId(), deviceId);
Pier Ventree0ae7a32016-11-23 09:57:42 -08001012 // XXX We could convert to forwarding to a single-port, via a MPLS interface,
1013 // or a MPLS SWAP (with-same) but that would have to be handled in the next-objective.
1014 // Also the pop-mpls logic used here won't work in non-BoS case.
Saurav Das4ce45962015-11-24 23:21:05 -08001015 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
Saurav Das8a0732e2015-11-20 15:27:53 -08001016 return Collections.emptySet();
1017 }
1018
Saurav Das423fe2b2015-12-04 10:52:59 -08001019 NextGroup next = getGroupForNextObjective(fwd.nextId());
1020 if (next != null) {
1021 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1022 // we only need the top level group's key to point the flow to it
1023 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
Pier Ventre140a8942016-11-02 07:26:38 -07001024 if (isNotMplsBos(selector) && group.type().equals(HASHED)) {
1025 log.warn("SR CONTINUE case cannot be handled as MPLS ECMP "
1026 + "is not implemented in OF-DPA yet. Aborting this flow {} -> next:{}"
1027 + "in this device {}", fwd.id(), fwd.nextId(), deviceId);
1028 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
1029 return Collections.emptySet();
1030 }
Saurav Das423fe2b2015-12-04 10:52:59 -08001031 if (group == null) {
1032 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
1033 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
1034 fail(fwd, ObjectiveError.GROUPMISSING);
1035 return Collections.emptySet();
1036 }
1037 tb.deferred().group(group.id());
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001038 // check if group is empty
1039 if (gkeys.size() == 1 && gkeys.get(0).size() == 1) {
1040 log.warn("Found empty group 0x{} in dev:{} .. will retry fwd:{}",
1041 Integer.toHexString(group.id().id()), deviceId, fwd.id());
1042 emptyGroup = true;
1043 }
Saurav Das25190812016-05-27 13:54:07 -07001044 } else {
1045 log.warn("Cannot find group for nextId:{} in dev:{}. Aborting fwd:{}",
1046 fwd.nextId(), deviceId, fwd.id());
1047 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
1048 return Collections.emptySet();
Saurav Das822c4e22015-10-23 10:51:11 -07001049 }
Saurav Das822c4e22015-10-23 10:51:11 -07001050 }
Charles Chancad338a2016-09-16 18:03:11 -07001051
1052 if (forTableId == MPLS_TABLE_1) {
Pier Ventre140a8942016-11-02 07:26:38 -07001053 if (mplsNextTable == MPLS_L3_TYPE_TABLE) {
Charles Chancad338a2016-09-16 18:03:11 -07001054 Ofdpa3SetMplsType setMplsType = new Ofdpa3SetMplsType(Ofdpa3MplsType.L3_PHP);
Saurav Das9c705342017-01-06 11:36:01 -08001055 // set mpls type as apply_action
1056 tb.immediate().extension(setMplsType, deviceId);
Charles Chancad338a2016-09-16 18:03:11 -07001057 }
1058 tb.transition(mplsNextTable);
1059 } else {
1060 tb.transition(ACL_TABLE);
1061 }
1062
Saurav Das822c4e22015-10-23 10:51:11 -07001063 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
1064 .fromApp(fwd.appId())
1065 .withPriority(fwd.priority())
1066 .forDevice(deviceId)
Saurav Das8a0732e2015-11-20 15:27:53 -08001067 .withSelector(filteredSelector.build())
1068 .withTreatment(tb.build())
1069 .forTable(forTableId);
Saurav Das822c4e22015-10-23 10:51:11 -07001070
1071 if (fwd.permanent()) {
1072 ruleBuilder.makePermanent();
1073 } else {
1074 ruleBuilder.makeTemporary(fwd.timeout());
1075 }
Flavio Castroe10fa242016-01-15 12:43:51 -08001076 Collection<FlowRule> flowRuleCollection = new ArrayList<>();
1077 flowRuleCollection.add(ruleBuilder.build());
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001078 if (!allowDefaultRoute) {
Pier Ventree0ae7a32016-11-23 09:57:42 -08001079 flowRuleCollection.add(
1080 defaultRoute(fwd, complementarySelector, forTableId, tb)
1081 );
Flavio Castroe10fa242016-01-15 12:43:51 -08001082 log.debug("Default rule 0.0.0.0/0 is being installed two rules");
1083 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001084 // XXX retrying flows may be necessary due to bug CORD-554
1085 if (emptyGroup) {
1086 executorService.schedule(new RetryFlows(fwd, flowRuleCollection),
1087 RETRY_MS, TimeUnit.MILLISECONDS);
1088 }
Flavio Castroe10fa242016-01-15 12:43:51 -08001089 return flowRuleCollection;
Saurav Das822c4e22015-10-23 10:51:11 -07001090 }
1091
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001092 protected int buildIpv4Selector(TrafficSelector.Builder builderToUpdate,
1093 TrafficSelector.Builder extBuilder,
1094 ForwardingObjective fwd,
1095 boolean allowDefaultRoute) {
1096 TrafficSelector selector = fwd.selector();
1097
1098 IpPrefix ipv4Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV4_DST)).ip();
1099 if (ipv4Dst.isMulticast()) {
1100 if (ipv4Dst.prefixLength() != 32) {
1101 log.warn("Multicast specific forwarding objective can only be /32");
1102 fail(fwd, ObjectiveError.BADPARAMS);
1103 return -1;
1104 }
1105 VlanId assignedVlan = readVlanFromSelector(fwd.meta());
1106 if (assignedVlan == null) {
1107 log.warn("VLAN ID required by multicast specific fwd obj is missing. Abort.");
1108 fail(fwd, ObjectiveError.BADPARAMS);
1109 return -1;
1110 }
1111 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
1112 builderToUpdate.extension(ofdpaMatchVlanVid, deviceId);
1113 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipv4Dst);
1114 log.debug("processing IPv4 multicast specific forwarding objective {} -> next:{}"
1115 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
1116 } else {
1117 if (ipv4Dst.prefixLength() == 0) {
1118 if (allowDefaultRoute) {
1119 // The entire IPV4_DST field is wildcarded intentionally
1120 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4);
1121 } else {
1122 // NOTE: The switch does not support matching 0.0.0.0/0
1123 // Split it into 0.0.0.0/1 and 128.0.0.0/1
1124 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4)
1125 .matchIPDst(IpPrefix.valueOf("0.0.0.0/1"));
1126 extBuilder.matchEthType(Ethernet.TYPE_IPV4)
1127 .matchIPDst(IpPrefix.valueOf("128.0.0.0/1"));
1128 }
1129 } else {
1130 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipv4Dst);
1131 }
1132 log.debug("processing IPv4 unicast specific forwarding objective {} -> next:{}"
1133 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
1134 }
1135 return 0;
1136 }
1137
1138 /**
1139 * Helper method to build Ipv6 selector using the selector provided by
1140 * a forwarding objective.
1141 *
1142 * @param builderToUpdate the builder to update
1143 * @param fwd the selector to read
1144 * @return 0 if the update ends correctly. -1 if the matches
1145 * are not yet supported
1146 */
1147 protected int buildIpv6Selector(TrafficSelector.Builder builderToUpdate,
1148 ForwardingObjective fwd) {
1149
1150 TrafficSelector selector = fwd.selector();
1151
1152 IpPrefix ipv6Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV6_DST)).ip();
1153 if (ipv6Dst.isMulticast()) {
1154 log.warn("IPv6 Multicast is currently not supported");
1155 fail(fwd, ObjectiveError.BADPARAMS);
1156 return -1;
1157 }
1158 if (ipv6Dst.prefixLength() != 0) {
1159 builderToUpdate.matchIPv6Dst(ipv6Dst);
1160 }
1161 builderToUpdate.matchEthType(Ethernet.TYPE_IPV6);
1162 log.debug("processing IPv6 unicast specific forwarding objective {} -> next:{}"
1163 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
1164 return 0;
1165 }
1166
Pier Ventree0ae7a32016-11-23 09:57:42 -08001167 protected FlowRule defaultRoute(ForwardingObjective fwd,
1168 TrafficSelector.Builder complementarySelector,
1169 int forTableId,
1170 TrafficTreatment.Builder tb) {
1171 FlowRule.Builder rule = DefaultFlowRule.builder()
1172 .fromApp(fwd.appId())
1173 .withPriority(fwd.priority())
1174 .forDevice(deviceId)
1175 .withSelector(complementarySelector.build())
1176 .withTreatment(tb.build())
1177 .forTable(forTableId);
1178 if (fwd.permanent()) {
1179 rule.makePermanent();
1180 } else {
1181 rule.makeTemporary(fwd.timeout());
1182 }
1183 return rule.build();
1184 }
1185
Saurav Das4ce45962015-11-24 23:21:05 -08001186 /**
1187 * Handles forwarding rules to the L2 bridging table. Flow actions are not
1188 * allowed in the bridging table - instead we use L2 Interface group or
1189 * L2 flood group
1190 *
1191 * @param fwd the forwarding objective
1192 * @return A collection of flow rules, or an empty set
1193 */
1194 protected Collection<FlowRule> processEthDstSpecific(ForwardingObjective fwd) {
1195 List<FlowRule> rules = new ArrayList<>();
1196
1197 // Build filtered selector
1198 TrafficSelector selector = fwd.selector();
1199 EthCriterion ethCriterion = (EthCriterion) selector
1200 .getCriterion(Criterion.Type.ETH_DST);
1201 VlanIdCriterion vlanIdCriterion = (VlanIdCriterion) selector
1202 .getCriterion(Criterion.Type.VLAN_VID);
1203
1204 if (vlanIdCriterion == null) {
1205 log.warn("Forwarding objective for bridging requires vlan. Not "
1206 + "installing fwd:{} in dev:{}", fwd.id(), deviceId);
1207 fail(fwd, ObjectiveError.BADPARAMS);
1208 return Collections.emptySet();
1209 }
1210
1211 TrafficSelector.Builder filteredSelectorBuilder =
1212 DefaultTrafficSelector.builder();
Yi Tseng3a77b4f2017-02-06 15:02:17 -08001213
1214 if (!ethCriterion.mac().equals(NONE) &&
1215 !ethCriterion.mac().equals(BROADCAST)) {
Saurav Das4ce45962015-11-24 23:21:05 -08001216 filteredSelectorBuilder.matchEthDst(ethCriterion.mac());
1217 log.debug("processing L2 forwarding objective:{} -> next:{} in dev:{}",
1218 fwd.id(), fwd.nextId(), deviceId);
1219 } else {
Yi Tseng3a77b4f2017-02-06 15:02:17 -08001220 // Use wildcard DST_MAC if the MacAddress is None or Broadcast
Saurav Das4ce45962015-11-24 23:21:05 -08001221 log.debug("processing L2 Broadcast forwarding objective:{} -> next:{} "
1222 + "in dev:{} for vlan:{}",
1223 fwd.id(), fwd.nextId(), deviceId, vlanIdCriterion.vlanId());
1224 }
Charles Chan14967c22015-12-07 11:11:50 -08001225 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vlanIdCriterion.vlanId());
1226 filteredSelectorBuilder.extension(ofdpaMatchVlanVid, deviceId);
Saurav Das4ce45962015-11-24 23:21:05 -08001227 TrafficSelector filteredSelector = filteredSelectorBuilder.build();
1228
1229 if (fwd.treatment() != null) {
1230 log.warn("Ignoring traffic treatment in fwd rule {} meant for L2 table"
1231 + "for dev:{}. Expecting only nextId", fwd.id(), deviceId);
1232 }
1233
1234 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
1235 if (fwd.nextId() != null) {
Saurav Das423fe2b2015-12-04 10:52:59 -08001236 NextGroup next = getGroupForNextObjective(fwd.nextId());
Saurav Das4ce45962015-11-24 23:21:05 -08001237 if (next != null) {
1238 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1239 // we only need the top level group's key to point the flow to it
1240 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
1241 if (group != null) {
1242 treatmentBuilder.deferred().group(group.id());
1243 } else {
1244 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
1245 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
1246 fail(fwd, ObjectiveError.GROUPMISSING);
1247 return Collections.emptySet();
1248 }
1249 }
1250 }
1251 treatmentBuilder.immediate().transition(ACL_TABLE);
1252 TrafficTreatment filteredTreatment = treatmentBuilder.build();
1253
1254 // Build bridging table entries
1255 FlowRule.Builder flowRuleBuilder = DefaultFlowRule.builder();
1256 flowRuleBuilder.fromApp(fwd.appId())
1257 .withPriority(fwd.priority())
1258 .forDevice(deviceId)
1259 .withSelector(filteredSelector)
1260 .withTreatment(filteredTreatment)
1261 .forTable(BRIDGING_TABLE);
1262 if (fwd.permanent()) {
1263 flowRuleBuilder.makePermanent();
1264 } else {
1265 flowRuleBuilder.makeTemporary(fwd.timeout());
1266 }
1267 rules.add(flowRuleBuilder.build());
1268 return rules;
1269 }
1270
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001271 //////////////////////////////////////
1272 // Helper Methods and Classes
1273 //////////////////////////////////////
1274
1275 private boolean isSupportedEthTypeObjective(ForwardingObjective fwd) {
1276 TrafficSelector selector = fwd.selector();
1277 EthTypeCriterion ethType = (EthTypeCriterion) selector
1278 .getCriterion(Criterion.Type.ETH_TYPE);
1279 return !((ethType == null) ||
1280 ((ethType.ethType().toShort() != Ethernet.TYPE_IPV4) &&
Pier Ventree0ae7a32016-11-23 09:57:42 -08001281 (ethType.ethType().toShort() != Ethernet.MPLS_UNICAST)) &&
1282 (ethType.ethType().toShort() != Ethernet.TYPE_IPV6));
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001283 }
1284
1285 private boolean isSupportedEthDstObjective(ForwardingObjective fwd) {
1286 TrafficSelector selector = fwd.selector();
1287 EthCriterion ethDst = (EthCriterion) selector
1288 .getCriterion(Criterion.Type.ETH_DST);
1289 VlanIdCriterion vlanId = (VlanIdCriterion) selector
1290 .getCriterion(Criterion.Type.VLAN_VID);
1291 return !(ethDst == null && vlanId == null);
1292 }
1293
Saurav Das423fe2b2015-12-04 10:52:59 -08001294 protected NextGroup getGroupForNextObjective(Integer nextId) {
1295 NextGroup next = flowObjectiveStore.getNextGroup(nextId);
1296 if (next != null) {
1297 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1298 if (gkeys != null && !gkeys.isEmpty()) {
1299 return next;
1300 } else {
1301 log.warn("Empty next group found in FlowObjective store for "
1302 + "next-id:{} in dev:{}", nextId, deviceId);
1303 }
1304 } else {
1305 log.warn("next-id {} not found in Flow objective store for dev:{}",
1306 nextId, deviceId);
1307 }
1308 return null;
1309 }
1310
Charles Chan188ebf52015-12-23 00:15:11 -08001311 protected static void pass(Objective obj) {
Sho SHIMIZUef7e2902016-02-12 18:38:29 -08001312 obj.context().ifPresent(context -> context.onSuccess(obj));
Saurav Das822c4e22015-10-23 10:51:11 -07001313 }
1314
Charles Chan188ebf52015-12-23 00:15:11 -08001315 protected static void fail(Objective obj, ObjectiveError error) {
Sho SHIMIZUef7e2902016-02-12 18:38:29 -08001316 obj.context().ifPresent(context -> context.onError(obj, error));
Saurav Das822c4e22015-10-23 10:51:11 -07001317 }
Saurav Das24431192016-03-07 19:13:00 -08001318
Saurav Das24431192016-03-07 19:13:00 -08001319 @Override
1320 public List<String> getNextMappings(NextGroup nextGroup) {
1321 List<String> mappings = new ArrayList<>();
1322 List<Deque<GroupKey>> gkeys = appKryo.deserialize(nextGroup.data());
1323 for (Deque<GroupKey> gkd : gkeys) {
1324 Group lastGroup = null;
Saurav Das8be4e3a2016-03-11 17:19:07 -08001325 StringBuffer gchain = new StringBuffer();
Saurav Das24431192016-03-07 19:13:00 -08001326 for (GroupKey gk : gkd) {
1327 Group g = groupService.getGroup(deviceId, gk);
Saurav Das8be4e3a2016-03-11 17:19:07 -08001328 if (g == null) {
Saurav Das25190812016-05-27 13:54:07 -07001329 gchain.append(" NoGrp").append(" -->");
Saurav Das8be4e3a2016-03-11 17:19:07 -08001330 continue;
1331 }
1332 gchain.append(" 0x").append(Integer.toHexString(g.id().id()))
1333 .append(" -->");
Saurav Das24431192016-03-07 19:13:00 -08001334 lastGroup = g;
1335 }
1336 // add port information for last group in group-chain
Saurav Das25190812016-05-27 13:54:07 -07001337 List<Instruction> lastGroupIns = new ArrayList<Instruction>();
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001338 if (lastGroup != null && !lastGroup.buckets().buckets().isEmpty()) {
Saurav Das25190812016-05-27 13:54:07 -07001339 lastGroupIns = lastGroup.buckets().buckets().get(0)
1340 .treatment().allInstructions();
1341 }
1342 for (Instruction i: lastGroupIns) {
Saurav Das24431192016-03-07 19:13:00 -08001343 if (i instanceof OutputInstruction) {
Saurav Das8be4e3a2016-03-11 17:19:07 -08001344 gchain.append(" port:").append(((OutputInstruction) i).port());
Saurav Das24431192016-03-07 19:13:00 -08001345 }
1346 }
Saurav Das8be4e3a2016-03-11 17:19:07 -08001347 mappings.add(gchain.toString());
Saurav Das24431192016-03-07 19:13:00 -08001348 }
1349 return mappings;
1350 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001351
Pier Ventre140a8942016-11-02 07:26:38 -07001352 static boolean isMplsBos(TrafficSelector selector) {
1353 MplsBosCriterion bosCriterion = (MplsBosCriterion) selector.getCriterion(MPLS_BOS);
1354 return bosCriterion != null && bosCriterion.mplsBos();
1355 }
1356
1357 static boolean isNotMplsBos(TrafficSelector selector) {
1358 MplsBosCriterion bosCriterion = (MplsBosCriterion) selector.getCriterion(MPLS_BOS);
1359 return bosCriterion != null && !bosCriterion.mplsBos();
1360 }
1361
Charles Chan5b9df8d2016-03-28 22:21:40 -07001362 protected static VlanId readVlanFromSelector(TrafficSelector selector) {
Saurav Das59232cf2016-04-27 18:35:50 -07001363 if (selector == null) {
1364 return null;
1365 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001366 Criterion criterion = selector.getCriterion(Criterion.Type.VLAN_VID);
1367 return (criterion == null)
1368 ? null : ((VlanIdCriterion) criterion).vlanId();
1369 }
1370
1371 protected static IpPrefix readIpDstFromSelector(TrafficSelector selector) {
Saurav Das59232cf2016-04-27 18:35:50 -07001372 if (selector == null) {
1373 return null;
1374 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001375 Criterion criterion = selector.getCriterion(Criterion.Type.IPV4_DST);
1376 return (criterion == null) ? null : ((IPCriterion) criterion).ip();
1377 }
Charles Chand55e84d2016-03-30 17:54:24 -07001378
1379 private static VlanId readVlanFromTreatment(TrafficTreatment treatment) {
Saurav Das59232cf2016-04-27 18:35:50 -07001380 if (treatment == null) {
1381 return null;
1382 }
Charles Chand55e84d2016-03-30 17:54:24 -07001383 for (Instruction i : treatment.allInstructions()) {
1384 if (i instanceof ModVlanIdInstruction) {
1385 return ((ModVlanIdInstruction) i).vlanId();
1386 }
1387 }
1388 return null;
1389 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001390
1391 /**
1392 * Utility class that retries sending flows a fixed number of times, even if
1393 * some of the attempts are successful. Used only for forwarding objectives.
1394 */
1395 protected final class RetryFlows implements Runnable {
1396 int attempts = MAX_RETRY_ATTEMPTS;
1397 private Collection<FlowRule> retryFlows;
1398 private ForwardingObjective fwd;
1399
1400 RetryFlows(ForwardingObjective fwd, Collection<FlowRule> retryFlows) {
1401 this.fwd = fwd;
1402 this.retryFlows = retryFlows;
1403 }
1404
1405 @Override
1406 public void run() {
1407 log.info("RETRY FLOWS ATTEMPT# {} for fwd:{} rules:{}",
1408 MAX_RETRY_ATTEMPTS - attempts, fwd.id(), retryFlows.size());
1409 sendForward(fwd, retryFlows);
1410 if (--attempts > 0) {
1411 executorService.schedule(this, RETRY_MS, TimeUnit.MILLISECONDS);
1412 }
1413 }
1414 }
1415
Saurav Das822c4e22015-10-23 10:51:11 -07001416}