blob: e2fc30da0c40f59c77d50efc31b60f6fd2ca5215 [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;
Jonathan Hart3c259162015-10-21 21:31:19 -070021import org.onosproject.net.DeviceId;
Sho SHIMIZUe397ffe2015-05-12 16:24:23 -070022import org.onosproject.net.OchSignal;
Charles M.C. Chanfe421812015-01-12 18:20:51 +080023import org.onosproject.net.PortNumber;
Jonathan Hart3c259162015-10-21 21:31:19 -070024import org.onosproject.net.driver.DefaultDriverData;
25import org.onosproject.net.driver.DefaultDriverHandler;
26import org.onosproject.net.driver.Driver;
27import org.onosproject.net.driver.DriverService;
Brian O'Connorabafb502014-12-02 22:26:20 -080028import org.onosproject.net.flow.FlowRule;
29import org.onosproject.net.flow.TrafficTreatment;
Jonathan Hart3c259162015-10-21 21:31:19 -070030import org.onosproject.net.flow.instructions.ExtensionInstruction;
Brian O'Connorabafb502014-12-02 22:26:20 -080031import org.onosproject.net.flow.instructions.Instruction;
alshabib9af70072015-02-09 14:34:16 -080032import org.onosproject.net.flow.instructions.Instructions;
sangho8995ac52015-02-04 11:29:03 -080033import org.onosproject.net.flow.instructions.Instructions.GroupInstruction;
alshabib9af70072015-02-09 14:34:16 -080034import org.onosproject.net.flow.instructions.Instructions.OutputInstruction;
Steffen Gebertbbfdaaa2015-09-29 11:01:46 +020035import org.onosproject.net.flow.instructions.Instructions.SetQueueInstruction;
Brian O'Connorabafb502014-12-02 22:26:20 -080036import org.onosproject.net.flow.instructions.L0ModificationInstruction;
37import org.onosproject.net.flow.instructions.L0ModificationInstruction.ModLambdaInstruction;
Sho SHIMIZUe397ffe2015-05-12 16:24:23 -070038import org.onosproject.net.flow.instructions.L0ModificationInstruction.ModOchSignalInstruction;
Brian O'Connorabafb502014-12-02 22:26:20 -080039import org.onosproject.net.flow.instructions.L2ModificationInstruction;
40import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModEtherInstruction;
Saurav Das73a7dd42015-08-19 22:20:31 -070041import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModMplsBosInstruction;
Jonathan Hart29afca32015-01-20 18:17:39 -080042import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModMplsLabelInstruction;
Jonathan Hart3c259162015-10-21 21:31:19 -070043import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModTunnelIdInstruction;
Brian O'Connorabafb502014-12-02 22:26:20 -080044import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModVlanIdInstruction;
45import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModVlanPcpInstruction;
Brian O'Connorabafb502014-12-02 22:26:20 -080046import org.onosproject.net.flow.instructions.L2ModificationInstruction.PushHeaderInstructions;
47import org.onosproject.net.flow.instructions.L3ModificationInstruction;
48import org.onosproject.net.flow.instructions.L3ModificationInstruction.ModIPInstruction;
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -080049import org.onosproject.net.flow.instructions.L3ModificationInstruction.ModIPv6FlowLabelInstruction;
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -070050import org.onosproject.net.flow.instructions.L4ModificationInstruction;
51import org.onosproject.net.flow.instructions.L4ModificationInstruction.ModTransportPortInstruction;
Jonathan Hart3c259162015-10-21 21:31:19 -070052import org.onosproject.openflow.controller.ExtensionInterpreter;
Jonathan Hart86e59352014-10-22 10:42:16 -070053import org.projectfloodlight.openflow.protocol.OFFactory;
54import org.projectfloodlight.openflow.protocol.OFFlowAdd;
55import org.projectfloodlight.openflow.protocol.OFFlowDelete;
56import org.projectfloodlight.openflow.protocol.OFFlowMod;
57import org.projectfloodlight.openflow.protocol.OFFlowModFlags;
58import org.projectfloodlight.openflow.protocol.action.OFAction;
sangho8995ac52015-02-04 11:29:03 -080059import org.projectfloodlight.openflow.protocol.action.OFActionGroup;
Charles M.C. Chanfe421812015-01-12 18:20:51 +080060import org.projectfloodlight.openflow.protocol.action.OFActionOutput;
Steffen Gebertbbfdaaa2015-09-29 11:01:46 +020061import org.projectfloodlight.openflow.protocol.action.OFActionSetQueue;
alshabib9af70072015-02-09 14:34:16 -080062import org.projectfloodlight.openflow.protocol.instruction.OFInstruction;
Jonathan Hart86e59352014-10-22 10:42:16 -070063import org.projectfloodlight.openflow.protocol.match.Match;
64import org.projectfloodlight.openflow.protocol.oxm.OFOxm;
65import org.projectfloodlight.openflow.types.CircuitSignalID;
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -080066import org.projectfloodlight.openflow.types.EthType;
Jonathan Hart86e59352014-10-22 10:42:16 -070067import org.projectfloodlight.openflow.types.IPv4Address;
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -080068import org.projectfloodlight.openflow.types.IPv6Address;
69import org.projectfloodlight.openflow.types.IPv6FlowLabel;
Jonathan Hart86e59352014-10-22 10:42:16 -070070import org.projectfloodlight.openflow.types.MacAddress;
Saurav Das73a7dd42015-08-19 22:20:31 -070071import org.projectfloodlight.openflow.types.OFBooleanValue;
Jonathan Hart86e59352014-10-22 10:42:16 -070072import org.projectfloodlight.openflow.types.OFBufferId;
sangho8995ac52015-02-04 11:29:03 -080073import org.projectfloodlight.openflow.types.OFGroup;
Jonathan Hart86e59352014-10-22 10:42:16 -070074import org.projectfloodlight.openflow.types.OFPort;
75import org.projectfloodlight.openflow.types.OFVlanVidMatch;
alshabib9af70072015-02-09 14:34:16 -080076import org.projectfloodlight.openflow.types.TableId;
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -070077import org.projectfloodlight.openflow.types.TransportPort;
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -080078import org.projectfloodlight.openflow.types.U32;
Jonathan Hart86e59352014-10-22 10:42:16 -070079import org.projectfloodlight.openflow.types.U64;
80import org.projectfloodlight.openflow.types.VlanPcp;
81import org.slf4j.Logger;
82import org.slf4j.LoggerFactory;
83
Jonathan Hart29afca32015-01-20 18:17:39 -080084import java.util.Collections;
85import java.util.LinkedList;
86import java.util.List;
87import java.util.Optional;
88
Jonathan Hart86e59352014-10-22 10:42:16 -070089/**
90 * Flow mod builder for OpenFlow 1.3+.
91 */
92public class FlowModBuilderVer13 extends FlowModBuilder {
93
alshabib10580802015-02-18 18:30:33 -080094 private final Logger log = LoggerFactory.getLogger(getClass());
Charles M.C. Chanfe421812015-01-12 18:20:51 +080095 private static final int OFPCML_NO_BUFFER = 0xffff;
Jonathan Hart86e59352014-10-22 10:42:16 -070096
97 private final TrafficTreatment treatment;
Jonathan Hart3c259162015-10-21 21:31:19 -070098 private final DeviceId deviceId;
Jonathan Hart86e59352014-10-22 10:42:16 -070099
100 /**
101 * Constructor for a flow mod builder for OpenFlow 1.3.
102 *
103 * @param flowRule the flow rule to transform into a flow mod
104 * @param factory the OpenFlow factory to use to build the flow mod
Pavlin Radoslavov119fd5c2014-11-25 19:08:19 -0800105 * @param xid the transaction ID
Charles Chan30ba4002015-11-05 14:45:16 -0800106 * @param driverService the device driver service
Jonathan Hart86e59352014-10-22 10:42:16 -0700107 */
Jonathan Hart3c259162015-10-21 21:31:19 -0700108 protected FlowModBuilderVer13(FlowRule flowRule, OFFactory factory, Optional<Long> xid,
109 Optional<DriverService> driverService) {
110 super(flowRule, factory, xid, driverService);
Jonathan Hart86e59352014-10-22 10:42:16 -0700111
112 this.treatment = flowRule.treatment();
Jonathan Hart3c259162015-10-21 21:31:19 -0700113 this.deviceId = flowRule.deviceId();
Jonathan Hart86e59352014-10-22 10:42:16 -0700114 }
115
116 @Override
117 public OFFlowAdd buildFlowAdd() {
118 Match match = buildMatch();
alshabib346b5b32015-03-06 00:42:16 -0800119 List<OFAction> deferredActions = buildActions(treatment.deferred());
120 List<OFAction> immediateActions = buildActions(treatment.immediate());
121 List<OFInstruction> instructions = Lists.newLinkedList();
Saurav Dascbe6de32015-03-01 18:30:46 -0800122
Jonathan Hartd4a8bba2014-10-28 12:44:20 -0700123
alshabib346b5b32015-03-06 00:42:16 -0800124 if (treatment.clearedDeferred()) {
125 instructions.add(factory().instructions().clearActions());
126 }
sangho1e575652015-05-14 00:39:53 -0700127 if (immediateActions.size() > 0) {
128 instructions.add(factory().instructions().applyActions(immediateActions));
129 }
alshabib346b5b32015-03-06 00:42:16 -0800130 if (deferredActions.size() > 0) {
131 instructions.add(factory().instructions().writeActions(deferredActions));
132 }
133 if (treatment.tableTransition() != null) {
134 instructions.add(buildTableGoto(treatment.tableTransition()));
Saurav Dascbe6de32015-03-01 18:30:46 -0800135 }
Saurav Das86af8f12015-05-25 23:55:33 -0700136 if (treatment.writeMetadata() != null) {
137 instructions.add(buildMetadata(treatment.writeMetadata()));
138 }
chengfane264bef2015-09-28 08:38:19 +0800139 if (treatment.metered() != null) {
140 instructions.add(buildMeter(treatment.metered()));
141 }
Jonathan Hart86e59352014-10-22 10:42:16 -0700142
143 long cookie = flowRule().id().value();
144
Jonathan Hart86e59352014-10-22 10:42:16 -0700145 OFFlowAdd fm = factory().buildFlowAdd()
Brian O'Connor427a1762014-11-19 18:40:32 -0800146 .setXid(xid)
Jonathan Hart86e59352014-10-22 10:42:16 -0700147 .setCookie(U64.of(cookie))
148 .setBufferId(OFBufferId.NO_BUFFER)
alshabib9af70072015-02-09 14:34:16 -0800149 .setInstructions(instructions)
Jonathan Hart86e59352014-10-22 10:42:16 -0700150 .setMatch(match)
151 .setFlags(Collections.singleton(OFFlowModFlags.SEND_FLOW_REM))
152 .setPriority(flowRule().priority())
alshabib08d98982015-04-21 16:25:50 -0700153 .setTableId(TableId.of(flowRule().tableId()))
Jonathan Hart86e59352014-10-22 10:42:16 -0700154 .build();
155
156 return fm;
157 }
158
159 @Override
160 public OFFlowMod buildFlowMod() {
161 Match match = buildMatch();
alshabib346b5b32015-03-06 00:42:16 -0800162 List<OFAction> deferredActions = buildActions(treatment.deferred());
163 List<OFAction> immediateActions = buildActions(treatment.immediate());
164 List<OFInstruction> instructions = Lists.newLinkedList();
Jonathan Harteda33872015-02-18 10:22:32 -0800165
alshabib346b5b32015-03-06 00:42:16 -0800166
167 if (immediateActions.size() > 0) {
168 instructions.add(factory().instructions().applyActions(immediateActions));
169 }
170 if (treatment.clearedDeferred()) {
171 instructions.add(factory().instructions().clearActions());
172 }
173 if (deferredActions.size() > 0) {
174 instructions.add(factory().instructions().writeActions(deferredActions));
175 }
176 if (treatment.tableTransition() != null) {
177 instructions.add(buildTableGoto(treatment.tableTransition()));
Saurav Dascbe6de32015-03-01 18:30:46 -0800178 }
Saurav Das86af8f12015-05-25 23:55:33 -0700179 if (treatment.writeMetadata() != null) {
180 instructions.add(buildMetadata(treatment.writeMetadata()));
181 }
alshabib10c810b2015-08-18 16:59:04 -0700182 if (treatment.metered() != null) {
183 instructions.add(buildMeter(treatment.metered()));
184 }
Jonathan Hart86e59352014-10-22 10:42:16 -0700185
186 long cookie = flowRule().id().value();
187
Jonathan Hart86e59352014-10-22 10:42:16 -0700188 OFFlowMod fm = factory().buildFlowModify()
Brian O'Connor427a1762014-11-19 18:40:32 -0800189 .setXid(xid)
Jonathan Hart86e59352014-10-22 10:42:16 -0700190 .setCookie(U64.of(cookie))
191 .setBufferId(OFBufferId.NO_BUFFER)
alshabib9af70072015-02-09 14:34:16 -0800192 .setInstructions(instructions)
Jonathan Hart86e59352014-10-22 10:42:16 -0700193 .setMatch(match)
194 .setFlags(Collections.singleton(OFFlowModFlags.SEND_FLOW_REM))
195 .setPriority(flowRule().priority())
alshabib08d98982015-04-21 16:25:50 -0700196 .setTableId(TableId.of(flowRule().tableId()))
Jonathan Hart86e59352014-10-22 10:42:16 -0700197 .build();
198
199 return fm;
200 }
201
202 @Override
203 public OFFlowDelete buildFlowDel() {
204 Match match = buildMatch();
Jonathan Hart86e59352014-10-22 10:42:16 -0700205
206 long cookie = flowRule().id().value();
207
208 OFFlowDelete fm = factory().buildFlowDelete()
Brian O'Connor427a1762014-11-19 18:40:32 -0800209 .setXid(xid)
Jonathan Hart86e59352014-10-22 10:42:16 -0700210 .setCookie(U64.of(cookie))
211 .setBufferId(OFBufferId.NO_BUFFER)
Jonathan Hart86e59352014-10-22 10:42:16 -0700212 .setMatch(match)
213 .setFlags(Collections.singleton(OFFlowModFlags.SEND_FLOW_REM))
214 .setPriority(flowRule().priority())
alshabib08d98982015-04-21 16:25:50 -0700215 .setTableId(TableId.of(flowRule().tableId()))
Jonathan Hart86e59352014-10-22 10:42:16 -0700216 .build();
217
218 return fm;
219 }
220
alshabib346b5b32015-03-06 00:42:16 -0800221 private List<OFAction> buildActions(List<Instruction> treatments) {
Jonathan Hart86e59352014-10-22 10:42:16 -0700222 if (treatment == null) {
Sho SHIMIZU6601dd72015-05-11 19:00:29 -0700223 return Collections.emptyList();
Jonathan Hart86e59352014-10-22 10:42:16 -0700224 }
Sho SHIMIZU6601dd72015-05-11 19:00:29 -0700225
226 boolean tableFound = false;
227 List<OFAction> actions = new LinkedList<>();
alshabib346b5b32015-03-06 00:42:16 -0800228 for (Instruction i : treatments) {
Jonathan Hart86e59352014-10-22 10:42:16 -0700229 switch (i.type()) {
alshabib9af70072015-02-09 14:34:16 -0800230 case DROP:
Charles Chan7efabeb2015-09-28 15:12:19 -0700231 case NOACTION:
Sho SHIMIZUb35ed362015-04-28 11:05:19 -0700232 return Collections.emptyList();
alshabib9af70072015-02-09 14:34:16 -0800233 case L0MODIFICATION:
234 actions.add(buildL0Modification(i));
235 break;
236 case L2MODIFICATION:
237 actions.add(buildL2Modification(i));
238 break;
239 case L3MODIFICATION:
240 actions.add(buildL3Modification(i));
241 break;
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700242 case L4MODIFICATION:
243 actions.add(buildL4Modification(i));
244 break;
alshabib9af70072015-02-09 14:34:16 -0800245 case OUTPUT:
246 OutputInstruction out = (OutputInstruction) i;
247 OFActionOutput.Builder action = factory().actions().buildOutput()
248 .setPort(OFPort.of((int) out.port().toLong()));
249 if (out.port().equals(PortNumber.CONTROLLER)) {
250 action.setMaxLen(OFPCML_NO_BUFFER);
251 }
252 actions.add(action.build());
253 break;
254 case GROUP:
255 GroupInstruction group = (GroupInstruction) i;
256 OFActionGroup.Builder groupBuilder = factory().actions().buildGroup()
257 .setGroup(OFGroup.of(group.groupId().id()));
258 actions.add(groupBuilder.build());
259 break;
Steffen Gebertbbfdaaa2015-09-29 11:01:46 +0200260 case QUEUE:
261 SetQueueInstruction queue = (SetQueueInstruction) i;
262 OFActionSetQueue.Builder queueBuilder = factory().actions().buildSetQueue()
263 .setQueueId(queue.queueId());
264 actions.add(queueBuilder.build());
265 break;
alshabib10580802015-02-18 18:30:33 -0800266 case TABLE:
267 //FIXME: should not occur here.
Saurav Dascbe6de32015-03-01 18:30:46 -0800268 tableFound = true;
alshabib10580802015-02-18 18:30:33 -0800269 break;
Jonathan Hart3c259162015-10-21 21:31:19 -0700270 case EXTENSION:
271 actions.add(buildExtensionAction(((Instructions.ExtensionInstructionWrapper) i)
272 .extensionInstruction()));
273 break;
alshabib9af70072015-02-09 14:34:16 -0800274 default:
275 log.warn("Instruction type {} not yet implemented.", i.type());
Jonathan Hart86e59352014-10-22 10:42:16 -0700276 }
277 }
Saurav Dascbe6de32015-03-01 18:30:46 -0800278 if (tableFound && actions.isEmpty()) {
279 // handles the case where there are no actions, but there is
280 // a goto instruction for the next table
Sho SHIMIZU6601dd72015-05-11 19:00:29 -0700281 return Collections.emptyList();
Saurav Dascbe6de32015-03-01 18:30:46 -0800282 }
Jonathan Hart86e59352014-10-22 10:42:16 -0700283 return actions;
284 }
285
alshabib9af70072015-02-09 14:34:16 -0800286 private OFInstruction buildTableGoto(Instructions.TableTypeTransition i) {
287 OFInstruction instruction = factory().instructions().gotoTable(
alshabibd17abc22015-04-21 18:26:35 -0700288 TableId.of(i.tableId()));
alshabib9af70072015-02-09 14:34:16 -0800289 return instruction;
290 }
291
Saurav Das86af8f12015-05-25 23:55:33 -0700292 private OFInstruction buildMetadata(Instructions.MetadataInstruction m) {
293 OFInstruction instruction = factory().instructions().writeMetadata(
294 U64.of(m.metadata()), U64.of(m.metadataMask()));
295 return instruction;
296 }
297
alshabib10c810b2015-08-18 16:59:04 -0700298 private OFInstruction buildMeter(Instructions.MeterInstruction metered) {
299 return factory().instructions().meter(metered.meterId().id());
300 }
301
302
Jonathan Hart86e59352014-10-22 10:42:16 -0700303 private OFAction buildL0Modification(Instruction i) {
304 L0ModificationInstruction l0m = (L0ModificationInstruction) i;
305 switch (l0m.subtype()) {
Sho SHIMIZU5cd7ce92015-05-11 19:10:38 -0700306 case LAMBDA:
Sho SHIMIZUe397ffe2015-05-12 16:24:23 -0700307 return buildModLambdaInstruction((ModLambdaInstruction) i);
308 case OCH:
309 try {
310 return buildModOchSignalInstruction((ModOchSignalInstruction) i);
Sho SHIMIZUc17042d2015-05-28 12:07:23 -0700311 } catch (NoMappingFoundException e) {
Sho SHIMIZUe397ffe2015-05-12 16:24:23 -0700312 log.warn(e.getMessage());
313 break;
314 }
Sho SHIMIZU5cd7ce92015-05-11 19:10:38 -0700315 default:
316 log.warn("Unimplemented action type {}.", l0m.subtype());
317 break;
Jonathan Hart86e59352014-10-22 10:42:16 -0700318 }
319 return null;
320 }
321
Sho SHIMIZUe397ffe2015-05-12 16:24:23 -0700322 private OFAction buildModLambdaInstruction(ModLambdaInstruction instruction) {
323 return factory().actions().circuit(factory().oxms().ochSigidBasic(
324 new CircuitSignalID((byte) 1, (byte) 2, instruction.lambda(), (short) 1)));
325 }
326
327 private OFAction buildModOchSignalInstruction(ModOchSignalInstruction instruction) {
328 OchSignal signal = instruction.lambda();
Sho SHIMIZUc17042d2015-05-28 12:07:23 -0700329 byte gridType = OpenFlowValueMapper.lookupGridType(signal.gridType());
330 byte channelSpacing = OpenFlowValueMapper.lookupChannelSpacing(signal.channelSpacing());
Sho SHIMIZUe397ffe2015-05-12 16:24:23 -0700331
332 return factory().actions().circuit(factory().oxms().ochSigidBasic(
333 new CircuitSignalID(gridType, channelSpacing,
334 (short) signal.spacingMultiplier(), (short) signal.slotGranularity())
335 ));
336 }
337
Jonathan Hart86e59352014-10-22 10:42:16 -0700338 private OFAction buildL2Modification(Instruction i) {
339 L2ModificationInstruction l2m = (L2ModificationInstruction) i;
340 ModEtherInstruction eth;
341 OFOxm<?> oxm = null;
342 switch (l2m.subtype()) {
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800343 case ETH_DST:
344 eth = (ModEtherInstruction) l2m;
345 oxm = factory().oxms().ethDst(MacAddress.of(eth.mac().toLong()));
346 break;
347 case ETH_SRC:
348 eth = (ModEtherInstruction) l2m;
349 oxm = factory().oxms().ethSrc(MacAddress.of(eth.mac().toLong()));
350 break;
351 case VLAN_ID:
352 ModVlanIdInstruction vlanId = (ModVlanIdInstruction) l2m;
353 oxm = factory().oxms().vlanVid(OFVlanVidMatch.ofVlan(vlanId.vlanId().toShort()));
354 break;
355 case VLAN_PCP:
356 ModVlanPcpInstruction vlanPcp = (ModVlanPcpInstruction) l2m;
357 oxm = factory().oxms().vlanPcp(VlanPcp.of(vlanPcp.vlanPcp()));
358 break;
359 case MPLS_PUSH:
360 PushHeaderInstructions pushHeaderInstructions =
361 (PushHeaderInstructions) l2m;
362 return factory().actions().pushMpls(EthType.of(pushHeaderInstructions
alshabib7b808c52015-06-26 14:22:24 -0700363 .ethernetType().toShort()));
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800364 case MPLS_POP:
Jonathan Hart67fc0972015-03-19 15:21:20 -0700365 PushHeaderInstructions popHeaderInstructions =
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800366 (PushHeaderInstructions) l2m;
367 return factory().actions().popMpls(EthType.of(popHeaderInstructions
alshabib7b808c52015-06-26 14:22:24 -0700368 .ethernetType().toShort()));
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800369 case MPLS_LABEL:
370 ModMplsLabelInstruction mplsLabel =
371 (ModMplsLabelInstruction) l2m;
HIGUCHI Yuta04b49fc2015-08-28 09:58:58 -0700372 oxm = factory().oxms().mplsLabel(U32.of(mplsLabel.mplsLabel().toInt()));
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800373 break;
Saurav Das73a7dd42015-08-19 22:20:31 -0700374 case MPLS_BOS:
375 ModMplsBosInstruction mplsBos = (ModMplsBosInstruction) l2m;
376 oxm = factory().oxms()
377 .mplsBos(mplsBos.mplsBos() ? OFBooleanValue.TRUE
378 : OFBooleanValue.FALSE);
379 break;
sangho3f97a17d2015-01-29 22:56:29 -0800380 case DEC_MPLS_TTL:
381 return factory().actions().decMplsTtl();
Saurav Dasfbe25c52015-03-04 11:12:00 -0800382 case VLAN_POP:
383 return factory().actions().popVlan();
Jonathan Hart54b406b2015-03-06 16:24:14 -0800384 case VLAN_PUSH:
385 PushHeaderInstructions pushVlanInstruction = (PushHeaderInstructions) l2m;
386 return factory().actions().pushVlan(
alshabib7b808c52015-06-26 14:22:24 -0700387 EthType.of(pushVlanInstruction.ethernetType().toShort()));
Hyunsun Moona08c5d02015-07-14 17:53:00 -0700388 case TUNNEL_ID:
389 ModTunnelIdInstruction tunnelId = (ModTunnelIdInstruction) l2m;
390 oxm = factory().oxms().tunnelId(U64.of(tunnelId.tunnelId()));
391 break;
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800392 default:
393 log.warn("Unimplemented action type {}.", l2m.subtype());
394 break;
Jonathan Hart86e59352014-10-22 10:42:16 -0700395 }
396
397 if (oxm != null) {
398 return factory().actions().buildSetField().setField(oxm).build();
399 }
400 return null;
401 }
402
403 private OFAction buildL3Modification(Instruction i) {
404 L3ModificationInstruction l3m = (L3ModificationInstruction) i;
405 ModIPInstruction ip;
Pavlin Radoslavov23e398d2014-11-05 15:17:57 -0800406 Ip4Address ip4;
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800407 Ip6Address ip6;
Jonathan Hart86e59352014-10-22 10:42:16 -0700408 OFOxm<?> oxm = null;
409 switch (l3m.subtype()) {
Sho SHIMIZU5cd7ce92015-05-11 19:10:38 -0700410 case IPV4_SRC:
411 ip = (ModIPInstruction) i;
412 ip4 = ip.ip().getIp4Address();
413 oxm = factory().oxms().ipv4Src(IPv4Address.of(ip4.toInt()));
414 break;
415 case IPV4_DST:
416 ip = (ModIPInstruction) i;
417 ip4 = ip.ip().getIp4Address();
418 oxm = factory().oxms().ipv4Dst(IPv4Address.of(ip4.toInt()));
419 break;
420 case IPV6_SRC:
421 ip = (ModIPInstruction) i;
422 ip6 = ip.ip().getIp6Address();
423 oxm = factory().oxms().ipv6Src(IPv6Address.of(ip6.toOctets()));
424 break;
425 case IPV6_DST:
426 ip = (ModIPInstruction) i;
427 ip6 = ip.ip().getIp6Address();
428 oxm = factory().oxms().ipv6Dst(IPv6Address.of(ip6.toOctets()));
429 break;
430 case IPV6_FLABEL:
431 ModIPv6FlowLabelInstruction flowLabelInstruction =
432 (ModIPv6FlowLabelInstruction) i;
433 int flowLabel = flowLabelInstruction.flowLabel();
434 oxm = factory().oxms().ipv6Flabel(IPv6FlowLabel.of(flowLabel));
435 break;
436 case DEC_TTL:
437 return factory().actions().decNwTtl();
438 case TTL_IN:
439 return factory().actions().copyTtlIn();
440 case TTL_OUT:
441 return factory().actions().copyTtlOut();
442 default:
443 log.warn("Unimplemented action type {}.", l3m.subtype());
444 break;
Jonathan Hart86e59352014-10-22 10:42:16 -0700445 }
446
447 if (oxm != null) {
448 return factory().actions().buildSetField().setField(oxm).build();
449 }
450 return null;
451 }
452
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700453 private OFAction buildL4Modification(Instruction i) {
454 L4ModificationInstruction l4m = (L4ModificationInstruction) i;
455 ModTransportPortInstruction tp;
456 OFOxm<?> oxm = null;
457 switch (l4m.subtype()) {
458 case TCP_SRC:
459 tp = (ModTransportPortInstruction) l4m;
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700460 oxm = factory().oxms().tcpSrc(TransportPort.of(tp.port().toInt()));
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700461 break;
462 case TCP_DST:
463 tp = (ModTransportPortInstruction) l4m;
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700464 oxm = factory().oxms().tcpDst(TransportPort.of(tp.port().toInt()));
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700465 break;
466 case UDP_SRC:
467 tp = (ModTransportPortInstruction) l4m;
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700468 oxm = factory().oxms().udpSrc(TransportPort.of(tp.port().toInt()));
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700469 break;
470 case UDP_DST:
471 tp = (ModTransportPortInstruction) l4m;
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700472 oxm = factory().oxms().udpDst(TransportPort.of(tp.port().toInt()));
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700473 break;
474 default:
475 log.warn("Unimplemented action type {}.", l4m.subtype());
476 break;
477 }
478
479 if (oxm != null) {
480 return factory().actions().buildSetField().setField(oxm).build();
481 }
482 return null;
483 }
484
Jonathan Hart3c259162015-10-21 21:31:19 -0700485 private OFAction buildExtensionAction(ExtensionInstruction i) {
486 if (!driverService.isPresent()) {
487 log.error("No driver service present");
488 return null;
489 }
490 Driver driver = driverService.get().getDriver(deviceId);
491 if (driver.hasBehaviour(ExtensionInterpreter.class)) {
492 DefaultDriverHandler handler =
493 new DefaultDriverHandler(new DefaultDriverData(driver, deviceId));
494 ExtensionInterpreter interpreter = handler.behaviour(ExtensionInterpreter.class);
495 return interpreter.mapInstruction(factory(), i);
496 }
497
498 return null;
499 }
500
Jonathan Hart86e59352014-10-22 10:42:16 -0700501}