blob: fd677c6a82f6d08bd4beb3b79263aa35be30bcdb [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 Das018605f2017-02-18 14:05:44 -0800390 log.debug("{} MAC filtering rules in TMAC table: {} for dev: {}",
391 (install) ? "adding" : "removing", tmacRule, deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700392 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 Das018605f2017-02-18 14:05:44 -0800423 log.debug("{} VLAN filtering rule in VLAN table: {} for dev: {}",
424 (install) ? "adding" : "removing", filteringRule, deviceId);
Charles Chan14967c22015-12-07 11:11:50 -0800425 ops = install ? ops.add(filteringRule) : ops.remove(filteringRule);
426 }
427
428 ops.newStage();
429
430 for (FlowRule assignmentRule : assignmentRules) {
Saurav Das018605f2017-02-18 14:05:44 -0800431 log.debug("{} VLAN assignment rule in VLAN table: {} for dev: {}",
432 (install) ? "adding" : "removing", assignmentRule, deviceId);
Charles Chan14967c22015-12-07 11:11:50 -0800433 ops = install ? ops.add(assignmentRule) : ops.remove(assignmentRule);
Saurav Das822c4e22015-10-23 10:51:11 -0700434 }
435 }
436
Saurav Das822c4e22015-10-23 10:51:11 -0700437 // apply filtering flow rules
438 flowRuleService.apply(ops.build(new FlowRuleOperationsContext() {
439 @Override
440 public void onSuccess(FlowRuleOperations ops) {
Saurav Das018605f2017-02-18 14:05:44 -0800441 log.debug("Applied {} filtering rules in device {}",
Saurav Das822c4e22015-10-23 10:51:11 -0700442 ops.stages().get(0).size(), deviceId);
443 pass(filt);
444 }
445
446 @Override
447 public void onError(FlowRuleOperations ops) {
448 log.info("Failed to apply all filtering rules in dev {}", deviceId);
449 fail(filt, ObjectiveError.FLOWINSTALLATIONFAILED);
450 }
451 }));
452
453 }
454
455 /**
456 * Allows untagged packets into pipeline by assigning a vlan id.
Saurav Das0e99e2b2015-10-28 12:39:42 -0700457 * Vlan assignment is done by the application.
Saurav Das822c4e22015-10-23 10:51:11 -0700458 * Allows tagged packets into pipeline as per configured port-vlan info.
Saurav Das0e99e2b2015-10-28 12:39:42 -0700459 *
Charles Chanf9e98652016-09-07 16:54:23 -0700460 * @param portCriterion port on device for which this filter is programmed
461 * @param vidCriterion vlan assigned to port, or NONE for untagged
462 * @param assignedVlan assigned vlan-id for untagged packets
463 * @param applicationId for application programming this filter
Saurav Das822c4e22015-10-23 10:51:11 -0700464 * @return list of FlowRule for port-vlan filters
465 */
466 protected List<FlowRule> processVlanIdFilter(PortCriterion portCriterion,
467 VlanIdCriterion vidCriterion,
Saurav Das0e99e2b2015-10-28 12:39:42 -0700468 VlanId assignedVlan,
Saurav Das822c4e22015-10-23 10:51:11 -0700469 ApplicationId applicationId) {
Charles Chanf9e98652016-09-07 16:54:23 -0700470 return processVlanIdFilterInternal(portCriterion, vidCriterion, assignedVlan,
471 applicationId, true);
472 }
473
474 /**
475 * Internal implementation of processVlanIdFilter.
476 * <p>
477 * The is_present bit in set_vlan_vid action is required to be 0 in OFDPA i12.
478 * Since it is non-OF spec, we need an extension treatment for that.
479 * The useSetVlanExtension must be set to false for OFDPA i12.
480 * </p>
481 *
482 * @param portCriterion port on device for which this filter is programmed
483 * @param vidCriterion vlan assigned to port, or NONE for untagged
484 * @param assignedVlan assigned vlan-id for untagged packets
485 * @param applicationId for application programming this filter
486 * @param useSetVlanExtension use the setVlanVid extension that has is_present bit set to 0.
487 * @return list of FlowRule for port-vlan filters
488 */
489 protected List<FlowRule> processVlanIdFilterInternal(PortCriterion portCriterion,
490 VlanIdCriterion vidCriterion,
491 VlanId assignedVlan,
492 ApplicationId applicationId,
493 boolean useSetVlanExtension) {
Charles Chan14967c22015-12-07 11:11:50 -0800494 List<FlowRule> rules = new ArrayList<>();
Saurav Das822c4e22015-10-23 10:51:11 -0700495 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
496 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
Charles Chan14967c22015-12-07 11:11:50 -0800497 TrafficSelector.Builder preSelector = null;
498 TrafficTreatment.Builder preTreatment = null;
499
Saurav Das4f980082015-11-05 13:39:15 -0800500 treatment.transition(TMAC_TABLE);
501
Saurav Das822c4e22015-10-23 10:51:11 -0700502 if (vidCriterion.vlanId() == VlanId.NONE) {
503 // untagged packets are assigned vlans
Charles Chanbe8aea42016-02-24 12:04:47 -0800504 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(VlanId.NONE);
Charles Chan14967c22015-12-07 11:11:50 -0800505 selector.extension(ofdpaMatchVlanVid, deviceId);
Charles Chanf9e98652016-09-07 16:54:23 -0700506 if (useSetVlanExtension) {
507 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(assignedVlan);
508 treatment.extension(ofdpaSetVlanVid, deviceId);
509 } else {
510 treatment.setVlanId(assignedVlan);
511 }
Charles Chan14967c22015-12-07 11:11:50 -0800512
513 preSelector = DefaultTrafficSelector.builder();
514 OfdpaMatchVlanVid preOfdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
515 preSelector.extension(preOfdpaMatchVlanVid, deviceId);
516 preTreatment = DefaultTrafficTreatment.builder().transition(TMAC_TABLE);
517
Saurav Das4f980082015-11-05 13:39:15 -0800518 } else {
Charles Chan14967c22015-12-07 11:11:50 -0800519 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vidCriterion.vlanId());
520 selector.extension(ofdpaMatchVlanVid, deviceId);
Charles Chand55e84d2016-03-30 17:54:24 -0700521
522 if (!assignedVlan.equals(vidCriterion.vlanId())) {
Charles Chanc03782d2017-01-31 13:57:55 -0800523 if (useSetVlanExtension) {
524 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(assignedVlan);
525 treatment.extension(ofdpaSetVlanVid, deviceId);
526 } else {
527 treatment.setVlanId(assignedVlan);
528 }
Charles Chand55e84d2016-03-30 17:54:24 -0700529 }
Saurav Das822c4e22015-10-23 10:51:11 -0700530 }
Saurav Das822c4e22015-10-23 10:51:11 -0700531
532 // ofdpa cannot match on ALL portnumber, so we need to use separate
533 // rules for each port.
Charles Chan14967c22015-12-07 11:11:50 -0800534 List<PortNumber> portnums = new ArrayList<>();
Saurav Das822c4e22015-10-23 10:51:11 -0700535 if (portCriterion.port() == PortNumber.ALL) {
536 for (Port port : deviceService.getPorts(deviceId)) {
537 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
538 portnums.add(port.number());
539 }
540 }
541 } else {
542 portnums.add(portCriterion.port());
543 }
Saurav Das4f980082015-11-05 13:39:15 -0800544
Saurav Das822c4e22015-10-23 10:51:11 -0700545 for (PortNumber pnum : portnums) {
Saurav Das4f980082015-11-05 13:39:15 -0800546 // create rest of flowrule
Saurav Das822c4e22015-10-23 10:51:11 -0700547 selector.matchInPort(pnum);
548 FlowRule rule = DefaultFlowRule.builder()
549 .forDevice(deviceId)
550 .withSelector(selector.build())
551 .withTreatment(treatment.build())
552 .withPriority(DEFAULT_PRIORITY)
553 .fromApp(applicationId)
554 .makePermanent()
555 .forTable(VLAN_TABLE).build();
Charles Chan14967c22015-12-07 11:11:50 -0800556
557 if (preSelector != null) {
558 preSelector.matchInPort(pnum);
559 FlowRule preRule = DefaultFlowRule.builder()
560 .forDevice(deviceId)
561 .withSelector(preSelector.build())
562 .withTreatment(preTreatment.build())
563 .withPriority(DEFAULT_PRIORITY)
564 .fromApp(applicationId)
565 .makePermanent()
566 .forTable(VLAN_TABLE).build();
567 rules.add(preRule);
568 }
569
Saurav Das822c4e22015-10-23 10:51:11 -0700570 rules.add(rule);
571 }
572 return rules;
573 }
574
575 /**
576 * Allows routed packets with correct destination MAC to be directed
577 * to unicast-IP routing table or MPLS forwarding table.
Saurav Das822c4e22015-10-23 10:51:11 -0700578 *
579 * @param portCriterion port on device for which this filter is programmed
580 * @param ethCriterion dstMac of device for which is filter is programmed
581 * @param vidCriterion vlan assigned to port, or NONE for untagged
Saurav Das0e99e2b2015-10-28 12:39:42 -0700582 * @param assignedVlan assigned vlan-id for untagged packets
Saurav Das822c4e22015-10-23 10:51:11 -0700583 * @param applicationId for application programming this filter
584 * @return list of FlowRule for port-vlan filters
585
586 */
587 protected List<FlowRule> processEthDstFilter(PortCriterion portCriterion,
588 EthCriterion ethCriterion,
589 VlanIdCriterion vidCriterion,
Saurav Das0e99e2b2015-10-28 12:39:42 -0700590 VlanId assignedVlan,
Saurav Das822c4e22015-10-23 10:51:11 -0700591 ApplicationId applicationId) {
Charles Chan5270ed02016-01-30 23:22:37 -0800592 // Consider PortNumber.ANY as wildcard. Match ETH_DST only
593 if (portCriterion != null && portCriterion.port() == PortNumber.ANY) {
594 return processEthDstOnlyFilter(ethCriterion, applicationId);
595 }
596
Charles Chan5b9df8d2016-03-28 22:21:40 -0700597 // Multicast MAC
598 if (ethCriterion.mask() != null) {
599 return processMcastEthDstFilter(ethCriterion, applicationId);
600 }
601
Saurav Das822c4e22015-10-23 10:51:11 -0700602 //handling untagged packets via assigned VLAN
603 if (vidCriterion.vlanId() == VlanId.NONE) {
Saurav Das0e99e2b2015-10-28 12:39:42 -0700604 vidCriterion = (VlanIdCriterion) Criteria.matchVlanId(assignedVlan);
Saurav Das822c4e22015-10-23 10:51:11 -0700605 }
606 // ofdpa cannot match on ALL portnumber, so we need to use separate
607 // rules for each port.
608 List<PortNumber> portnums = new ArrayList<PortNumber>();
609 if (portCriterion.port() == PortNumber.ALL) {
610 for (Port port : deviceService.getPorts(deviceId)) {
611 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
612 portnums.add(port.number());
613 }
614 }
615 } else {
616 portnums.add(portCriterion.port());
617 }
618
619 List<FlowRule> rules = new ArrayList<FlowRule>();
620 for (PortNumber pnum : portnums) {
Charles Chan14967c22015-12-07 11:11:50 -0800621 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vidCriterion.vlanId());
Saurav Das822c4e22015-10-23 10:51:11 -0700622 // for unicast IP packets
623 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
624 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
625 selector.matchInPort(pnum);
Charles Chan14967c22015-12-07 11:11:50 -0800626 selector.extension(ofdpaMatchVlanVid, deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700627 selector.matchEthType(Ethernet.TYPE_IPV4);
628 selector.matchEthDst(ethCriterion.mac());
629 treatment.transition(UNICAST_ROUTING_TABLE);
630 FlowRule rule = DefaultFlowRule.builder()
631 .forDevice(deviceId)
632 .withSelector(selector.build())
633 .withTreatment(treatment.build())
634 .withPriority(DEFAULT_PRIORITY)
635 .fromApp(applicationId)
636 .makePermanent()
637 .forTable(TMAC_TABLE).build();
638 rules.add(rule);
639 //for MPLS packets
640 selector = DefaultTrafficSelector.builder();
641 treatment = DefaultTrafficTreatment.builder();
642 selector.matchInPort(pnum);
Charles Chan14967c22015-12-07 11:11:50 -0800643 selector.extension(ofdpaMatchVlanVid, deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700644 selector.matchEthType(Ethernet.MPLS_UNICAST);
645 selector.matchEthDst(ethCriterion.mac());
646 treatment.transition(MPLS_TABLE_0);
647 rule = DefaultFlowRule.builder()
648 .forDevice(deviceId)
649 .withSelector(selector.build())
650 .withTreatment(treatment.build())
651 .withPriority(DEFAULT_PRIORITY)
652 .fromApp(applicationId)
653 .makePermanent()
654 .forTable(TMAC_TABLE).build();
655 rules.add(rule);
Pier Ventree0ae7a32016-11-23 09:57:42 -0800656 /*
657 * TMAC rules for IPv6 packets
658 */
659 selector = DefaultTrafficSelector.builder();
660 treatment = DefaultTrafficTreatment.builder();
661 selector.matchInPort(pnum);
662 selector.extension(ofdpaMatchVlanVid, deviceId);
663 selector.matchEthType(Ethernet.TYPE_IPV6);
664 selector.matchEthDst(ethCriterion.mac());
665 treatment.transition(UNICAST_ROUTING_TABLE);
666 rule = DefaultFlowRule.builder()
667 .forDevice(deviceId)
668 .withSelector(selector.build())
669 .withTreatment(treatment.build())
670 .withPriority(DEFAULT_PRIORITY)
671 .fromApp(applicationId)
672 .makePermanent()
673 .forTable(TMAC_TABLE).build();
674 rules.add(rule);
Saurav Das822c4e22015-10-23 10:51:11 -0700675 }
676 return rules;
677 }
678
Charles Chan5270ed02016-01-30 23:22:37 -0800679 protected List<FlowRule> processEthDstOnlyFilter(EthCriterion ethCriterion,
680 ApplicationId applicationId) {
Pier Luigi0e358632017-01-31 09:35:05 -0800681 ImmutableList.Builder<FlowRule> builder = ImmutableList.builder();
682
Charles Chan5270ed02016-01-30 23:22:37 -0800683 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
684 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
685 selector.matchEthType(Ethernet.TYPE_IPV4);
686 selector.matchEthDst(ethCriterion.mac());
687 treatment.transition(UNICAST_ROUTING_TABLE);
688 FlowRule rule = DefaultFlowRule.builder()
689 .forDevice(deviceId)
690 .withSelector(selector.build())
691 .withTreatment(treatment.build())
692 .withPriority(DEFAULT_PRIORITY)
693 .fromApp(applicationId)
694 .makePermanent()
695 .forTable(TMAC_TABLE).build();
Pier Luigi0e358632017-01-31 09:35:05 -0800696 builder.add(rule);
697
698 selector = DefaultTrafficSelector.builder();
699 treatment = DefaultTrafficTreatment.builder();
700 selector.matchEthType(Ethernet.TYPE_IPV6);
701 selector.matchEthDst(ethCriterion.mac());
702 treatment.transition(UNICAST_ROUTING_TABLE);
703 rule = DefaultFlowRule.builder()
704 .forDevice(deviceId)
705 .withSelector(selector.build())
706 .withTreatment(treatment.build())
707 .withPriority(DEFAULT_PRIORITY)
708 .fromApp(applicationId)
709 .makePermanent()
710 .forTable(TMAC_TABLE).build();
711 return builder.add(rule).build();
Charles Chan5270ed02016-01-30 23:22:37 -0800712 }
713
Charles Chan5b9df8d2016-03-28 22:21:40 -0700714 protected List<FlowRule> processMcastEthDstFilter(EthCriterion ethCriterion,
715 ApplicationId applicationId) {
716 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
717 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
718 selector.matchEthType(Ethernet.TYPE_IPV4);
719 selector.matchEthDstMasked(ethCriterion.mac(), ethCriterion.mask());
720 treatment.transition(MULTICAST_ROUTING_TABLE);
721 FlowRule rule = DefaultFlowRule.builder()
722 .forDevice(deviceId)
723 .withSelector(selector.build())
724 .withTreatment(treatment.build())
725 .withPriority(DEFAULT_PRIORITY)
726 .fromApp(applicationId)
727 .makePermanent()
728 .forTable(TMAC_TABLE).build();
729 return ImmutableList.<FlowRule>builder().add(rule).build();
730 }
731
Saurav Das822c4e22015-10-23 10:51:11 -0700732 private Collection<FlowRule> processForward(ForwardingObjective fwd) {
733 switch (fwd.flag()) {
734 case SPECIFIC:
735 return processSpecific(fwd);
736 case VERSATILE:
737 return processVersatile(fwd);
738 default:
739 fail(fwd, ObjectiveError.UNKNOWN);
740 log.warn("Unknown forwarding flag {}", fwd.flag());
741 }
742 return Collections.emptySet();
743 }
744
745 /**
746 * In the OF-DPA 2.0 pipeline, versatile forwarding objectives go to the
747 * ACL table.
748 * @param fwd the forwarding objective of type 'versatile'
749 * @return a collection of flow rules to be sent to the switch. An empty
750 * collection may be returned if there is a problem in processing
751 * the flow rule
752 */
Saurav Das52025962016-01-28 22:30:01 -0800753 protected Collection<FlowRule> processVersatile(ForwardingObjective fwd) {
Saurav Das018605f2017-02-18 14:05:44 -0800754 log.debug("Processing versatile forwarding objective:{} in dev:{}",
Saurav Dasd2fded02016-12-02 15:43:47 -0800755 fwd.id(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700756
757 EthTypeCriterion ethType =
Saurav Das77b5e902016-01-27 17:01:59 -0800758 (EthTypeCriterion) fwd.selector().getCriterion(Criterion.Type.ETH_TYPE);
Saurav Das822c4e22015-10-23 10:51:11 -0700759 if (ethType == null) {
Saurav Dasd2fded02016-12-02 15:43:47 -0800760 log.error("Versatile forwarding objective:{} must include ethType",
761 fwd.id());
Saurav Das822c4e22015-10-23 10:51:11 -0700762 fail(fwd, ObjectiveError.BADPARAMS);
763 return Collections.emptySet();
764 }
765 if (fwd.nextId() == null && fwd.treatment() == null) {
766 log.error("Forwarding objective {} from {} must contain "
767 + "nextId or Treatment", fwd.selector(), fwd.appId());
Zsolt Haraszti9faab752016-02-17 15:55:20 -0800768 fail(fwd, ObjectiveError.BADPARAMS);
Saurav Das822c4e22015-10-23 10:51:11 -0700769 return Collections.emptySet();
770 }
Saurav Das49cb5a12016-01-16 22:54:07 -0800771
Saurav Das77b5e902016-01-27 17:01:59 -0800772 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
773 fwd.selector().criteria().forEach(criterion -> {
774 if (criterion instanceof VlanIdCriterion) {
775 VlanId vlanId = ((VlanIdCriterion) criterion).vlanId();
776 // ensure that match does not include vlan = NONE as OF-DPA does not
777 // match untagged packets this way in the ACL table.
778 if (vlanId.equals(VlanId.NONE)) {
779 return;
780 }
781 OfdpaMatchVlanVid ofdpaMatchVlanVid =
782 new OfdpaMatchVlanVid(vlanId);
783 sbuilder.extension(ofdpaMatchVlanVid, deviceId);
Pier Ventree0ae7a32016-11-23 09:57:42 -0800784 } else if (criterion instanceof Icmpv6TypeCriterion ||
785 criterion instanceof Icmpv6CodeCriterion) {
786 /*
787 * We silenty discard these criterions, our current
788 * OFDPA platform does not support these matches on
789 * the ACL table.
790 */
791 log.warn("ICMPv6 Type and ICMPv6 Code are not supported");
Saurav Das77b5e902016-01-27 17:01:59 -0800792 } else {
793 sbuilder.add(criterion);
794 }
795 });
796
Saurav Das822c4e22015-10-23 10:51:11 -0700797 // XXX driver does not currently do type checking as per Tables 65-67 in
798 // OFDPA 2.0 spec. The only allowed treatment is a punt to the controller.
Saurav Das49cb5a12016-01-16 22:54:07 -0800799 TrafficTreatment.Builder ttBuilder = DefaultTrafficTreatment.builder();
800 if (fwd.treatment() != null) {
801 for (Instruction ins : fwd.treatment().allInstructions()) {
802 if (ins instanceof OutputInstruction) {
803 OutputInstruction o = (OutputInstruction) ins;
804 if (o.port() == PortNumber.CONTROLLER) {
805 ttBuilder.add(o);
806 } else {
807 log.warn("Only allowed treatments in versatile forwarding "
808 + "objectives are punts to the controller");
809 }
810 } else {
811 log.warn("Cannot process instruction in versatile fwd {}", ins);
812 }
Saurav Das822c4e22015-10-23 10:51:11 -0700813 }
Charles Chan2df0e8a2017-01-09 11:45:08 -0800814 if (fwd.treatment().clearedDeferred()) {
815 ttBuilder.wipeDeferred();
816 }
Saurav Das822c4e22015-10-23 10:51:11 -0700817 }
Saurav Das822c4e22015-10-23 10:51:11 -0700818 if (fwd.nextId() != null) {
Saurav Das49cb5a12016-01-16 22:54:07 -0800819 // overide case
820 NextGroup next = getGroupForNextObjective(fwd.nextId());
821 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
822 // we only need the top level group's key to point the flow to it
823 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
824 if (group == null) {
825 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
826 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
827 fail(fwd, ObjectiveError.GROUPMISSING);
828 return Collections.emptySet();
829 }
830 ttBuilder.deferred().group(group.id());
Saurav Das822c4e22015-10-23 10:51:11 -0700831 }
Saurav Das49cb5a12016-01-16 22:54:07 -0800832
833 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
834 .fromApp(fwd.appId())
835 .withPriority(fwd.priority())
836 .forDevice(deviceId)
Saurav Das77b5e902016-01-27 17:01:59 -0800837 .withSelector(sbuilder.build())
Saurav Das49cb5a12016-01-16 22:54:07 -0800838 .withTreatment(ttBuilder.build())
839 .makePermanent()
840 .forTable(ACL_TABLE);
841 return Collections.singletonList(ruleBuilder.build());
Saurav Das822c4e22015-10-23 10:51:11 -0700842 }
843
844 /**
845 * In the OF-DPA 2.0 pipeline, specific forwarding refers to the IP table
Saurav Das8a0732e2015-11-20 15:27:53 -0800846 * (unicast or multicast) or the L2 table (mac + vlan) or the MPLS table.
Saurav Das822c4e22015-10-23 10:51:11 -0700847 *
848 * @param fwd the forwarding objective of type 'specific'
849 * @return a collection of flow rules. Typically there will be only one
850 * for this type of forwarding objective. An empty set may be
851 * returned if there is an issue in processing the objective.
852 */
Saurav Das8a0732e2015-11-20 15:27:53 -0800853 protected Collection<FlowRule> processSpecific(ForwardingObjective fwd) {
Saurav Das25190812016-05-27 13:54:07 -0700854 log.debug("Processing specific fwd objective:{} in dev:{} with next:{}",
Saurav Das4ce45962015-11-24 23:21:05 -0800855 fwd.id(), deviceId, fwd.nextId());
856 boolean isEthTypeObj = isSupportedEthTypeObjective(fwd);
857 boolean isEthDstObj = isSupportedEthDstObjective(fwd);
858
859 if (isEthTypeObj) {
860 return processEthTypeSpecific(fwd);
861 } else if (isEthDstObj) {
862 return processEthDstSpecific(fwd);
863 } else {
864 log.warn("processSpecific: Unsupported forwarding objective "
865 + "criteria fwd:{} in dev:{}", fwd.nextId(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700866 fail(fwd, ObjectiveError.UNSUPPORTED);
867 return Collections.emptySet();
868 }
Saurav Das4ce45962015-11-24 23:21:05 -0800869 }
870
Saurav Das4ce45962015-11-24 23:21:05 -0800871 /**
872 * Handles forwarding rules to the IP and MPLS tables.
873 *
874 * @param fwd the forwarding objective
875 * @return A collection of flow rules, or an empty set
876 */
877 protected Collection<FlowRule> processEthTypeSpecific(ForwardingObjective fwd) {
Charles Chancad338a2016-09-16 18:03:11 -0700878 return processEthTypeSpecificInternal(fwd, false, ACL_TABLE);
Charles Chanf9e98652016-09-07 16:54:23 -0700879 }
880
881 /**
882 * Internal implementation of processEthTypeSpecific.
883 * <p>
884 * Wildcarded IPv4_DST is not supported in OFDPA i12. Therefore, we break
885 * the rule into 0.0.0.0/1 and 128.0.0.0/1.
886 * The allowDefaultRoute must be set to false for OFDPA i12.
887 * </p>
888 *
889 * @param fwd the forwarding objective
890 * @param allowDefaultRoute allow wildcarded IPv4_DST or not
Ray Milkey0bb1e102016-11-10 14:51:27 -0800891 * @param mplsNextTable next MPLS table
Charles Chanf9e98652016-09-07 16:54:23 -0700892 * @return A collection of flow rules, or an empty set
893 */
894 protected Collection<FlowRule> processEthTypeSpecificInternal(ForwardingObjective fwd,
Charles Chancad338a2016-09-16 18:03:11 -0700895 boolean allowDefaultRoute,
896 int mplsNextTable) {
Saurav Das4ce45962015-11-24 23:21:05 -0800897 TrafficSelector selector = fwd.selector();
898 EthTypeCriterion ethType =
899 (EthTypeCriterion) selector.getCriterion(Criterion.Type.ETH_TYPE);
Flavio Castroe10fa242016-01-15 12:43:51 -0800900 boolean popMpls = false;
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700901 boolean emptyGroup = false;
Charles Chan188ebf52015-12-23 00:15:11 -0800902 int forTableId;
Saurav Das8a0732e2015-11-20 15:27:53 -0800903 TrafficSelector.Builder filteredSelector = DefaultTrafficSelector.builder();
Charles Chan14967c22015-12-07 11:11:50 -0800904 TrafficTreatment.Builder tb = DefaultTrafficTreatment.builder();
Flavio Castroe10fa242016-01-15 12:43:51 -0800905 TrafficSelector.Builder complementarySelector = DefaultTrafficSelector.builder();
Charles Chan14967c22015-12-07 11:11:50 -0800906
Saurav Das8a0732e2015-11-20 15:27:53 -0800907 if (ethType.ethType().toShort() == Ethernet.TYPE_IPV4) {
Pier Luigi3bfe32c2017-01-30 09:47:36 -0800908 if (buildIpv4Selector(filteredSelector, complementarySelector, fwd, allowDefaultRoute) < 0) {
909 return Collections.emptyList();
910 }
911 // We need to set properly the next table
Flavio Castroe10fa242016-01-15 12:43:51 -0800912 IpPrefix ipv4Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV4_DST)).ip();
Charles Chan5b9df8d2016-03-28 22:21:40 -0700913 if (ipv4Dst.isMulticast()) {
Charles Chan5b9df8d2016-03-28 22:21:40 -0700914 forTableId = MULTICAST_ROUTING_TABLE;
Flavio Castroe10fa242016-01-15 12:43:51 -0800915 } else {
Charles Chan5b9df8d2016-03-28 22:21:40 -0700916 forTableId = UNICAST_ROUTING_TABLE;
Flavio Castroe10fa242016-01-15 12:43:51 -0800917 }
Charles Chan14967c22015-12-07 11:11:50 -0800918 if (fwd.treatment() != null) {
919 for (Instruction instr : fwd.treatment().allInstructions()) {
920 if (instr instanceof L3ModificationInstruction &&
921 ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
Saurav Das9c705342017-01-06 11:36:01 -0800922 // XXX decrementing IP ttl is done automatically for routing, this
923 // action is ignored or rejected in ofdpa as it is not fully implemented
924 //tb.deferred().add(instr);
Charles Chan14967c22015-12-07 11:11:50 -0800925 }
926 }
927 }
928
Pier Ventree0ae7a32016-11-23 09:57:42 -0800929 } else if (ethType.ethType().toShort() == Ethernet.TYPE_IPV6) {
930 if (buildIpv6Selector(filteredSelector, fwd) < 0) {
931 return Collections.emptyList();
932 }
933 forTableId = UNICAST_ROUTING_TABLE;
934 if (fwd.treatment() != null) {
935 for (Instruction instr : fwd.treatment().allInstructions()) {
936 if (instr instanceof L3ModificationInstruction &&
937 ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
938 // 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);
941 }
942 }
943 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800944 } else {
945 filteredSelector
946 .matchEthType(Ethernet.MPLS_UNICAST)
947 .matchMplsLabel(((MplsCriterion)
948 selector.getCriterion(Criterion.Type.MPLS_LABEL)).label());
949 MplsBosCriterion bos = (MplsBosCriterion) selector
Pier Ventre140a8942016-11-02 07:26:38 -0700950 .getCriterion(MPLS_BOS);
Saurav Das8a0732e2015-11-20 15:27:53 -0800951 if (bos != null) {
952 filteredSelector.matchMplsBos(bos.mplsBos());
953 }
954 forTableId = MPLS_TABLE_1;
Saurav Das4ce45962015-11-24 23:21:05 -0800955 log.debug("processing MPLS specific forwarding objective {} -> next:{}"
956 + " in dev {}", fwd.id(), fwd.nextId(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700957
Charles Chan14967c22015-12-07 11:11:50 -0800958 if (fwd.treatment() != null) {
959 for (Instruction instr : fwd.treatment().allInstructions()) {
960 if (instr instanceof L2ModificationInstruction &&
961 ((L2ModificationInstruction) instr).subtype() == L2SubType.MPLS_POP) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800962 popMpls = true;
Pier Luigi3bfe32c2017-01-30 09:47:36 -0800963 // OF-DPA does not pop in MPLS table in some cases. For the L3 VPN, it requires
Saurav Das9c705342017-01-06 11:36:01 -0800964 // setting the MPLS_TYPE so pop can happen down the pipeline
Pier Luigi3bfe32c2017-01-30 09:47:36 -0800965 if (mplsNextTable == MPLS_TYPE_TABLE && isNotMplsBos(selector)) {
966 tb.immediate().popMpls();
967 }
Charles Chan14967c22015-12-07 11:11:50 -0800968 }
969 if (instr instanceof L3ModificationInstruction &&
970 ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
971 // FIXME Should modify the app to send the correct DEC_MPLS_TTL instruction
972 tb.immediate().decMplsTtl();
973 }
974 if (instr instanceof L3ModificationInstruction &&
975 ((L3ModificationInstruction) instr).subtype() == L3SubType.TTL_IN) {
976 tb.immediate().add(instr);
977 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800978 }
979 }
980 }
Saurav Das822c4e22015-10-23 10:51:11 -0700981
982 if (fwd.nextId() != null) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800983 if (forTableId == MPLS_TABLE_1 && !popMpls) {
984 log.warn("SR CONTINUE case cannot be handled as MPLS ECMP "
Saurav Das25190812016-05-27 13:54:07 -0700985 + "is not implemented in OF-DPA yet. Aborting this flow {} -> next:{}"
986 + "in this device {}", fwd.id(), fwd.nextId(), deviceId);
Pier Ventree0ae7a32016-11-23 09:57:42 -0800987 // XXX We could convert to forwarding to a single-port, via a MPLS interface,
988 // or a MPLS SWAP (with-same) but that would have to be handled in the next-objective.
989 // Also the pop-mpls logic used here won't work in non-BoS case.
Saurav Das4ce45962015-11-24 23:21:05 -0800990 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
Saurav Das8a0732e2015-11-20 15:27:53 -0800991 return Collections.emptySet();
992 }
993
Saurav Das423fe2b2015-12-04 10:52:59 -0800994 NextGroup next = getGroupForNextObjective(fwd.nextId());
995 if (next != null) {
996 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
997 // we only need the top level group's key to point the flow to it
998 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
Pier Ventre140a8942016-11-02 07:26:38 -0700999 if (isNotMplsBos(selector) && group.type().equals(HASHED)) {
1000 log.warn("SR CONTINUE case cannot be handled as MPLS ECMP "
1001 + "is not implemented in OF-DPA yet. Aborting this flow {} -> next:{}"
1002 + "in this device {}", fwd.id(), fwd.nextId(), deviceId);
1003 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
1004 return Collections.emptySet();
1005 }
Saurav Das423fe2b2015-12-04 10:52:59 -08001006 if (group == null) {
1007 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
1008 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
1009 fail(fwd, ObjectiveError.GROUPMISSING);
1010 return Collections.emptySet();
1011 }
1012 tb.deferred().group(group.id());
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001013 // check if group is empty
1014 if (gkeys.size() == 1 && gkeys.get(0).size() == 1) {
1015 log.warn("Found empty group 0x{} in dev:{} .. will retry fwd:{}",
1016 Integer.toHexString(group.id().id()), deviceId, fwd.id());
1017 emptyGroup = true;
1018 }
Saurav Das25190812016-05-27 13:54:07 -07001019 } else {
1020 log.warn("Cannot find group for nextId:{} in dev:{}. Aborting fwd:{}",
1021 fwd.nextId(), deviceId, fwd.id());
1022 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
1023 return Collections.emptySet();
Saurav Das822c4e22015-10-23 10:51:11 -07001024 }
Saurav Das822c4e22015-10-23 10:51:11 -07001025 }
Charles Chancad338a2016-09-16 18:03:11 -07001026
1027 if (forTableId == MPLS_TABLE_1) {
Pier Ventre140a8942016-11-02 07:26:38 -07001028 if (mplsNextTable == MPLS_L3_TYPE_TABLE) {
Charles Chancad338a2016-09-16 18:03:11 -07001029 Ofdpa3SetMplsType setMplsType = new Ofdpa3SetMplsType(Ofdpa3MplsType.L3_PHP);
Saurav Das9c705342017-01-06 11:36:01 -08001030 // set mpls type as apply_action
1031 tb.immediate().extension(setMplsType, deviceId);
Charles Chancad338a2016-09-16 18:03:11 -07001032 }
1033 tb.transition(mplsNextTable);
1034 } else {
1035 tb.transition(ACL_TABLE);
1036 }
1037
Saurav Das822c4e22015-10-23 10:51:11 -07001038 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
1039 .fromApp(fwd.appId())
1040 .withPriority(fwd.priority())
1041 .forDevice(deviceId)
Saurav Das8a0732e2015-11-20 15:27:53 -08001042 .withSelector(filteredSelector.build())
1043 .withTreatment(tb.build())
1044 .forTable(forTableId);
Saurav Das822c4e22015-10-23 10:51:11 -07001045
1046 if (fwd.permanent()) {
1047 ruleBuilder.makePermanent();
1048 } else {
1049 ruleBuilder.makeTemporary(fwd.timeout());
1050 }
Flavio Castroe10fa242016-01-15 12:43:51 -08001051 Collection<FlowRule> flowRuleCollection = new ArrayList<>();
1052 flowRuleCollection.add(ruleBuilder.build());
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001053 if (!allowDefaultRoute) {
Pier Ventree0ae7a32016-11-23 09:57:42 -08001054 flowRuleCollection.add(
1055 defaultRoute(fwd, complementarySelector, forTableId, tb)
1056 );
Flavio Castroe10fa242016-01-15 12:43:51 -08001057 log.debug("Default rule 0.0.0.0/0 is being installed two rules");
1058 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001059 // XXX retrying flows may be necessary due to bug CORD-554
1060 if (emptyGroup) {
1061 executorService.schedule(new RetryFlows(fwd, flowRuleCollection),
1062 RETRY_MS, TimeUnit.MILLISECONDS);
1063 }
Flavio Castroe10fa242016-01-15 12:43:51 -08001064 return flowRuleCollection;
Saurav Das822c4e22015-10-23 10:51:11 -07001065 }
1066
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001067 protected int buildIpv4Selector(TrafficSelector.Builder builderToUpdate,
1068 TrafficSelector.Builder extBuilder,
1069 ForwardingObjective fwd,
1070 boolean allowDefaultRoute) {
1071 TrafficSelector selector = fwd.selector();
1072
1073 IpPrefix ipv4Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV4_DST)).ip();
1074 if (ipv4Dst.isMulticast()) {
1075 if (ipv4Dst.prefixLength() != 32) {
1076 log.warn("Multicast specific forwarding objective can only be /32");
1077 fail(fwd, ObjectiveError.BADPARAMS);
1078 return -1;
1079 }
1080 VlanId assignedVlan = readVlanFromSelector(fwd.meta());
1081 if (assignedVlan == null) {
1082 log.warn("VLAN ID required by multicast specific fwd obj is missing. Abort.");
1083 fail(fwd, ObjectiveError.BADPARAMS);
1084 return -1;
1085 }
1086 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
1087 builderToUpdate.extension(ofdpaMatchVlanVid, deviceId);
1088 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipv4Dst);
1089 log.debug("processing IPv4 multicast specific forwarding objective {} -> next:{}"
1090 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
1091 } else {
1092 if (ipv4Dst.prefixLength() == 0) {
1093 if (allowDefaultRoute) {
1094 // The entire IPV4_DST field is wildcarded intentionally
1095 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4);
1096 } else {
1097 // NOTE: The switch does not support matching 0.0.0.0/0
1098 // Split it into 0.0.0.0/1 and 128.0.0.0/1
1099 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4)
1100 .matchIPDst(IpPrefix.valueOf("0.0.0.0/1"));
1101 extBuilder.matchEthType(Ethernet.TYPE_IPV4)
1102 .matchIPDst(IpPrefix.valueOf("128.0.0.0/1"));
1103 }
1104 } else {
1105 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipv4Dst);
1106 }
1107 log.debug("processing IPv4 unicast specific forwarding objective {} -> next:{}"
1108 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
1109 }
1110 return 0;
1111 }
1112
1113 /**
1114 * Helper method to build Ipv6 selector using the selector provided by
1115 * a forwarding objective.
1116 *
1117 * @param builderToUpdate the builder to update
1118 * @param fwd the selector to read
1119 * @return 0 if the update ends correctly. -1 if the matches
1120 * are not yet supported
1121 */
1122 protected int buildIpv6Selector(TrafficSelector.Builder builderToUpdate,
1123 ForwardingObjective fwd) {
1124
1125 TrafficSelector selector = fwd.selector();
1126
1127 IpPrefix ipv6Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV6_DST)).ip();
1128 if (ipv6Dst.isMulticast()) {
1129 log.warn("IPv6 Multicast is currently not supported");
1130 fail(fwd, ObjectiveError.BADPARAMS);
1131 return -1;
1132 }
1133 if (ipv6Dst.prefixLength() != 0) {
1134 builderToUpdate.matchIPv6Dst(ipv6Dst);
1135 }
1136 builderToUpdate.matchEthType(Ethernet.TYPE_IPV6);
1137 log.debug("processing IPv6 unicast specific forwarding objective {} -> next:{}"
1138 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
1139 return 0;
1140 }
1141
Pier Ventree0ae7a32016-11-23 09:57:42 -08001142 protected FlowRule defaultRoute(ForwardingObjective fwd,
1143 TrafficSelector.Builder complementarySelector,
1144 int forTableId,
1145 TrafficTreatment.Builder tb) {
1146 FlowRule.Builder rule = DefaultFlowRule.builder()
1147 .fromApp(fwd.appId())
1148 .withPriority(fwd.priority())
1149 .forDevice(deviceId)
1150 .withSelector(complementarySelector.build())
1151 .withTreatment(tb.build())
1152 .forTable(forTableId);
1153 if (fwd.permanent()) {
1154 rule.makePermanent();
1155 } else {
1156 rule.makeTemporary(fwd.timeout());
1157 }
1158 return rule.build();
1159 }
1160
Saurav Das4ce45962015-11-24 23:21:05 -08001161 /**
1162 * Handles forwarding rules to the L2 bridging table. Flow actions are not
1163 * allowed in the bridging table - instead we use L2 Interface group or
1164 * L2 flood group
1165 *
1166 * @param fwd the forwarding objective
1167 * @return A collection of flow rules, or an empty set
1168 */
1169 protected Collection<FlowRule> processEthDstSpecific(ForwardingObjective fwd) {
1170 List<FlowRule> rules = new ArrayList<>();
1171
1172 // Build filtered selector
1173 TrafficSelector selector = fwd.selector();
1174 EthCriterion ethCriterion = (EthCriterion) selector
1175 .getCriterion(Criterion.Type.ETH_DST);
1176 VlanIdCriterion vlanIdCriterion = (VlanIdCriterion) selector
1177 .getCriterion(Criterion.Type.VLAN_VID);
1178
1179 if (vlanIdCriterion == null) {
1180 log.warn("Forwarding objective for bridging requires vlan. Not "
1181 + "installing fwd:{} in dev:{}", fwd.id(), deviceId);
1182 fail(fwd, ObjectiveError.BADPARAMS);
1183 return Collections.emptySet();
1184 }
1185
1186 TrafficSelector.Builder filteredSelectorBuilder =
1187 DefaultTrafficSelector.builder();
Yi Tseng3a77b4f2017-02-06 15:02:17 -08001188
1189 if (!ethCriterion.mac().equals(NONE) &&
1190 !ethCriterion.mac().equals(BROADCAST)) {
Saurav Das4ce45962015-11-24 23:21:05 -08001191 filteredSelectorBuilder.matchEthDst(ethCriterion.mac());
1192 log.debug("processing L2 forwarding objective:{} -> next:{} in dev:{}",
1193 fwd.id(), fwd.nextId(), deviceId);
1194 } else {
Yi Tseng3a77b4f2017-02-06 15:02:17 -08001195 // Use wildcard DST_MAC if the MacAddress is None or Broadcast
Saurav Das4ce45962015-11-24 23:21:05 -08001196 log.debug("processing L2 Broadcast forwarding objective:{} -> next:{} "
1197 + "in dev:{} for vlan:{}",
1198 fwd.id(), fwd.nextId(), deviceId, vlanIdCriterion.vlanId());
1199 }
Charles Chan14967c22015-12-07 11:11:50 -08001200 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vlanIdCriterion.vlanId());
1201 filteredSelectorBuilder.extension(ofdpaMatchVlanVid, deviceId);
Saurav Das4ce45962015-11-24 23:21:05 -08001202 TrafficSelector filteredSelector = filteredSelectorBuilder.build();
1203
1204 if (fwd.treatment() != null) {
1205 log.warn("Ignoring traffic treatment in fwd rule {} meant for L2 table"
1206 + "for dev:{}. Expecting only nextId", fwd.id(), deviceId);
1207 }
1208
1209 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
1210 if (fwd.nextId() != null) {
Saurav Das423fe2b2015-12-04 10:52:59 -08001211 NextGroup next = getGroupForNextObjective(fwd.nextId());
Saurav Das4ce45962015-11-24 23:21:05 -08001212 if (next != null) {
1213 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1214 // we only need the top level group's key to point the flow to it
1215 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
1216 if (group != null) {
1217 treatmentBuilder.deferred().group(group.id());
1218 } else {
1219 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
1220 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
1221 fail(fwd, ObjectiveError.GROUPMISSING);
1222 return Collections.emptySet();
1223 }
1224 }
1225 }
1226 treatmentBuilder.immediate().transition(ACL_TABLE);
1227 TrafficTreatment filteredTreatment = treatmentBuilder.build();
1228
1229 // Build bridging table entries
1230 FlowRule.Builder flowRuleBuilder = DefaultFlowRule.builder();
1231 flowRuleBuilder.fromApp(fwd.appId())
1232 .withPriority(fwd.priority())
1233 .forDevice(deviceId)
1234 .withSelector(filteredSelector)
1235 .withTreatment(filteredTreatment)
1236 .forTable(BRIDGING_TABLE);
1237 if (fwd.permanent()) {
1238 flowRuleBuilder.makePermanent();
1239 } else {
1240 flowRuleBuilder.makeTemporary(fwd.timeout());
1241 }
1242 rules.add(flowRuleBuilder.build());
1243 return rules;
1244 }
1245
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001246 //////////////////////////////////////
1247 // Helper Methods and Classes
1248 //////////////////////////////////////
1249
1250 private boolean isSupportedEthTypeObjective(ForwardingObjective fwd) {
1251 TrafficSelector selector = fwd.selector();
1252 EthTypeCriterion ethType = (EthTypeCriterion) selector
1253 .getCriterion(Criterion.Type.ETH_TYPE);
1254 return !((ethType == null) ||
1255 ((ethType.ethType().toShort() != Ethernet.TYPE_IPV4) &&
Pier Ventree0ae7a32016-11-23 09:57:42 -08001256 (ethType.ethType().toShort() != Ethernet.MPLS_UNICAST)) &&
1257 (ethType.ethType().toShort() != Ethernet.TYPE_IPV6));
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001258 }
1259
1260 private boolean isSupportedEthDstObjective(ForwardingObjective fwd) {
1261 TrafficSelector selector = fwd.selector();
1262 EthCriterion ethDst = (EthCriterion) selector
1263 .getCriterion(Criterion.Type.ETH_DST);
1264 VlanIdCriterion vlanId = (VlanIdCriterion) selector
1265 .getCriterion(Criterion.Type.VLAN_VID);
1266 return !(ethDst == null && vlanId == null);
1267 }
1268
Saurav Das423fe2b2015-12-04 10:52:59 -08001269 protected NextGroup getGroupForNextObjective(Integer nextId) {
1270 NextGroup next = flowObjectiveStore.getNextGroup(nextId);
1271 if (next != null) {
1272 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1273 if (gkeys != null && !gkeys.isEmpty()) {
1274 return next;
1275 } else {
1276 log.warn("Empty next group found in FlowObjective store for "
1277 + "next-id:{} in dev:{}", nextId, deviceId);
1278 }
1279 } else {
1280 log.warn("next-id {} not found in Flow objective store for dev:{}",
1281 nextId, deviceId);
1282 }
1283 return null;
1284 }
1285
Charles Chan188ebf52015-12-23 00:15:11 -08001286 protected static void pass(Objective obj) {
Sho SHIMIZUef7e2902016-02-12 18:38:29 -08001287 obj.context().ifPresent(context -> context.onSuccess(obj));
Saurav Das822c4e22015-10-23 10:51:11 -07001288 }
1289
Charles Chan188ebf52015-12-23 00:15:11 -08001290 protected static void fail(Objective obj, ObjectiveError error) {
Sho SHIMIZUef7e2902016-02-12 18:38:29 -08001291 obj.context().ifPresent(context -> context.onError(obj, error));
Saurav Das822c4e22015-10-23 10:51:11 -07001292 }
Saurav Das24431192016-03-07 19:13:00 -08001293
Saurav Das24431192016-03-07 19:13:00 -08001294 @Override
1295 public List<String> getNextMappings(NextGroup nextGroup) {
1296 List<String> mappings = new ArrayList<>();
1297 List<Deque<GroupKey>> gkeys = appKryo.deserialize(nextGroup.data());
1298 for (Deque<GroupKey> gkd : gkeys) {
1299 Group lastGroup = null;
Saurav Das8be4e3a2016-03-11 17:19:07 -08001300 StringBuffer gchain = new StringBuffer();
Saurav Das24431192016-03-07 19:13:00 -08001301 for (GroupKey gk : gkd) {
1302 Group g = groupService.getGroup(deviceId, gk);
Saurav Das8be4e3a2016-03-11 17:19:07 -08001303 if (g == null) {
Saurav Das25190812016-05-27 13:54:07 -07001304 gchain.append(" NoGrp").append(" -->");
Saurav Das8be4e3a2016-03-11 17:19:07 -08001305 continue;
1306 }
1307 gchain.append(" 0x").append(Integer.toHexString(g.id().id()))
1308 .append(" -->");
Saurav Das24431192016-03-07 19:13:00 -08001309 lastGroup = g;
1310 }
1311 // add port information for last group in group-chain
Saurav Das25190812016-05-27 13:54:07 -07001312 List<Instruction> lastGroupIns = new ArrayList<Instruction>();
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001313 if (lastGroup != null && !lastGroup.buckets().buckets().isEmpty()) {
Saurav Das25190812016-05-27 13:54:07 -07001314 lastGroupIns = lastGroup.buckets().buckets().get(0)
1315 .treatment().allInstructions();
1316 }
1317 for (Instruction i: lastGroupIns) {
Saurav Das24431192016-03-07 19:13:00 -08001318 if (i instanceof OutputInstruction) {
Saurav Das8be4e3a2016-03-11 17:19:07 -08001319 gchain.append(" port:").append(((OutputInstruction) i).port());
Saurav Das24431192016-03-07 19:13:00 -08001320 }
1321 }
Saurav Das8be4e3a2016-03-11 17:19:07 -08001322 mappings.add(gchain.toString());
Saurav Das24431192016-03-07 19:13:00 -08001323 }
1324 return mappings;
1325 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001326
Pier Ventre140a8942016-11-02 07:26:38 -07001327 static boolean isMplsBos(TrafficSelector selector) {
1328 MplsBosCriterion bosCriterion = (MplsBosCriterion) selector.getCriterion(MPLS_BOS);
1329 return bosCriterion != null && bosCriterion.mplsBos();
1330 }
1331
1332 static boolean isNotMplsBos(TrafficSelector selector) {
1333 MplsBosCriterion bosCriterion = (MplsBosCriterion) selector.getCriterion(MPLS_BOS);
1334 return bosCriterion != null && !bosCriterion.mplsBos();
1335 }
1336
Charles Chan5b9df8d2016-03-28 22:21:40 -07001337 protected static VlanId readVlanFromSelector(TrafficSelector selector) {
Saurav Das59232cf2016-04-27 18:35:50 -07001338 if (selector == null) {
1339 return null;
1340 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001341 Criterion criterion = selector.getCriterion(Criterion.Type.VLAN_VID);
1342 return (criterion == null)
1343 ? null : ((VlanIdCriterion) criterion).vlanId();
1344 }
1345
1346 protected static IpPrefix readIpDstFromSelector(TrafficSelector selector) {
Saurav Das59232cf2016-04-27 18:35:50 -07001347 if (selector == null) {
1348 return null;
1349 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001350 Criterion criterion = selector.getCriterion(Criterion.Type.IPV4_DST);
1351 return (criterion == null) ? null : ((IPCriterion) criterion).ip();
1352 }
Charles Chand55e84d2016-03-30 17:54:24 -07001353
1354 private static VlanId readVlanFromTreatment(TrafficTreatment treatment) {
Saurav Das59232cf2016-04-27 18:35:50 -07001355 if (treatment == null) {
1356 return null;
1357 }
Charles Chand55e84d2016-03-30 17:54:24 -07001358 for (Instruction i : treatment.allInstructions()) {
1359 if (i instanceof ModVlanIdInstruction) {
1360 return ((ModVlanIdInstruction) i).vlanId();
1361 }
1362 }
1363 return null;
1364 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001365
1366 /**
1367 * Utility class that retries sending flows a fixed number of times, even if
1368 * some of the attempts are successful. Used only for forwarding objectives.
1369 */
1370 protected final class RetryFlows implements Runnable {
1371 int attempts = MAX_RETRY_ATTEMPTS;
1372 private Collection<FlowRule> retryFlows;
1373 private ForwardingObjective fwd;
1374
1375 RetryFlows(ForwardingObjective fwd, Collection<FlowRule> retryFlows) {
1376 this.fwd = fwd;
1377 this.retryFlows = retryFlows;
1378 }
1379
1380 @Override
1381 public void run() {
1382 log.info("RETRY FLOWS ATTEMPT# {} for fwd:{} rules:{}",
1383 MAX_RETRY_ATTEMPTS - attempts, fwd.id(), retryFlows.size());
1384 sendForward(fwd, retryFlows);
1385 if (--attempts > 0) {
1386 executorService.schedule(this, RETRY_MS, TimeUnit.MILLISECONDS);
1387 }
1388 }
1389 }
1390
Saurav Das822c4e22015-10-23 10:51:11 -07001391}