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