blob: f9da1c693c92130f60cf6436e7c10d2e5762c665 [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
Ray Milkey34c95902015-04-15 09:47:53 -07002 * Copyright 2014-2015 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 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.provider.of.flow.impl;
Jonathan Hart86e59352014-10-22 10:42:16 -070017
alshabib346b5b32015-03-06 00:42:16 -080018import com.google.common.collect.Lists;
Jonathan Hart29afca32015-01-20 18:17:39 -080019import org.onlab.packet.Ip4Address;
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -080020import org.onlab.packet.Ip6Address;
Sho SHIMIZUe397ffe2015-05-12 16:24:23 -070021import org.onosproject.net.OchSignal;
Charles M.C. Chanfe421812015-01-12 18:20:51 +080022import org.onosproject.net.PortNumber;
Jonathan Hart3c259162015-10-21 21:31:19 -070023import org.onosproject.net.driver.DefaultDriverData;
24import org.onosproject.net.driver.DefaultDriverHandler;
25import org.onosproject.net.driver.Driver;
26import org.onosproject.net.driver.DriverService;
Brian O'Connorabafb502014-12-02 22:26:20 -080027import org.onosproject.net.flow.FlowRule;
28import org.onosproject.net.flow.TrafficTreatment;
alshabib880b6442015-11-23 22:13:04 -080029import org.onosproject.net.flow.instructions.ExtensionTreatment;
Brian O'Connorabafb502014-12-02 22:26:20 -080030import org.onosproject.net.flow.instructions.Instruction;
alshabib9af70072015-02-09 14:34:16 -080031import org.onosproject.net.flow.instructions.Instructions;
sangho8995ac52015-02-04 11:29:03 -080032import org.onosproject.net.flow.instructions.Instructions.GroupInstruction;
alshabib9af70072015-02-09 14:34:16 -080033import org.onosproject.net.flow.instructions.Instructions.OutputInstruction;
Steffen Gebertbbfdaaa2015-09-29 11:01:46 +020034import org.onosproject.net.flow.instructions.Instructions.SetQueueInstruction;
Brian O'Connorabafb502014-12-02 22:26:20 -080035import org.onosproject.net.flow.instructions.L0ModificationInstruction;
36import org.onosproject.net.flow.instructions.L0ModificationInstruction.ModLambdaInstruction;
Sho SHIMIZUe397ffe2015-05-12 16:24:23 -070037import org.onosproject.net.flow.instructions.L0ModificationInstruction.ModOchSignalInstruction;
Brian O'Connorabafb502014-12-02 22:26:20 -080038import org.onosproject.net.flow.instructions.L2ModificationInstruction;
39import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModEtherInstruction;
Saurav Das73a7dd42015-08-19 22:20:31 -070040import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModMplsBosInstruction;
Jonathan Hart29afca32015-01-20 18:17:39 -080041import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModMplsLabelInstruction;
Jonathan Hart3c259162015-10-21 21:31:19 -070042import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModTunnelIdInstruction;
Brian O'Connorabafb502014-12-02 22:26:20 -080043import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModVlanIdInstruction;
44import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModVlanPcpInstruction;
Brian O'Connorabafb502014-12-02 22:26:20 -080045import org.onosproject.net.flow.instructions.L2ModificationInstruction.PushHeaderInstructions;
46import org.onosproject.net.flow.instructions.L3ModificationInstruction;
47import org.onosproject.net.flow.instructions.L3ModificationInstruction.ModIPInstruction;
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -080048import org.onosproject.net.flow.instructions.L3ModificationInstruction.ModIPv6FlowLabelInstruction;
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -070049import org.onosproject.net.flow.instructions.L4ModificationInstruction;
50import org.onosproject.net.flow.instructions.L4ModificationInstruction.ModTransportPortInstruction;
alshabib880b6442015-11-23 22:13:04 -080051import org.onosproject.openflow.controller.ExtensionTreatmentInterpreter;
Jonathan Hart86e59352014-10-22 10:42:16 -070052import org.projectfloodlight.openflow.protocol.OFFactory;
53import org.projectfloodlight.openflow.protocol.OFFlowAdd;
54import org.projectfloodlight.openflow.protocol.OFFlowDelete;
55import org.projectfloodlight.openflow.protocol.OFFlowMod;
56import org.projectfloodlight.openflow.protocol.OFFlowModFlags;
57import org.projectfloodlight.openflow.protocol.action.OFAction;
sangho8995ac52015-02-04 11:29:03 -080058import org.projectfloodlight.openflow.protocol.action.OFActionGroup;
Charles M.C. Chanfe421812015-01-12 18:20:51 +080059import org.projectfloodlight.openflow.protocol.action.OFActionOutput;
Steffen Gebertbbfdaaa2015-09-29 11:01:46 +020060import org.projectfloodlight.openflow.protocol.action.OFActionSetQueue;
alshabib9af70072015-02-09 14:34:16 -080061import org.projectfloodlight.openflow.protocol.instruction.OFInstruction;
Jonathan Hart86e59352014-10-22 10:42:16 -070062import org.projectfloodlight.openflow.protocol.match.Match;
63import org.projectfloodlight.openflow.protocol.oxm.OFOxm;
64import org.projectfloodlight.openflow.types.CircuitSignalID;
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -080065import org.projectfloodlight.openflow.types.EthType;
Jonathan Hart86e59352014-10-22 10:42:16 -070066import org.projectfloodlight.openflow.types.IPv4Address;
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -080067import org.projectfloodlight.openflow.types.IPv6Address;
68import org.projectfloodlight.openflow.types.IPv6FlowLabel;
Jonathan Hart86e59352014-10-22 10:42:16 -070069import org.projectfloodlight.openflow.types.MacAddress;
Saurav Das73a7dd42015-08-19 22:20:31 -070070import org.projectfloodlight.openflow.types.OFBooleanValue;
Jonathan Hart86e59352014-10-22 10:42:16 -070071import org.projectfloodlight.openflow.types.OFBufferId;
sangho8995ac52015-02-04 11:29:03 -080072import org.projectfloodlight.openflow.types.OFGroup;
Jonathan Hart86e59352014-10-22 10:42:16 -070073import org.projectfloodlight.openflow.types.OFPort;
74import org.projectfloodlight.openflow.types.OFVlanVidMatch;
alshabib9af70072015-02-09 14:34:16 -080075import org.projectfloodlight.openflow.types.TableId;
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -070076import org.projectfloodlight.openflow.types.TransportPort;
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -080077import org.projectfloodlight.openflow.types.U32;
Jonathan Hart86e59352014-10-22 10:42:16 -070078import org.projectfloodlight.openflow.types.U64;
79import org.projectfloodlight.openflow.types.VlanPcp;
80import org.slf4j.Logger;
81import org.slf4j.LoggerFactory;
82
Jonathan Hart29afca32015-01-20 18:17:39 -080083import java.util.Collections;
84import java.util.LinkedList;
85import java.util.List;
86import java.util.Optional;
87
Jonathan Hart86e59352014-10-22 10:42:16 -070088/**
89 * Flow mod builder for OpenFlow 1.3+.
90 */
91public class FlowModBuilderVer13 extends FlowModBuilder {
92
alshabib10580802015-02-18 18:30:33 -080093 private final Logger log = LoggerFactory.getLogger(getClass());
Charles M.C. Chanfe421812015-01-12 18:20:51 +080094 private static final int OFPCML_NO_BUFFER = 0xffff;
Jonathan Hart86e59352014-10-22 10:42:16 -070095
96 private final TrafficTreatment treatment;
97
98 /**
99 * Constructor for a flow mod builder for OpenFlow 1.3.
100 *
101 * @param flowRule the flow rule to transform into a flow mod
102 * @param factory the OpenFlow factory to use to build the flow mod
Pavlin Radoslavov119fd5c2014-11-25 19:08:19 -0800103 * @param xid the transaction ID
Charles Chan30ba4002015-11-05 14:45:16 -0800104 * @param driverService the device driver service
Jonathan Hart86e59352014-10-22 10:42:16 -0700105 */
Jonathan Hart3c259162015-10-21 21:31:19 -0700106 protected FlowModBuilderVer13(FlowRule flowRule, OFFactory factory, Optional<Long> xid,
107 Optional<DriverService> driverService) {
108 super(flowRule, factory, xid, driverService);
Jonathan Hart86e59352014-10-22 10:42:16 -0700109
110 this.treatment = flowRule.treatment();
111 }
112
113 @Override
114 public OFFlowAdd buildFlowAdd() {
115 Match match = buildMatch();
alshabib346b5b32015-03-06 00:42:16 -0800116 List<OFAction> deferredActions = buildActions(treatment.deferred());
117 List<OFAction> immediateActions = buildActions(treatment.immediate());
118 List<OFInstruction> instructions = Lists.newLinkedList();
Saurav Dascbe6de32015-03-01 18:30:46 -0800119
Jonathan Hartd4a8bba2014-10-28 12:44:20 -0700120
alshabib346b5b32015-03-06 00:42:16 -0800121 if (treatment.clearedDeferred()) {
122 instructions.add(factory().instructions().clearActions());
123 }
sangho1e575652015-05-14 00:39:53 -0700124 if (immediateActions.size() > 0) {
125 instructions.add(factory().instructions().applyActions(immediateActions));
126 }
alshabib346b5b32015-03-06 00:42:16 -0800127 if (deferredActions.size() > 0) {
128 instructions.add(factory().instructions().writeActions(deferredActions));
129 }
130 if (treatment.tableTransition() != null) {
131 instructions.add(buildTableGoto(treatment.tableTransition()));
Saurav Dascbe6de32015-03-01 18:30:46 -0800132 }
Saurav Das86af8f12015-05-25 23:55:33 -0700133 if (treatment.writeMetadata() != null) {
134 instructions.add(buildMetadata(treatment.writeMetadata()));
135 }
chengfane264bef2015-09-28 08:38:19 +0800136 if (treatment.metered() != null) {
137 instructions.add(buildMeter(treatment.metered()));
138 }
Jonathan Hart86e59352014-10-22 10:42:16 -0700139
140 long cookie = flowRule().id().value();
141
Jonathan Hart86e59352014-10-22 10:42:16 -0700142 OFFlowAdd fm = factory().buildFlowAdd()
Brian O'Connor427a1762014-11-19 18:40:32 -0800143 .setXid(xid)
Jonathan Hart86e59352014-10-22 10:42:16 -0700144 .setCookie(U64.of(cookie))
145 .setBufferId(OFBufferId.NO_BUFFER)
alshabib9af70072015-02-09 14:34:16 -0800146 .setInstructions(instructions)
Jonathan Hart86e59352014-10-22 10:42:16 -0700147 .setMatch(match)
148 .setFlags(Collections.singleton(OFFlowModFlags.SEND_FLOW_REM))
149 .setPriority(flowRule().priority())
alshabib08d98982015-04-21 16:25:50 -0700150 .setTableId(TableId.of(flowRule().tableId()))
Jonathan Hart86e59352014-10-22 10:42:16 -0700151 .build();
152
153 return fm;
154 }
155
156 @Override
157 public OFFlowMod buildFlowMod() {
158 Match match = buildMatch();
alshabib346b5b32015-03-06 00:42:16 -0800159 List<OFAction> deferredActions = buildActions(treatment.deferred());
160 List<OFAction> immediateActions = buildActions(treatment.immediate());
161 List<OFInstruction> instructions = Lists.newLinkedList();
Jonathan Harteda33872015-02-18 10:22:32 -0800162
alshabib346b5b32015-03-06 00:42:16 -0800163
164 if (immediateActions.size() > 0) {
165 instructions.add(factory().instructions().applyActions(immediateActions));
166 }
167 if (treatment.clearedDeferred()) {
168 instructions.add(factory().instructions().clearActions());
169 }
170 if (deferredActions.size() > 0) {
171 instructions.add(factory().instructions().writeActions(deferredActions));
172 }
173 if (treatment.tableTransition() != null) {
174 instructions.add(buildTableGoto(treatment.tableTransition()));
Saurav Dascbe6de32015-03-01 18:30:46 -0800175 }
Saurav Das86af8f12015-05-25 23:55:33 -0700176 if (treatment.writeMetadata() != null) {
177 instructions.add(buildMetadata(treatment.writeMetadata()));
178 }
alshabib10c810b2015-08-18 16:59:04 -0700179 if (treatment.metered() != null) {
180 instructions.add(buildMeter(treatment.metered()));
181 }
Jonathan Hart86e59352014-10-22 10:42:16 -0700182
183 long cookie = flowRule().id().value();
184
Jonathan Hart86e59352014-10-22 10:42:16 -0700185 OFFlowMod fm = factory().buildFlowModify()
Brian O'Connor427a1762014-11-19 18:40:32 -0800186 .setXid(xid)
Jonathan Hart86e59352014-10-22 10:42:16 -0700187 .setCookie(U64.of(cookie))
188 .setBufferId(OFBufferId.NO_BUFFER)
alshabib9af70072015-02-09 14:34:16 -0800189 .setInstructions(instructions)
Jonathan Hart86e59352014-10-22 10:42:16 -0700190 .setMatch(match)
191 .setFlags(Collections.singleton(OFFlowModFlags.SEND_FLOW_REM))
192 .setPriority(flowRule().priority())
alshabib08d98982015-04-21 16:25:50 -0700193 .setTableId(TableId.of(flowRule().tableId()))
Jonathan Hart86e59352014-10-22 10:42:16 -0700194 .build();
195
196 return fm;
197 }
198
199 @Override
200 public OFFlowDelete buildFlowDel() {
201 Match match = buildMatch();
Jonathan Hart86e59352014-10-22 10:42:16 -0700202
203 long cookie = flowRule().id().value();
204
205 OFFlowDelete fm = factory().buildFlowDelete()
Brian O'Connor427a1762014-11-19 18:40:32 -0800206 .setXid(xid)
Jonathan Hart86e59352014-10-22 10:42:16 -0700207 .setCookie(U64.of(cookie))
208 .setBufferId(OFBufferId.NO_BUFFER)
Jonathan Hart86e59352014-10-22 10:42:16 -0700209 .setMatch(match)
210 .setFlags(Collections.singleton(OFFlowModFlags.SEND_FLOW_REM))
211 .setPriority(flowRule().priority())
alshabib08d98982015-04-21 16:25:50 -0700212 .setTableId(TableId.of(flowRule().tableId()))
Jonathan Hart86e59352014-10-22 10:42:16 -0700213 .build();
214
215 return fm;
216 }
217
alshabib346b5b32015-03-06 00:42:16 -0800218 private List<OFAction> buildActions(List<Instruction> treatments) {
Jonathan Hart86e59352014-10-22 10:42:16 -0700219 if (treatment == null) {
Sho SHIMIZU6601dd72015-05-11 19:00:29 -0700220 return Collections.emptyList();
Jonathan Hart86e59352014-10-22 10:42:16 -0700221 }
Sho SHIMIZU6601dd72015-05-11 19:00:29 -0700222
223 boolean tableFound = false;
224 List<OFAction> actions = new LinkedList<>();
alshabib346b5b32015-03-06 00:42:16 -0800225 for (Instruction i : treatments) {
Jonathan Hart86e59352014-10-22 10:42:16 -0700226 switch (i.type()) {
alshabib9af70072015-02-09 14:34:16 -0800227 case DROP:
Charles Chan7efabeb2015-09-28 15:12:19 -0700228 case NOACTION:
Sho SHIMIZUb35ed362015-04-28 11:05:19 -0700229 return Collections.emptyList();
alshabib9af70072015-02-09 14:34:16 -0800230 case L0MODIFICATION:
231 actions.add(buildL0Modification(i));
232 break;
233 case L2MODIFICATION:
234 actions.add(buildL2Modification(i));
235 break;
236 case L3MODIFICATION:
237 actions.add(buildL3Modification(i));
238 break;
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700239 case L4MODIFICATION:
240 actions.add(buildL4Modification(i));
241 break;
alshabib9af70072015-02-09 14:34:16 -0800242 case OUTPUT:
243 OutputInstruction out = (OutputInstruction) i;
244 OFActionOutput.Builder action = factory().actions().buildOutput()
245 .setPort(OFPort.of((int) out.port().toLong()));
246 if (out.port().equals(PortNumber.CONTROLLER)) {
247 action.setMaxLen(OFPCML_NO_BUFFER);
248 }
249 actions.add(action.build());
250 break;
251 case GROUP:
252 GroupInstruction group = (GroupInstruction) i;
253 OFActionGroup.Builder groupBuilder = factory().actions().buildGroup()
254 .setGroup(OFGroup.of(group.groupId().id()));
255 actions.add(groupBuilder.build());
256 break;
Steffen Gebertbbfdaaa2015-09-29 11:01:46 +0200257 case QUEUE:
258 SetQueueInstruction queue = (SetQueueInstruction) i;
259 OFActionSetQueue.Builder queueBuilder = factory().actions().buildSetQueue()
260 .setQueueId(queue.queueId());
261 actions.add(queueBuilder.build());
262 break;
alshabib10580802015-02-18 18:30:33 -0800263 case TABLE:
264 //FIXME: should not occur here.
Saurav Dascbe6de32015-03-01 18:30:46 -0800265 tableFound = true;
alshabib10580802015-02-18 18:30:33 -0800266 break;
Jonathan Hart3c259162015-10-21 21:31:19 -0700267 case EXTENSION:
268 actions.add(buildExtensionAction(((Instructions.ExtensionInstructionWrapper) i)
269 .extensionInstruction()));
270 break;
alshabib9af70072015-02-09 14:34:16 -0800271 default:
272 log.warn("Instruction type {} not yet implemented.", i.type());
Jonathan Hart86e59352014-10-22 10:42:16 -0700273 }
274 }
Saurav Dascbe6de32015-03-01 18:30:46 -0800275 if (tableFound && actions.isEmpty()) {
276 // handles the case where there are no actions, but there is
277 // a goto instruction for the next table
Sho SHIMIZU6601dd72015-05-11 19:00:29 -0700278 return Collections.emptyList();
Saurav Dascbe6de32015-03-01 18:30:46 -0800279 }
Jonathan Hart86e59352014-10-22 10:42:16 -0700280 return actions;
281 }
282
alshabib9af70072015-02-09 14:34:16 -0800283 private OFInstruction buildTableGoto(Instructions.TableTypeTransition i) {
284 OFInstruction instruction = factory().instructions().gotoTable(
alshabibd17abc22015-04-21 18:26:35 -0700285 TableId.of(i.tableId()));
alshabib9af70072015-02-09 14:34:16 -0800286 return instruction;
287 }
288
Saurav Das86af8f12015-05-25 23:55:33 -0700289 private OFInstruction buildMetadata(Instructions.MetadataInstruction m) {
290 OFInstruction instruction = factory().instructions().writeMetadata(
291 U64.of(m.metadata()), U64.of(m.metadataMask()));
292 return instruction;
293 }
294
alshabib10c810b2015-08-18 16:59:04 -0700295 private OFInstruction buildMeter(Instructions.MeterInstruction metered) {
296 return factory().instructions().meter(metered.meterId().id());
297 }
298
299
Jonathan Hart86e59352014-10-22 10:42:16 -0700300 private OFAction buildL0Modification(Instruction i) {
301 L0ModificationInstruction l0m = (L0ModificationInstruction) i;
302 switch (l0m.subtype()) {
Sho SHIMIZU5cd7ce92015-05-11 19:10:38 -0700303 case LAMBDA:
Sho SHIMIZUe397ffe2015-05-12 16:24:23 -0700304 return buildModLambdaInstruction((ModLambdaInstruction) i);
305 case OCH:
306 try {
307 return buildModOchSignalInstruction((ModOchSignalInstruction) i);
Sho SHIMIZUc17042d2015-05-28 12:07:23 -0700308 } catch (NoMappingFoundException e) {
Sho SHIMIZUe397ffe2015-05-12 16:24:23 -0700309 log.warn(e.getMessage());
310 break;
311 }
Sho SHIMIZU5cd7ce92015-05-11 19:10:38 -0700312 default:
313 log.warn("Unimplemented action type {}.", l0m.subtype());
314 break;
Jonathan Hart86e59352014-10-22 10:42:16 -0700315 }
316 return null;
317 }
318
Sho SHIMIZUe397ffe2015-05-12 16:24:23 -0700319 private OFAction buildModLambdaInstruction(ModLambdaInstruction instruction) {
Marc De Leenheerc2999f32015-11-20 15:52:16 -0800320 return factory().actions().circuit(factory().oxms().expOchSigId(
Sho SHIMIZUe397ffe2015-05-12 16:24:23 -0700321 new CircuitSignalID((byte) 1, (byte) 2, instruction.lambda(), (short) 1)));
322 }
323
324 private OFAction buildModOchSignalInstruction(ModOchSignalInstruction instruction) {
325 OchSignal signal = instruction.lambda();
Sho SHIMIZUc17042d2015-05-28 12:07:23 -0700326 byte gridType = OpenFlowValueMapper.lookupGridType(signal.gridType());
327 byte channelSpacing = OpenFlowValueMapper.lookupChannelSpacing(signal.channelSpacing());
Sho SHIMIZUe397ffe2015-05-12 16:24:23 -0700328
Marc De Leenheerc2999f32015-11-20 15:52:16 -0800329 return factory().actions().circuit(factory().oxms().expOchSigId(
Sho SHIMIZUe397ffe2015-05-12 16:24:23 -0700330 new CircuitSignalID(gridType, channelSpacing,
331 (short) signal.spacingMultiplier(), (short) signal.slotGranularity())
332 ));
333 }
334
Jonathan Hart86e59352014-10-22 10:42:16 -0700335 private OFAction buildL2Modification(Instruction i) {
336 L2ModificationInstruction l2m = (L2ModificationInstruction) i;
337 ModEtherInstruction eth;
338 OFOxm<?> oxm = null;
339 switch (l2m.subtype()) {
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800340 case ETH_DST:
341 eth = (ModEtherInstruction) l2m;
342 oxm = factory().oxms().ethDst(MacAddress.of(eth.mac().toLong()));
343 break;
344 case ETH_SRC:
345 eth = (ModEtherInstruction) l2m;
346 oxm = factory().oxms().ethSrc(MacAddress.of(eth.mac().toLong()));
347 break;
348 case VLAN_ID:
349 ModVlanIdInstruction vlanId = (ModVlanIdInstruction) l2m;
350 oxm = factory().oxms().vlanVid(OFVlanVidMatch.ofVlan(vlanId.vlanId().toShort()));
351 break;
352 case VLAN_PCP:
353 ModVlanPcpInstruction vlanPcp = (ModVlanPcpInstruction) l2m;
354 oxm = factory().oxms().vlanPcp(VlanPcp.of(vlanPcp.vlanPcp()));
355 break;
356 case MPLS_PUSH:
357 PushHeaderInstructions pushHeaderInstructions =
358 (PushHeaderInstructions) l2m;
359 return factory().actions().pushMpls(EthType.of(pushHeaderInstructions
alshabib7b808c52015-06-26 14:22:24 -0700360 .ethernetType().toShort()));
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800361 case MPLS_POP:
Jonathan Hart67fc0972015-03-19 15:21:20 -0700362 PushHeaderInstructions popHeaderInstructions =
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800363 (PushHeaderInstructions) l2m;
364 return factory().actions().popMpls(EthType.of(popHeaderInstructions
alshabib7b808c52015-06-26 14:22:24 -0700365 .ethernetType().toShort()));
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800366 case MPLS_LABEL:
367 ModMplsLabelInstruction mplsLabel =
368 (ModMplsLabelInstruction) l2m;
HIGUCHI Yuta04b49fc2015-08-28 09:58:58 -0700369 oxm = factory().oxms().mplsLabel(U32.of(mplsLabel.mplsLabel().toInt()));
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800370 break;
Saurav Das73a7dd42015-08-19 22:20:31 -0700371 case MPLS_BOS:
372 ModMplsBosInstruction mplsBos = (ModMplsBosInstruction) l2m;
373 oxm = factory().oxms()
374 .mplsBos(mplsBos.mplsBos() ? OFBooleanValue.TRUE
375 : OFBooleanValue.FALSE);
376 break;
sangho3f97a17d2015-01-29 22:56:29 -0800377 case DEC_MPLS_TTL:
378 return factory().actions().decMplsTtl();
Saurav Dasfbe25c52015-03-04 11:12:00 -0800379 case VLAN_POP:
380 return factory().actions().popVlan();
Jonathan Hart54b406b2015-03-06 16:24:14 -0800381 case VLAN_PUSH:
382 PushHeaderInstructions pushVlanInstruction = (PushHeaderInstructions) l2m;
383 return factory().actions().pushVlan(
alshabib7b808c52015-06-26 14:22:24 -0700384 EthType.of(pushVlanInstruction.ethernetType().toShort()));
Hyunsun Moona08c5d02015-07-14 17:53:00 -0700385 case TUNNEL_ID:
386 ModTunnelIdInstruction tunnelId = (ModTunnelIdInstruction) l2m;
387 oxm = factory().oxms().tunnelId(U64.of(tunnelId.tunnelId()));
388 break;
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800389 default:
390 log.warn("Unimplemented action type {}.", l2m.subtype());
391 break;
Jonathan Hart86e59352014-10-22 10:42:16 -0700392 }
393
394 if (oxm != null) {
395 return factory().actions().buildSetField().setField(oxm).build();
396 }
397 return null;
398 }
399
400 private OFAction buildL3Modification(Instruction i) {
401 L3ModificationInstruction l3m = (L3ModificationInstruction) i;
402 ModIPInstruction ip;
Pavlin Radoslavov23e398d2014-11-05 15:17:57 -0800403 Ip4Address ip4;
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800404 Ip6Address ip6;
Jonathan Hart86e59352014-10-22 10:42:16 -0700405 OFOxm<?> oxm = null;
406 switch (l3m.subtype()) {
Sho SHIMIZU5cd7ce92015-05-11 19:10:38 -0700407 case IPV4_SRC:
408 ip = (ModIPInstruction) i;
409 ip4 = ip.ip().getIp4Address();
410 oxm = factory().oxms().ipv4Src(IPv4Address.of(ip4.toInt()));
411 break;
412 case IPV4_DST:
413 ip = (ModIPInstruction) i;
414 ip4 = ip.ip().getIp4Address();
415 oxm = factory().oxms().ipv4Dst(IPv4Address.of(ip4.toInt()));
416 break;
417 case IPV6_SRC:
418 ip = (ModIPInstruction) i;
419 ip6 = ip.ip().getIp6Address();
420 oxm = factory().oxms().ipv6Src(IPv6Address.of(ip6.toOctets()));
421 break;
422 case IPV6_DST:
423 ip = (ModIPInstruction) i;
424 ip6 = ip.ip().getIp6Address();
425 oxm = factory().oxms().ipv6Dst(IPv6Address.of(ip6.toOctets()));
426 break;
427 case IPV6_FLABEL:
428 ModIPv6FlowLabelInstruction flowLabelInstruction =
429 (ModIPv6FlowLabelInstruction) i;
430 int flowLabel = flowLabelInstruction.flowLabel();
431 oxm = factory().oxms().ipv6Flabel(IPv6FlowLabel.of(flowLabel));
432 break;
433 case DEC_TTL:
434 return factory().actions().decNwTtl();
435 case TTL_IN:
436 return factory().actions().copyTtlIn();
437 case TTL_OUT:
438 return factory().actions().copyTtlOut();
439 default:
440 log.warn("Unimplemented action type {}.", l3m.subtype());
441 break;
Jonathan Hart86e59352014-10-22 10:42:16 -0700442 }
443
444 if (oxm != null) {
445 return factory().actions().buildSetField().setField(oxm).build();
446 }
447 return null;
448 }
449
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700450 private OFAction buildL4Modification(Instruction i) {
451 L4ModificationInstruction l4m = (L4ModificationInstruction) i;
452 ModTransportPortInstruction tp;
453 OFOxm<?> oxm = null;
454 switch (l4m.subtype()) {
455 case TCP_SRC:
456 tp = (ModTransportPortInstruction) l4m;
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700457 oxm = factory().oxms().tcpSrc(TransportPort.of(tp.port().toInt()));
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700458 break;
459 case TCP_DST:
460 tp = (ModTransportPortInstruction) l4m;
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700461 oxm = factory().oxms().tcpDst(TransportPort.of(tp.port().toInt()));
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700462 break;
463 case UDP_SRC:
464 tp = (ModTransportPortInstruction) l4m;
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700465 oxm = factory().oxms().udpSrc(TransportPort.of(tp.port().toInt()));
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700466 break;
467 case UDP_DST:
468 tp = (ModTransportPortInstruction) l4m;
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700469 oxm = factory().oxms().udpDst(TransportPort.of(tp.port().toInt()));
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700470 break;
471 default:
472 log.warn("Unimplemented action type {}.", l4m.subtype());
473 break;
474 }
475
476 if (oxm != null) {
477 return factory().actions().buildSetField().setField(oxm).build();
478 }
479 return null;
480 }
481
alshabib880b6442015-11-23 22:13:04 -0800482 private OFAction buildExtensionAction(ExtensionTreatment i) {
Jonathan Hart3c259162015-10-21 21:31:19 -0700483 if (!driverService.isPresent()) {
484 log.error("No driver service present");
485 return null;
486 }
487 Driver driver = driverService.get().getDriver(deviceId);
alshabib880b6442015-11-23 22:13:04 -0800488 if (driver.hasBehaviour(ExtensionTreatmentInterpreter.class)) {
Jonathan Hart3c259162015-10-21 21:31:19 -0700489 DefaultDriverHandler handler =
490 new DefaultDriverHandler(new DefaultDriverData(driver, deviceId));
alshabib880b6442015-11-23 22:13:04 -0800491 ExtensionTreatmentInterpreter interpreter = handler.behaviour(ExtensionTreatmentInterpreter.class);
Jonathan Hart3c259162015-10-21 21:31:19 -0700492 return interpreter.mapInstruction(factory(), i);
493 }
494
495 return null;
496 }
497
Jonathan Hart86e59352014-10-22 10:42:16 -0700498}