Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 1 | /* |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 2 | * Copyright 2014 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 | |
| 18 | import java.util.Collections; |
| 19 | import java.util.LinkedList; |
| 20 | import java.util.List; |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 21 | import java.util.Optional; |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 22 | |
Charles M.C. Chan | fe42181 | 2015-01-12 18:20:51 +0800 | [diff] [blame] | 23 | import org.onosproject.net.PortNumber; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 24 | import org.onosproject.net.flow.FlowRule; |
| 25 | import org.onosproject.net.flow.TrafficTreatment; |
| 26 | import org.onosproject.net.flow.instructions.Instruction; |
| 27 | import org.onosproject.net.flow.instructions.Instructions.OutputInstruction; |
| 28 | import org.onosproject.net.flow.instructions.L0ModificationInstruction; |
| 29 | import org.onosproject.net.flow.instructions.L0ModificationInstruction.ModLambdaInstruction; |
| 30 | import org.onosproject.net.flow.instructions.L2ModificationInstruction; |
| 31 | import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModEtherInstruction; |
| 32 | import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModVlanIdInstruction; |
| 33 | import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModVlanPcpInstruction; |
| 34 | import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModMplsLabelInstruction; |
| 35 | import org.onosproject.net.flow.instructions.L2ModificationInstruction.PushHeaderInstructions; |
| 36 | import org.onosproject.net.flow.instructions.L3ModificationInstruction; |
| 37 | import org.onosproject.net.flow.instructions.L3ModificationInstruction.ModIPInstruction; |
Pavlin Radoslavov | 23e398d | 2014-11-05 15:17:57 -0800 | [diff] [blame] | 38 | import org.onlab.packet.Ip4Address; |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 39 | import org.projectfloodlight.openflow.protocol.OFFactory; |
| 40 | import org.projectfloodlight.openflow.protocol.OFFlowAdd; |
| 41 | import org.projectfloodlight.openflow.protocol.OFFlowDelete; |
| 42 | import org.projectfloodlight.openflow.protocol.OFFlowMod; |
| 43 | import org.projectfloodlight.openflow.protocol.OFFlowModFlags; |
| 44 | import org.projectfloodlight.openflow.protocol.action.OFAction; |
Charles M.C. Chan | fe42181 | 2015-01-12 18:20:51 +0800 | [diff] [blame] | 45 | import org.projectfloodlight.openflow.protocol.action.OFActionOutput; |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 46 | import org.projectfloodlight.openflow.protocol.match.Match; |
| 47 | import org.projectfloodlight.openflow.protocol.oxm.OFOxm; |
| 48 | import org.projectfloodlight.openflow.types.CircuitSignalID; |
Praseed Balakrishnan | 8c67d17 | 2014-11-10 10:15:41 -0800 | [diff] [blame] | 49 | import org.projectfloodlight.openflow.types.EthType; |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 50 | import org.projectfloodlight.openflow.types.IPv4Address; |
| 51 | import org.projectfloodlight.openflow.types.MacAddress; |
| 52 | import org.projectfloodlight.openflow.types.OFBufferId; |
| 53 | import org.projectfloodlight.openflow.types.OFPort; |
| 54 | import org.projectfloodlight.openflow.types.OFVlanVidMatch; |
Praseed Balakrishnan | 8c67d17 | 2014-11-10 10:15:41 -0800 | [diff] [blame] | 55 | import org.projectfloodlight.openflow.types.U32; |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 56 | import org.projectfloodlight.openflow.types.U64; |
| 57 | import org.projectfloodlight.openflow.types.VlanPcp; |
| 58 | import org.slf4j.Logger; |
| 59 | import org.slf4j.LoggerFactory; |
| 60 | |
| 61 | /** |
| 62 | * Flow mod builder for OpenFlow 1.3+. |
| 63 | */ |
| 64 | public class FlowModBuilderVer13 extends FlowModBuilder { |
| 65 | |
| 66 | private static final Logger log = LoggerFactory.getLogger(FlowModBuilderVer10.class); |
Charles M.C. Chan | fe42181 | 2015-01-12 18:20:51 +0800 | [diff] [blame] | 67 | private static final int OFPCML_NO_BUFFER = 0xffff; |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 68 | |
| 69 | private final TrafficTreatment treatment; |
| 70 | |
| 71 | /** |
| 72 | * Constructor for a flow mod builder for OpenFlow 1.3. |
| 73 | * |
| 74 | * @param flowRule the flow rule to transform into a flow mod |
| 75 | * @param factory the OpenFlow factory to use to build the flow mod |
Pavlin Radoslavov | 119fd5c | 2014-11-25 19:08:19 -0800 | [diff] [blame] | 76 | * @param xid the transaction ID |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 77 | */ |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 78 | protected FlowModBuilderVer13(FlowRule flowRule, OFFactory factory, Optional<Long> xid) { |
| 79 | super(flowRule, factory, xid); |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 80 | |
| 81 | this.treatment = flowRule.treatment(); |
| 82 | } |
| 83 | |
| 84 | @Override |
| 85 | public OFFlowAdd buildFlowAdd() { |
| 86 | Match match = buildMatch(); |
Jonathan Hart | d4a8bba | 2014-10-28 12:44:20 -0700 | [diff] [blame] | 87 | List<OFAction> actions = buildActions(); |
| 88 | |
| 89 | // FIXME had to revert back to using apply-actions instead of |
| 90 | // write-actions because LINC-OE apparently doesn't support |
| 91 | // write-actions. I would prefer to change this back in the future |
| 92 | // because apply-actions is an optional instruction in OF 1.3. |
| 93 | |
| 94 | //OFInstruction writeActions = |
| 95 | //factory().instructions().writeActions(actions); |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 96 | |
| 97 | long cookie = flowRule().id().value(); |
| 98 | |
alshabib | 4785eec | 2014-12-04 16:45:45 -0800 | [diff] [blame] | 99 | |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 100 | OFFlowAdd fm = factory().buildFlowAdd() |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 101 | .setXid(xid) |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 102 | .setCookie(U64.of(cookie)) |
| 103 | .setBufferId(OFBufferId.NO_BUFFER) |
Jonathan Hart | d4a8bba | 2014-10-28 12:44:20 -0700 | [diff] [blame] | 104 | .setActions(actions) |
| 105 | //.setInstructions(Collections.singletonList(writeActions)) |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 106 | .setMatch(match) |
| 107 | .setFlags(Collections.singleton(OFFlowModFlags.SEND_FLOW_REM)) |
| 108 | .setPriority(flowRule().priority()) |
| 109 | .build(); |
| 110 | |
| 111 | return fm; |
| 112 | } |
| 113 | |
| 114 | @Override |
| 115 | public OFFlowMod buildFlowMod() { |
| 116 | Match match = buildMatch(); |
Jonathan Hart | d4a8bba | 2014-10-28 12:44:20 -0700 | [diff] [blame] | 117 | List<OFAction> actions = buildActions(); |
| 118 | //OFInstruction writeActions = |
| 119 | //factory().instructions().writeActions(actions); |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 120 | |
| 121 | long cookie = flowRule().id().value(); |
| 122 | |
alshabib | 4785eec | 2014-12-04 16:45:45 -0800 | [diff] [blame] | 123 | |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 124 | OFFlowMod fm = factory().buildFlowModify() |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 125 | .setXid(xid) |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 126 | .setCookie(U64.of(cookie)) |
| 127 | .setBufferId(OFBufferId.NO_BUFFER) |
Jonathan Hart | d4a8bba | 2014-10-28 12:44:20 -0700 | [diff] [blame] | 128 | .setActions(actions) |
| 129 | //.setInstructions(Collections.singletonList(writeActions)) |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 130 | .setMatch(match) |
| 131 | .setFlags(Collections.singleton(OFFlowModFlags.SEND_FLOW_REM)) |
| 132 | .setPriority(flowRule().priority()) |
| 133 | .build(); |
| 134 | |
| 135 | return fm; |
| 136 | } |
| 137 | |
| 138 | @Override |
| 139 | public OFFlowDelete buildFlowDel() { |
| 140 | Match match = buildMatch(); |
Jonathan Hart | d4a8bba | 2014-10-28 12:44:20 -0700 | [diff] [blame] | 141 | List<OFAction> actions = buildActions(); |
| 142 | //OFInstruction writeActions = |
| 143 | //factory().instructions().writeActions(actions); |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 144 | |
| 145 | long cookie = flowRule().id().value(); |
| 146 | |
| 147 | OFFlowDelete fm = factory().buildFlowDelete() |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 148 | .setXid(xid) |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 149 | .setCookie(U64.of(cookie)) |
| 150 | .setBufferId(OFBufferId.NO_BUFFER) |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 151 | .setMatch(match) |
| 152 | .setFlags(Collections.singleton(OFFlowModFlags.SEND_FLOW_REM)) |
| 153 | .setPriority(flowRule().priority()) |
| 154 | .build(); |
| 155 | |
| 156 | return fm; |
| 157 | } |
| 158 | |
| 159 | private List<OFAction> buildActions() { |
| 160 | List<OFAction> actions = new LinkedList<>(); |
| 161 | if (treatment == null) { |
| 162 | return actions; |
| 163 | } |
| 164 | for (Instruction i : treatment.instructions()) { |
| 165 | switch (i.type()) { |
| 166 | case DROP: |
| 167 | log.warn("Saw drop action; assigning drop action"); |
| 168 | return new LinkedList<>(); |
| 169 | case L0MODIFICATION: |
| 170 | actions.add(buildL0Modification(i)); |
| 171 | break; |
| 172 | case L2MODIFICATION: |
| 173 | actions.add(buildL2Modification(i)); |
| 174 | break; |
| 175 | case L3MODIFICATION: |
| 176 | actions.add(buildL3Modification(i)); |
| 177 | break; |
| 178 | case OUTPUT: |
| 179 | OutputInstruction out = (OutputInstruction) i; |
Charles M.C. Chan | fe42181 | 2015-01-12 18:20:51 +0800 | [diff] [blame] | 180 | OFActionOutput.Builder action = factory().actions().buildOutput() |
| 181 | .setPort(OFPort.of((int) out.port().toLong())); |
| 182 | if (out.port().equals(PortNumber.CONTROLLER)) { |
| 183 | action.setMaxLen(OFPCML_NO_BUFFER); |
| 184 | } |
| 185 | actions.add(action.build()); |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 186 | break; |
| 187 | case GROUP: |
| 188 | default: |
| 189 | log.warn("Instruction type {} not yet implemented.", i.type()); |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | return actions; |
| 194 | } |
| 195 | |
| 196 | private OFAction buildL0Modification(Instruction i) { |
| 197 | L0ModificationInstruction l0m = (L0ModificationInstruction) i; |
| 198 | switch (l0m.subtype()) { |
| 199 | case LAMBDA: |
| 200 | ModLambdaInstruction ml = (ModLambdaInstruction) i; |
| 201 | return factory().actions().circuit(factory().oxms().ochSigidBasic( |
| 202 | new CircuitSignalID((byte) 1, (byte) 2, ml.lambda(), (short) 1))); |
| 203 | default: |
| 204 | log.warn("Unimplemented action type {}.", l0m.subtype()); |
| 205 | break; |
| 206 | } |
| 207 | return null; |
| 208 | } |
| 209 | |
| 210 | private OFAction buildL2Modification(Instruction i) { |
| 211 | L2ModificationInstruction l2m = (L2ModificationInstruction) i; |
| 212 | ModEtherInstruction eth; |
| 213 | OFOxm<?> oxm = null; |
| 214 | switch (l2m.subtype()) { |
Praseed Balakrishnan | 8c67d17 | 2014-11-10 10:15:41 -0800 | [diff] [blame] | 215 | case ETH_DST: |
| 216 | eth = (ModEtherInstruction) l2m; |
| 217 | oxm = factory().oxms().ethDst(MacAddress.of(eth.mac().toLong())); |
| 218 | break; |
| 219 | case ETH_SRC: |
| 220 | eth = (ModEtherInstruction) l2m; |
| 221 | oxm = factory().oxms().ethSrc(MacAddress.of(eth.mac().toLong())); |
| 222 | break; |
| 223 | case VLAN_ID: |
| 224 | ModVlanIdInstruction vlanId = (ModVlanIdInstruction) l2m; |
| 225 | oxm = factory().oxms().vlanVid(OFVlanVidMatch.ofVlan(vlanId.vlanId().toShort())); |
| 226 | break; |
| 227 | case VLAN_PCP: |
| 228 | ModVlanPcpInstruction vlanPcp = (ModVlanPcpInstruction) l2m; |
| 229 | oxm = factory().oxms().vlanPcp(VlanPcp.of(vlanPcp.vlanPcp())); |
| 230 | break; |
| 231 | case MPLS_PUSH: |
| 232 | PushHeaderInstructions pushHeaderInstructions = |
| 233 | (PushHeaderInstructions) l2m; |
| 234 | return factory().actions().pushMpls(EthType.of(pushHeaderInstructions |
| 235 | .ethernetType().getEtherType())); |
| 236 | case MPLS_POP: |
| 237 | PushHeaderInstructions popHeaderInstructions = |
| 238 | (PushHeaderInstructions) l2m; |
| 239 | return factory().actions().popMpls(EthType.of(popHeaderInstructions |
| 240 | .ethernetType().getEtherType())); |
| 241 | case MPLS_LABEL: |
| 242 | ModMplsLabelInstruction mplsLabel = |
| 243 | (ModMplsLabelInstruction) l2m; |
| 244 | oxm = factory().oxms().mplsLabel(U32.of(mplsLabel.label() |
| 245 | .longValue())); |
| 246 | |
| 247 | break; |
| 248 | default: |
| 249 | log.warn("Unimplemented action type {}.", l2m.subtype()); |
| 250 | break; |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | if (oxm != null) { |
| 254 | return factory().actions().buildSetField().setField(oxm).build(); |
| 255 | } |
| 256 | return null; |
| 257 | } |
| 258 | |
| 259 | private OFAction buildL3Modification(Instruction i) { |
| 260 | L3ModificationInstruction l3m = (L3ModificationInstruction) i; |
| 261 | ModIPInstruction ip; |
Pavlin Radoslavov | 23e398d | 2014-11-05 15:17:57 -0800 | [diff] [blame] | 262 | Ip4Address ip4; |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 263 | OFOxm<?> oxm = null; |
| 264 | switch (l3m.subtype()) { |
| 265 | case IP_DST: |
| 266 | ip = (ModIPInstruction) i; |
Pavlin Radoslavov | 23e398d | 2014-11-05 15:17:57 -0800 | [diff] [blame] | 267 | ip4 = ip.ip().getIp4Address(); |
| 268 | oxm = factory().oxms().ipv4Dst(IPv4Address.of(ip4.toInt())); |
Ray Milkey | 241b96a | 2014-11-17 13:08:20 -0800 | [diff] [blame] | 269 | break; |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 270 | case IP_SRC: |
| 271 | ip = (ModIPInstruction) i; |
Pavlin Radoslavov | 23e398d | 2014-11-05 15:17:57 -0800 | [diff] [blame] | 272 | ip4 = ip.ip().getIp4Address(); |
| 273 | oxm = factory().oxms().ipv4Src(IPv4Address.of(ip4.toInt())); |
Ray Milkey | 241b96a | 2014-11-17 13:08:20 -0800 | [diff] [blame] | 274 | break; |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 275 | default: |
| 276 | log.warn("Unimplemented action type {}.", l3m.subtype()); |
| 277 | break; |
| 278 | } |
| 279 | |
| 280 | if (oxm != null) { |
| 281 | return factory().actions().buildSetField().setField(oxm).build(); |
| 282 | } |
| 283 | return null; |
| 284 | } |
| 285 | |
| 286 | } |