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 | |
Thomas Vachuska | f4df005 | 2015-01-06 12:30:11 -0800 | [diff] [blame] | 18 | import org.onlab.packet.Ip4Address; |
| 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; |
| 24 | import org.onosproject.net.flow.instructions.L2ModificationInstruction; |
| 25 | import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModEtherInstruction; |
| 26 | import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModVlanIdInstruction; |
| 27 | import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModVlanPcpInstruction; |
| 28 | import org.onosproject.net.flow.instructions.L3ModificationInstruction; |
| 29 | import org.onosproject.net.flow.instructions.L3ModificationInstruction.ModIPInstruction; |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 30 | import org.projectfloodlight.openflow.protocol.OFFactory; |
| 31 | import org.projectfloodlight.openflow.protocol.OFFlowAdd; |
| 32 | import org.projectfloodlight.openflow.protocol.OFFlowDelete; |
| 33 | import org.projectfloodlight.openflow.protocol.OFFlowMod; |
| 34 | import org.projectfloodlight.openflow.protocol.OFFlowModFlags; |
| 35 | import org.projectfloodlight.openflow.protocol.action.OFAction; |
Thomas Vachuska | f4df005 | 2015-01-06 12:30:11 -0800 | [diff] [blame] | 36 | import org.projectfloodlight.openflow.protocol.action.OFActionOutput; |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 37 | import org.projectfloodlight.openflow.protocol.match.Match; |
| 38 | import org.projectfloodlight.openflow.types.IPv4Address; |
| 39 | import org.projectfloodlight.openflow.types.MacAddress; |
| 40 | import org.projectfloodlight.openflow.types.OFBufferId; |
| 41 | import org.projectfloodlight.openflow.types.OFPort; |
| 42 | import org.projectfloodlight.openflow.types.U64; |
| 43 | import org.projectfloodlight.openflow.types.VlanPcp; |
| 44 | import org.projectfloodlight.openflow.types.VlanVid; |
| 45 | import org.slf4j.Logger; |
| 46 | import org.slf4j.LoggerFactory; |
| 47 | |
Thomas Vachuska | f4df005 | 2015-01-06 12:30:11 -0800 | [diff] [blame] | 48 | import java.util.Collections; |
| 49 | import java.util.LinkedList; |
| 50 | import java.util.List; |
| 51 | import java.util.Optional; |
| 52 | |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 53 | /** |
| 54 | * Flow mod builder for OpenFlow 1.0. |
| 55 | */ |
| 56 | public class FlowModBuilderVer10 extends FlowModBuilder { |
| 57 | |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 58 | private final Logger log = LoggerFactory.getLogger(getClass()); |
Thomas Vachuska | f4df005 | 2015-01-06 12:30:11 -0800 | [diff] [blame] | 59 | private static final int OFPCML_NO_BUFFER = 0xffff; |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 60 | |
| 61 | private final TrafficTreatment treatment; |
| 62 | |
| 63 | /** |
| 64 | * Constructor for a flow mod builder for OpenFlow 1.0. |
| 65 | * |
| 66 | * @param flowRule the flow rule to transform into a flow mod |
| 67 | * @param factory the OpenFlow factory to use to build the flow mod |
Pavlin Radoslavov | 119fd5c | 2014-11-25 19:08:19 -0800 | [diff] [blame] | 68 | * @param xid the transaction ID |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 69 | */ |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 70 | protected FlowModBuilderVer10(FlowRule flowRule, |
| 71 | OFFactory factory, Optional<Long> xid) { |
| 72 | super(flowRule, factory, xid); |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 73 | |
| 74 | this.treatment = flowRule.treatment(); |
| 75 | } |
| 76 | |
| 77 | @Override |
| 78 | public OFFlowAdd buildFlowAdd() { |
| 79 | Match match = buildMatch(); |
| 80 | List<OFAction> actions = buildActions(); |
| 81 | |
| 82 | long cookie = flowRule().id().value(); |
| 83 | |
alshabib | 4785eec | 2014-12-04 16:45:45 -0800 | [diff] [blame] | 84 | |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 85 | OFFlowAdd fm = factory().buildFlowAdd() |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 86 | .setXid(xid) |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 87 | .setCookie(U64.of(cookie)) |
| 88 | .setBufferId(OFBufferId.NO_BUFFER) |
| 89 | .setActions(actions) |
| 90 | .setMatch(match) |
| 91 | .setFlags(Collections.singleton(OFFlowModFlags.SEND_FLOW_REM)) |
| 92 | .setPriority(flowRule().priority()) |
| 93 | .build(); |
| 94 | |
| 95 | return fm; |
| 96 | } |
| 97 | |
| 98 | @Override |
| 99 | public OFFlowMod buildFlowMod() { |
| 100 | Match match = buildMatch(); |
| 101 | List<OFAction> actions = buildActions(); |
| 102 | |
| 103 | long cookie = flowRule().id().value(); |
| 104 | |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 105 | OFFlowMod fm = factory().buildFlowModify() |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 106 | .setXid(xid) |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 107 | .setCookie(U64.of(cookie)) |
| 108 | .setBufferId(OFBufferId.NO_BUFFER) |
| 109 | .setActions(actions) |
| 110 | .setMatch(match) |
| 111 | .setFlags(Collections.singleton(OFFlowModFlags.SEND_FLOW_REM)) |
| 112 | .setPriority(flowRule().priority()) |
| 113 | .build(); |
| 114 | |
| 115 | return fm; |
| 116 | } |
| 117 | |
| 118 | @Override |
| 119 | public OFFlowDelete buildFlowDel() { |
| 120 | Match match = buildMatch(); |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 121 | |
| 122 | long cookie = flowRule().id().value(); |
| 123 | |
| 124 | OFFlowDelete fm = factory().buildFlowDelete() |
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 | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 128 | .setMatch(match) |
| 129 | .setFlags(Collections.singleton(OFFlowModFlags.SEND_FLOW_REM)) |
| 130 | .setPriority(flowRule().priority()) |
| 131 | .build(); |
| 132 | |
| 133 | return fm; |
| 134 | } |
| 135 | |
| 136 | private List<OFAction> buildActions() { |
| 137 | List<OFAction> acts = new LinkedList<>(); |
| 138 | if (treatment == null) { |
| 139 | return acts; |
| 140 | } |
| 141 | for (Instruction i : treatment.instructions()) { |
| 142 | switch (i.type()) { |
| 143 | case DROP: |
| 144 | log.warn("Saw drop action; assigning drop action"); |
| 145 | return new LinkedList<>(); |
| 146 | case L2MODIFICATION: |
| 147 | acts.add(buildL2Modification(i)); |
| 148 | break; |
| 149 | case L3MODIFICATION: |
| 150 | acts.add(buildL3Modification(i)); |
| 151 | break; |
| 152 | case OUTPUT: |
| 153 | OutputInstruction out = (OutputInstruction) i; |
Thomas Vachuska | f4df005 | 2015-01-06 12:30:11 -0800 | [diff] [blame] | 154 | OFActionOutput.Builder action = factory().actions().buildOutput() |
| 155 | .setPort(OFPort.of((int) out.port().toLong())); |
| 156 | if (out.port().equals(PortNumber.CONTROLLER)) { |
| 157 | action.setMaxLen(OFPCML_NO_BUFFER); |
| 158 | } |
| 159 | acts.add(action.build()); |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 160 | break; |
| 161 | case L0MODIFICATION: |
| 162 | case GROUP: |
| 163 | log.warn("Instruction type {} not supported with protocol version {}", |
| 164 | i.type(), factory().getVersion()); |
| 165 | break; |
| 166 | default: |
| 167 | log.warn("Instruction type {} not yet implemented.", i.type()); |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | return acts; |
| 172 | } |
| 173 | |
| 174 | private OFAction buildL3Modification(Instruction i) { |
| 175 | L3ModificationInstruction l3m = (L3ModificationInstruction) i; |
| 176 | ModIPInstruction ip; |
Pavlin Radoslavov | 23e398d | 2014-11-05 15:17:57 -0800 | [diff] [blame] | 177 | Ip4Address ip4; |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 178 | switch (l3m.subtype()) { |
Pavlin Radoslavov | febe82c | 2015-02-11 19:08:15 -0800 | [diff] [blame] | 179 | case IPV4_SRC: |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 180 | ip = (ModIPInstruction) i; |
Pavlin Radoslavov | 23e398d | 2014-11-05 15:17:57 -0800 | [diff] [blame] | 181 | ip4 = ip.ip().getIp4Address(); |
| 182 | return factory().actions().setNwSrc(IPv4Address.of(ip4.toInt())); |
Pavlin Radoslavov | febe82c | 2015-02-11 19:08:15 -0800 | [diff] [blame] | 183 | case IPV4_DST: |
| 184 | ip = (ModIPInstruction) i; |
| 185 | ip4 = ip.ip().getIp4Address(); |
| 186 | return factory().actions().setNwDst(IPv4Address.of(ip4.toInt())); |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 187 | default: |
| 188 | log.warn("Unimplemented action type {}.", l3m.subtype()); |
| 189 | break; |
| 190 | } |
| 191 | return null; |
| 192 | } |
| 193 | |
| 194 | private OFAction buildL2Modification(Instruction i) { |
| 195 | L2ModificationInstruction l2m = (L2ModificationInstruction) i; |
| 196 | ModEtherInstruction eth; |
| 197 | switch (l2m.subtype()) { |
| 198 | case ETH_DST: |
| 199 | eth = (ModEtherInstruction) l2m; |
| 200 | return factory().actions().setDlDst(MacAddress.of(eth.mac().toLong())); |
| 201 | case ETH_SRC: |
| 202 | eth = (ModEtherInstruction) l2m; |
| 203 | return factory().actions().setDlSrc(MacAddress.of(eth.mac().toLong())); |
| 204 | case VLAN_ID: |
| 205 | ModVlanIdInstruction vlanId = (ModVlanIdInstruction) l2m; |
Ray Milkey | 7808105 | 2014-11-05 10:38:12 -0800 | [diff] [blame] | 206 | return factory().actions().setVlanVid(VlanVid.ofVlan(vlanId.vlanId().toShort())); |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 207 | case VLAN_PCP: |
| 208 | ModVlanPcpInstruction vlanPcp = (ModVlanPcpInstruction) l2m; |
| 209 | return factory().actions().setVlanPcp(VlanPcp.of(vlanPcp.vlanPcp())); |
alshabib | ab21b2d | 2015-03-04 18:35:33 -0800 | [diff] [blame] | 210 | case STRIP_VLAN: |
| 211 | return factory().actions().stripVlan(); |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 212 | default: |
| 213 | log.warn("Unimplemented action type {}.", l2m.subtype()); |
| 214 | break; |
| 215 | } |
| 216 | return null; |
| 217 | } |
| 218 | |
| 219 | } |