alshabib | 6b5cfec | 2014-09-18 17:42:18 -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.List; |
| 6 | |
| 7 | import org.onlab.onos.net.DeviceId; |
| 8 | import org.onlab.onos.net.PortNumber; |
| 9 | import org.onlab.onos.net.flow.DefaultFlowRule; |
| 10 | import org.onlab.onos.net.flow.DefaultTrafficSelector; |
| 11 | import org.onlab.onos.net.flow.DefaultTrafficTreatment; |
| 12 | import org.onlab.onos.net.flow.FlowRule; |
alshabib | a7f7ca8 | 2014-09-22 11:41:23 -0700 | [diff] [blame] | 13 | import org.onlab.onos.net.flow.FlowRule.FlowRuleState; |
alshabib | 6b5cfec | 2014-09-18 17:42:18 -0700 | [diff] [blame] | 14 | import org.onlab.onos.net.flow.TrafficSelector; |
| 15 | import org.onlab.onos.net.flow.TrafficTreatment; |
alshabib | 6b5cfec | 2014-09-18 17:42:18 -0700 | [diff] [blame] | 16 | import org.onlab.onos.openflow.controller.Dpid; |
Ayaka Koshibe | 1d56fe4 | 2014-09-19 16:51:58 -0700 | [diff] [blame] | 17 | import org.onlab.packet.IpPrefix; |
alshabib | 6b5cfec | 2014-09-18 17:42:18 -0700 | [diff] [blame] | 18 | import org.onlab.packet.MacAddress; |
| 19 | import org.onlab.packet.VlanId; |
| 20 | import org.projectfloodlight.openflow.protocol.OFFlowRemoved; |
alshabib | 6eb438a | 2014-10-01 16:39:37 -0700 | [diff] [blame] | 21 | import org.projectfloodlight.openflow.protocol.OFFlowRemovedReason; |
alshabib | 6b5cfec | 2014-09-18 17:42:18 -0700 | [diff] [blame] | 22 | import org.projectfloodlight.openflow.protocol.OFFlowStatsEntry; |
| 23 | import org.projectfloodlight.openflow.protocol.action.OFAction; |
| 24 | import org.projectfloodlight.openflow.protocol.action.OFActionOutput; |
| 25 | import org.projectfloodlight.openflow.protocol.action.OFActionSetDlDst; |
| 26 | import org.projectfloodlight.openflow.protocol.action.OFActionSetDlSrc; |
| 27 | import org.projectfloodlight.openflow.protocol.action.OFActionSetNwDst; |
| 28 | import org.projectfloodlight.openflow.protocol.action.OFActionSetNwSrc; |
| 29 | import org.projectfloodlight.openflow.protocol.action.OFActionSetVlanPcp; |
| 30 | import org.projectfloodlight.openflow.protocol.action.OFActionSetVlanVid; |
| 31 | import org.projectfloodlight.openflow.protocol.match.Match; |
| 32 | import org.projectfloodlight.openflow.protocol.match.MatchField; |
| 33 | import org.projectfloodlight.openflow.types.IPv4Address; |
| 34 | import org.slf4j.Logger; |
| 35 | |
| 36 | public class FlowRuleBuilder { |
| 37 | private final Logger log = getLogger(getClass()); |
| 38 | |
| 39 | private final OFFlowStatsEntry stat; |
| 40 | private final OFFlowRemoved removed; |
| 41 | |
| 42 | private final Match match; |
| 43 | private final List<OFAction> actions; |
| 44 | |
| 45 | private final Dpid dpid; |
| 46 | |
| 47 | |
| 48 | |
| 49 | |
| 50 | public FlowRuleBuilder(Dpid dpid, OFFlowStatsEntry entry) { |
| 51 | this.stat = entry; |
| 52 | this.match = entry.getMatch(); |
| 53 | this.actions = entry.getActions(); |
| 54 | this.dpid = dpid; |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 55 | this.removed = null; |
alshabib | 6b5cfec | 2014-09-18 17:42:18 -0700 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | public FlowRuleBuilder(Dpid dpid, OFFlowRemoved removed) { |
| 59 | this.match = removed.getMatch(); |
| 60 | this.removed = removed; |
| 61 | |
| 62 | this.dpid = dpid; |
| 63 | this.actions = null; |
| 64 | this.stat = null; |
| 65 | |
| 66 | } |
| 67 | |
| 68 | public FlowRule build() { |
| 69 | if (stat != null) { |
| 70 | return new DefaultFlowRule(DeviceId.deviceId(Dpid.uri(dpid)), |
| 71 | buildSelector(), buildTreatment(), stat.getPriority(), |
alshabib | a7f7ca8 | 2014-09-22 11:41:23 -0700 | [diff] [blame] | 72 | FlowRuleState.ADDED, stat.getDurationNsec() / 1000000, |
alshabib | 6b5cfec | 2014-09-18 17:42:18 -0700 | [diff] [blame] | 73 | stat.getPacketCount().getValue(), stat.getByteCount().getValue(), |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 74 | stat.getCookie().getValue(), false, stat.getIdleTimeout()); |
alshabib | 6b5cfec | 2014-09-18 17:42:18 -0700 | [diff] [blame] | 75 | } else { |
| 76 | // TODO: revisit potentially. |
| 77 | return new DefaultFlowRule(DeviceId.deviceId(Dpid.uri(dpid)), |
| 78 | buildSelector(), null, removed.getPriority(), |
alshabib | a7f7ca8 | 2014-09-22 11:41:23 -0700 | [diff] [blame] | 79 | FlowRuleState.REMOVED, removed.getDurationNsec() / 1000000, |
alshabib | 6b5cfec | 2014-09-18 17:42:18 -0700 | [diff] [blame] | 80 | removed.getPacketCount().getValue(), removed.getByteCount().getValue(), |
alshabib | 6eb438a | 2014-10-01 16:39:37 -0700 | [diff] [blame] | 81 | removed.getCookie().getValue(), |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 82 | removed.getReason() == OFFlowRemovedReason.IDLE_TIMEOUT.ordinal(), |
| 83 | stat.getIdleTimeout()); |
alshabib | 6b5cfec | 2014-09-18 17:42:18 -0700 | [diff] [blame] | 84 | } |
| 85 | } |
| 86 | |
| 87 | |
| 88 | private TrafficTreatment buildTreatment() { |
tom | 9a693fd | 2014-10-03 11:32:19 -0700 | [diff] [blame^] | 89 | TrafficTreatment.Builder builder = DefaultTrafficTreatment.builder(); |
alshabib | 6b5cfec | 2014-09-18 17:42:18 -0700 | [diff] [blame] | 90 | // If this is a drop rule |
| 91 | if (actions.size() == 0) { |
alshabib | 010c31d | 2014-09-26 10:01:12 -0700 | [diff] [blame] | 92 | builder.drop(); |
alshabib | 6b5cfec | 2014-09-18 17:42:18 -0700 | [diff] [blame] | 93 | return builder.build(); |
| 94 | } |
| 95 | for (OFAction act : actions) { |
| 96 | switch (act.getType()) { |
| 97 | case OUTPUT: |
| 98 | OFActionOutput out = (OFActionOutput) act; |
alshabib | 010c31d | 2014-09-26 10:01:12 -0700 | [diff] [blame] | 99 | builder.setOutput( |
| 100 | PortNumber.portNumber(out.getPort().getPortNumber())); |
alshabib | 6b5cfec | 2014-09-18 17:42:18 -0700 | [diff] [blame] | 101 | break; |
| 102 | case SET_VLAN_VID: |
alshabib | 010c31d | 2014-09-26 10:01:12 -0700 | [diff] [blame] | 103 | OFActionSetVlanVid vlan = (OFActionSetVlanVid) act; |
| 104 | builder.setVlanId(VlanId.vlanId(vlan.getVlanVid().getVlan())); |
| 105 | break; |
| 106 | case SET_VLAN_PCP: |
alshabib | 6b5cfec | 2014-09-18 17:42:18 -0700 | [diff] [blame] | 107 | OFActionSetVlanPcp pcp = (OFActionSetVlanPcp) act; |
alshabib | 010c31d | 2014-09-26 10:01:12 -0700 | [diff] [blame] | 108 | builder.setVlanId(VlanId.vlanId(pcp.getVlanPcp().getValue())); |
alshabib | 6b5cfec | 2014-09-18 17:42:18 -0700 | [diff] [blame] | 109 | break; |
| 110 | case SET_DL_DST: |
| 111 | OFActionSetDlDst dldst = (OFActionSetDlDst) act; |
alshabib | 010c31d | 2014-09-26 10:01:12 -0700 | [diff] [blame] | 112 | builder.setEthDst( |
| 113 | MacAddress.valueOf(dldst.getDlAddr().getLong())); |
alshabib | 6b5cfec | 2014-09-18 17:42:18 -0700 | [diff] [blame] | 114 | break; |
| 115 | case SET_DL_SRC: |
| 116 | OFActionSetDlSrc dlsrc = (OFActionSetDlSrc) act; |
alshabib | 010c31d | 2014-09-26 10:01:12 -0700 | [diff] [blame] | 117 | builder.setEthSrc( |
| 118 | MacAddress.valueOf(dlsrc.getDlAddr().getLong())); |
alshabib | 6b5cfec | 2014-09-18 17:42:18 -0700 | [diff] [blame] | 119 | |
| 120 | break; |
| 121 | case SET_NW_DST: |
| 122 | OFActionSetNwDst nwdst = (OFActionSetNwDst) act; |
| 123 | IPv4Address di = nwdst.getNwAddr(); |
| 124 | if (di.isCidrMask()) { |
alshabib | 010c31d | 2014-09-26 10:01:12 -0700 | [diff] [blame] | 125 | builder.setIpDst(IpPrefix.valueOf(di.getInt(), |
| 126 | di.asCidrMaskLength())); |
alshabib | 6b5cfec | 2014-09-18 17:42:18 -0700 | [diff] [blame] | 127 | } else { |
alshabib | 010c31d | 2014-09-26 10:01:12 -0700 | [diff] [blame] | 128 | builder.setIpDst(IpPrefix.valueOf(di.getInt())); |
alshabib | 6b5cfec | 2014-09-18 17:42:18 -0700 | [diff] [blame] | 129 | } |
| 130 | break; |
| 131 | case SET_NW_SRC: |
| 132 | OFActionSetNwSrc nwsrc = (OFActionSetNwSrc) act; |
| 133 | IPv4Address si = nwsrc.getNwAddr(); |
| 134 | if (si.isCidrMask()) { |
alshabib | 010c31d | 2014-09-26 10:01:12 -0700 | [diff] [blame] | 135 | builder.setIpSrc(IpPrefix.valueOf(si.getInt(), |
| 136 | si.asCidrMaskLength())); |
alshabib | 6b5cfec | 2014-09-18 17:42:18 -0700 | [diff] [blame] | 137 | } else { |
alshabib | 010c31d | 2014-09-26 10:01:12 -0700 | [diff] [blame] | 138 | builder.setIpSrc(IpPrefix.valueOf(si.getInt())); |
alshabib | 6b5cfec | 2014-09-18 17:42:18 -0700 | [diff] [blame] | 139 | } |
| 140 | break; |
| 141 | case SET_TP_DST: |
| 142 | case SET_TP_SRC: |
| 143 | case POP_MPLS: |
| 144 | case POP_PBB: |
| 145 | case POP_VLAN: |
| 146 | case PUSH_MPLS: |
| 147 | case PUSH_PBB: |
| 148 | case PUSH_VLAN: |
| 149 | case SET_FIELD: |
| 150 | case SET_MPLS_LABEL: |
| 151 | case SET_MPLS_TC: |
| 152 | case SET_MPLS_TTL: |
| 153 | case SET_NW_ECN: |
| 154 | case SET_NW_TOS: |
| 155 | case SET_NW_TTL: |
| 156 | case SET_QUEUE: |
| 157 | case STRIP_VLAN: |
| 158 | case COPY_TTL_IN: |
| 159 | case COPY_TTL_OUT: |
| 160 | case DEC_MPLS_TTL: |
| 161 | case DEC_NW_TTL: |
| 162 | case ENQUEUE: |
| 163 | case EXPERIMENTER: |
| 164 | case GROUP: |
| 165 | default: |
| 166 | log.warn("Action type {} not yet implemented.", act.getType()); |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | return builder.build(); |
| 171 | } |
| 172 | |
| 173 | private TrafficSelector buildSelector() { |
tom | 9a693fd | 2014-10-03 11:32:19 -0700 | [diff] [blame^] | 174 | TrafficSelector.Builder builder = DefaultTrafficSelector.builder(); |
alshabib | 6b5cfec | 2014-09-18 17:42:18 -0700 | [diff] [blame] | 175 | for (MatchField<?> field : match.getMatchFields()) { |
| 176 | switch (field.id) { |
| 177 | case IN_PORT: |
alshabib | 010c31d | 2014-09-26 10:01:12 -0700 | [diff] [blame] | 178 | builder.matchInport(PortNumber |
| 179 | .portNumber(match.get(MatchField.IN_PORT).getPortNumber())); |
alshabib | 6b5cfec | 2014-09-18 17:42:18 -0700 | [diff] [blame] | 180 | break; |
| 181 | case ETH_SRC: |
| 182 | MacAddress sMac = MacAddress.valueOf(match.get(MatchField.ETH_SRC).getLong()); |
alshabib | 010c31d | 2014-09-26 10:01:12 -0700 | [diff] [blame] | 183 | builder.matchEthSrc(sMac); |
alshabib | 6b5cfec | 2014-09-18 17:42:18 -0700 | [diff] [blame] | 184 | break; |
| 185 | case ETH_DST: |
| 186 | MacAddress dMac = MacAddress.valueOf(match.get(MatchField.ETH_DST).getLong()); |
alshabib | 010c31d | 2014-09-26 10:01:12 -0700 | [diff] [blame] | 187 | builder.matchEthDst(dMac); |
alshabib | 6b5cfec | 2014-09-18 17:42:18 -0700 | [diff] [blame] | 188 | break; |
| 189 | case ETH_TYPE: |
| 190 | int ethType = match.get(MatchField.ETH_TYPE).getValue(); |
alshabib | 010c31d | 2014-09-26 10:01:12 -0700 | [diff] [blame] | 191 | builder.matchEthType((short) ethType); |
alshabib | 6b5cfec | 2014-09-18 17:42:18 -0700 | [diff] [blame] | 192 | break; |
| 193 | case IPV4_DST: |
| 194 | IPv4Address di = match.get(MatchField.IPV4_DST); |
Ayaka Koshibe | 1d56fe4 | 2014-09-19 16:51:58 -0700 | [diff] [blame] | 195 | IpPrefix dip; |
alshabib | 6b5cfec | 2014-09-18 17:42:18 -0700 | [diff] [blame] | 196 | if (di.isCidrMask()) { |
Ayaka Koshibe | 1d56fe4 | 2014-09-19 16:51:58 -0700 | [diff] [blame] | 197 | dip = IpPrefix.valueOf(di.getInt(), di.asCidrMaskLength()); |
alshabib | 6b5cfec | 2014-09-18 17:42:18 -0700 | [diff] [blame] | 198 | } else { |
Ayaka Koshibe | 1d56fe4 | 2014-09-19 16:51:58 -0700 | [diff] [blame] | 199 | dip = IpPrefix.valueOf(di.getInt()); |
alshabib | 6b5cfec | 2014-09-18 17:42:18 -0700 | [diff] [blame] | 200 | } |
alshabib | 010c31d | 2014-09-26 10:01:12 -0700 | [diff] [blame] | 201 | builder.matchIPDst(dip); |
alshabib | 6b5cfec | 2014-09-18 17:42:18 -0700 | [diff] [blame] | 202 | break; |
| 203 | case IPV4_SRC: |
| 204 | IPv4Address si = match.get(MatchField.IPV4_SRC); |
Ayaka Koshibe | 1d56fe4 | 2014-09-19 16:51:58 -0700 | [diff] [blame] | 205 | IpPrefix sip; |
alshabib | 6b5cfec | 2014-09-18 17:42:18 -0700 | [diff] [blame] | 206 | if (si.isCidrMask()) { |
Ayaka Koshibe | 1d56fe4 | 2014-09-19 16:51:58 -0700 | [diff] [blame] | 207 | sip = IpPrefix.valueOf(si.getInt(), si.asCidrMaskLength()); |
alshabib | 6b5cfec | 2014-09-18 17:42:18 -0700 | [diff] [blame] | 208 | } else { |
Ayaka Koshibe | 1d56fe4 | 2014-09-19 16:51:58 -0700 | [diff] [blame] | 209 | sip = IpPrefix.valueOf(si.getInt()); |
alshabib | 6b5cfec | 2014-09-18 17:42:18 -0700 | [diff] [blame] | 210 | } |
alshabib | 010c31d | 2014-09-26 10:01:12 -0700 | [diff] [blame] | 211 | builder.matchIPSrc(sip); |
alshabib | 6b5cfec | 2014-09-18 17:42:18 -0700 | [diff] [blame] | 212 | break; |
| 213 | case IP_PROTO: |
| 214 | short proto = match.get(MatchField.IP_PROTO).getIpProtocolNumber(); |
alshabib | 010c31d | 2014-09-26 10:01:12 -0700 | [diff] [blame] | 215 | builder.matchIPProtocol((byte) proto); |
alshabib | 6b5cfec | 2014-09-18 17:42:18 -0700 | [diff] [blame] | 216 | break; |
| 217 | case VLAN_PCP: |
| 218 | byte vlanPcp = match.get(MatchField.VLAN_PCP).getValue(); |
alshabib | 010c31d | 2014-09-26 10:01:12 -0700 | [diff] [blame] | 219 | builder.matchVlanPcp(vlanPcp); |
alshabib | 6b5cfec | 2014-09-18 17:42:18 -0700 | [diff] [blame] | 220 | break; |
| 221 | case VLAN_VID: |
| 222 | VlanId vlanId = VlanId.vlanId(match.get(MatchField.VLAN_VID).getVlan()); |
alshabib | 010c31d | 2014-09-26 10:01:12 -0700 | [diff] [blame] | 223 | builder.matchVlanId(vlanId); |
alshabib | 6b5cfec | 2014-09-18 17:42:18 -0700 | [diff] [blame] | 224 | break; |
| 225 | case ARP_OP: |
| 226 | case ARP_SHA: |
| 227 | case ARP_SPA: |
| 228 | case ARP_THA: |
| 229 | case ARP_TPA: |
| 230 | case ICMPV4_CODE: |
| 231 | case ICMPV4_TYPE: |
| 232 | case ICMPV6_CODE: |
| 233 | case ICMPV6_TYPE: |
| 234 | case IN_PHY_PORT: |
| 235 | case IPV6_DST: |
| 236 | case IPV6_FLABEL: |
| 237 | case IPV6_ND_SLL: |
| 238 | case IPV6_ND_TARGET: |
| 239 | case IPV6_ND_TLL: |
| 240 | case IPV6_SRC: |
| 241 | case IP_DSCP: |
| 242 | case IP_ECN: |
| 243 | case METADATA: |
| 244 | case MPLS_LABEL: |
| 245 | case MPLS_TC: |
| 246 | case SCTP_DST: |
| 247 | case SCTP_SRC: |
| 248 | case TCP_DST: |
| 249 | case TCP_SRC: |
| 250 | case TUNNEL_ID: |
| 251 | case UDP_DST: |
| 252 | case UDP_SRC: |
| 253 | default: |
| 254 | log.warn("Match type {} not yet implemented.", field.id); |
| 255 | |
| 256 | |
| 257 | } |
| 258 | } |
| 259 | return builder.build(); |
| 260 | } |
| 261 | |
| 262 | } |