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