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 | */ |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 16 | package org.onlab.onos.provider.of.flow.impl; |
| 17 | |
| 18 | import static org.slf4j.LoggerFactory.getLogger; |
| 19 | |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 20 | import org.onlab.onos.net.flow.FlowRule; |
| 21 | import org.onlab.onos.net.flow.TrafficSelector; |
Praseed Balakrishnan | 2dd5abd | 2014-11-03 14:56:28 -0800 | [diff] [blame] | 22 | import org.onlab.onos.net.flow.criteria.Criteria; |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 23 | import org.onlab.onos.net.flow.criteria.Criteria.EthCriterion; |
| 24 | import org.onlab.onos.net.flow.criteria.Criteria.EthTypeCriterion; |
| 25 | import org.onlab.onos.net.flow.criteria.Criteria.IPCriterion; |
| 26 | import org.onlab.onos.net.flow.criteria.Criteria.IPProtocolCriterion; |
Marc De Leenheer | 4908775 | 2014-10-23 13:54:09 -0700 | [diff] [blame] | 27 | import org.onlab.onos.net.flow.criteria.Criteria.LambdaCriterion; |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 28 | import org.onlab.onos.net.flow.criteria.Criteria.PortCriterion; |
Jonathan Hart | 34bc614 | 2014-10-17 11:00:43 -0700 | [diff] [blame] | 29 | import org.onlab.onos.net.flow.criteria.Criteria.TcpPortCriterion; |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 30 | import org.onlab.onos.net.flow.criteria.Criteria.VlanIdCriterion; |
| 31 | import org.onlab.onos.net.flow.criteria.Criteria.VlanPcpCriterion; |
| 32 | import org.onlab.onos.net.flow.criteria.Criterion; |
Pavlin Radoslavov | 23e398d | 2014-11-05 15:17:57 -0800 | [diff] [blame] | 33 | import org.onlab.packet.Ip4Address; |
| 34 | import org.onlab.packet.Ip4Prefix; |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 35 | import org.projectfloodlight.openflow.protocol.OFFactory; |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 36 | import org.projectfloodlight.openflow.protocol.OFFlowAdd; |
| 37 | import org.projectfloodlight.openflow.protocol.OFFlowDelete; |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 38 | import org.projectfloodlight.openflow.protocol.OFFlowMod; |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 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; |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 47 | import org.projectfloodlight.openflow.types.OFPort; |
| 48 | import org.projectfloodlight.openflow.types.OFVlanVidMatch; |
Jonathan Hart | 34bc614 | 2014-10-17 11:00:43 -0700 | [diff] [blame] | 49 | import org.projectfloodlight.openflow.types.TransportPort; |
Praseed Balakrishnan | 8c67d17 | 2014-11-10 10:15:41 -0800 | [diff] [blame] | 50 | import org.projectfloodlight.openflow.types.U32; |
Praseed Balakrishnan | 2dd5abd | 2014-11-03 14:56:28 -0800 | [diff] [blame] | 51 | import org.projectfloodlight.openflow.types.U8; |
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 | |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 56 | import java.util.Optional; |
| 57 | |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 58 | /** |
| 59 | * Builder for OpenFlow flow mods based on FlowRules. |
| 60 | */ |
| 61 | public abstract class FlowModBuilder { |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 62 | |
| 63 | private final Logger log = getLogger(getClass()); |
| 64 | |
| 65 | private final OFFactory factory; |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 66 | private final FlowRule flowRule; |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 67 | private final TrafficSelector selector; |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 68 | protected final Long xid; |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 69 | |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 70 | /** |
| 71 | * Creates a new flow mod builder. |
| 72 | * |
| 73 | * @param flowRule the flow rule to transform into a flow mod |
| 74 | * @param factory the OpenFlow factory to use to build the flow mod |
Pavlin Radoslavov | 119fd5c | 2014-11-25 19:08:19 -0800 | [diff] [blame] | 75 | * @param xid the transaction ID |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 76 | * @return the new flow mod builder |
| 77 | */ |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 78 | public static FlowModBuilder builder(FlowRule flowRule, |
| 79 | OFFactory factory, Optional<Long> xid) { |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 80 | switch (factory.getVersion()) { |
| 81 | case OF_10: |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 82 | return new FlowModBuilderVer10(flowRule, factory, xid); |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 83 | case OF_13: |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 84 | return new FlowModBuilderVer13(flowRule, factory, xid); |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 85 | default: |
| 86 | throw new UnsupportedOperationException( |
| 87 | "No flow mod builder for protocol version " + factory.getVersion()); |
| 88 | } |
| 89 | } |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 90 | |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 91 | /** |
| 92 | * Constructs a flow mod builder. |
| 93 | * |
| 94 | * @param flowRule the flow rule to transform into a flow mod |
| 95 | * @param factory the OpenFlow factory to use to build the flow mod |
Pavlin Radoslavov | 119fd5c | 2014-11-25 19:08:19 -0800 | [diff] [blame] | 96 | * @param xid the transaction ID |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 97 | */ |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 98 | protected FlowModBuilder(FlowRule flowRule, OFFactory factory, Optional<Long> xid) { |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 99 | this.factory = factory; |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 100 | this.flowRule = flowRule; |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 101 | this.selector = flowRule.selector(); |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 102 | this.xid = xid.orElse((long) 0); |
| 103 | |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 104 | } |
| 105 | |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 106 | /** |
| 107 | * Builds an ADD flow mod. |
| 108 | * |
| 109 | * @return the flow mod |
| 110 | */ |
| 111 | public abstract OFFlowAdd buildFlowAdd(); |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 112 | |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 113 | /** |
| 114 | * Builds a MODIFY flow mod. |
| 115 | * |
| 116 | * @return the flow mod |
| 117 | */ |
| 118 | public abstract OFFlowMod buildFlowMod(); |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 119 | |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 120 | /** |
| 121 | * Builds a DELETE flow mod. |
| 122 | * |
| 123 | * @return the flow mod |
| 124 | */ |
| 125 | public abstract OFFlowDelete buildFlowDel(); |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 126 | |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 127 | /** |
| 128 | * Builds the match for the flow mod. |
| 129 | * |
| 130 | * @return the match |
| 131 | */ |
| 132 | protected Match buildMatch() { |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 133 | Match.Builder mBuilder = factory.buildMatch(); |
| 134 | EthCriterion eth; |
| 135 | IPCriterion ip; |
Pavlin Radoslavov | 23e398d | 2014-11-05 15:17:57 -0800 | [diff] [blame] | 136 | Ip4Prefix ip4Prefix; |
Jonathan Hart | 34bc614 | 2014-10-17 11:00:43 -0700 | [diff] [blame] | 137 | TcpPortCriterion tp; |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 138 | for (Criterion c : selector.criteria()) { |
| 139 | switch (c.type()) { |
| 140 | case IN_PORT: |
| 141 | PortCriterion inport = (PortCriterion) c; |
| 142 | mBuilder.setExact(MatchField.IN_PORT, OFPort.of((int) inport.port().toLong())); |
| 143 | break; |
| 144 | case ETH_SRC: |
| 145 | eth = (EthCriterion) c; |
| 146 | mBuilder.setExact(MatchField.ETH_SRC, MacAddress.of(eth.mac().toLong())); |
| 147 | break; |
| 148 | case ETH_DST: |
| 149 | eth = (EthCriterion) c; |
| 150 | mBuilder.setExact(MatchField.ETH_DST, MacAddress.of(eth.mac().toLong())); |
| 151 | break; |
| 152 | case ETH_TYPE: |
| 153 | EthTypeCriterion ethType = (EthTypeCriterion) c; |
| 154 | mBuilder.setExact(MatchField.ETH_TYPE, EthType.of(ethType.ethType())); |
| 155 | break; |
| 156 | case IPV4_DST: |
| 157 | ip = (IPCriterion) c; |
Pavlin Radoslavov | 23e398d | 2014-11-05 15:17:57 -0800 | [diff] [blame] | 158 | ip4Prefix = ip.ip().getIp4Prefix(); |
| 159 | if (ip4Prefix.prefixLength() != Ip4Prefix.MAX_MASK_LENGTH) { |
| 160 | Ip4Address maskAddr = |
| 161 | Ip4Address.makeMaskPrefix(ip4Prefix.prefixLength()); |
Pavlin Radoslavov | 855ea2d | 2014-10-30 15:32:39 -0700 | [diff] [blame] | 162 | Masked<IPv4Address> maskedIp = |
Pavlin Radoslavov | 23e398d | 2014-11-05 15:17:57 -0800 | [diff] [blame] | 163 | Masked.of(IPv4Address.of(ip4Prefix.address().toInt()), |
Pavlin Radoslavov | 855ea2d | 2014-10-30 15:32:39 -0700 | [diff] [blame] | 164 | IPv4Address.of(maskAddr.toInt())); |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 165 | mBuilder.setMasked(MatchField.IPV4_DST, maskedIp); |
| 166 | } else { |
Pavlin Radoslavov | 855ea2d | 2014-10-30 15:32:39 -0700 | [diff] [blame] | 167 | mBuilder.setExact(MatchField.IPV4_DST, |
Pavlin Radoslavov | 23e398d | 2014-11-05 15:17:57 -0800 | [diff] [blame] | 168 | IPv4Address.of(ip4Prefix.address().toInt())); |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 169 | } |
| 170 | break; |
| 171 | case IPV4_SRC: |
| 172 | ip = (IPCriterion) c; |
Pavlin Radoslavov | 23e398d | 2014-11-05 15:17:57 -0800 | [diff] [blame] | 173 | ip4Prefix = ip.ip().getIp4Prefix(); |
| 174 | if (ip4Prefix.prefixLength() != Ip4Prefix.MAX_MASK_LENGTH) { |
| 175 | Ip4Address maskAddr = |
| 176 | Ip4Address.makeMaskPrefix(ip4Prefix.prefixLength()); |
Pavlin Radoslavov | 855ea2d | 2014-10-30 15:32:39 -0700 | [diff] [blame] | 177 | Masked<IPv4Address> maskedIp = |
Pavlin Radoslavov | 23e398d | 2014-11-05 15:17:57 -0800 | [diff] [blame] | 178 | Masked.of(IPv4Address.of(ip4Prefix.address().toInt()), |
Pavlin Radoslavov | 855ea2d | 2014-10-30 15:32:39 -0700 | [diff] [blame] | 179 | IPv4Address.of(maskAddr.toInt())); |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 180 | mBuilder.setMasked(MatchField.IPV4_SRC, maskedIp); |
| 181 | } else { |
Pavlin Radoslavov | 855ea2d | 2014-10-30 15:32:39 -0700 | [diff] [blame] | 182 | mBuilder.setExact(MatchField.IPV4_SRC, |
Pavlin Radoslavov | 23e398d | 2014-11-05 15:17:57 -0800 | [diff] [blame] | 183 | IPv4Address.of(ip4Prefix.address().toInt())); |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 184 | } |
| 185 | break; |
| 186 | case IP_PROTO: |
| 187 | IPProtocolCriterion p = (IPProtocolCriterion) c; |
| 188 | mBuilder.setExact(MatchField.IP_PROTO, IpProtocol.of(p.protocol())); |
| 189 | break; |
| 190 | case VLAN_PCP: |
| 191 | VlanPcpCriterion vpcp = (VlanPcpCriterion) c; |
| 192 | mBuilder.setExact(MatchField.VLAN_PCP, VlanPcp.of(vpcp.priority())); |
| 193 | break; |
| 194 | case VLAN_VID: |
| 195 | VlanIdCriterion vid = (VlanIdCriterion) c; |
| 196 | mBuilder.setExact(MatchField.VLAN_VID, |
| 197 | OFVlanVidMatch.ofVlanVid(VlanVid.ofVlan(vid.vlanId().toShort()))); |
| 198 | break; |
Jonathan Hart | 34bc614 | 2014-10-17 11:00:43 -0700 | [diff] [blame] | 199 | case TCP_DST: |
| 200 | tp = (TcpPortCriterion) c; |
| 201 | mBuilder.setExact(MatchField.TCP_DST, TransportPort.of(tp.tcpPort())); |
| 202 | break; |
| 203 | case TCP_SRC: |
| 204 | tp = (TcpPortCriterion) c; |
| 205 | mBuilder.setExact(MatchField.TCP_SRC, TransportPort.of(tp.tcpPort())); |
| 206 | break; |
Praseed Balakrishnan | 8c67d17 | 2014-11-10 10:15:41 -0800 | [diff] [blame] | 207 | case MPLS_LABEL: |
| 208 | Criteria.MplsCriterion mp = (Criteria.MplsCriterion) c; |
| 209 | mBuilder.setExact(MatchField.MPLS_LABEL, |
| 210 | U32.of(mp.label().intValue())); |
| 211 | break; |
Marc De Leenheer | 4908775 | 2014-10-23 13:54:09 -0700 | [diff] [blame] | 212 | case OCH_SIGID: |
| 213 | LambdaCriterion lc = (LambdaCriterion) c; |
| 214 | mBuilder.setExact(MatchField.OCH_SIGID, |
| 215 | new CircuitSignalID((byte) 1, (byte) 2, lc.lambda(), (short) 1)); |
| 216 | break; |
Praseed Balakrishnan | 2dd5abd | 2014-11-03 14:56:28 -0800 | [diff] [blame] | 217 | case OCH_SIGTYPE: |
| 218 | Criteria.OpticalSignalTypeCriterion sc = |
| 219 | (Criteria.OpticalSignalTypeCriterion) c; |
| 220 | mBuilder.setExact(MatchField.OCH_SIGTYPE, |
| 221 | U8.of(sc.signalType())); |
| 222 | break; |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 223 | case ARP_OP: |
| 224 | case ARP_SHA: |
| 225 | case ARP_SPA: |
| 226 | case ARP_THA: |
| 227 | case ARP_TPA: |
| 228 | case ICMPV4_CODE: |
| 229 | case ICMPV4_TYPE: |
| 230 | case ICMPV6_CODE: |
| 231 | case ICMPV6_TYPE: |
| 232 | case IN_PHY_PORT: |
| 233 | case IPV6_DST: |
| 234 | case IPV6_EXTHDR: |
| 235 | case IPV6_FLABEL: |
| 236 | case IPV6_ND_SLL: |
| 237 | case IPV6_ND_TARGET: |
| 238 | case IPV6_ND_TLL: |
| 239 | case IPV6_SRC: |
| 240 | case IP_DSCP: |
| 241 | case IP_ECN: |
| 242 | case METADATA: |
| 243 | case MPLS_BOS: |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 244 | case MPLS_TC: |
| 245 | case PBB_ISID: |
| 246 | case SCTP_DST: |
| 247 | case SCTP_SRC: |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 248 | case TUNNEL_ID: |
| 249 | case UDP_DST: |
| 250 | case UDP_SRC: |
| 251 | default: |
| 252 | log.warn("Match type {} not yet implemented.", c.type()); |
| 253 | } |
| 254 | } |
| 255 | return mBuilder.build(); |
| 256 | } |
| 257 | |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 258 | /** |
| 259 | * Returns the flow rule for this builder. |
| 260 | * |
| 261 | * @return the flow rule |
| 262 | */ |
| 263 | protected FlowRule flowRule() { |
| 264 | return flowRule; |
| 265 | } |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 266 | |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 267 | /** |
| 268 | * Returns the factory used for building OpenFlow constructs. |
| 269 | * |
| 270 | * @return the factory |
| 271 | */ |
| 272 | protected OFFactory factory() { |
| 273 | return factory; |
| 274 | } |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 275 | |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 276 | } |