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