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