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; |
Sho SHIMIZU | e397ffe | 2015-05-12 16:24:23 -0700 | [diff] [blame] | 21 | import org.onosproject.net.OchSignal; |
Charles M.C. Chan | fe42181 | 2015-01-12 18:20:51 +0800 | [diff] [blame] | 22 | import org.onosproject.net.PortNumber; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 23 | import org.onosproject.net.flow.FlowRule; |
| 24 | import org.onosproject.net.flow.TrafficTreatment; |
| 25 | import org.onosproject.net.flow.instructions.Instruction; |
alshabib | 9af7007 | 2015-02-09 14:34:16 -0800 | [diff] [blame] | 26 | import org.onosproject.net.flow.instructions.Instructions; |
sangho | 8995ac5 | 2015-02-04 11:29:03 -0800 | [diff] [blame] | 27 | import org.onosproject.net.flow.instructions.Instructions.GroupInstruction; |
alshabib | 9af7007 | 2015-02-09 14:34:16 -0800 | [diff] [blame] | 28 | import org.onosproject.net.flow.instructions.Instructions.OutputInstruction; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 29 | import org.onosproject.net.flow.instructions.L0ModificationInstruction; |
| 30 | import org.onosproject.net.flow.instructions.L0ModificationInstruction.ModLambdaInstruction; |
Sho SHIMIZU | e397ffe | 2015-05-12 16:24:23 -0700 | [diff] [blame] | 31 | import org.onosproject.net.flow.instructions.L0ModificationInstruction.ModOchSignalInstruction; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 32 | import org.onosproject.net.flow.instructions.L2ModificationInstruction; |
| 33 | import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModEtherInstruction; |
Saurav Das | 73a7dd4 | 2015-08-19 22:20:31 -0700 | [diff] [blame] | 34 | import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModMplsBosInstruction; |
Jonathan Hart | 29afca3 | 2015-01-20 18:17:39 -0800 | [diff] [blame] | 35 | import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModMplsLabelInstruction; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 36 | import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModVlanIdInstruction; |
| 37 | import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModVlanPcpInstruction; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 38 | import org.onosproject.net.flow.instructions.L2ModificationInstruction.PushHeaderInstructions; |
Hyunsun Moon | a08c5d0 | 2015-07-14 17:53:00 -0700 | [diff] [blame] | 39 | import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModTunnelIdInstruction; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 40 | import org.onosproject.net.flow.instructions.L3ModificationInstruction; |
| 41 | import org.onosproject.net.flow.instructions.L3ModificationInstruction.ModIPInstruction; |
Pavlin Radoslavov | febe82c | 2015-02-11 19:08:15 -0800 | [diff] [blame] | 42 | import org.onosproject.net.flow.instructions.L3ModificationInstruction.ModIPv6FlowLabelInstruction; |
Hyunsun Moon | c8bd97c | 2015-07-18 22:47:33 -0700 | [diff] [blame] | 43 | import org.onosproject.net.flow.instructions.L4ModificationInstruction; |
| 44 | import org.onosproject.net.flow.instructions.L4ModificationInstruction.ModTransportPortInstruction; |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 45 | import org.projectfloodlight.openflow.protocol.OFFactory; |
| 46 | import org.projectfloodlight.openflow.protocol.OFFlowAdd; |
| 47 | import org.projectfloodlight.openflow.protocol.OFFlowDelete; |
| 48 | import org.projectfloodlight.openflow.protocol.OFFlowMod; |
| 49 | import org.projectfloodlight.openflow.protocol.OFFlowModFlags; |
| 50 | import org.projectfloodlight.openflow.protocol.action.OFAction; |
sangho | 8995ac5 | 2015-02-04 11:29:03 -0800 | [diff] [blame] | 51 | import org.projectfloodlight.openflow.protocol.action.OFActionGroup; |
Charles M.C. Chan | fe42181 | 2015-01-12 18:20:51 +0800 | [diff] [blame] | 52 | import org.projectfloodlight.openflow.protocol.action.OFActionOutput; |
alshabib | 9af7007 | 2015-02-09 14:34:16 -0800 | [diff] [blame] | 53 | import org.projectfloodlight.openflow.protocol.instruction.OFInstruction; |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 54 | import org.projectfloodlight.openflow.protocol.match.Match; |
| 55 | import org.projectfloodlight.openflow.protocol.oxm.OFOxm; |
| 56 | import org.projectfloodlight.openflow.types.CircuitSignalID; |
Praseed Balakrishnan | 8c67d17 | 2014-11-10 10:15:41 -0800 | [diff] [blame] | 57 | import org.projectfloodlight.openflow.types.EthType; |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 58 | import org.projectfloodlight.openflow.types.IPv4Address; |
Pavlin Radoslavov | febe82c | 2015-02-11 19:08:15 -0800 | [diff] [blame] | 59 | import org.projectfloodlight.openflow.types.IPv6Address; |
| 60 | import org.projectfloodlight.openflow.types.IPv6FlowLabel; |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 61 | import org.projectfloodlight.openflow.types.MacAddress; |
Saurav Das | 73a7dd4 | 2015-08-19 22:20:31 -0700 | [diff] [blame] | 62 | import org.projectfloodlight.openflow.types.OFBooleanValue; |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 63 | import org.projectfloodlight.openflow.types.OFBufferId; |
sangho | 8995ac5 | 2015-02-04 11:29:03 -0800 | [diff] [blame] | 64 | import org.projectfloodlight.openflow.types.OFGroup; |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 65 | import org.projectfloodlight.openflow.types.OFPort; |
| 66 | import org.projectfloodlight.openflow.types.OFVlanVidMatch; |
alshabib | 9af7007 | 2015-02-09 14:34:16 -0800 | [diff] [blame] | 67 | import org.projectfloodlight.openflow.types.TableId; |
Hyunsun Moon | c8bd97c | 2015-07-18 22:47:33 -0700 | [diff] [blame] | 68 | import org.projectfloodlight.openflow.types.TransportPort; |
Praseed Balakrishnan | 8c67d17 | 2014-11-10 10:15:41 -0800 | [diff] [blame] | 69 | import org.projectfloodlight.openflow.types.U32; |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 70 | import org.projectfloodlight.openflow.types.U64; |
| 71 | import org.projectfloodlight.openflow.types.VlanPcp; |
| 72 | import org.slf4j.Logger; |
| 73 | import org.slf4j.LoggerFactory; |
| 74 | |
Jonathan Hart | 29afca3 | 2015-01-20 18:17:39 -0800 | [diff] [blame] | 75 | import java.util.Collections; |
| 76 | import java.util.LinkedList; |
| 77 | import java.util.List; |
| 78 | import java.util.Optional; |
| 79 | |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 80 | /** |
| 81 | * Flow mod builder for OpenFlow 1.3+. |
| 82 | */ |
| 83 | public class FlowModBuilderVer13 extends FlowModBuilder { |
| 84 | |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 85 | private final Logger log = LoggerFactory.getLogger(getClass()); |
Charles M.C. Chan | fe42181 | 2015-01-12 18:20:51 +0800 | [diff] [blame] | 86 | private static final int OFPCML_NO_BUFFER = 0xffff; |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 87 | |
| 88 | private final TrafficTreatment treatment; |
| 89 | |
| 90 | /** |
| 91 | * Constructor for a flow mod builder for OpenFlow 1.3. |
| 92 | * |
| 93 | * @param flowRule the flow rule to transform into a flow mod |
| 94 | * @param factory the OpenFlow factory to use to build the flow mod |
Pavlin Radoslavov | 119fd5c | 2014-11-25 19:08:19 -0800 | [diff] [blame] | 95 | * @param xid the transaction ID |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 96 | */ |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 97 | protected FlowModBuilderVer13(FlowRule flowRule, OFFactory factory, Optional<Long> xid) { |
| 98 | super(flowRule, factory, xid); |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 99 | |
| 100 | this.treatment = flowRule.treatment(); |
| 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public OFFlowAdd buildFlowAdd() { |
| 105 | Match match = buildMatch(); |
alshabib | 346b5b3 | 2015-03-06 00:42:16 -0800 | [diff] [blame] | 106 | List<OFAction> deferredActions = buildActions(treatment.deferred()); |
| 107 | List<OFAction> immediateActions = buildActions(treatment.immediate()); |
| 108 | List<OFInstruction> instructions = Lists.newLinkedList(); |
Saurav Das | cbe6de3 | 2015-03-01 18:30:46 -0800 | [diff] [blame] | 109 | |
Jonathan Hart | d4a8bba | 2014-10-28 12:44:20 -0700 | [diff] [blame] | 110 | |
alshabib | 346b5b3 | 2015-03-06 00:42:16 -0800 | [diff] [blame] | 111 | if (treatment.clearedDeferred()) { |
| 112 | instructions.add(factory().instructions().clearActions()); |
| 113 | } |
sangho | 1e57565 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 114 | if (immediateActions.size() > 0) { |
| 115 | instructions.add(factory().instructions().applyActions(immediateActions)); |
| 116 | } |
alshabib | 346b5b3 | 2015-03-06 00:42:16 -0800 | [diff] [blame] | 117 | if (deferredActions.size() > 0) { |
| 118 | instructions.add(factory().instructions().writeActions(deferredActions)); |
| 119 | } |
| 120 | if (treatment.tableTransition() != null) { |
| 121 | instructions.add(buildTableGoto(treatment.tableTransition())); |
Saurav Das | cbe6de3 | 2015-03-01 18:30:46 -0800 | [diff] [blame] | 122 | } |
Saurav Das | 86af8f1 | 2015-05-25 23:55:33 -0700 | [diff] [blame] | 123 | if (treatment.writeMetadata() != null) { |
| 124 | instructions.add(buildMetadata(treatment.writeMetadata())); |
| 125 | } |
chengfan | e264bef | 2015-09-28 08:38:19 +0800 | [diff] [blame] | 126 | if (treatment.metered() != null) { |
| 127 | instructions.add(buildMeter(treatment.metered())); |
| 128 | } |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 129 | |
| 130 | long cookie = flowRule().id().value(); |
| 131 | |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 132 | OFFlowAdd fm = factory().buildFlowAdd() |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 133 | .setXid(xid) |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 134 | .setCookie(U64.of(cookie)) |
| 135 | .setBufferId(OFBufferId.NO_BUFFER) |
alshabib | 9af7007 | 2015-02-09 14:34:16 -0800 | [diff] [blame] | 136 | .setInstructions(instructions) |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 137 | .setMatch(match) |
| 138 | .setFlags(Collections.singleton(OFFlowModFlags.SEND_FLOW_REM)) |
| 139 | .setPriority(flowRule().priority()) |
alshabib | 08d9898 | 2015-04-21 16:25:50 -0700 | [diff] [blame] | 140 | .setTableId(TableId.of(flowRule().tableId())) |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 141 | .build(); |
| 142 | |
| 143 | return fm; |
| 144 | } |
| 145 | |
| 146 | @Override |
| 147 | public OFFlowMod buildFlowMod() { |
| 148 | Match match = buildMatch(); |
alshabib | 346b5b3 | 2015-03-06 00:42:16 -0800 | [diff] [blame] | 149 | List<OFAction> deferredActions = buildActions(treatment.deferred()); |
| 150 | List<OFAction> immediateActions = buildActions(treatment.immediate()); |
| 151 | List<OFInstruction> instructions = Lists.newLinkedList(); |
Jonathan Hart | eda3387 | 2015-02-18 10:22:32 -0800 | [diff] [blame] | 152 | |
alshabib | 346b5b3 | 2015-03-06 00:42:16 -0800 | [diff] [blame] | 153 | |
| 154 | if (immediateActions.size() > 0) { |
| 155 | instructions.add(factory().instructions().applyActions(immediateActions)); |
| 156 | } |
| 157 | if (treatment.clearedDeferred()) { |
| 158 | instructions.add(factory().instructions().clearActions()); |
| 159 | } |
| 160 | if (deferredActions.size() > 0) { |
| 161 | instructions.add(factory().instructions().writeActions(deferredActions)); |
| 162 | } |
| 163 | if (treatment.tableTransition() != null) { |
| 164 | instructions.add(buildTableGoto(treatment.tableTransition())); |
Saurav Das | cbe6de3 | 2015-03-01 18:30:46 -0800 | [diff] [blame] | 165 | } |
Saurav Das | 86af8f1 | 2015-05-25 23:55:33 -0700 | [diff] [blame] | 166 | if (treatment.writeMetadata() != null) { |
| 167 | instructions.add(buildMetadata(treatment.writeMetadata())); |
| 168 | } |
alshabib | 10c810b | 2015-08-18 16:59:04 -0700 | [diff] [blame] | 169 | if (treatment.metered() != null) { |
| 170 | instructions.add(buildMeter(treatment.metered())); |
| 171 | } |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 172 | |
| 173 | long cookie = flowRule().id().value(); |
| 174 | |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 175 | OFFlowMod fm = factory().buildFlowModify() |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 176 | .setXid(xid) |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 177 | .setCookie(U64.of(cookie)) |
| 178 | .setBufferId(OFBufferId.NO_BUFFER) |
alshabib | 9af7007 | 2015-02-09 14:34:16 -0800 | [diff] [blame] | 179 | .setInstructions(instructions) |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 180 | .setMatch(match) |
| 181 | .setFlags(Collections.singleton(OFFlowModFlags.SEND_FLOW_REM)) |
| 182 | .setPriority(flowRule().priority()) |
alshabib | 08d9898 | 2015-04-21 16:25:50 -0700 | [diff] [blame] | 183 | .setTableId(TableId.of(flowRule().tableId())) |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 184 | .build(); |
| 185 | |
| 186 | return fm; |
| 187 | } |
| 188 | |
| 189 | @Override |
| 190 | public OFFlowDelete buildFlowDel() { |
| 191 | Match match = buildMatch(); |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 192 | |
| 193 | long cookie = flowRule().id().value(); |
| 194 | |
| 195 | OFFlowDelete fm = factory().buildFlowDelete() |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 196 | .setXid(xid) |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 197 | .setCookie(U64.of(cookie)) |
| 198 | .setBufferId(OFBufferId.NO_BUFFER) |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 199 | .setMatch(match) |
| 200 | .setFlags(Collections.singleton(OFFlowModFlags.SEND_FLOW_REM)) |
| 201 | .setPriority(flowRule().priority()) |
alshabib | 08d9898 | 2015-04-21 16:25:50 -0700 | [diff] [blame] | 202 | .setTableId(TableId.of(flowRule().tableId())) |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 203 | .build(); |
| 204 | |
| 205 | return fm; |
| 206 | } |
| 207 | |
alshabib | 346b5b3 | 2015-03-06 00:42:16 -0800 | [diff] [blame] | 208 | private List<OFAction> buildActions(List<Instruction> treatments) { |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 209 | if (treatment == null) { |
Sho SHIMIZU | 6601dd7 | 2015-05-11 19:00:29 -0700 | [diff] [blame] | 210 | return Collections.emptyList(); |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 211 | } |
Sho SHIMIZU | 6601dd7 | 2015-05-11 19:00:29 -0700 | [diff] [blame] | 212 | |
| 213 | boolean tableFound = false; |
| 214 | List<OFAction> actions = new LinkedList<>(); |
alshabib | 346b5b3 | 2015-03-06 00:42:16 -0800 | [diff] [blame] | 215 | for (Instruction i : treatments) { |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 216 | switch (i.type()) { |
alshabib | 9af7007 | 2015-02-09 14:34:16 -0800 | [diff] [blame] | 217 | case DROP: |
Charles Chan | 7efabeb | 2015-09-28 15:12:19 -0700 | [diff] [blame^] | 218 | case NOACTION: |
Sho SHIMIZU | b35ed36 | 2015-04-28 11:05:19 -0700 | [diff] [blame] | 219 | return Collections.emptyList(); |
alshabib | 9af7007 | 2015-02-09 14:34:16 -0800 | [diff] [blame] | 220 | case L0MODIFICATION: |
| 221 | actions.add(buildL0Modification(i)); |
| 222 | break; |
| 223 | case L2MODIFICATION: |
| 224 | actions.add(buildL2Modification(i)); |
| 225 | break; |
| 226 | case L3MODIFICATION: |
| 227 | actions.add(buildL3Modification(i)); |
| 228 | break; |
Hyunsun Moon | c8bd97c | 2015-07-18 22:47:33 -0700 | [diff] [blame] | 229 | case L4MODIFICATION: |
| 230 | actions.add(buildL4Modification(i)); |
| 231 | break; |
alshabib | 9af7007 | 2015-02-09 14:34:16 -0800 | [diff] [blame] | 232 | case OUTPUT: |
| 233 | OutputInstruction out = (OutputInstruction) i; |
| 234 | OFActionOutput.Builder action = factory().actions().buildOutput() |
| 235 | .setPort(OFPort.of((int) out.port().toLong())); |
| 236 | if (out.port().equals(PortNumber.CONTROLLER)) { |
| 237 | action.setMaxLen(OFPCML_NO_BUFFER); |
| 238 | } |
| 239 | actions.add(action.build()); |
| 240 | break; |
| 241 | case GROUP: |
| 242 | GroupInstruction group = (GroupInstruction) i; |
| 243 | OFActionGroup.Builder groupBuilder = factory().actions().buildGroup() |
| 244 | .setGroup(OFGroup.of(group.groupId().id())); |
| 245 | actions.add(groupBuilder.build()); |
| 246 | break; |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 247 | case TABLE: |
| 248 | //FIXME: should not occur here. |
Saurav Das | cbe6de3 | 2015-03-01 18:30:46 -0800 | [diff] [blame] | 249 | tableFound = true; |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 250 | break; |
alshabib | 9af7007 | 2015-02-09 14:34:16 -0800 | [diff] [blame] | 251 | default: |
| 252 | log.warn("Instruction type {} not yet implemented.", i.type()); |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 253 | } |
| 254 | } |
Saurav Das | cbe6de3 | 2015-03-01 18:30:46 -0800 | [diff] [blame] | 255 | if (tableFound && actions.isEmpty()) { |
| 256 | // handles the case where there are no actions, but there is |
| 257 | // a goto instruction for the next table |
Sho SHIMIZU | 6601dd7 | 2015-05-11 19:00:29 -0700 | [diff] [blame] | 258 | return Collections.emptyList(); |
Saurav Das | cbe6de3 | 2015-03-01 18:30:46 -0800 | [diff] [blame] | 259 | } |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 260 | return actions; |
| 261 | } |
| 262 | |
alshabib | 9af7007 | 2015-02-09 14:34:16 -0800 | [diff] [blame] | 263 | private OFInstruction buildTableGoto(Instructions.TableTypeTransition i) { |
| 264 | OFInstruction instruction = factory().instructions().gotoTable( |
alshabib | d17abc2 | 2015-04-21 18:26:35 -0700 | [diff] [blame] | 265 | TableId.of(i.tableId())); |
alshabib | 9af7007 | 2015-02-09 14:34:16 -0800 | [diff] [blame] | 266 | return instruction; |
| 267 | } |
| 268 | |
Saurav Das | 86af8f1 | 2015-05-25 23:55:33 -0700 | [diff] [blame] | 269 | private OFInstruction buildMetadata(Instructions.MetadataInstruction m) { |
| 270 | OFInstruction instruction = factory().instructions().writeMetadata( |
| 271 | U64.of(m.metadata()), U64.of(m.metadataMask())); |
| 272 | return instruction; |
| 273 | } |
| 274 | |
alshabib | 10c810b | 2015-08-18 16:59:04 -0700 | [diff] [blame] | 275 | private OFInstruction buildMeter(Instructions.MeterInstruction metered) { |
| 276 | return factory().instructions().meter(metered.meterId().id()); |
| 277 | } |
| 278 | |
| 279 | |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 280 | private OFAction buildL0Modification(Instruction i) { |
| 281 | L0ModificationInstruction l0m = (L0ModificationInstruction) i; |
| 282 | switch (l0m.subtype()) { |
Sho SHIMIZU | 5cd7ce9 | 2015-05-11 19:10:38 -0700 | [diff] [blame] | 283 | case LAMBDA: |
Sho SHIMIZU | e397ffe | 2015-05-12 16:24:23 -0700 | [diff] [blame] | 284 | return buildModLambdaInstruction((ModLambdaInstruction) i); |
| 285 | case OCH: |
| 286 | try { |
| 287 | return buildModOchSignalInstruction((ModOchSignalInstruction) i); |
Sho SHIMIZU | c17042d | 2015-05-28 12:07:23 -0700 | [diff] [blame] | 288 | } catch (NoMappingFoundException e) { |
Sho SHIMIZU | e397ffe | 2015-05-12 16:24:23 -0700 | [diff] [blame] | 289 | log.warn(e.getMessage()); |
| 290 | break; |
| 291 | } |
Sho SHIMIZU | 5cd7ce9 | 2015-05-11 19:10:38 -0700 | [diff] [blame] | 292 | default: |
| 293 | log.warn("Unimplemented action type {}.", l0m.subtype()); |
| 294 | break; |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 295 | } |
| 296 | return null; |
| 297 | } |
| 298 | |
Sho SHIMIZU | e397ffe | 2015-05-12 16:24:23 -0700 | [diff] [blame] | 299 | private OFAction buildModLambdaInstruction(ModLambdaInstruction instruction) { |
| 300 | return factory().actions().circuit(factory().oxms().ochSigidBasic( |
| 301 | new CircuitSignalID((byte) 1, (byte) 2, instruction.lambda(), (short) 1))); |
| 302 | } |
| 303 | |
| 304 | private OFAction buildModOchSignalInstruction(ModOchSignalInstruction instruction) { |
| 305 | OchSignal signal = instruction.lambda(); |
Sho SHIMIZU | c17042d | 2015-05-28 12:07:23 -0700 | [diff] [blame] | 306 | byte gridType = OpenFlowValueMapper.lookupGridType(signal.gridType()); |
| 307 | byte channelSpacing = OpenFlowValueMapper.lookupChannelSpacing(signal.channelSpacing()); |
Sho SHIMIZU | e397ffe | 2015-05-12 16:24:23 -0700 | [diff] [blame] | 308 | |
| 309 | return factory().actions().circuit(factory().oxms().ochSigidBasic( |
| 310 | new CircuitSignalID(gridType, channelSpacing, |
| 311 | (short) signal.spacingMultiplier(), (short) signal.slotGranularity()) |
| 312 | )); |
| 313 | } |
| 314 | |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 315 | private OFAction buildL2Modification(Instruction i) { |
| 316 | L2ModificationInstruction l2m = (L2ModificationInstruction) i; |
| 317 | ModEtherInstruction eth; |
| 318 | OFOxm<?> oxm = null; |
| 319 | switch (l2m.subtype()) { |
Praseed Balakrishnan | 8c67d17 | 2014-11-10 10:15:41 -0800 | [diff] [blame] | 320 | case ETH_DST: |
| 321 | eth = (ModEtherInstruction) l2m; |
| 322 | oxm = factory().oxms().ethDst(MacAddress.of(eth.mac().toLong())); |
| 323 | break; |
| 324 | case ETH_SRC: |
| 325 | eth = (ModEtherInstruction) l2m; |
| 326 | oxm = factory().oxms().ethSrc(MacAddress.of(eth.mac().toLong())); |
| 327 | break; |
| 328 | case VLAN_ID: |
| 329 | ModVlanIdInstruction vlanId = (ModVlanIdInstruction) l2m; |
| 330 | oxm = factory().oxms().vlanVid(OFVlanVidMatch.ofVlan(vlanId.vlanId().toShort())); |
| 331 | break; |
| 332 | case VLAN_PCP: |
| 333 | ModVlanPcpInstruction vlanPcp = (ModVlanPcpInstruction) l2m; |
| 334 | oxm = factory().oxms().vlanPcp(VlanPcp.of(vlanPcp.vlanPcp())); |
| 335 | break; |
| 336 | case MPLS_PUSH: |
| 337 | PushHeaderInstructions pushHeaderInstructions = |
| 338 | (PushHeaderInstructions) l2m; |
| 339 | return factory().actions().pushMpls(EthType.of(pushHeaderInstructions |
alshabib | 7b808c5 | 2015-06-26 14:22:24 -0700 | [diff] [blame] | 340 | .ethernetType().toShort())); |
Praseed Balakrishnan | 8c67d17 | 2014-11-10 10:15:41 -0800 | [diff] [blame] | 341 | case MPLS_POP: |
Jonathan Hart | 67fc097 | 2015-03-19 15:21:20 -0700 | [diff] [blame] | 342 | PushHeaderInstructions popHeaderInstructions = |
Praseed Balakrishnan | 8c67d17 | 2014-11-10 10:15:41 -0800 | [diff] [blame] | 343 | (PushHeaderInstructions) l2m; |
| 344 | return factory().actions().popMpls(EthType.of(popHeaderInstructions |
alshabib | 7b808c5 | 2015-06-26 14:22:24 -0700 | [diff] [blame] | 345 | .ethernetType().toShort())); |
Praseed Balakrishnan | 8c67d17 | 2014-11-10 10:15:41 -0800 | [diff] [blame] | 346 | case MPLS_LABEL: |
| 347 | ModMplsLabelInstruction mplsLabel = |
| 348 | (ModMplsLabelInstruction) l2m; |
HIGUCHI Yuta | 04b49fc | 2015-08-28 09:58:58 -0700 | [diff] [blame] | 349 | oxm = factory().oxms().mplsLabel(U32.of(mplsLabel.mplsLabel().toInt())); |
Praseed Balakrishnan | 8c67d17 | 2014-11-10 10:15:41 -0800 | [diff] [blame] | 350 | break; |
Saurav Das | 73a7dd4 | 2015-08-19 22:20:31 -0700 | [diff] [blame] | 351 | case MPLS_BOS: |
| 352 | ModMplsBosInstruction mplsBos = (ModMplsBosInstruction) l2m; |
| 353 | oxm = factory().oxms() |
| 354 | .mplsBos(mplsBos.mplsBos() ? OFBooleanValue.TRUE |
| 355 | : OFBooleanValue.FALSE); |
| 356 | break; |
sangho | 3f97a17d | 2015-01-29 22:56:29 -0800 | [diff] [blame] | 357 | case DEC_MPLS_TTL: |
| 358 | return factory().actions().decMplsTtl(); |
Saurav Das | fbe25c5 | 2015-03-04 11:12:00 -0800 | [diff] [blame] | 359 | case VLAN_POP: |
| 360 | return factory().actions().popVlan(); |
Jonathan Hart | 54b406b | 2015-03-06 16:24:14 -0800 | [diff] [blame] | 361 | case VLAN_PUSH: |
| 362 | PushHeaderInstructions pushVlanInstruction = (PushHeaderInstructions) l2m; |
| 363 | return factory().actions().pushVlan( |
alshabib | 7b808c5 | 2015-06-26 14:22:24 -0700 | [diff] [blame] | 364 | EthType.of(pushVlanInstruction.ethernetType().toShort())); |
Hyunsun Moon | a08c5d0 | 2015-07-14 17:53:00 -0700 | [diff] [blame] | 365 | case TUNNEL_ID: |
| 366 | ModTunnelIdInstruction tunnelId = (ModTunnelIdInstruction) l2m; |
| 367 | oxm = factory().oxms().tunnelId(U64.of(tunnelId.tunnelId())); |
| 368 | break; |
Praseed Balakrishnan | 8c67d17 | 2014-11-10 10:15:41 -0800 | [diff] [blame] | 369 | default: |
| 370 | log.warn("Unimplemented action type {}.", l2m.subtype()); |
| 371 | break; |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | if (oxm != null) { |
| 375 | return factory().actions().buildSetField().setField(oxm).build(); |
| 376 | } |
| 377 | return null; |
| 378 | } |
| 379 | |
| 380 | private OFAction buildL3Modification(Instruction i) { |
| 381 | L3ModificationInstruction l3m = (L3ModificationInstruction) i; |
| 382 | ModIPInstruction ip; |
Pavlin Radoslavov | 23e398d | 2014-11-05 15:17:57 -0800 | [diff] [blame] | 383 | Ip4Address ip4; |
Pavlin Radoslavov | febe82c | 2015-02-11 19:08:15 -0800 | [diff] [blame] | 384 | Ip6Address ip6; |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 385 | OFOxm<?> oxm = null; |
| 386 | switch (l3m.subtype()) { |
Sho SHIMIZU | 5cd7ce9 | 2015-05-11 19:10:38 -0700 | [diff] [blame] | 387 | case IPV4_SRC: |
| 388 | ip = (ModIPInstruction) i; |
| 389 | ip4 = ip.ip().getIp4Address(); |
| 390 | oxm = factory().oxms().ipv4Src(IPv4Address.of(ip4.toInt())); |
| 391 | break; |
| 392 | case IPV4_DST: |
| 393 | ip = (ModIPInstruction) i; |
| 394 | ip4 = ip.ip().getIp4Address(); |
| 395 | oxm = factory().oxms().ipv4Dst(IPv4Address.of(ip4.toInt())); |
| 396 | break; |
| 397 | case IPV6_SRC: |
| 398 | ip = (ModIPInstruction) i; |
| 399 | ip6 = ip.ip().getIp6Address(); |
| 400 | oxm = factory().oxms().ipv6Src(IPv6Address.of(ip6.toOctets())); |
| 401 | break; |
| 402 | case IPV6_DST: |
| 403 | ip = (ModIPInstruction) i; |
| 404 | ip6 = ip.ip().getIp6Address(); |
| 405 | oxm = factory().oxms().ipv6Dst(IPv6Address.of(ip6.toOctets())); |
| 406 | break; |
| 407 | case IPV6_FLABEL: |
| 408 | ModIPv6FlowLabelInstruction flowLabelInstruction = |
| 409 | (ModIPv6FlowLabelInstruction) i; |
| 410 | int flowLabel = flowLabelInstruction.flowLabel(); |
| 411 | oxm = factory().oxms().ipv6Flabel(IPv6FlowLabel.of(flowLabel)); |
| 412 | break; |
| 413 | case DEC_TTL: |
| 414 | return factory().actions().decNwTtl(); |
| 415 | case TTL_IN: |
| 416 | return factory().actions().copyTtlIn(); |
| 417 | case TTL_OUT: |
| 418 | return factory().actions().copyTtlOut(); |
| 419 | default: |
| 420 | log.warn("Unimplemented action type {}.", l3m.subtype()); |
| 421 | break; |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | if (oxm != null) { |
| 425 | return factory().actions().buildSetField().setField(oxm).build(); |
| 426 | } |
| 427 | return null; |
| 428 | } |
| 429 | |
Hyunsun Moon | c8bd97c | 2015-07-18 22:47:33 -0700 | [diff] [blame] | 430 | private OFAction buildL4Modification(Instruction i) { |
| 431 | L4ModificationInstruction l4m = (L4ModificationInstruction) i; |
| 432 | ModTransportPortInstruction tp; |
| 433 | OFOxm<?> oxm = null; |
| 434 | switch (l4m.subtype()) { |
| 435 | case TCP_SRC: |
| 436 | tp = (ModTransportPortInstruction) l4m; |
Hyunsun Moon | cf732fb | 2015-08-22 21:04:23 -0700 | [diff] [blame] | 437 | oxm = factory().oxms().tcpSrc(TransportPort.of(tp.port().toInt())); |
Hyunsun Moon | c8bd97c | 2015-07-18 22:47:33 -0700 | [diff] [blame] | 438 | break; |
| 439 | case TCP_DST: |
| 440 | tp = (ModTransportPortInstruction) l4m; |
Hyunsun Moon | cf732fb | 2015-08-22 21:04:23 -0700 | [diff] [blame] | 441 | oxm = factory().oxms().tcpDst(TransportPort.of(tp.port().toInt())); |
Hyunsun Moon | c8bd97c | 2015-07-18 22:47:33 -0700 | [diff] [blame] | 442 | break; |
| 443 | case UDP_SRC: |
| 444 | tp = (ModTransportPortInstruction) l4m; |
Hyunsun Moon | cf732fb | 2015-08-22 21:04:23 -0700 | [diff] [blame] | 445 | oxm = factory().oxms().udpSrc(TransportPort.of(tp.port().toInt())); |
Hyunsun Moon | c8bd97c | 2015-07-18 22:47:33 -0700 | [diff] [blame] | 446 | break; |
| 447 | case UDP_DST: |
| 448 | tp = (ModTransportPortInstruction) l4m; |
Hyunsun Moon | cf732fb | 2015-08-22 21:04:23 -0700 | [diff] [blame] | 449 | oxm = factory().oxms().udpDst(TransportPort.of(tp.port().toInt())); |
Hyunsun Moon | c8bd97c | 2015-07-18 22:47:33 -0700 | [diff] [blame] | 450 | break; |
| 451 | default: |
| 452 | log.warn("Unimplemented action type {}.", l4m.subtype()); |
| 453 | break; |
| 454 | } |
| 455 | |
| 456 | if (oxm != null) { |
| 457 | return factory().actions().buildSetField().setField(oxm).build(); |
| 458 | } |
| 459 | return null; |
| 460 | } |
| 461 | |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 462 | } |