Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 1 | /* |
Ray Milkey | 34c9590 | 2015-04-15 09:47:53 -0700 | [diff] [blame] | 2 | * Copyright 2014-2015 Open Networking Laboratory |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 15 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.provider.of.flow.impl; |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 17 | |
alshabib | 346b5b3 | 2015-03-06 00:42:16 -0800 | [diff] [blame] | 18 | import com.google.common.collect.Lists; |
Jonathan Hart | 29afca3 | 2015-01-20 18:17:39 -0800 | [diff] [blame] | 19 | import org.onlab.packet.Ip4Address; |
Pavlin Radoslavov | febe82c | 2015-02-11 19:08:15 -0800 | [diff] [blame] | 20 | import org.onlab.packet.Ip6Address; |
Jonathan Hart | 3c25916 | 2015-10-21 21:31:19 -0700 | [diff] [blame] | 21 | import org.onosproject.net.DeviceId; |
Sho SHIMIZU | e397ffe | 2015-05-12 16:24:23 -0700 | [diff] [blame] | 22 | import org.onosproject.net.OchSignal; |
Charles M.C. Chan | fe42181 | 2015-01-12 18:20:51 +0800 | [diff] [blame] | 23 | import org.onosproject.net.PortNumber; |
Jonathan Hart | 3c25916 | 2015-10-21 21:31:19 -0700 | [diff] [blame] | 24 | import org.onosproject.net.driver.DefaultDriverData; |
| 25 | import org.onosproject.net.driver.DefaultDriverHandler; |
| 26 | import org.onosproject.net.driver.Driver; |
| 27 | import org.onosproject.net.driver.DriverService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 28 | import org.onosproject.net.flow.FlowRule; |
| 29 | import org.onosproject.net.flow.TrafficTreatment; |
Jonathan Hart | 3c25916 | 2015-10-21 21:31:19 -0700 | [diff] [blame] | 30 | import org.onosproject.net.flow.instructions.ExtensionInstruction; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 31 | import org.onosproject.net.flow.instructions.Instruction; |
alshabib | 9af7007 | 2015-02-09 14:34:16 -0800 | [diff] [blame] | 32 | import org.onosproject.net.flow.instructions.Instructions; |
sangho | 8995ac5 | 2015-02-04 11:29:03 -0800 | [diff] [blame] | 33 | import org.onosproject.net.flow.instructions.Instructions.GroupInstruction; |
alshabib | 9af7007 | 2015-02-09 14:34:16 -0800 | [diff] [blame] | 34 | import org.onosproject.net.flow.instructions.Instructions.OutputInstruction; |
Steffen Gebert | bbfdaaa | 2015-09-29 11:01:46 +0200 | [diff] [blame] | 35 | import org.onosproject.net.flow.instructions.Instructions.SetQueueInstruction; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 36 | import org.onosproject.net.flow.instructions.L0ModificationInstruction; |
| 37 | import org.onosproject.net.flow.instructions.L0ModificationInstruction.ModLambdaInstruction; |
Sho SHIMIZU | e397ffe | 2015-05-12 16:24:23 -0700 | [diff] [blame] | 38 | import org.onosproject.net.flow.instructions.L0ModificationInstruction.ModOchSignalInstruction; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 39 | import org.onosproject.net.flow.instructions.L2ModificationInstruction; |
| 40 | import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModEtherInstruction; |
Saurav Das | 73a7dd4 | 2015-08-19 22:20:31 -0700 | [diff] [blame] | 41 | import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModMplsBosInstruction; |
Jonathan Hart | 29afca3 | 2015-01-20 18:17:39 -0800 | [diff] [blame] | 42 | import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModMplsLabelInstruction; |
Jonathan Hart | 3c25916 | 2015-10-21 21:31:19 -0700 | [diff] [blame] | 43 | import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModTunnelIdInstruction; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 44 | import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModVlanIdInstruction; |
| 45 | import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModVlanPcpInstruction; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 46 | import org.onosproject.net.flow.instructions.L2ModificationInstruction.PushHeaderInstructions; |
| 47 | import org.onosproject.net.flow.instructions.L3ModificationInstruction; |
| 48 | import org.onosproject.net.flow.instructions.L3ModificationInstruction.ModIPInstruction; |
Pavlin Radoslavov | febe82c | 2015-02-11 19:08:15 -0800 | [diff] [blame] | 49 | import org.onosproject.net.flow.instructions.L3ModificationInstruction.ModIPv6FlowLabelInstruction; |
Hyunsun Moon | c8bd97c | 2015-07-18 22:47:33 -0700 | [diff] [blame] | 50 | import org.onosproject.net.flow.instructions.L4ModificationInstruction; |
| 51 | import org.onosproject.net.flow.instructions.L4ModificationInstruction.ModTransportPortInstruction; |
Jonathan Hart | 3c25916 | 2015-10-21 21:31:19 -0700 | [diff] [blame] | 52 | import org.onosproject.openflow.controller.ExtensionInterpreter; |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 53 | import org.projectfloodlight.openflow.protocol.OFFactory; |
| 54 | import org.projectfloodlight.openflow.protocol.OFFlowAdd; |
| 55 | import org.projectfloodlight.openflow.protocol.OFFlowDelete; |
| 56 | import org.projectfloodlight.openflow.protocol.OFFlowMod; |
| 57 | import org.projectfloodlight.openflow.protocol.OFFlowModFlags; |
| 58 | import org.projectfloodlight.openflow.protocol.action.OFAction; |
sangho | 8995ac5 | 2015-02-04 11:29:03 -0800 | [diff] [blame] | 59 | import org.projectfloodlight.openflow.protocol.action.OFActionGroup; |
Charles M.C. Chan | fe42181 | 2015-01-12 18:20:51 +0800 | [diff] [blame] | 60 | import org.projectfloodlight.openflow.protocol.action.OFActionOutput; |
Steffen Gebert | bbfdaaa | 2015-09-29 11:01:46 +0200 | [diff] [blame] | 61 | import org.projectfloodlight.openflow.protocol.action.OFActionSetQueue; |
alshabib | 9af7007 | 2015-02-09 14:34:16 -0800 | [diff] [blame] | 62 | import org.projectfloodlight.openflow.protocol.instruction.OFInstruction; |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 63 | import org.projectfloodlight.openflow.protocol.match.Match; |
| 64 | import org.projectfloodlight.openflow.protocol.oxm.OFOxm; |
| 65 | import org.projectfloodlight.openflow.types.CircuitSignalID; |
Praseed Balakrishnan | 8c67d17 | 2014-11-10 10:15:41 -0800 | [diff] [blame] | 66 | import org.projectfloodlight.openflow.types.EthType; |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 67 | import org.projectfloodlight.openflow.types.IPv4Address; |
Pavlin Radoslavov | febe82c | 2015-02-11 19:08:15 -0800 | [diff] [blame] | 68 | import org.projectfloodlight.openflow.types.IPv6Address; |
| 69 | import org.projectfloodlight.openflow.types.IPv6FlowLabel; |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 70 | import org.projectfloodlight.openflow.types.MacAddress; |
Saurav Das | 73a7dd4 | 2015-08-19 22:20:31 -0700 | [diff] [blame] | 71 | import org.projectfloodlight.openflow.types.OFBooleanValue; |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 72 | import org.projectfloodlight.openflow.types.OFBufferId; |
sangho | 8995ac5 | 2015-02-04 11:29:03 -0800 | [diff] [blame] | 73 | import org.projectfloodlight.openflow.types.OFGroup; |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 74 | import org.projectfloodlight.openflow.types.OFPort; |
| 75 | import org.projectfloodlight.openflow.types.OFVlanVidMatch; |
alshabib | 9af7007 | 2015-02-09 14:34:16 -0800 | [diff] [blame] | 76 | import org.projectfloodlight.openflow.types.TableId; |
Hyunsun Moon | c8bd97c | 2015-07-18 22:47:33 -0700 | [diff] [blame] | 77 | import org.projectfloodlight.openflow.types.TransportPort; |
Praseed Balakrishnan | 8c67d17 | 2014-11-10 10:15:41 -0800 | [diff] [blame] | 78 | import org.projectfloodlight.openflow.types.U32; |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 79 | import org.projectfloodlight.openflow.types.U64; |
| 80 | import org.projectfloodlight.openflow.types.VlanPcp; |
| 81 | import org.slf4j.Logger; |
| 82 | import org.slf4j.LoggerFactory; |
| 83 | |
Jonathan Hart | 29afca3 | 2015-01-20 18:17:39 -0800 | [diff] [blame] | 84 | import java.util.Collections; |
| 85 | import java.util.LinkedList; |
| 86 | import java.util.List; |
| 87 | import java.util.Optional; |
| 88 | |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 89 | /** |
| 90 | * Flow mod builder for OpenFlow 1.3+. |
| 91 | */ |
| 92 | public class FlowModBuilderVer13 extends FlowModBuilder { |
| 93 | |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 94 | private final Logger log = LoggerFactory.getLogger(getClass()); |
Charles M.C. Chan | fe42181 | 2015-01-12 18:20:51 +0800 | [diff] [blame] | 95 | private static final int OFPCML_NO_BUFFER = 0xffff; |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 96 | |
| 97 | private final TrafficTreatment treatment; |
Jonathan Hart | 3c25916 | 2015-10-21 21:31:19 -0700 | [diff] [blame] | 98 | private final DeviceId deviceId; |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 99 | |
| 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 Radoslavov | 119fd5c | 2014-11-25 19:08:19 -0800 | [diff] [blame] | 105 | * @param xid the transaction ID |
Charles Chan | 30ba400 | 2015-11-05 14:45:16 -0800 | [diff] [blame^] | 106 | * @param driverService the device driver service |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 107 | */ |
Jonathan Hart | 3c25916 | 2015-10-21 21:31:19 -0700 | [diff] [blame] | 108 | protected FlowModBuilderVer13(FlowRule flowRule, OFFactory factory, Optional<Long> xid, |
| 109 | Optional<DriverService> driverService) { |
| 110 | super(flowRule, factory, xid, driverService); |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 111 | |
| 112 | this.treatment = flowRule.treatment(); |
Jonathan Hart | 3c25916 | 2015-10-21 21:31:19 -0700 | [diff] [blame] | 113 | this.deviceId = flowRule.deviceId(); |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | @Override |
| 117 | public OFFlowAdd buildFlowAdd() { |
| 118 | Match match = buildMatch(); |
alshabib | 346b5b3 | 2015-03-06 00:42:16 -0800 | [diff] [blame] | 119 | List<OFAction> deferredActions = buildActions(treatment.deferred()); |
| 120 | List<OFAction> immediateActions = buildActions(treatment.immediate()); |
| 121 | List<OFInstruction> instructions = Lists.newLinkedList(); |
Saurav Das | cbe6de3 | 2015-03-01 18:30:46 -0800 | [diff] [blame] | 122 | |
Jonathan Hart | d4a8bba | 2014-10-28 12:44:20 -0700 | [diff] [blame] | 123 | |
alshabib | 346b5b3 | 2015-03-06 00:42:16 -0800 | [diff] [blame] | 124 | if (treatment.clearedDeferred()) { |
| 125 | instructions.add(factory().instructions().clearActions()); |
| 126 | } |
sangho | 1e57565 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 127 | if (immediateActions.size() > 0) { |
| 128 | instructions.add(factory().instructions().applyActions(immediateActions)); |
| 129 | } |
alshabib | 346b5b3 | 2015-03-06 00:42:16 -0800 | [diff] [blame] | 130 | if (deferredActions.size() > 0) { |
| 131 | instructions.add(factory().instructions().writeActions(deferredActions)); |
| 132 | } |
| 133 | if (treatment.tableTransition() != null) { |
| 134 | instructions.add(buildTableGoto(treatment.tableTransition())); |
Saurav Das | cbe6de3 | 2015-03-01 18:30:46 -0800 | [diff] [blame] | 135 | } |
Saurav Das | 86af8f1 | 2015-05-25 23:55:33 -0700 | [diff] [blame] | 136 | if (treatment.writeMetadata() != null) { |
| 137 | instructions.add(buildMetadata(treatment.writeMetadata())); |
| 138 | } |
chengfan | e264bef | 2015-09-28 08:38:19 +0800 | [diff] [blame] | 139 | if (treatment.metered() != null) { |
| 140 | instructions.add(buildMeter(treatment.metered())); |
| 141 | } |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 142 | |
| 143 | long cookie = flowRule().id().value(); |
| 144 | |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 145 | OFFlowAdd fm = factory().buildFlowAdd() |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 146 | .setXid(xid) |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 147 | .setCookie(U64.of(cookie)) |
| 148 | .setBufferId(OFBufferId.NO_BUFFER) |
alshabib | 9af7007 | 2015-02-09 14:34:16 -0800 | [diff] [blame] | 149 | .setInstructions(instructions) |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 150 | .setMatch(match) |
| 151 | .setFlags(Collections.singleton(OFFlowModFlags.SEND_FLOW_REM)) |
| 152 | .setPriority(flowRule().priority()) |
alshabib | 08d9898 | 2015-04-21 16:25:50 -0700 | [diff] [blame] | 153 | .setTableId(TableId.of(flowRule().tableId())) |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 154 | .build(); |
| 155 | |
| 156 | return fm; |
| 157 | } |
| 158 | |
| 159 | @Override |
| 160 | public OFFlowMod buildFlowMod() { |
| 161 | Match match = buildMatch(); |
alshabib | 346b5b3 | 2015-03-06 00:42:16 -0800 | [diff] [blame] | 162 | List<OFAction> deferredActions = buildActions(treatment.deferred()); |
| 163 | List<OFAction> immediateActions = buildActions(treatment.immediate()); |
| 164 | List<OFInstruction> instructions = Lists.newLinkedList(); |
Jonathan Hart | eda3387 | 2015-02-18 10:22:32 -0800 | [diff] [blame] | 165 | |
alshabib | 346b5b3 | 2015-03-06 00:42:16 -0800 | [diff] [blame] | 166 | |
| 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 Das | cbe6de3 | 2015-03-01 18:30:46 -0800 | [diff] [blame] | 178 | } |
Saurav Das | 86af8f1 | 2015-05-25 23:55:33 -0700 | [diff] [blame] | 179 | if (treatment.writeMetadata() != null) { |
| 180 | instructions.add(buildMetadata(treatment.writeMetadata())); |
| 181 | } |
alshabib | 10c810b | 2015-08-18 16:59:04 -0700 | [diff] [blame] | 182 | if (treatment.metered() != null) { |
| 183 | instructions.add(buildMeter(treatment.metered())); |
| 184 | } |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 185 | |
| 186 | long cookie = flowRule().id().value(); |
| 187 | |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 188 | OFFlowMod fm = factory().buildFlowModify() |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 189 | .setXid(xid) |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 190 | .setCookie(U64.of(cookie)) |
| 191 | .setBufferId(OFBufferId.NO_BUFFER) |
alshabib | 9af7007 | 2015-02-09 14:34:16 -0800 | [diff] [blame] | 192 | .setInstructions(instructions) |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 193 | .setMatch(match) |
| 194 | .setFlags(Collections.singleton(OFFlowModFlags.SEND_FLOW_REM)) |
| 195 | .setPriority(flowRule().priority()) |
alshabib | 08d9898 | 2015-04-21 16:25:50 -0700 | [diff] [blame] | 196 | .setTableId(TableId.of(flowRule().tableId())) |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 197 | .build(); |
| 198 | |
| 199 | return fm; |
| 200 | } |
| 201 | |
| 202 | @Override |
| 203 | public OFFlowDelete buildFlowDel() { |
| 204 | Match match = buildMatch(); |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 205 | |
| 206 | long cookie = flowRule().id().value(); |
| 207 | |
| 208 | OFFlowDelete fm = factory().buildFlowDelete() |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 209 | .setXid(xid) |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 210 | .setCookie(U64.of(cookie)) |
| 211 | .setBufferId(OFBufferId.NO_BUFFER) |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 212 | .setMatch(match) |
| 213 | .setFlags(Collections.singleton(OFFlowModFlags.SEND_FLOW_REM)) |
| 214 | .setPriority(flowRule().priority()) |
alshabib | 08d9898 | 2015-04-21 16:25:50 -0700 | [diff] [blame] | 215 | .setTableId(TableId.of(flowRule().tableId())) |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 216 | .build(); |
| 217 | |
| 218 | return fm; |
| 219 | } |
| 220 | |
alshabib | 346b5b3 | 2015-03-06 00:42:16 -0800 | [diff] [blame] | 221 | private List<OFAction> buildActions(List<Instruction> treatments) { |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 222 | if (treatment == null) { |
Sho SHIMIZU | 6601dd7 | 2015-05-11 19:00:29 -0700 | [diff] [blame] | 223 | return Collections.emptyList(); |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 224 | } |
Sho SHIMIZU | 6601dd7 | 2015-05-11 19:00:29 -0700 | [diff] [blame] | 225 | |
| 226 | boolean tableFound = false; |
| 227 | List<OFAction> actions = new LinkedList<>(); |
alshabib | 346b5b3 | 2015-03-06 00:42:16 -0800 | [diff] [blame] | 228 | for (Instruction i : treatments) { |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 229 | switch (i.type()) { |
alshabib | 9af7007 | 2015-02-09 14:34:16 -0800 | [diff] [blame] | 230 | case DROP: |
Charles Chan | 7efabeb | 2015-09-28 15:12:19 -0700 | [diff] [blame] | 231 | case NOACTION: |
Sho SHIMIZU | b35ed36 | 2015-04-28 11:05:19 -0700 | [diff] [blame] | 232 | return Collections.emptyList(); |
alshabib | 9af7007 | 2015-02-09 14:34:16 -0800 | [diff] [blame] | 233 | 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 Moon | c8bd97c | 2015-07-18 22:47:33 -0700 | [diff] [blame] | 242 | case L4MODIFICATION: |
| 243 | actions.add(buildL4Modification(i)); |
| 244 | break; |
alshabib | 9af7007 | 2015-02-09 14:34:16 -0800 | [diff] [blame] | 245 | 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 Gebert | bbfdaaa | 2015-09-29 11:01:46 +0200 | [diff] [blame] | 260 | 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; |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 266 | case TABLE: |
| 267 | //FIXME: should not occur here. |
Saurav Das | cbe6de3 | 2015-03-01 18:30:46 -0800 | [diff] [blame] | 268 | tableFound = true; |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 269 | break; |
Jonathan Hart | 3c25916 | 2015-10-21 21:31:19 -0700 | [diff] [blame] | 270 | case EXTENSION: |
| 271 | actions.add(buildExtensionAction(((Instructions.ExtensionInstructionWrapper) i) |
| 272 | .extensionInstruction())); |
| 273 | break; |
alshabib | 9af7007 | 2015-02-09 14:34:16 -0800 | [diff] [blame] | 274 | default: |
| 275 | log.warn("Instruction type {} not yet implemented.", i.type()); |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 276 | } |
| 277 | } |
Saurav Das | cbe6de3 | 2015-03-01 18:30:46 -0800 | [diff] [blame] | 278 | 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 SHIMIZU | 6601dd7 | 2015-05-11 19:00:29 -0700 | [diff] [blame] | 281 | return Collections.emptyList(); |
Saurav Das | cbe6de3 | 2015-03-01 18:30:46 -0800 | [diff] [blame] | 282 | } |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 283 | return actions; |
| 284 | } |
| 285 | |
alshabib | 9af7007 | 2015-02-09 14:34:16 -0800 | [diff] [blame] | 286 | private OFInstruction buildTableGoto(Instructions.TableTypeTransition i) { |
| 287 | OFInstruction instruction = factory().instructions().gotoTable( |
alshabib | d17abc2 | 2015-04-21 18:26:35 -0700 | [diff] [blame] | 288 | TableId.of(i.tableId())); |
alshabib | 9af7007 | 2015-02-09 14:34:16 -0800 | [diff] [blame] | 289 | return instruction; |
| 290 | } |
| 291 | |
Saurav Das | 86af8f1 | 2015-05-25 23:55:33 -0700 | [diff] [blame] | 292 | 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 | |
alshabib | 10c810b | 2015-08-18 16:59:04 -0700 | [diff] [blame] | 298 | private OFInstruction buildMeter(Instructions.MeterInstruction metered) { |
| 299 | return factory().instructions().meter(metered.meterId().id()); |
| 300 | } |
| 301 | |
| 302 | |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 303 | private OFAction buildL0Modification(Instruction i) { |
| 304 | L0ModificationInstruction l0m = (L0ModificationInstruction) i; |
| 305 | switch (l0m.subtype()) { |
Sho SHIMIZU | 5cd7ce9 | 2015-05-11 19:10:38 -0700 | [diff] [blame] | 306 | case LAMBDA: |
Sho SHIMIZU | e397ffe | 2015-05-12 16:24:23 -0700 | [diff] [blame] | 307 | return buildModLambdaInstruction((ModLambdaInstruction) i); |
| 308 | case OCH: |
| 309 | try { |
| 310 | return buildModOchSignalInstruction((ModOchSignalInstruction) i); |
Sho SHIMIZU | c17042d | 2015-05-28 12:07:23 -0700 | [diff] [blame] | 311 | } catch (NoMappingFoundException e) { |
Sho SHIMIZU | e397ffe | 2015-05-12 16:24:23 -0700 | [diff] [blame] | 312 | log.warn(e.getMessage()); |
| 313 | break; |
| 314 | } |
Sho SHIMIZU | 5cd7ce9 | 2015-05-11 19:10:38 -0700 | [diff] [blame] | 315 | default: |
| 316 | log.warn("Unimplemented action type {}.", l0m.subtype()); |
| 317 | break; |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 318 | } |
| 319 | return null; |
| 320 | } |
| 321 | |
Sho SHIMIZU | e397ffe | 2015-05-12 16:24:23 -0700 | [diff] [blame] | 322 | 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 SHIMIZU | c17042d | 2015-05-28 12:07:23 -0700 | [diff] [blame] | 329 | byte gridType = OpenFlowValueMapper.lookupGridType(signal.gridType()); |
| 330 | byte channelSpacing = OpenFlowValueMapper.lookupChannelSpacing(signal.channelSpacing()); |
Sho SHIMIZU | e397ffe | 2015-05-12 16:24:23 -0700 | [diff] [blame] | 331 | |
| 332 | return factory().actions().circuit(factory().oxms().ochSigidBasic( |
| 333 | new CircuitSignalID(gridType, channelSpacing, |
| 334 | (short) signal.spacingMultiplier(), (short) signal.slotGranularity()) |
| 335 | )); |
| 336 | } |
| 337 | |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 338 | private OFAction buildL2Modification(Instruction i) { |
| 339 | L2ModificationInstruction l2m = (L2ModificationInstruction) i; |
| 340 | ModEtherInstruction eth; |
| 341 | OFOxm<?> oxm = null; |
| 342 | switch (l2m.subtype()) { |
Praseed Balakrishnan | 8c67d17 | 2014-11-10 10:15:41 -0800 | [diff] [blame] | 343 | 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 |
alshabib | 7b808c5 | 2015-06-26 14:22:24 -0700 | [diff] [blame] | 363 | .ethernetType().toShort())); |
Praseed Balakrishnan | 8c67d17 | 2014-11-10 10:15:41 -0800 | [diff] [blame] | 364 | case MPLS_POP: |
Jonathan Hart | 67fc097 | 2015-03-19 15:21:20 -0700 | [diff] [blame] | 365 | PushHeaderInstructions popHeaderInstructions = |
Praseed Balakrishnan | 8c67d17 | 2014-11-10 10:15:41 -0800 | [diff] [blame] | 366 | (PushHeaderInstructions) l2m; |
| 367 | return factory().actions().popMpls(EthType.of(popHeaderInstructions |
alshabib | 7b808c5 | 2015-06-26 14:22:24 -0700 | [diff] [blame] | 368 | .ethernetType().toShort())); |
Praseed Balakrishnan | 8c67d17 | 2014-11-10 10:15:41 -0800 | [diff] [blame] | 369 | case MPLS_LABEL: |
| 370 | ModMplsLabelInstruction mplsLabel = |
| 371 | (ModMplsLabelInstruction) l2m; |
HIGUCHI Yuta | 04b49fc | 2015-08-28 09:58:58 -0700 | [diff] [blame] | 372 | oxm = factory().oxms().mplsLabel(U32.of(mplsLabel.mplsLabel().toInt())); |
Praseed Balakrishnan | 8c67d17 | 2014-11-10 10:15:41 -0800 | [diff] [blame] | 373 | break; |
Saurav Das | 73a7dd4 | 2015-08-19 22:20:31 -0700 | [diff] [blame] | 374 | case MPLS_BOS: |
| 375 | ModMplsBosInstruction mplsBos = (ModMplsBosInstruction) l2m; |
| 376 | oxm = factory().oxms() |
| 377 | .mplsBos(mplsBos.mplsBos() ? OFBooleanValue.TRUE |
| 378 | : OFBooleanValue.FALSE); |
| 379 | break; |
sangho | 3f97a17d | 2015-01-29 22:56:29 -0800 | [diff] [blame] | 380 | case DEC_MPLS_TTL: |
| 381 | return factory().actions().decMplsTtl(); |
Saurav Das | fbe25c5 | 2015-03-04 11:12:00 -0800 | [diff] [blame] | 382 | case VLAN_POP: |
| 383 | return factory().actions().popVlan(); |
Jonathan Hart | 54b406b | 2015-03-06 16:24:14 -0800 | [diff] [blame] | 384 | case VLAN_PUSH: |
| 385 | PushHeaderInstructions pushVlanInstruction = (PushHeaderInstructions) l2m; |
| 386 | return factory().actions().pushVlan( |
alshabib | 7b808c5 | 2015-06-26 14:22:24 -0700 | [diff] [blame] | 387 | EthType.of(pushVlanInstruction.ethernetType().toShort())); |
Hyunsun Moon | a08c5d0 | 2015-07-14 17:53:00 -0700 | [diff] [blame] | 388 | case TUNNEL_ID: |
| 389 | ModTunnelIdInstruction tunnelId = (ModTunnelIdInstruction) l2m; |
| 390 | oxm = factory().oxms().tunnelId(U64.of(tunnelId.tunnelId())); |
| 391 | break; |
Praseed Balakrishnan | 8c67d17 | 2014-11-10 10:15:41 -0800 | [diff] [blame] | 392 | default: |
| 393 | log.warn("Unimplemented action type {}.", l2m.subtype()); |
| 394 | break; |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 395 | } |
| 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 Radoslavov | 23e398d | 2014-11-05 15:17:57 -0800 | [diff] [blame] | 406 | Ip4Address ip4; |
Pavlin Radoslavov | febe82c | 2015-02-11 19:08:15 -0800 | [diff] [blame] | 407 | Ip6Address ip6; |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 408 | OFOxm<?> oxm = null; |
| 409 | switch (l3m.subtype()) { |
Sho SHIMIZU | 5cd7ce9 | 2015-05-11 19:10:38 -0700 | [diff] [blame] | 410 | 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 Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | if (oxm != null) { |
| 448 | return factory().actions().buildSetField().setField(oxm).build(); |
| 449 | } |
| 450 | return null; |
| 451 | } |
| 452 | |
Hyunsun Moon | c8bd97c | 2015-07-18 22:47:33 -0700 | [diff] [blame] | 453 | 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 Moon | cf732fb | 2015-08-22 21:04:23 -0700 | [diff] [blame] | 460 | oxm = factory().oxms().tcpSrc(TransportPort.of(tp.port().toInt())); |
Hyunsun Moon | c8bd97c | 2015-07-18 22:47:33 -0700 | [diff] [blame] | 461 | break; |
| 462 | case TCP_DST: |
| 463 | tp = (ModTransportPortInstruction) l4m; |
Hyunsun Moon | cf732fb | 2015-08-22 21:04:23 -0700 | [diff] [blame] | 464 | oxm = factory().oxms().tcpDst(TransportPort.of(tp.port().toInt())); |
Hyunsun Moon | c8bd97c | 2015-07-18 22:47:33 -0700 | [diff] [blame] | 465 | break; |
| 466 | case UDP_SRC: |
| 467 | tp = (ModTransportPortInstruction) l4m; |
Hyunsun Moon | cf732fb | 2015-08-22 21:04:23 -0700 | [diff] [blame] | 468 | oxm = factory().oxms().udpSrc(TransportPort.of(tp.port().toInt())); |
Hyunsun Moon | c8bd97c | 2015-07-18 22:47:33 -0700 | [diff] [blame] | 469 | break; |
| 470 | case UDP_DST: |
| 471 | tp = (ModTransportPortInstruction) l4m; |
Hyunsun Moon | cf732fb | 2015-08-22 21:04:23 -0700 | [diff] [blame] | 472 | oxm = factory().oxms().udpDst(TransportPort.of(tp.port().toInt())); |
Hyunsun Moon | c8bd97c | 2015-07-18 22:47:33 -0700 | [diff] [blame] | 473 | 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 Hart | 3c25916 | 2015-10-21 21:31:19 -0700 | [diff] [blame] | 485 | 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 Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 501 | } |