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