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