blob: da30eea40cec3531f52f531fe64c2d7ab67c5412 [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Thomas Vachuska781d18b2014-10-27 10:31:25 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * 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
Thomas Vachuska781d18b2014-10-27 10:31:25 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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.
Thomas Vachuska781d18b2014-10-27 10:31:25 -070015 */
Thomas Vachuska95caba32016-04-04 10:42:05 -070016package org.onosproject.provider.of.flow.util;
alshabib6b5cfec2014-09-18 17:42:18 -070017
Jonathan Hart8cf212a2015-10-29 17:42:03 -070018import com.google.common.collect.Lists;
Ray Milkey84d5a292016-02-22 14:04:01 -080019import org.onlab.packet.EthType;
Jonathan Hartd0ef7522014-12-11 11:51:18 -080020import org.onlab.packet.Ip4Address;
21import org.onlab.packet.Ip4Prefix;
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -080022import org.onlab.packet.Ip6Address;
Charles M.C. Chan52fae7d2015-01-17 00:35:53 +080023import org.onlab.packet.Ip6Prefix;
Jonathan Hartd0ef7522014-12-11 11:51:18 -080024import org.onlab.packet.MacAddress;
Michele Santuari4b6019e2014-12-19 11:31:45 +010025import org.onlab.packet.MplsLabel;
Hyunsun Mooncf732fb2015-08-22 21:04:23 -070026import org.onlab.packet.TpPort;
Jonathan Hartd0ef7522014-12-11 11:51:18 -080027import org.onlab.packet.VlanId;
Yi Tsengfa394de2017-02-01 11:26:40 -080028import org.onosproject.core.GroupId;
Brian O'Connorabafb502014-12-02 22:26:20 -080029import org.onosproject.net.DeviceId;
Sho SHIMIZU4f828ee2015-05-28 09:38:21 -070030import org.onosproject.net.Lambda;
Yafit Hadar73514612015-11-04 10:14:21 +020031import org.onosproject.net.OduSignalId;
Brian O'Connorabafb502014-12-02 22:26:20 -080032import org.onosproject.net.PortNumber;
Jonathan Hart3c259162015-10-21 21:31:19 -070033import org.onosproject.net.driver.DefaultDriverData;
34import org.onosproject.net.driver.DefaultDriverHandler;
35import org.onosproject.net.driver.Driver;
36import org.onosproject.net.driver.DriverHandler;
37import org.onosproject.net.driver.DriverService;
Brian O'Connorabafb502014-12-02 22:26:20 -080038import org.onosproject.net.flow.DefaultFlowEntry;
39import org.onosproject.net.flow.DefaultFlowRule;
40import org.onosproject.net.flow.DefaultTrafficSelector;
41import org.onosproject.net.flow.DefaultTrafficTreatment;
42import org.onosproject.net.flow.FlowEntry;
43import org.onosproject.net.flow.FlowEntry.FlowEntryState;
44import org.onosproject.net.flow.FlowRule;
45import org.onosproject.net.flow.TrafficSelector;
46import org.onosproject.net.flow.TrafficTreatment;
Charles Chan14967c22015-12-07 11:11:50 -080047import org.onosproject.net.flow.criteria.ExtensionSelectorType.ExtensionSelectorTypes;
Sho SHIMIZU9553bb82015-06-30 16:02:45 -070048import org.onosproject.net.flow.instructions.Instructions;
Charles Chan14967c22015-12-07 11:11:50 -080049import org.onosproject.openflow.controller.ExtensionSelectorInterpreter;
alshabib880b6442015-11-23 22:13:04 -080050import org.onosproject.openflow.controller.ExtensionTreatmentInterpreter;
Sangsik Yoonb1b823f2016-05-16 18:55:39 +090051import org.onosproject.provider.of.flow.impl.NewAdaptiveFlowStatsCollector;
Brian O'Connor72cb19a2015-01-16 16:14:41 -080052import org.projectfloodlight.openflow.protocol.OFFlowMod;
alshabib6b5cfec2014-09-18 17:42:18 -070053import org.projectfloodlight.openflow.protocol.OFFlowRemoved;
54import org.projectfloodlight.openflow.protocol.OFFlowStatsEntry;
Charles Chan14967c22015-12-07 11:11:50 -080055import org.projectfloodlight.openflow.protocol.OFMatchV3;
Yuta HIGUCHI6512f3e2017-05-18 17:21:24 -070056import org.projectfloodlight.openflow.protocol.OFObject;
Brian O'Connor21564612015-12-05 19:24:59 -080057import org.projectfloodlight.openflow.protocol.OFVersion;
alshabib6b5cfec2014-09-18 17:42:18 -070058import org.projectfloodlight.openflow.protocol.action.OFAction;
Marc De Leenheer49087752014-10-23 13:54:09 -070059import org.projectfloodlight.openflow.protocol.action.OFActionCircuit;
Steffen Gebertba2d3b72015-10-22 11:14:31 +020060import org.projectfloodlight.openflow.protocol.action.OFActionEnqueue;
Marc De Leenheer49087752014-10-23 13:54:09 -070061import org.projectfloodlight.openflow.protocol.action.OFActionExperimenter;
sangho8995ac52015-02-04 11:29:03 -080062import org.projectfloodlight.openflow.protocol.action.OFActionGroup;
alshabib6b5cfec2014-09-18 17:42:18 -070063import org.projectfloodlight.openflow.protocol.action.OFActionOutput;
sangho3f97a17d2015-01-29 22:56:29 -080064import org.projectfloodlight.openflow.protocol.action.OFActionPopMpls;
Konstantinos Kanonakis9215ff22016-11-04 13:28:11 -050065import org.projectfloodlight.openflow.protocol.action.OFActionPushVlan;
alshabib6b5cfec2014-09-18 17:42:18 -070066import org.projectfloodlight.openflow.protocol.action.OFActionSetDlDst;
67import org.projectfloodlight.openflow.protocol.action.OFActionSetDlSrc;
Jonathan Hartd0ef7522014-12-11 11:51:18 -080068import org.projectfloodlight.openflow.protocol.action.OFActionSetField;
alshabib6b5cfec2014-09-18 17:42:18 -070069import org.projectfloodlight.openflow.protocol.action.OFActionSetNwDst;
70import org.projectfloodlight.openflow.protocol.action.OFActionSetNwSrc;
Jonathan Hart8cf212a2015-10-29 17:42:03 -070071import org.projectfloodlight.openflow.protocol.action.OFActionSetQueue;
alshabib6b5cfec2014-09-18 17:42:18 -070072import org.projectfloodlight.openflow.protocol.action.OFActionSetVlanPcp;
73import org.projectfloodlight.openflow.protocol.action.OFActionSetVlanVid;
alshabib19fdc122014-10-03 11:38:19 -070074import org.projectfloodlight.openflow.protocol.instruction.OFInstruction;
Jonathan Hartcf783202014-11-24 18:55:42 -080075import org.projectfloodlight.openflow.protocol.instruction.OFInstructionApplyActions;
alshabibbdcbb102015-04-22 14:16:38 -070076import org.projectfloodlight.openflow.protocol.instruction.OFInstructionGotoTable;
alshabib346b5b32015-03-06 00:42:16 -080077import org.projectfloodlight.openflow.protocol.instruction.OFInstructionWriteActions;
Saurav Das86af8f12015-05-25 23:55:33 -070078import org.projectfloodlight.openflow.protocol.instruction.OFInstructionWriteMetadata;
alshabib6b5cfec2014-09-18 17:42:18 -070079import org.projectfloodlight.openflow.protocol.match.Match;
80import org.projectfloodlight.openflow.protocol.match.MatchField;
Jonathan Hartd0ef7522014-12-11 11:51:18 -080081import org.projectfloodlight.openflow.protocol.oxm.OFOxm;
Yafit Hadar4cc65f72016-02-10 11:23:06 +020082import org.projectfloodlight.openflow.protocol.oxm.OFOxmOchSigid;
alshabib346b5b32015-03-06 00:42:16 -080083import org.projectfloodlight.openflow.protocol.ver13.OFFactoryVer13;
Sho SHIMIZU4f828ee2015-05-28 09:38:21 -070084import org.projectfloodlight.openflow.types.CircuitSignalID;
alshabib6b5cfec2014-09-18 17:42:18 -070085import org.projectfloodlight.openflow.types.IPv4Address;
Charles M.C. Chan52fae7d2015-01-17 00:35:53 +080086import org.projectfloodlight.openflow.types.IPv6Address;
Jonathan Hart0e12fad2014-10-17 14:54:58 -070087import org.projectfloodlight.openflow.types.Masked;
Pier Ventre23f78672016-11-15 08:46:34 -080088import org.projectfloodlight.openflow.types.OFBooleanValue;
Jonathan Hartd0ef7522014-12-11 11:51:18 -080089import org.projectfloodlight.openflow.types.OFVlanVidMatch;
Jonathan Harte4e74f02016-03-03 12:57:40 -080090import org.projectfloodlight.openflow.types.OduSignalID;
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -070091import org.projectfloodlight.openflow.types.TransportPort;
sangho3f97a17d2015-01-29 22:56:29 -080092import org.projectfloodlight.openflow.types.U32;
Hyunsun Moona08c5d02015-07-14 17:53:00 -070093import org.projectfloodlight.openflow.types.U64;
Sho SHIMIZU4f828ee2015-05-28 09:38:21 -070094import org.projectfloodlight.openflow.types.U8;
Jonathan Hartd0ef7522014-12-11 11:51:18 -080095import org.projectfloodlight.openflow.types.VlanPcp;
alshabib6b5cfec2014-09-18 17:42:18 -070096import org.slf4j.Logger;
Jian Lia0d9a172016-04-01 16:58:06 -070097import org.slf4j.LoggerFactory;
alshabib6b5cfec2014-09-18 17:42:18 -070098
Jonathan Hart8cf212a2015-10-29 17:42:03 -070099import java.util.List;
100
Thiago Santos877914d2016-07-20 18:29:29 -0300101import static java.util.concurrent.TimeUnit.NANOSECONDS;
102import static java.util.concurrent.TimeUnit.SECONDS;
alshabib44852fb2016-04-15 15:56:51 -0700103import static org.onosproject.net.flow.criteria.Criteria.*;
Yafit Hadar73514612015-11-04 10:14:21 +0200104import static org.onosproject.net.flow.instructions.Instructions.modL0Lambda;
105import static org.onosproject.net.flow.instructions.Instructions.modL1OduSignalId;
alshabib44852fb2016-04-15 15:56:51 -0700106import static org.onosproject.provider.of.flow.util.OpenFlowValueMapper.*;
alshabib19fdc122014-10-03 11:38:19 -0700107
alshabib1c319ff2014-10-04 20:29:09 -0700108public class FlowEntryBuilder {
Jian Lia0d9a172016-04-01 16:58:06 -0700109 private static final Logger log = LoggerFactory.getLogger(FlowEntryBuilder.class);
alshabib6b5cfec2014-09-18 17:42:18 -0700110
111 private final OFFlowStatsEntry stat;
112 private final OFFlowRemoved removed;
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800113 private final OFFlowMod flowMod;
alshabib6b5cfec2014-09-18 17:42:18 -0700114
115 private final Match match;
alshabib346b5b32015-03-06 00:42:16 -0800116
Jonathan Hart67fc0972015-03-19 15:21:20 -0700117 // All actions are contained in an OFInstruction. For OF1.0
118 // the instruction type is apply instruction (immediate set in ONOS speak)
alshabib346b5b32015-03-06 00:42:16 -0800119 private final List<OFInstruction> instructions;
alshabib6b5cfec2014-09-18 17:42:18 -0700120
Jonathan Harte4e74f02016-03-03 12:57:40 -0800121 private final DeviceId deviceId;
alshabib6b5cfec2014-09-18 17:42:18 -0700122
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800123 public enum FlowType { STAT, REMOVED, MOD }
124
125 private final FlowType type;
alshabib6b5cfec2014-09-18 17:42:18 -0700126
Jonathan Hart3c259162015-10-21 21:31:19 -0700127 private final DriverService driverService;
128
Sangsik Yoonb1b823f2016-05-16 18:55:39 +0900129 // NewAdaptiveFlowStatsCollector for AdaptiveFlowSampling mode,
130 // null is not AFM mode, namely SimpleStatsCollector mode
131 private NewAdaptiveFlowStatsCollector afsc;
132
Jonathan Harte4e74f02016-03-03 12:57:40 -0800133 public FlowEntryBuilder(DeviceId deviceId, OFFlowStatsEntry entry, DriverService driverService) {
Saurav Dasfa2fa932015-03-03 11:29:48 -0800134 this.stat = entry;
135 this.match = entry.getMatch();
alshabib346b5b32015-03-06 00:42:16 -0800136 this.instructions = getInstructions(entry);
Jonathan Harte4e74f02016-03-03 12:57:40 -0800137 this.deviceId = deviceId;
Saurav Dasfa2fa932015-03-03 11:29:48 -0800138 this.removed = null;
139 this.flowMod = null;
140 this.type = FlowType.STAT;
Jonathan Hart3c259162015-10-21 21:31:19 -0700141 this.driverService = driverService;
Sangsik Yoonb1b823f2016-05-16 18:55:39 +0900142 this.afsc = null;
Saurav Dasfa2fa932015-03-03 11:29:48 -0800143 }
144
Jonathan Harte4e74f02016-03-03 12:57:40 -0800145 public FlowEntryBuilder(DeviceId deviceId, OFFlowRemoved removed, DriverService driverService) {
alshabib6b5cfec2014-09-18 17:42:18 -0700146 this.match = removed.getMatch();
147 this.removed = removed;
Jonathan Harte4e74f02016-03-03 12:57:40 -0800148 this.deviceId = deviceId;
alshabib346b5b32015-03-06 00:42:16 -0800149 this.instructions = null;
alshabib6b5cfec2014-09-18 17:42:18 -0700150 this.stat = null;
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800151 this.flowMod = null;
152 this.type = FlowType.REMOVED;
Jonathan Hart3c259162015-10-21 21:31:19 -0700153 this.driverService = driverService;
Sangsik Yoonb1b823f2016-05-16 18:55:39 +0900154 this.afsc = null;
alshabib6b5cfec2014-09-18 17:42:18 -0700155 }
156
Jonathan Harte4e74f02016-03-03 12:57:40 -0800157 public FlowEntryBuilder(DeviceId deviceId, OFFlowMod fm, DriverService driverService) {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800158 this.match = fm.getMatch();
Jonathan Harte4e74f02016-03-03 12:57:40 -0800159 this.deviceId = deviceId;
alshabib346b5b32015-03-06 00:42:16 -0800160 this.instructions = getInstructions(fm);
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800161 this.type = FlowType.MOD;
162 this.flowMod = fm;
163 this.stat = null;
164 this.removed = null;
Jonathan Hart3c259162015-10-21 21:31:19 -0700165 this.driverService = driverService;
Sangsik Yoonb1b823f2016-05-16 18:55:39 +0900166 this.afsc = null;
167 }
168
169 public FlowEntryBuilder withSetAfsc(NewAdaptiveFlowStatsCollector afsc) {
170 this.afsc = afsc;
171 return this;
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800172 }
alshabib1c319ff2014-10-04 20:29:09 -0700173
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800174 public FlowEntry build(FlowEntryState... state) {
Brian O'Connor21564612015-12-05 19:24:59 -0800175 FlowRule.Builder builder;
176 try {
177 switch (this.type) {
178 case STAT:
179 builder = DefaultFlowRule.builder()
Jonathan Harte4e74f02016-03-03 12:57:40 -0800180 .forDevice(deviceId)
Brian O'Connor21564612015-12-05 19:24:59 -0800181 .withSelector(buildSelector())
182 .withTreatment(buildTreatment())
183 .withPriority(stat.getPriority())
Thiago Santos943cd542017-01-13 13:05:09 -0300184 .withIdleTimeout(stat.getIdleTimeout())
Jonathan Hartd1ce4b02016-01-11 13:28:36 -0800185 .withCookie(stat.getCookie().getValue());
186 if (stat.getVersion() != OFVersion.OF_10) {
187 builder.forTable(stat.getTableId().getValue());
188 }
alshabibbdcbb102015-04-22 14:16:38 -0700189
Sangsik Yoonb1b823f2016-05-16 18:55:39 +0900190 if (afsc != null) {
191 FlowEntry.FlowLiveType liveType = afsc.calFlowLiveType(stat.getDurationSec());
192 return new DefaultFlowEntry(builder.build(), FlowEntryState.ADDED,
193 SECONDS.toNanos(stat.getDurationSec())
194 + stat.getDurationNsec(), NANOSECONDS,
195 liveType,
196 stat.getPacketCount().getValue(),
197 stat.getByteCount().getValue());
198 } else {
199 return new DefaultFlowEntry(builder.build(), FlowEntryState.ADDED,
200 stat.getDurationSec(),
201 stat.getPacketCount().getValue(),
202 stat.getByteCount().getValue());
203 }
204
Brian O'Connor21564612015-12-05 19:24:59 -0800205 case REMOVED:
206 builder = DefaultFlowRule.builder()
Jonathan Harte4e74f02016-03-03 12:57:40 -0800207 .forDevice(deviceId)
Brian O'Connor21564612015-12-05 19:24:59 -0800208 .withSelector(buildSelector())
209 .withPriority(removed.getPriority())
Thiago Santos943cd542017-01-13 13:05:09 -0300210 .withIdleTimeout(removed.getIdleTimeout())
Murat Parlakisikc6759e82016-06-29 03:22:22 -0700211 .withCookie(removed.getCookie().getValue())
Jimmy Jine9b7a022016-08-12 16:56:48 -0700212 .withReason(FlowRule.FlowRemoveReason.parseShort((short) removed.getReason().ordinal()));
Murat Parlakisikc6759e82016-06-29 03:22:22 -0700213
Brian O'Connor21564612015-12-05 19:24:59 -0800214 if (removed.getVersion() != OFVersion.OF_10) {
215 builder.forTable(removed.getTableId().getValue());
216 }
alshabibbdcbb102015-04-22 14:16:38 -0700217
Sangsik Yoonb1b823f2016-05-16 18:55:39 +0900218 if (afsc != null) {
219 FlowEntry.FlowLiveType liveType = afsc.calFlowLiveType(removed.getDurationSec());
220 return new DefaultFlowEntry(builder.build(), FlowEntryState.REMOVED,
221 SECONDS.toNanos(removed.getDurationSec())
222 + removed.getDurationNsec(), NANOSECONDS,
223 liveType,
224 removed.getPacketCount().getValue(),
225 removed.getByteCount().getValue());
226 } else {
227 return new DefaultFlowEntry(builder.build(), FlowEntryState.REMOVED,
228 removed.getDurationSec(),
229 removed.getPacketCount().getValue(),
230 removed.getByteCount().getValue());
231 }
232
Brian O'Connor21564612015-12-05 19:24:59 -0800233 case MOD:
234 FlowEntryState flowState = state.length > 0 ? state[0] : FlowEntryState.FAILED;
235 builder = DefaultFlowRule.builder()
Jonathan Harte4e74f02016-03-03 12:57:40 -0800236 .forDevice(deviceId)
Brian O'Connor21564612015-12-05 19:24:59 -0800237 .withSelector(buildSelector())
238 .withTreatment(buildTreatment())
239 .withPriority(flowMod.getPriority())
Thiago Santos943cd542017-01-13 13:05:09 -0300240 .withIdleTimeout(flowMod.getIdleTimeout())
Brian O'Connor21564612015-12-05 19:24:59 -0800241 .withCookie(flowMod.getCookie().getValue());
242 if (flowMod.getVersion() != OFVersion.OF_10) {
243 builder.forTable(flowMod.getTableId().getValue());
244 }
alshabibbdcbb102015-04-22 14:16:38 -0700245
Sangsik Yoonb1b823f2016-05-16 18:55:39 +0900246 if (afsc != null) {
247 FlowEntry.FlowLiveType liveType = FlowEntry.FlowLiveType.IMMEDIATE;
248 return new DefaultFlowEntry(builder.build(), flowState, 0, liveType, 0, 0);
249 } else {
250 return new DefaultFlowEntry(builder.build(), flowState, 0, 0, 0);
251 }
Brian O'Connor21564612015-12-05 19:24:59 -0800252 default:
253 log.error("Unknown flow type : {}", this.type);
254 return null;
255 }
256 } catch (UnsupportedOperationException e) {
257 log.warn("Error building flow entry", e);
258 return null;
alshabib6b5cfec2014-09-18 17:42:18 -0700259 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800260
alshabib6b5cfec2014-09-18 17:42:18 -0700261 }
262
alshabib346b5b32015-03-06 00:42:16 -0800263 private List<OFInstruction> getInstructions(OFFlowMod entry) {
alshabib19fdc122014-10-03 11:38:19 -0700264 switch (entry.getVersion()) {
265 case OF_10:
Jonathan Hart8ef6d3b2015-03-08 21:21:27 -0700266 return Lists.newArrayList(OFFactoryVer13.INSTANCE.instructions()
267 .applyActions(
268 entry.getActions()));
alshabib19fdc122014-10-03 11:38:19 -0700269 case OF_11:
270 case OF_12:
271 case OF_13:
Yuta HIGUCHI6512f3e2017-05-18 17:21:24 -0700272 case OF_14:
273 case OF_15:
alshabib346b5b32015-03-06 00:42:16 -0800274 return entry.getInstructions();
275 default:
276 log.warn("Unknown OF version {}", entry.getVersion());
277 }
278 return Lists.newLinkedList();
279 }
280
281 private List<OFInstruction> getInstructions(OFFlowStatsEntry entry) {
282 switch (entry.getVersion()) {
283 case OF_10:
284 return Lists.newArrayList(
285 OFFactoryVer13.INSTANCE.instructions().applyActions(entry.getActions()));
286 case OF_11:
287 case OF_12:
288 case OF_13:
Yuta HIGUCHI6512f3e2017-05-18 17:21:24 -0700289 case OF_14:
290 case OF_15:
alshabib346b5b32015-03-06 00:42:16 -0800291 return entry.getInstructions();
alshabib19fdc122014-10-03 11:38:19 -0700292 default:
293 log.warn("Unknown OF version {}", entry.getVersion());
294 }
295 return Lists.newLinkedList();
296 }
alshabib6b5cfec2014-09-18 17:42:18 -0700297
298 private TrafficTreatment buildTreatment() {
tom9a693fd2014-10-03 11:32:19 -0700299 TrafficTreatment.Builder builder = DefaultTrafficTreatment.builder();
alshabib346b5b32015-03-06 00:42:16 -0800300 for (OFInstruction in : instructions) {
301 switch (in.getType()) {
302 case GOTO_TABLE:
alshabibbdcbb102015-04-22 14:16:38 -0700303 builder.transition(((int) ((OFInstructionGotoTable) in)
304 .getTableId().getValue()));
alshabib346b5b32015-03-06 00:42:16 -0800305 break;
306 case WRITE_METADATA:
Saurav Das86af8f12015-05-25 23:55:33 -0700307 OFInstructionWriteMetadata m = (OFInstructionWriteMetadata) in;
308 builder.writeMetadata(m.getMetadata().getValue(),
309 m.getMetadataMask().getValue());
alshabib346b5b32015-03-06 00:42:16 -0800310 break;
311 case WRITE_ACTIONS:
312 builder.deferred();
313 buildActions(((OFInstructionWriteActions) in).getActions(),
314 builder);
315 break;
316 case APPLY_ACTIONS:
317 builder.immediate();
318 buildActions(((OFInstructionApplyActions) in).getActions(),
319 builder);
320 break;
321 case CLEAR_ACTIONS:
322 builder.wipeDeferred();
323 break;
324 case EXPERIMENTER:
325 break;
326 case METER:
327 break;
328 default:
329 log.warn("Unknown instructions type {}", in.getType());
alshabib6b5cfec2014-09-18 17:42:18 -0700330 }
331 }
332
333 return builder.build();
334 }
335
Jian Lia0d9a172016-04-01 16:58:06 -0700336 /**
337 * Configures traffic treatment builder with a given collection of actions.
338 *
339 * @param actions a set of OpenFlow actions
340 * @param builder traffic treatment builder
341 * @param driverHandler driver handler
342 * @param deviceId device identifier
343 * @return configured traffic treatment builder
344 */
345 public static TrafficTreatment.Builder configureTreatmentBuilder(List<OFAction> actions,
346 TrafficTreatment.Builder builder,
347 DriverHandler driverHandler,
348 DeviceId deviceId) {
349 ExtensionTreatmentInterpreter interpreter;
Charles Chan14967c22015-12-07 11:11:50 -0800350 if (driverHandler.hasBehaviour(ExtensionTreatmentInterpreter.class)) {
Jian Lia0d9a172016-04-01 16:58:06 -0700351 interpreter = driverHandler.behaviour(ExtensionTreatmentInterpreter.class);
Charles Chan14967c22015-12-07 11:11:50 -0800352 } else {
Jian Lia0d9a172016-04-01 16:58:06 -0700353 interpreter = null;
Charles Chan14967c22015-12-07 11:11:50 -0800354 }
355
alshabib346b5b32015-03-06 00:42:16 -0800356 for (OFAction act : actions) {
357 switch (act.getType()) {
358 case OUTPUT:
359 OFActionOutput out = (OFActionOutput) act;
360 builder.setOutput(
361 PortNumber.portNumber(out.getPort().getPortNumber()));
362 break;
363 case SET_VLAN_VID:
364 OFActionSetVlanVid vlan = (OFActionSetVlanVid) act;
365 builder.setVlanId(VlanId.vlanId(vlan.getVlanVid().getVlan()));
366 break;
367 case SET_VLAN_PCP:
368 OFActionSetVlanPcp pcp = (OFActionSetVlanPcp) act;
369 builder.setVlanPcp(pcp.getVlanPcp().getValue());
370 break;
371 case SET_DL_DST:
372 OFActionSetDlDst dldst = (OFActionSetDlDst) act;
373 builder.setEthDst(
374 MacAddress.valueOf(dldst.getDlAddr().getLong()));
375 break;
376 case SET_DL_SRC:
377 OFActionSetDlSrc dlsrc = (OFActionSetDlSrc) act;
378 builder.setEthSrc(
379 MacAddress.valueOf(dlsrc.getDlAddr().getLong()));
alshabib346b5b32015-03-06 00:42:16 -0800380 break;
381 case SET_NW_DST:
382 OFActionSetNwDst nwdst = (OFActionSetNwDst) act;
383 IPv4Address di = nwdst.getNwAddr();
384 builder.setIpDst(Ip4Address.valueOf(di.getInt()));
385 break;
386 case SET_NW_SRC:
387 OFActionSetNwSrc nwsrc = (OFActionSetNwSrc) act;
388 IPv4Address si = nwsrc.getNwAddr();
389 builder.setIpSrc(Ip4Address.valueOf(si.getInt()));
390 break;
391 case EXPERIMENTER:
392 OFActionExperimenter exp = (OFActionExperimenter) act;
393 if (exp.getExperimenter() == 0x80005A06 ||
394 exp.getExperimenter() == 0x748771) {
395 OFActionCircuit ct = (OFActionCircuit) exp;
Yafit Hadar4cc65f72016-02-10 11:23:06 +0200396 CircuitSignalID circuitSignalID = ((OFOxmOchSigid) ct.getField()).getValue();
397 builder.add(Instructions.modL0Lambda(Lambda.ochSignal(
398 lookupGridType(circuitSignalID.getGridType()),
399 lookupChannelSpacing(circuitSignalID.getChannelSpacing()),
400 circuitSignalID.getChannelNumber(), circuitSignalID.getSpectralWidth())));
Jian Lia0d9a172016-04-01 16:58:06 -0700401 } else if (interpreter != null) {
402 builder.extension(interpreter.mapAction(exp), deviceId);
alshabib346b5b32015-03-06 00:42:16 -0800403 } else {
404 log.warn("Unsupported OFActionExperimenter {}", exp.getExperimenter());
405 }
406 break;
407 case SET_FIELD:
408 OFActionSetField setField = (OFActionSetField) act;
Jian Lia0d9a172016-04-01 16:58:06 -0700409 handleSetField(builder, setField, driverHandler, deviceId);
alshabib346b5b32015-03-06 00:42:16 -0800410 break;
411 case POP_MPLS:
412 OFActionPopMpls popMpls = (OFActionPopMpls) act;
Ray Milkey84d5a292016-02-22 14:04:01 -0800413 builder.popMpls(new EthType(popMpls.getEthertype().getValue()));
alshabib346b5b32015-03-06 00:42:16 -0800414 break;
415 case PUSH_MPLS:
416 builder.pushMpls();
417 break;
418 case COPY_TTL_IN:
419 builder.copyTtlIn();
420 break;
421 case COPY_TTL_OUT:
422 builder.copyTtlOut();
423 break;
424 case DEC_MPLS_TTL:
425 builder.decMplsTtl();
426 break;
427 case DEC_NW_TTL:
428 builder.decNwTtl();
429 break;
430 case GROUP:
431 OFActionGroup group = (OFActionGroup) act;
Yi Tsengfa394de2017-02-01 11:26:40 -0800432 builder.group(new GroupId(group.getGroup().getGroupNumber()));
alshabib346b5b32015-03-06 00:42:16 -0800433 break;
Steffen Gebertbbfdaaa2015-09-29 11:01:46 +0200434 case SET_QUEUE:
435 OFActionSetQueue setQueue = (OFActionSetQueue) act;
436 builder.setQueue(setQueue.getQueueId());
437 break;
Steffen Gebertba2d3b72015-10-22 11:14:31 +0200438 case ENQUEUE:
439 OFActionEnqueue enqueue = (OFActionEnqueue) act;
Jian Liffef5002016-04-04 23:27:37 -0700440 builder.setQueue(enqueue.getQueueId(),
441 PortNumber.portNumber(enqueue.getPort().getPortNumber()));
Steffen Gebertba2d3b72015-10-22 11:14:31 +0200442 break;
Jonathan Hart67fc0972015-03-19 15:21:20 -0700443 case STRIP_VLAN:
alshabib346b5b32015-03-06 00:42:16 -0800444 case POP_VLAN:
445 builder.popVlan();
446 break;
Jonathan Hart8ef6d3b2015-03-08 21:21:27 -0700447 case PUSH_VLAN:
Konstantinos Kanonakis9215ff22016-11-04 13:28:11 -0500448 OFActionPushVlan pushVlan = (OFActionPushVlan) act;
449 builder.pushVlan(new EthType((short) pushVlan.getEthertype().getValue()));
Jonathan Hart8ef6d3b2015-03-08 21:21:27 -0700450 break;
alshabib346b5b32015-03-06 00:42:16 -0800451 case SET_TP_DST:
452 case SET_TP_SRC:
453 case POP_PBB:
454 case PUSH_PBB:
alshabib346b5b32015-03-06 00:42:16 -0800455 case SET_MPLS_LABEL:
456 case SET_MPLS_TC:
457 case SET_MPLS_TTL:
458 case SET_NW_ECN:
459 case SET_NW_TOS:
460 case SET_NW_TTL:
alshabib346b5b32015-03-06 00:42:16 -0800461
alshabib346b5b32015-03-06 00:42:16 -0800462 default:
463 log.warn("Action type {} not yet implemented.", act.getType());
464 }
465 }
466 return builder;
467 }
468
Jian Lia0d9a172016-04-01 16:58:06 -0700469 private TrafficTreatment.Builder buildActions(List<OFAction> actions,
470 TrafficTreatment.Builder builder) {
Jonathan Harte4e74f02016-03-03 12:57:40 -0800471 DriverHandler driverHandler = getDriver(deviceId);
Jian Lia0d9a172016-04-01 16:58:06 -0700472
473 return configureTreatmentBuilder(actions, builder, driverHandler, deviceId);
474 }
475
Charles Chancad338a2016-09-16 18:03:11 -0700476 // CHECKSTYLE IGNORE MethodLength FOR NEXT 1 LINES
Jian Lia0d9a172016-04-01 16:58:06 -0700477 private static void handleSetField(TrafficTreatment.Builder builder,
478 OFActionSetField action,
479 DriverHandler driverHandler,
480 DeviceId deviceId) {
Charles Chan14967c22015-12-07 11:11:50 -0800481 ExtensionTreatmentInterpreter treatmentInterpreter;
482 if (driverHandler.hasBehaviour(ExtensionTreatmentInterpreter.class)) {
483 treatmentInterpreter = driverHandler.behaviour(ExtensionTreatmentInterpreter.class);
484 } else {
485 treatmentInterpreter = null;
486 }
Jonathan Hart3c259162015-10-21 21:31:19 -0700487 OFOxm<?> oxm = action.getField();
Jonathan Hartd0ef7522014-12-11 11:51:18 -0800488 switch (oxm.getMatchField().id) {
489 case VLAN_PCP:
490 @SuppressWarnings("unchecked")
491 OFOxm<VlanPcp> vlanpcp = (OFOxm<VlanPcp>) oxm;
492 builder.setVlanPcp(vlanpcp.getValue().getValue());
493 break;
494 case VLAN_VID:
Charles Chan4211baa2016-04-20 17:10:40 -0700495 if (treatmentInterpreter != null) {
496 try {
497 builder.extension(treatmentInterpreter.mapAction(action), deviceId);
498 break;
499 } catch (UnsupportedOperationException e) {
500 log.debug("Unsupported action extension; defaulting to native OF");
501 }
Charles Chan14967c22015-12-07 11:11:50 -0800502 }
alshabib44852fb2016-04-15 15:56:51 -0700503 @SuppressWarnings("unchecked")
504 OFOxm<OFVlanVidMatch> vlanvid = (OFOxm<OFVlanVidMatch>) oxm;
505 builder.setVlanId(VlanId.vlanId(vlanvid.getValue().getVlan()));
Jonathan Hartd0ef7522014-12-11 11:51:18 -0800506 break;
507 case ETH_DST:
508 @SuppressWarnings("unchecked")
509 OFOxm<org.projectfloodlight.openflow.types.MacAddress> ethdst =
510 (OFOxm<org.projectfloodlight.openflow.types.MacAddress>) oxm;
511 builder.setEthDst(MacAddress.valueOf(ethdst.getValue().getLong()));
512 break;
513 case ETH_SRC:
514 @SuppressWarnings("unchecked")
515 OFOxm<org.projectfloodlight.openflow.types.MacAddress> ethsrc =
516 (OFOxm<org.projectfloodlight.openflow.types.MacAddress>) oxm;
517 builder.setEthSrc(MacAddress.valueOf(ethsrc.getValue().getLong()));
518 break;
519 case IPV4_DST:
520 @SuppressWarnings("unchecked")
521 OFOxm<IPv4Address> ip4dst = (OFOxm<IPv4Address>) oxm;
522 builder.setIpDst(Ip4Address.valueOf(ip4dst.getValue().getInt()));
523 break;
524 case IPV4_SRC:
525 @SuppressWarnings("unchecked")
526 OFOxm<IPv4Address> ip4src = (OFOxm<IPv4Address>) oxm;
527 builder.setIpSrc(Ip4Address.valueOf(ip4src.getValue().getInt()));
528 break;
sangho3f97a17d2015-01-29 22:56:29 -0800529 case MPLS_LABEL:
530 @SuppressWarnings("unchecked")
531 OFOxm<U32> labelId = (OFOxm<U32>) oxm;
Michele Santuari4b6019e2014-12-19 11:31:45 +0100532 builder.setMpls(MplsLabel.mplsLabel((int) labelId.getValue().getValue()));
sangho3f97a17d2015-01-29 22:56:29 -0800533 break;
Saurav Das73a7dd42015-08-19 22:20:31 -0700534 case MPLS_BOS:
535 @SuppressWarnings("unchecked")
Pier Ventre23f78672016-11-15 08:46:34 -0800536 OFOxm<OFBooleanValue> mplsBos = (OFOxm<OFBooleanValue>) oxm;
537 builder.setMplsBos(mplsBos.getValue().getValue());
Saurav Das73a7dd42015-08-19 22:20:31 -0700538 break;
Hyunsun Moona08c5d02015-07-14 17:53:00 -0700539 case TUNNEL_ID:
540 @SuppressWarnings("unchecked")
541 OFOxm<U64> tunnelId = (OFOxm<U64>) oxm;
542 builder.setTunnelId(tunnelId.getValue().getValue());
543 break;
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700544 case TCP_DST:
545 @SuppressWarnings("unchecked")
546 OFOxm<TransportPort> tcpdst = (OFOxm<TransportPort>) oxm;
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700547 builder.setTcpDst(TpPort.tpPort(tcpdst.getValue().getPort()));
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700548 break;
549 case TCP_SRC:
550 @SuppressWarnings("unchecked")
551 OFOxm<TransportPort> tcpsrc = (OFOxm<TransportPort>) oxm;
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700552 builder.setTcpSrc(TpPort.tpPort(tcpsrc.getValue().getPort()));
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700553 break;
554 case UDP_DST:
555 @SuppressWarnings("unchecked")
556 OFOxm<TransportPort> udpdst = (OFOxm<TransportPort>) oxm;
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700557 builder.setUdpDst(TpPort.tpPort(udpdst.getValue().getPort()));
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700558 break;
559 case UDP_SRC:
560 @SuppressWarnings("unchecked")
561 OFOxm<TransportPort> udpsrc = (OFOxm<TransportPort>) oxm;
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700562 builder.setUdpSrc(TpPort.tpPort(udpsrc.getValue().getPort()));
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700563 break;
Jonathan Hart3c259162015-10-21 21:31:19 -0700564 case TUNNEL_IPV4_DST:
Phaneendra Manda8db7d092016-06-04 00:17:24 +0530565 case NSP:
566 case NSI:
567 case NSH_C1:
568 case NSH_C2:
569 case NSH_C3:
570 case NSH_C4:
571 case NSH_MDTYPE:
572 case NSH_NP:
573 case ENCAP_ETH_SRC:
574 case ENCAP_ETH_DST:
575 case ENCAP_ETH_TYPE:
576 case TUN_GPE_NP:
Jian Liffef5002016-04-04 23:27:37 -0700577 if (treatmentInterpreter != null) {
578 try {
579 builder.extension(treatmentInterpreter.mapAction(action), deviceId);
580 } catch (UnsupportedOperationException e) {
alshabib44852fb2016-04-15 15:56:51 -0700581 log.debug(e.getMessage());
Jian Liffef5002016-04-04 23:27:37 -0700582 }
Jonathan Hart3c259162015-10-21 21:31:19 -0700583 }
584 break;
Yafit Hadar73514612015-11-04 10:14:21 +0200585 case EXP_ODU_SIG_ID:
586 @SuppressWarnings("unchecked")
587 OFOxm<OduSignalID> oduID = (OFOxm<OduSignalID>) oxm;
588 OduSignalID oduSignalID = oduID.getValue();
589 OduSignalId oduSignalId = OduSignalId.oduSignalId(oduSignalID.getTpn(),
590 oduSignalID.getTslen(),
591 oduSignalID.getTsmap());
592 builder.add(modL1OduSignalId(oduSignalId));
593 break;
594 case EXP_OCH_SIG_ID:
595 try {
596 @SuppressWarnings("unchecked")
597 OFOxm<CircuitSignalID> ochId = (OFOxm<CircuitSignalID>) oxm;
598 CircuitSignalID circuitSignalID = ochId.getValue();
599 builder.add(modL0Lambda(Lambda.ochSignal(
600 lookupGridType(circuitSignalID.getGridType()),
601 lookupChannelSpacing(circuitSignalID.getChannelSpacing()),
602 circuitSignalID.getChannelNumber(), circuitSignalID.getSpectralWidth())));
603 } catch (NoMappingFoundException e) {
604 log.warn(e.getMessage());
605 break;
606 }
607 break;
Jonathan Hartd0ef7522014-12-11 11:51:18 -0800608 case ARP_OP:
lishuai67574ce2016-05-12 16:39:59 +0800609 @SuppressWarnings("unchecked")
610 OFOxm<org.projectfloodlight.openflow.types.ArpOpcode> arpop =
611 (OFOxm<org.projectfloodlight.openflow.types.ArpOpcode>) oxm;
612 builder.setArpOp((short) arpop.getValue().getOpcode());
613 break;
Jonathan Hartd0ef7522014-12-11 11:51:18 -0800614 case ARP_SHA:
lishuai67574ce2016-05-12 16:39:59 +0800615 @SuppressWarnings("unchecked")
616 OFOxm<org.projectfloodlight.openflow.types.MacAddress> arpsha =
617 (OFOxm<org.projectfloodlight.openflow.types.MacAddress>) oxm;
618 builder.setArpSha(MacAddress.valueOf(arpsha.getValue().getLong()));
619 break;
Jonathan Hartd0ef7522014-12-11 11:51:18 -0800620 case ARP_SPA:
lishuai67574ce2016-05-12 16:39:59 +0800621 @SuppressWarnings("unchecked")
622 OFOxm<IPv4Address> arpspa = (OFOxm<IPv4Address>) oxm;
623 builder.setArpSpa(Ip4Address.valueOf(arpspa.getValue().getInt()));
624 break;
Charles Chancad338a2016-09-16 18:03:11 -0700625 case OFDPA_MPLS_TYPE:
Pier Ventre6f630052016-10-18 09:58:41 -0700626 case OFDPA_OVID:
Pier Ventre9cf536b2016-10-21 13:30:18 -0700627 case OFDPA_MPLS_L2_PORT:
Pier Ventredb252cc2016-10-21 21:54:26 -0700628 case OFDPA_QOS_INDEX:
Charles Chancad338a2016-09-16 18:03:11 -0700629 if (treatmentInterpreter != null) {
630 try {
631 builder.extension(treatmentInterpreter.mapAction(action), deviceId);
632 break;
633 } catch (UnsupportedOperationException e) {
634 log.warn("Unsupported action extension");
635 }
636 }
637 break;
Jonathan Hartd0ef7522014-12-11 11:51:18 -0800638 case ARP_THA:
639 case ARP_TPA:
640 case BSN_EGR_PORT_GROUP_ID:
641 case BSN_GLOBAL_VRF_ALLOWED:
642 case BSN_IN_PORTS_128:
643 case BSN_L3_DST_CLASS_ID:
644 case BSN_L3_INTERFACE_CLASS_ID:
645 case BSN_L3_SRC_CLASS_ID:
646 case BSN_LAG_ID:
647 case BSN_TCP_FLAGS:
648 case BSN_UDF0:
649 case BSN_UDF1:
650 case BSN_UDF2:
651 case BSN_UDF3:
652 case BSN_UDF4:
653 case BSN_UDF5:
654 case BSN_UDF6:
655 case BSN_UDF7:
656 case BSN_VLAN_XLATE_PORT_GROUP_ID:
657 case BSN_VRF:
658 case ETH_TYPE:
659 case ICMPV4_CODE:
660 case ICMPV4_TYPE:
661 case ICMPV6_CODE:
662 case ICMPV6_TYPE:
663 case IN_PHY_PORT:
664 case IN_PORT:
665 case IPV6_DST:
666 case IPV6_FLABEL:
667 case IPV6_ND_SLL:
668 case IPV6_ND_TARGET:
669 case IPV6_ND_TLL:
670 case IPV6_SRC:
671 case IP_DSCP:
672 case IP_ECN:
673 case IP_PROTO:
674 case METADATA:
Jonathan Hartd0ef7522014-12-11 11:51:18 -0800675 case MPLS_TC:
676 case OCH_SIGID:
677 case OCH_SIGID_BASIC:
678 case OCH_SIGTYPE:
679 case OCH_SIGTYPE_BASIC:
680 case SCTP_DST:
681 case SCTP_SRC:
Yafit Hadar73514612015-11-04 10:14:21 +0200682 case EXP_ODU_SIGTYPE:
683 case EXP_OCH_SIGTYPE:
Jonathan Hartd0ef7522014-12-11 11:51:18 -0800684 default:
685 log.warn("Set field type {} not yet implemented.", oxm.getMatchField().id);
686 break;
687 }
688 }
689
Sho SHIMIZU6f1b09e2015-05-05 11:26:22 -0700690 // CHECKSTYLE IGNORE MethodLength FOR NEXT 1 LINES
alshabib6b5cfec2014-09-18 17:42:18 -0700691 private TrafficSelector buildSelector() {
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800692 MacAddress mac;
693 Ip4Prefix ip4Prefix;
694 Ip6Address ip6Address;
695 Ip6Prefix ip6Prefix;
BitOhenryc1e5fcc2015-11-23 20:47:53 +0800696 Ip4Address ip;
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800697
Jonathan Harte4e74f02016-03-03 12:57:40 -0800698 DriverHandler driverHandler = getDriver(deviceId);
Charles Chan14967c22015-12-07 11:11:50 -0800699 ExtensionSelectorInterpreter selectorInterpreter;
700 if (driverHandler.hasBehaviour(ExtensionSelectorInterpreter.class)) {
701 selectorInterpreter = driverHandler.behaviour(ExtensionSelectorInterpreter.class);
702 } else {
703 selectorInterpreter = null;
704 }
705
tom9a693fd2014-10-03 11:32:19 -0700706 TrafficSelector.Builder builder = DefaultTrafficSelector.builder();
alshabib6b5cfec2014-09-18 17:42:18 -0700707 for (MatchField<?> field : match.getMatchFields()) {
708 switch (field.id) {
709 case IN_PORT:
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800710 builder.matchInPort(PortNumber
alshabib010c31d2014-09-26 10:01:12 -0700711 .portNumber(match.get(MatchField.IN_PORT).getPortNumber()));
alshabib6b5cfec2014-09-18 17:42:18 -0700712 break;
Pavlin Radoslavovd0fd8412015-02-04 13:57:00 -0800713 case IN_PHY_PORT:
714 builder.matchInPhyPort(PortNumber
715 .portNumber(match.get(MatchField.IN_PHY_PORT).getPortNumber()));
716 break;
717 case METADATA:
718 long metadata =
719 match.get(MatchField.METADATA).getValue().getValue();
720 builder.matchMetadata(metadata);
721 break;
alshabib6b5cfec2014-09-18 17:42:18 -0700722 case ETH_DST:
Saurav Das9d6c86b2016-02-19 09:01:07 -0800723 if (match.isPartiallyMasked(MatchField.ETH_DST)) {
724 Masked<org.projectfloodlight.openflow.types.MacAddress> maskedMac =
725 match.getMasked(MatchField.ETH_DST);
726 builder.matchEthDstMasked(MacAddress.valueOf(maskedMac.getValue().getLong()),
727 MacAddress.valueOf(maskedMac.getMask().getLong()));
728 } else {
729 mac = MacAddress.valueOf(match.get(MatchField.ETH_DST).getLong());
730 builder.matchEthDst(mac);
731 }
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800732 break;
733 case ETH_SRC:
Saurav Das9d6c86b2016-02-19 09:01:07 -0800734 if (match.isPartiallyMasked(MatchField.ETH_SRC)) {
735 Masked<org.projectfloodlight.openflow.types.MacAddress> maskedMac =
736 match.getMasked(MatchField.ETH_SRC);
737 builder.matchEthSrcMasked(MacAddress.valueOf(maskedMac.getValue().getLong()),
738 MacAddress.valueOf(maskedMac.getMask().getLong()));
739 } else {
740 mac = MacAddress.valueOf(match.get(MatchField.ETH_SRC).getLong());
741 builder.matchEthSrc(mac);
742 }
alshabib6b5cfec2014-09-18 17:42:18 -0700743 break;
744 case ETH_TYPE:
745 int ethType = match.get(MatchField.ETH_TYPE).getValue();
Jonathan Hart8cf212a2015-10-29 17:42:03 -0700746 builder.matchEthType((short) ethType);
alshabib6b5cfec2014-09-18 17:42:18 -0700747 break;
alshabib6b5cfec2014-09-18 17:42:18 -0700748 case VLAN_VID:
Charles Chan14967c22015-12-07 11:11:50 -0800749 if (selectorInterpreter != null &&
750 selectorInterpreter.supported(ExtensionSelectorTypes.OFDPA_MATCH_VLAN_VID.type())) {
Yuta HIGUCHI6512f3e2017-05-18 17:21:24 -0700751 if (isOF13OrLater(match)) {
Charles Chan14967c22015-12-07 11:11:50 -0800752 OFOxm oxm = ((OFMatchV3) match).getOxmList().get(MatchField.VLAN_VID);
753 builder.extension(selectorInterpreter.mapOxm(oxm),
Jonathan Harte4e74f02016-03-03 12:57:40 -0800754 deviceId);
Charles Chan14967c22015-12-07 11:11:50 -0800755 } else {
756 break;
Jonathan Hart6cd2f352015-01-13 17:44:45 -0800757 }
758 } else {
Charles Chan14967c22015-12-07 11:11:50 -0800759 VlanId vlanId = null;
760 if (match.isPartiallyMasked(MatchField.VLAN_VID)) {
761 Masked<OFVlanVidMatch> masked = match.getMasked(MatchField.VLAN_VID);
762 if (masked.getValue().equals(OFVlanVidMatch.PRESENT)
763 && masked.getMask().equals(OFVlanVidMatch.PRESENT)) {
764 vlanId = VlanId.ANY;
765 }
Jonathan Hart1468fee2015-07-16 18:50:34 -0700766 } else {
Charles Chan14967c22015-12-07 11:11:50 -0800767 if (!match.get(MatchField.VLAN_VID).isPresentBitSet()) {
768 vlanId = VlanId.NONE;
769 } else {
770 vlanId = VlanId.vlanId(match.get(MatchField.VLAN_VID).getVlan());
771 }
Jonathan Hart1468fee2015-07-16 18:50:34 -0700772 }
Charles Chan14967c22015-12-07 11:11:50 -0800773 if (vlanId != null) {
774 builder.matchVlanId(vlanId);
775 }
Jonathan Hart6cd2f352015-01-13 17:44:45 -0800776 }
alshabib6b5cfec2014-09-18 17:42:18 -0700777 break;
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800778 case VLAN_PCP:
779 byte vlanPcp = match.get(MatchField.VLAN_PCP).getValue();
780 builder.matchVlanPcp(vlanPcp);
781 break;
Pavlin Radoslavovd0fd8412015-02-04 13:57:00 -0800782 case IP_DSCP:
783 byte ipDscp = match.get(MatchField.IP_DSCP).getDscpValue();
784 builder.matchIPDscp(ipDscp);
785 break;
786 case IP_ECN:
787 byte ipEcn = match.get(MatchField.IP_ECN).getEcnValue();
788 builder.matchIPEcn(ipEcn);
789 break;
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800790 case IP_PROTO:
791 short proto = match.get(MatchField.IP_PROTO).getIpProtocolNumber();
792 builder.matchIPProtocol((byte) proto);
793 break;
794 case IPV4_SRC:
795 if (match.isPartiallyMasked(MatchField.IPV4_SRC)) {
796 Masked<IPv4Address> maskedIp = match.getMasked(MatchField.IPV4_SRC);
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800797 ip4Prefix = Ip4Prefix.valueOf(
798 maskedIp.getValue().getInt(),
799 maskedIp.getMask().asCidrMaskLength());
800 } else {
801 ip4Prefix = Ip4Prefix.valueOf(
802 match.get(MatchField.IPV4_SRC).getInt(),
803 Ip4Prefix.MAX_MASK_LENGTH);
804 }
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800805 builder.matchIPSrc(ip4Prefix);
806 break;
807 case IPV4_DST:
808 if (match.isPartiallyMasked(MatchField.IPV4_DST)) {
809 Masked<IPv4Address> maskedIp = match.getMasked(MatchField.IPV4_DST);
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800810 ip4Prefix = Ip4Prefix.valueOf(
811 maskedIp.getValue().getInt(),
812 maskedIp.getMask().asCidrMaskLength());
813 } else {
814 ip4Prefix = Ip4Prefix.valueOf(
815 match.get(MatchField.IPV4_DST).getInt(),
816 Ip4Prefix.MAX_MASK_LENGTH);
817 }
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800818 builder.matchIPDst(ip4Prefix);
Jonathan Hart34bc6142014-10-17 11:00:43 -0700819 break;
820 case TCP_SRC:
Andreas Gilbert75b882f72017-02-03 09:58:07 +0100821 if (match.isPartiallyMasked(MatchField.TCP_SRC)) {
822 Masked<org.projectfloodlight.openflow.types.TransportPort> maskedPort =
823 match.getMasked(MatchField.TCP_SRC);
824 builder.matchTcpSrcMasked(TpPort.tpPort(maskedPort.getValue().getPort()),
825 TpPort.tpPort(maskedPort.getMask().getPort()));
826 } else {
827 builder.matchTcpSrc(TpPort.tpPort(match.get(MatchField.TCP_SRC).getPort()));
828 }
Jonathan Hart34bc6142014-10-17 11:00:43 -0700829 break;
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800830 case TCP_DST:
Andreas Gilbert75b882f72017-02-03 09:58:07 +0100831 if (match.isPartiallyMasked(MatchField.TCP_DST)) {
832 Masked<org.projectfloodlight.openflow.types.TransportPort> maskedPort =
833 match.getMasked(MatchField.TCP_DST);
834 builder.matchTcpDstMasked(TpPort.tpPort(maskedPort.getValue().getPort()),
835 TpPort.tpPort(maskedPort.getMask().getPort()));
836 } else {
837 builder.matchTcpDst(TpPort.tpPort(match.get(MatchField.TCP_DST).getPort()));
838 }
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800839 break;
840 case UDP_SRC:
Andreas Gilbert75b882f72017-02-03 09:58:07 +0100841 if (match.isPartiallyMasked(MatchField.UDP_SRC)) {
842 Masked<org.projectfloodlight.openflow.types.TransportPort> maskedPort =
843 match.getMasked(MatchField.UDP_SRC);
844 builder.matchUdpSrcMasked(TpPort.tpPort(maskedPort.getValue().getPort()),
845 TpPort.tpPort(maskedPort.getMask().getPort()));
846 } else {
847 builder.matchUdpSrc(TpPort.tpPort(match.get(MatchField.UDP_SRC).getPort()));
848 }
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800849 break;
850 case UDP_DST:
Andreas Gilbert75b882f72017-02-03 09:58:07 +0100851 if (match.isPartiallyMasked(MatchField.UDP_DST)) {
852 Masked<org.projectfloodlight.openflow.types.TransportPort> maskedPort =
853 match.getMasked(MatchField.UDP_DST);
854 builder.matchUdpDstMasked(TpPort.tpPort(maskedPort.getValue().getPort()),
855 TpPort.tpPort(maskedPort.getMask().getPort()));
856 } else {
857 builder.matchUdpDst(TpPort.tpPort(match.get(MatchField.UDP_DST).getPort()));
858 }
Sho SHIMIZU6f1b09e2015-05-05 11:26:22 -0700859 break;
Michele Santuari4b6019e2014-12-19 11:31:45 +0100860 case MPLS_LABEL:
861 builder.matchMplsLabel(MplsLabel.mplsLabel((int) match.get(MatchField.MPLS_LABEL)
862 .getValue()));
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800863 break;
Saurav Dasffc5bbc2015-08-18 23:30:19 -0700864 case MPLS_BOS:
865 builder.matchMplsBos(match.get(MatchField.MPLS_BOS).getValue());
866 break;
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800867 case SCTP_SRC:
Andreas Gilbert75b882f72017-02-03 09:58:07 +0100868 if (match.isPartiallyMasked(MatchField.SCTP_SRC)) {
869 Masked<org.projectfloodlight.openflow.types.TransportPort> maskedPort =
870 match.getMasked(MatchField.SCTP_SRC);
871 builder.matchSctpSrcMasked(TpPort.tpPort(maskedPort.getValue().getPort()),
872 TpPort.tpPort(maskedPort.getMask().getPort()));
873 } else {
874 builder.matchSctpSrc(TpPort.tpPort(match.get(MatchField.SCTP_SRC).getPort()));
875 }
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800876 break;
877 case SCTP_DST:
Andreas Gilbert75b882f72017-02-03 09:58:07 +0100878 if (match.isPartiallyMasked(MatchField.SCTP_DST)) {
879 Masked<org.projectfloodlight.openflow.types.TransportPort> maskedPort =
880 match.getMasked(MatchField.SCTP_DST);
881 builder.matchSctpDstMasked(TpPort.tpPort(maskedPort.getValue().getPort()),
882 TpPort.tpPort(maskedPort.getMask().getPort()));
883 } else {
884 builder.matchSctpDst(TpPort.tpPort(match.get(MatchField.SCTP_DST).getPort()));
885 }
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800886 break;
887 case ICMPV4_TYPE:
888 byte icmpType = (byte) match.get(MatchField.ICMPV4_TYPE).getType();
889 builder.matchIcmpType(icmpType);
890 break;
891 case ICMPV4_CODE:
892 byte icmpCode = (byte) match.get(MatchField.ICMPV4_CODE).getCode();
893 builder.matchIcmpCode(icmpCode);
894 break;
895 case IPV6_SRC:
896 if (match.isPartiallyMasked(MatchField.IPV6_SRC)) {
897 Masked<IPv6Address> maskedIp = match.getMasked(MatchField.IPV6_SRC);
898 ip6Prefix = Ip6Prefix.valueOf(
899 maskedIp.getValue().getBytes(),
900 maskedIp.getMask().asCidrMaskLength());
901 } else {
902 ip6Prefix = Ip6Prefix.valueOf(
903 match.get(MatchField.IPV6_SRC).getBytes(),
904 Ip6Prefix.MAX_MASK_LENGTH);
905 }
906 builder.matchIPv6Src(ip6Prefix);
907 break;
908 case IPV6_DST:
909 if (match.isPartiallyMasked(MatchField.IPV6_DST)) {
910 Masked<IPv6Address> maskedIp = match.getMasked(MatchField.IPV6_DST);
911 ip6Prefix = Ip6Prefix.valueOf(
912 maskedIp.getValue().getBytes(),
913 maskedIp.getMask().asCidrMaskLength());
914 } else {
915 ip6Prefix = Ip6Prefix.valueOf(
916 match.get(MatchField.IPV6_DST).getBytes(),
917 Ip6Prefix.MAX_MASK_LENGTH);
918 }
919 builder.matchIPv6Dst(ip6Prefix);
920 break;
921 case IPV6_FLABEL:
922 int flowLabel =
923 match.get(MatchField.IPV6_FLABEL).getIPv6FlowLabelValue();
924 builder.matchIPv6FlowLabel(flowLabel);
925 break;
926 case ICMPV6_TYPE:
927 byte icmpv6type = (byte) match.get(MatchField.ICMPV6_TYPE).getValue();
928 builder.matchIcmpv6Type(icmpv6type);
929 break;
930 case ICMPV6_CODE:
931 byte icmpv6code = (byte) match.get(MatchField.ICMPV6_CODE).getValue();
932 builder.matchIcmpv6Code(icmpv6code);
933 break;
934 case IPV6_ND_TARGET:
935 ip6Address =
936 Ip6Address.valueOf(match.get(MatchField.IPV6_ND_TARGET).getBytes());
937 builder.matchIPv6NDTargetAddress(ip6Address);
938 break;
939 case IPV6_ND_SLL:
940 mac = MacAddress.valueOf(match.get(MatchField.IPV6_ND_SLL).getLong());
941 builder.matchIPv6NDSourceLinkLayerAddress(mac);
942 break;
943 case IPV6_ND_TLL:
944 mac = MacAddress.valueOf(match.get(MatchField.IPV6_ND_TLL).getLong());
945 builder.matchIPv6NDTargetLinkLayerAddress(mac);
946 break;
Pavlin Radoslavov5e4f7542015-02-06 18:18:21 -0800947 case IPV6_EXTHDR:
Charles M.C. Chan2184de12015-04-26 02:24:53 +0800948 builder.matchIPv6ExthdrFlags((short) match.get(MatchField.IPV6_EXTHDR)
Hyunsun Moona08c5d02015-07-14 17:53:00 -0700949 .getValue());
Pavlin Radoslavov5e4f7542015-02-06 18:18:21 -0800950 break;
Marc De Leenheer49087752014-10-23 13:54:09 -0700951 case OCH_SIGID:
Sho SHIMIZU4f828ee2015-05-28 09:38:21 -0700952 CircuitSignalID sigId = match.get(MatchField.OCH_SIGID);
953 builder.add(matchLambda(Lambda.ochSignal(
Sho SHIMIZUc17042d2015-05-28 12:07:23 -0700954 lookupGridType(sigId.getGridType()), lookupChannelSpacing(sigId.getChannelSpacing()),
Marc De Leenheer0b8b2ef2015-08-03 15:39:00 -0700955 sigId.getChannelNumber(), sigId.getSpectralWidth())
Sho SHIMIZU4f828ee2015-05-28 09:38:21 -0700956 ));
Marc De Leenheer49087752014-10-23 13:54:09 -0700957 break;
Praseed Balakrishnan2dd5abd2014-11-03 14:56:28 -0800958 case OCH_SIGTYPE:
Sho SHIMIZU4f828ee2015-05-28 09:38:21 -0700959 U8 sigType = match.get(MatchField.OCH_SIGTYPE);
Sho SHIMIZUc17042d2015-05-28 12:07:23 -0700960 builder.add(matchOchSignalType(lookupOchSignalType((byte) sigType.getValue())));
Praseed Balakrishnan2dd5abd2014-11-03 14:56:28 -0800961 break;
Yafit Hadar73514612015-11-04 10:14:21 +0200962 case EXP_OCH_SIG_ID:
963 try {
964 CircuitSignalID expSigId = match.get(MatchField.EXP_OCH_SIG_ID);
965 builder.add(matchLambda(Lambda.ochSignal(
966 lookupGridType(expSigId.getGridType()), lookupChannelSpacing(expSigId.getChannelSpacing()),
967 expSigId.getChannelNumber(), expSigId.getSpectralWidth())));
968 } catch (NoMappingFoundException e) {
969 log.warn(e.getMessage());
970 break;
971 }
972 break;
973 case EXP_OCH_SIGTYPE:
974 try {
975 U8 expOchSigType = match.get(MatchField.EXP_OCH_SIGTYPE);
976 builder.add(matchOchSignalType(lookupOchSignalType((byte) expOchSigType.getValue())));
977 } catch (NoMappingFoundException e) {
978 log.warn(e.getMessage());
979 break;
980 }
981 break;
982 case EXP_ODU_SIG_ID:
983 OduSignalId oduSignalId = OduSignalId.oduSignalId(match.get(MatchField.EXP_ODU_SIG_ID).getTpn(),
984 match.get(MatchField.EXP_ODU_SIG_ID).getTslen(),
985 match.get(MatchField.EXP_ODU_SIG_ID).getTsmap());
986 builder.add(matchOduSignalId(oduSignalId));
Frank Wang5733c382017-03-28 10:15:18 +0800987 break;
Yafit Hadar73514612015-11-04 10:14:21 +0200988 case EXP_ODU_SIGTYPE:
989 try {
990 U8 oduSigType = match.get(MatchField.EXP_ODU_SIGTYPE);
991 builder.add(matchOduSignalType(lookupOduSignalType((byte) oduSigType.getValue())));
992 } catch (NoMappingFoundException e) {
993 log.warn(e.getMessage());
994 break;
995 }
996 break;
Hyunsun Moona08c5d02015-07-14 17:53:00 -0700997 case TUNNEL_ID:
998 long tunnelId = match.get(MatchField.TUNNEL_ID).getValue();
999 builder.matchTunnelId(tunnelId);
1000 break;
BitOhenryb40129a2015-11-30 12:41:18 +08001001 case ARP_OP:
1002 int arpOp = match.get(MatchField.ARP_OP).getOpcode();
1003 builder.matchArpOp(arpOp);
1004 break;
BitOhenrya331b182015-11-23 08:39:37 +08001005 case ARP_SHA:
1006 mac = MacAddress.valueOf(match.get(MatchField.ARP_SHA).getLong());
1007 builder.matchArpSha(mac);
1008 break;
BitOhenry296b4542015-11-24 08:41:58 +08001009 case ARP_SPA:
1010 ip = Ip4Address.valueOf(match.get(MatchField.ARP_SPA).getInt());
1011 builder.matchArpSpa(ip);
1012 break;
BitOhenry76430852015-11-20 19:04:49 +08001013 case ARP_THA:
1014 mac = MacAddress.valueOf(match.get(MatchField.ARP_THA).getLong());
1015 builder.matchArpTha(mac);
1016 break;
BitOhenryc1e5fcc2015-11-23 20:47:53 +08001017 case ARP_TPA:
1018 ip = Ip4Address.valueOf(match.get(MatchField.ARP_TPA).getInt());
1019 builder.matchArpTpa(ip);
1020 break;
Phaneendra Manda8db7d092016-06-04 00:17:24 +05301021 case NSP:
1022 if (selectorInterpreter != null) {
1023 try {
1024 OFOxm oxm = ((OFMatchV3) match).getOxmList().get(MatchField.NSP);
1025 builder.extension(selectorInterpreter.mapOxm(oxm), deviceId);
1026 } catch (UnsupportedOperationException e) {
1027 log.debug(e.getMessage());
1028 }
1029 }
1030 break;
1031 case NSI:
1032 if (selectorInterpreter != null) {
1033 try {
1034 OFOxm oxm = ((OFMatchV3) match).getOxmList().get(MatchField.NSI);
1035 builder.extension(selectorInterpreter.mapOxm(oxm), deviceId);
1036 } catch (UnsupportedOperationException e) {
1037 log.debug(e.getMessage());
1038 }
1039 }
1040 break;
1041 case ENCAP_ETH_TYPE:
1042 if (selectorInterpreter != null) {
1043 try {
1044 OFOxm oxm = ((OFMatchV3) match).getOxmList().get(MatchField.ENCAP_ETH_TYPE);
1045 builder.extension(selectorInterpreter.mapOxm(oxm), deviceId);
1046 } catch (UnsupportedOperationException e) {
1047 log.debug(e.getMessage());
1048 }
1049 }
1050 break;
Frank Wang5733c382017-03-28 10:15:18 +08001051 case CONNTRACK_STATE:
1052 if (selectorInterpreter != null &&
1053 selectorInterpreter.supported(ExtensionSelectorTypes.NICIRA_MATCH_CONNTRACK_STATE.type())) {
1054 try {
1055 OFOxm oxm = ((OFMatchV3) match).getOxmList().get(MatchField.CONNTRACK_STATE);
1056 builder.extension(selectorInterpreter.mapOxm(oxm), deviceId);
1057 } catch (UnsupportedOperationException e) {
1058 log.debug(e.getMessage());
1059 }
1060 }
1061 break;
1062 case CONNTRACK_ZONE:
1063 if (selectorInterpreter != null &&
1064 selectorInterpreter.supported(ExtensionSelectorTypes.NICIRA_MATCH_CONNTRACK_ZONE.type())) {
1065 try {
1066 OFOxm oxm = ((OFMatchV3) match).getOxmList().get(MatchField.CONNTRACK_ZONE);
1067 builder.extension(selectorInterpreter.mapOxm(oxm), deviceId);
1068 } catch (UnsupportedOperationException e) {
1069 log.debug(e.getMessage());
1070 }
1071 }
1072 break;
1073 case CONNTRACK_MARK:
1074 if (selectorInterpreter != null &&
1075 selectorInterpreter.supported(ExtensionSelectorTypes.NICIRA_MATCH_CONNTRACK_MARK.type())) {
1076 try {
1077 OFOxm oxm = ((OFMatchV3) match).getOxmList().get(MatchField.CONNTRACK_MARK);
1078 builder.extension(selectorInterpreter.mapOxm(oxm), deviceId);
1079 } catch (UnsupportedOperationException e) {
1080 log.debug(e.getMessage());
1081 }
1082 }
1083 break;
Pier Ventre6f630052016-10-18 09:58:41 -07001084 case OFDPA_OVID:
1085 if (selectorInterpreter != null &&
1086 selectorInterpreter.supported(ExtensionSelectorTypes.OFDPA_MATCH_OVID.type())) {
Yuta HIGUCHI6512f3e2017-05-18 17:21:24 -07001087 if (isOF13OrLater(match)) {
Pier Ventre6f630052016-10-18 09:58:41 -07001088 OFOxm oxm = ((OFMatchV3) match).getOxmList().get(MatchField.OFDPA_OVID);
1089 builder.extension(selectorInterpreter.mapOxm(oxm),
1090 deviceId);
1091 } else {
1092 break;
1093 }
1094 }
1095 break;
Pier Ventre9cf536b2016-10-21 13:30:18 -07001096 case OFDPA_MPLS_L2_PORT:
1097 if (selectorInterpreter != null &&
1098 selectorInterpreter.supported(ExtensionSelectorTypes.OFDPA_MATCH_MPLS_L2_PORT.type())) {
Yuta HIGUCHI6512f3e2017-05-18 17:21:24 -07001099 if (isOF13OrLater(match)) {
Pier Ventre9cf536b2016-10-21 13:30:18 -07001100 OFOxm oxm = ((OFMatchV3) match).getOxmList().get(MatchField.OFDPA_MPLS_L2_PORT);
1101 builder.extension(selectorInterpreter.mapOxm(oxm),
1102 deviceId);
1103 } else {
1104 break;
1105 }
1106 }
1107 break;
alshabib6b5cfec2014-09-18 17:42:18 -07001108 case MPLS_TC:
alshabib6b5cfec2014-09-18 17:42:18 -07001109 default:
1110 log.warn("Match type {} not yet implemented.", field.id);
alshabib6b5cfec2014-09-18 17:42:18 -07001111 }
1112 }
1113 return builder.build();
1114 }
Jonathan Hart3c259162015-10-21 21:31:19 -07001115
Yuta HIGUCHI6512f3e2017-05-18 17:21:24 -07001116 /**
1117 * @param obj OpenFlow object to test
1118 * @return true if OFObject is OF_13 or later
1119 */
1120 private static boolean isOF13OrLater(OFObject obj) {
1121 return obj.getVersion().wireVersion >= OFVersion.OF_13.wireVersion;
1122 }
1123
Jian Liffef5002016-04-04 23:27:37 -07001124 private DriverHandler getDriver(DeviceId devId) {
1125 Driver driver = driverService.getDriver(devId);
1126 DriverHandler handler = new DefaultDriverHandler(new DefaultDriverData(driver, devId));
Jonathan Hart3c259162015-10-21 21:31:19 -07001127 return handler;
1128 }
alshabib6b5cfec2014-09-18 17:42:18 -07001129}