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