alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 1 | package org.onlab.onos.provider.of.flow.impl; |
| 2 | |
| 3 | import static org.slf4j.LoggerFactory.getLogger; |
| 4 | |
| 5 | import java.util.Collections; |
| 6 | import java.util.LinkedList; |
| 7 | import java.util.List; |
| 8 | |
alshabib | 6b5cfec | 2014-09-18 17:42:18 -0700 | [diff] [blame] | 9 | import org.onlab.onos.net.flow.FlowId; |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 10 | import org.onlab.onos.net.flow.FlowRule; |
| 11 | import org.onlab.onos.net.flow.TrafficSelector; |
| 12 | import org.onlab.onos.net.flow.TrafficTreatment; |
| 13 | import org.onlab.onos.net.flow.criteria.Criteria.EthCriterion; |
| 14 | import org.onlab.onos.net.flow.criteria.Criteria.EthTypeCriterion; |
| 15 | import org.onlab.onos.net.flow.criteria.Criteria.IPCriterion; |
| 16 | import org.onlab.onos.net.flow.criteria.Criteria.IPProtocolCriterion; |
Marc De Leenheer | 4908775 | 2014-10-23 13:54:09 -0700 | [diff] [blame] | 17 | import org.onlab.onos.net.flow.criteria.Criteria.LambdaCriterion; |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 18 | import org.onlab.onos.net.flow.criteria.Criteria.PortCriterion; |
Jonathan Hart | 34bc614 | 2014-10-17 11:00:43 -0700 | [diff] [blame] | 19 | import org.onlab.onos.net.flow.criteria.Criteria.TcpPortCriterion; |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 20 | import org.onlab.onos.net.flow.criteria.Criteria.VlanIdCriterion; |
| 21 | import org.onlab.onos.net.flow.criteria.Criteria.VlanPcpCriterion; |
| 22 | import org.onlab.onos.net.flow.criteria.Criterion; |
| 23 | import org.onlab.onos.net.flow.instructions.Instruction; |
| 24 | import org.onlab.onos.net.flow.instructions.Instructions.OutputInstruction; |
Marc De Leenheer | 4908775 | 2014-10-23 13:54:09 -0700 | [diff] [blame] | 25 | import org.onlab.onos.net.flow.instructions.L0ModificationInstruction; |
| 26 | import org.onlab.onos.net.flow.instructions.L0ModificationInstruction.ModLambdaInstruction; |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 27 | import org.onlab.onos.net.flow.instructions.L2ModificationInstruction; |
| 28 | import org.onlab.onos.net.flow.instructions.L2ModificationInstruction.ModEtherInstruction; |
| 29 | import org.onlab.onos.net.flow.instructions.L2ModificationInstruction.ModVlanIdInstruction; |
| 30 | import org.onlab.onos.net.flow.instructions.L2ModificationInstruction.ModVlanPcpInstruction; |
| 31 | import org.onlab.onos.net.flow.instructions.L3ModificationInstruction; |
| 32 | import org.onlab.onos.net.flow.instructions.L3ModificationInstruction.ModIPInstruction; |
| 33 | import org.projectfloodlight.openflow.protocol.OFFactory; |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 34 | import org.projectfloodlight.openflow.protocol.OFFlowAdd; |
| 35 | import org.projectfloodlight.openflow.protocol.OFFlowDelete; |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 36 | import org.projectfloodlight.openflow.protocol.OFFlowMod; |
| 37 | import org.projectfloodlight.openflow.protocol.OFFlowModFlags; |
| 38 | import org.projectfloodlight.openflow.protocol.action.OFAction; |
| 39 | import org.projectfloodlight.openflow.protocol.match.Match; |
| 40 | import org.projectfloodlight.openflow.protocol.match.MatchField; |
Marc De Leenheer | 4908775 | 2014-10-23 13:54:09 -0700 | [diff] [blame] | 41 | import org.projectfloodlight.openflow.types.CircuitSignalID; |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 42 | import org.projectfloodlight.openflow.types.EthType; |
| 43 | import org.projectfloodlight.openflow.types.IPv4Address; |
| 44 | import org.projectfloodlight.openflow.types.IpProtocol; |
| 45 | import org.projectfloodlight.openflow.types.MacAddress; |
| 46 | import org.projectfloodlight.openflow.types.Masked; |
| 47 | import org.projectfloodlight.openflow.types.OFBufferId; |
| 48 | import org.projectfloodlight.openflow.types.OFPort; |
| 49 | import org.projectfloodlight.openflow.types.OFVlanVidMatch; |
Jonathan Hart | 34bc614 | 2014-10-17 11:00:43 -0700 | [diff] [blame] | 50 | import org.projectfloodlight.openflow.types.TransportPort; |
alshabib | 6b5cfec | 2014-09-18 17:42:18 -0700 | [diff] [blame] | 51 | import org.projectfloodlight.openflow.types.U64; |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 52 | import org.projectfloodlight.openflow.types.VlanPcp; |
| 53 | import org.projectfloodlight.openflow.types.VlanVid; |
| 54 | import org.slf4j.Logger; |
| 55 | |
| 56 | |
| 57 | public class FlowModBuilder { |
| 58 | |
| 59 | private final Logger log = getLogger(getClass()); |
| 60 | |
| 61 | private final OFFactory factory; |
| 62 | private final TrafficTreatment treatment; |
| 63 | private final TrafficSelector selector; |
| 64 | |
| 65 | private final int priority; |
| 66 | |
alshabib | 6b5cfec | 2014-09-18 17:42:18 -0700 | [diff] [blame] | 67 | private final FlowId cookie; |
| 68 | |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 69 | |
| 70 | |
| 71 | public FlowModBuilder(FlowRule flowRule, OFFactory factory) { |
| 72 | this.factory = factory; |
| 73 | this.treatment = flowRule.treatment(); |
| 74 | this.selector = flowRule.selector(); |
| 75 | this.priority = flowRule.priority(); |
alshabib | 6b5cfec | 2014-09-18 17:42:18 -0700 | [diff] [blame] | 76 | this.cookie = flowRule.id(); |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 77 | } |
| 78 | |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 79 | public OFFlowAdd buildFlowAdd() { |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 80 | Match match = buildMatch(); |
| 81 | List<OFAction> actions = buildActions(); |
| 82 | |
| 83 | //TODO: what to do without bufferid? do we assume that there will be a pktout as well? |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 84 | OFFlowAdd fm = factory.buildFlowAdd() |
| 85 | .setXid(cookie.value()) |
alshabib | 6b5cfec | 2014-09-18 17:42:18 -0700 | [diff] [blame] | 86 | .setCookie(U64.of(cookie.value())) |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 87 | .setBufferId(OFBufferId.NO_BUFFER) |
| 88 | .setActions(actions) |
| 89 | .setMatch(match) |
| 90 | .setFlags(Collections.singleton(OFFlowModFlags.SEND_FLOW_REM)) |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 91 | .setPriority(priority) |
| 92 | .build(); |
| 93 | |
| 94 | return fm; |
| 95 | |
| 96 | } |
| 97 | |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 98 | public OFFlowMod buildFlowMod() { |
| 99 | Match match = buildMatch(); |
| 100 | List<OFAction> actions = buildActions(); |
| 101 | |
| 102 | //TODO: what to do without bufferid? do we assume that there will be a pktout as well? |
| 103 | OFFlowMod fm = factory.buildFlowModify() |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 104 | .setXid(cookie.value()) |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 105 | .setCookie(U64.of(cookie.value())) |
| 106 | .setBufferId(OFBufferId.NO_BUFFER) |
| 107 | .setActions(actions) |
| 108 | .setMatch(match) |
| 109 | .setFlags(Collections.singleton(OFFlowModFlags.SEND_FLOW_REM)) |
| 110 | .setPriority(priority) |
| 111 | .build(); |
| 112 | |
| 113 | return fm; |
| 114 | |
| 115 | } |
| 116 | |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 117 | public OFFlowDelete buildFlowDel() { |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 118 | Match match = buildMatch(); |
| 119 | List<OFAction> actions = buildActions(); |
| 120 | |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 121 | OFFlowDelete fm = factory.buildFlowDelete() |
| 122 | .setXid(cookie.value()) |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 123 | .setCookie(U64.of(cookie.value())) |
| 124 | .setBufferId(OFBufferId.NO_BUFFER) |
alshabib | 7814e9f | 2014-09-30 11:52:12 -0700 | [diff] [blame] | 125 | .setActions(actions) |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 126 | .setMatch(match) |
| 127 | .setFlags(Collections.singleton(OFFlowModFlags.SEND_FLOW_REM)) |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 128 | .setPriority(priority) |
| 129 | .build(); |
| 130 | |
| 131 | return fm; |
| 132 | } |
| 133 | |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 134 | private List<OFAction> buildActions() { |
| 135 | List<OFAction> acts = new LinkedList<>(); |
alshabib | 4906fab | 2014-09-29 23:58:12 -0700 | [diff] [blame] | 136 | if (treatment == null) { |
| 137 | return acts; |
| 138 | } |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 139 | for (Instruction i : treatment.instructions()) { |
| 140 | switch (i.type()) { |
| 141 | case DROP: |
| 142 | log.warn("Saw drop action; assigning drop action"); |
| 143 | return new LinkedList<>(); |
Marc De Leenheer | 4908775 | 2014-10-23 13:54:09 -0700 | [diff] [blame] | 144 | case L0MODIFICATION: |
| 145 | acts.add(buildL0Modification(i)); |
Marc De Leenheer | 9eb47ec | 2014-10-23 14:15:49 -0700 | [diff] [blame] | 146 | break; |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 147 | case L2MODIFICATION: |
| 148 | acts.add(buildL2Modification(i)); |
alshabib | f410eee | 2014-09-22 18:17:45 -0700 | [diff] [blame] | 149 | break; |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 150 | case L3MODIFICATION: |
| 151 | acts.add(buildL3Modification(i)); |
alshabib | f410eee | 2014-09-22 18:17:45 -0700 | [diff] [blame] | 152 | break; |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 153 | case OUTPUT: |
| 154 | OutputInstruction out = (OutputInstruction) i; |
| 155 | acts.add(factory.actions().buildOutput().setPort( |
| 156 | OFPort.of((int) out.port().toLong())).build()); |
| 157 | break; |
| 158 | case GROUP: |
| 159 | default: |
| 160 | log.warn("Instruction type {} not yet implemented.", i.type()); |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | return acts; |
| 165 | } |
| 166 | |
Marc De Leenheer | 4908775 | 2014-10-23 13:54:09 -0700 | [diff] [blame] | 167 | private OFAction buildL0Modification(Instruction i) { |
| 168 | L0ModificationInstruction l0m = (L0ModificationInstruction) i; |
| 169 | switch (l0m.subtype()) { |
| 170 | case LAMBDA: |
| 171 | ModLambdaInstruction ml = (ModLambdaInstruction) i; |
| 172 | return factory.actions().circuit(factory.oxms().ochSigidBasic( |
| 173 | new CircuitSignalID((byte) 1, (byte) 2, ml.lambda(), (short) 1))); |
| 174 | default: |
| 175 | log.warn("Unimplemented action type {}.", l0m.subtype()); |
| 176 | break; |
| 177 | } |
| 178 | return null; |
| 179 | } |
| 180 | |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 181 | private OFAction buildL3Modification(Instruction i) { |
| 182 | L3ModificationInstruction l3m = (L3ModificationInstruction) i; |
| 183 | ModIPInstruction ip; |
| 184 | switch (l3m.subtype()) { |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 185 | case IP_DST: |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 186 | ip = (ModIPInstruction) i; |
Jonathan Hart | bcae7bd | 2014-10-16 10:24:41 -0700 | [diff] [blame] | 187 | return factory.actions().setNwDst(IPv4Address.of(ip.ip().toInt())); |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 188 | case IP_SRC: |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 189 | ip = (ModIPInstruction) i; |
Jonathan Hart | bcae7bd | 2014-10-16 10:24:41 -0700 | [diff] [blame] | 190 | return factory.actions().setNwSrc(IPv4Address.of(ip.ip().toInt())); |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 191 | default: |
| 192 | log.warn("Unimplemented action type {}.", l3m.subtype()); |
| 193 | break; |
| 194 | } |
| 195 | return null; |
| 196 | } |
| 197 | |
| 198 | private OFAction buildL2Modification(Instruction i) { |
| 199 | L2ModificationInstruction l2m = (L2ModificationInstruction) i; |
| 200 | ModEtherInstruction eth; |
| 201 | switch (l2m.subtype()) { |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 202 | case ETH_DST: |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 203 | eth = (ModEtherInstruction) l2m; |
| 204 | return factory.actions().setDlDst(MacAddress.of(eth.mac().toLong())); |
alshabib | 99b8fdc | 2014-09-25 14:30:22 -0700 | [diff] [blame] | 205 | case ETH_SRC: |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 206 | eth = (ModEtherInstruction) l2m; |
| 207 | return factory.actions().setDlSrc(MacAddress.of(eth.mac().toLong())); |
| 208 | case VLAN_ID: |
| 209 | ModVlanIdInstruction vlanId = (ModVlanIdInstruction) l2m; |
| 210 | return factory.actions().setVlanVid(VlanVid.ofVlan(vlanId.vlanId.toShort())); |
| 211 | case VLAN_PCP: |
| 212 | ModVlanPcpInstruction vlanPcp = (ModVlanPcpInstruction) l2m; |
| 213 | return factory.actions().setVlanPcp(VlanPcp.of(vlanPcp.vlanPcp())); |
| 214 | default: |
| 215 | log.warn("Unimplemented action type {}.", l2m.subtype()); |
| 216 | break; |
| 217 | } |
| 218 | return null; |
| 219 | } |
| 220 | |
| 221 | private Match buildMatch() { |
| 222 | Match.Builder mBuilder = factory.buildMatch(); |
| 223 | EthCriterion eth; |
| 224 | IPCriterion ip; |
Jonathan Hart | 34bc614 | 2014-10-17 11:00:43 -0700 | [diff] [blame] | 225 | TcpPortCriterion tp; |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 226 | for (Criterion c : selector.criteria()) { |
| 227 | switch (c.type()) { |
| 228 | case IN_PORT: |
| 229 | PortCriterion inport = (PortCriterion) c; |
| 230 | mBuilder.setExact(MatchField.IN_PORT, OFPort.of((int) inport.port().toLong())); |
| 231 | break; |
| 232 | case ETH_SRC: |
| 233 | eth = (EthCriterion) c; |
| 234 | mBuilder.setExact(MatchField.ETH_SRC, MacAddress.of(eth.mac().toLong())); |
| 235 | break; |
| 236 | case ETH_DST: |
| 237 | eth = (EthCriterion) c; |
| 238 | mBuilder.setExact(MatchField.ETH_DST, MacAddress.of(eth.mac().toLong())); |
| 239 | break; |
| 240 | case ETH_TYPE: |
| 241 | EthTypeCriterion ethType = (EthTypeCriterion) c; |
| 242 | mBuilder.setExact(MatchField.ETH_TYPE, EthType.of(ethType.ethType())); |
| 243 | break; |
| 244 | case IPV4_DST: |
| 245 | ip = (IPCriterion) c; |
| 246 | if (ip.ip().isMasked()) { |
Jonathan Hart | bcae7bd | 2014-10-16 10:24:41 -0700 | [diff] [blame] | 247 | Masked<IPv4Address> maskedIp = Masked.of(IPv4Address.of(ip.ip().toInt()), |
| 248 | IPv4Address.of(ip.ip().netmask().toInt())); |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 249 | mBuilder.setMasked(MatchField.IPV4_DST, maskedIp); |
| 250 | } else { |
Jonathan Hart | bcae7bd | 2014-10-16 10:24:41 -0700 | [diff] [blame] | 251 | mBuilder.setExact(MatchField.IPV4_DST, IPv4Address.of(ip.ip().toInt())); |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 252 | } |
| 253 | break; |
| 254 | case IPV4_SRC: |
| 255 | ip = (IPCriterion) c; |
| 256 | if (ip.ip().isMasked()) { |
Jonathan Hart | bcae7bd | 2014-10-16 10:24:41 -0700 | [diff] [blame] | 257 | Masked<IPv4Address> maskedIp = Masked.of(IPv4Address.of(ip.ip().toInt()), |
| 258 | IPv4Address.of(ip.ip().netmask().toInt())); |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 259 | mBuilder.setMasked(MatchField.IPV4_SRC, maskedIp); |
| 260 | } else { |
Jonathan Hart | bcae7bd | 2014-10-16 10:24:41 -0700 | [diff] [blame] | 261 | mBuilder.setExact(MatchField.IPV4_SRC, IPv4Address.of(ip.ip().toInt())); |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 262 | } |
| 263 | break; |
| 264 | case IP_PROTO: |
| 265 | IPProtocolCriterion p = (IPProtocolCriterion) c; |
| 266 | mBuilder.setExact(MatchField.IP_PROTO, IpProtocol.of(p.protocol())); |
| 267 | break; |
| 268 | case VLAN_PCP: |
| 269 | VlanPcpCriterion vpcp = (VlanPcpCriterion) c; |
| 270 | mBuilder.setExact(MatchField.VLAN_PCP, VlanPcp.of(vpcp.priority())); |
| 271 | break; |
| 272 | case VLAN_VID: |
| 273 | VlanIdCriterion vid = (VlanIdCriterion) c; |
| 274 | mBuilder.setExact(MatchField.VLAN_VID, |
| 275 | OFVlanVidMatch.ofVlanVid(VlanVid.ofVlan(vid.vlanId().toShort()))); |
| 276 | break; |
Jonathan Hart | 34bc614 | 2014-10-17 11:00:43 -0700 | [diff] [blame] | 277 | case TCP_DST: |
| 278 | tp = (TcpPortCriterion) c; |
| 279 | mBuilder.setExact(MatchField.TCP_DST, TransportPort.of(tp.tcpPort())); |
| 280 | break; |
| 281 | case TCP_SRC: |
| 282 | tp = (TcpPortCriterion) c; |
| 283 | mBuilder.setExact(MatchField.TCP_SRC, TransportPort.of(tp.tcpPort())); |
| 284 | break; |
Marc De Leenheer | 4908775 | 2014-10-23 13:54:09 -0700 | [diff] [blame] | 285 | case OCH_SIGID: |
| 286 | LambdaCriterion lc = (LambdaCriterion) c; |
| 287 | mBuilder.setExact(MatchField.OCH_SIGID, |
| 288 | new CircuitSignalID((byte) 1, (byte) 2, lc.lambda(), (short) 1)); |
| 289 | break; |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 290 | case ARP_OP: |
| 291 | case ARP_SHA: |
| 292 | case ARP_SPA: |
| 293 | case ARP_THA: |
| 294 | case ARP_TPA: |
| 295 | case ICMPV4_CODE: |
| 296 | case ICMPV4_TYPE: |
| 297 | case ICMPV6_CODE: |
| 298 | case ICMPV6_TYPE: |
| 299 | case IN_PHY_PORT: |
| 300 | case IPV6_DST: |
| 301 | case IPV6_EXTHDR: |
| 302 | case IPV6_FLABEL: |
| 303 | case IPV6_ND_SLL: |
| 304 | case IPV6_ND_TARGET: |
| 305 | case IPV6_ND_TLL: |
| 306 | case IPV6_SRC: |
| 307 | case IP_DSCP: |
| 308 | case IP_ECN: |
| 309 | case METADATA: |
| 310 | case MPLS_BOS: |
| 311 | case MPLS_LABEL: |
| 312 | case MPLS_TC: |
| 313 | case PBB_ISID: |
| 314 | case SCTP_DST: |
| 315 | case SCTP_SRC: |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 316 | case TUNNEL_ID: |
| 317 | case UDP_DST: |
| 318 | case UDP_SRC: |
| 319 | default: |
| 320 | log.warn("Match type {} not yet implemented.", c.type()); |
| 321 | } |
| 322 | } |
| 323 | return mBuilder.build(); |
| 324 | } |
| 325 | |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 326 | |
| 327 | |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 328 | } |