blob: 9f6c6589265efafd2acfeef9e70e5cfffb38e68e [file] [log] [blame]
alshabib6b5cfec2014-09-18 17:42:18 -07001package org.onlab.onos.provider.of.flow.impl;
2
3import static org.slf4j.LoggerFactory.getLogger;
4
5import java.util.List;
6
7import org.onlab.onos.net.DeviceId;
8import org.onlab.onos.net.PortNumber;
alshabib1c319ff2014-10-04 20:29:09 -07009import org.onlab.onos.net.flow.DefaultFlowEntry;
alshabib6b5cfec2014-09-18 17:42:18 -070010import org.onlab.onos.net.flow.DefaultFlowRule;
11import org.onlab.onos.net.flow.DefaultTrafficSelector;
12import org.onlab.onos.net.flow.DefaultTrafficTreatment;
alshabib1c319ff2014-10-04 20:29:09 -070013import org.onlab.onos.net.flow.FlowEntry;
14import org.onlab.onos.net.flow.FlowEntry.FlowEntryState;
alshabib6b5cfec2014-09-18 17:42:18 -070015import org.onlab.onos.net.flow.FlowRule;
16import org.onlab.onos.net.flow.TrafficSelector;
17import org.onlab.onos.net.flow.TrafficTreatment;
alshabib6b5cfec2014-09-18 17:42:18 -070018import org.onlab.onos.openflow.controller.Dpid;
Ayaka Koshibe1d56fe42014-09-19 16:51:58 -070019import org.onlab.packet.IpPrefix;
alshabib6b5cfec2014-09-18 17:42:18 -070020import org.onlab.packet.MacAddress;
21import org.onlab.packet.VlanId;
22import org.projectfloodlight.openflow.protocol.OFFlowRemoved;
23import org.projectfloodlight.openflow.protocol.OFFlowStatsEntry;
alshabib19fdc122014-10-03 11:38:19 -070024import org.projectfloodlight.openflow.protocol.OFInstructionType;
alshabib6b5cfec2014-09-18 17:42:18 -070025import org.projectfloodlight.openflow.protocol.action.OFAction;
26import org.projectfloodlight.openflow.protocol.action.OFActionOutput;
27import org.projectfloodlight.openflow.protocol.action.OFActionSetDlDst;
28import org.projectfloodlight.openflow.protocol.action.OFActionSetDlSrc;
29import org.projectfloodlight.openflow.protocol.action.OFActionSetNwDst;
30import org.projectfloodlight.openflow.protocol.action.OFActionSetNwSrc;
31import org.projectfloodlight.openflow.protocol.action.OFActionSetVlanPcp;
32import org.projectfloodlight.openflow.protocol.action.OFActionSetVlanVid;
alshabib19fdc122014-10-03 11:38:19 -070033import org.projectfloodlight.openflow.protocol.instruction.OFInstruction;
34import org.projectfloodlight.openflow.protocol.instruction.OFInstructionApplyActions;
alshabib6b5cfec2014-09-18 17:42:18 -070035import org.projectfloodlight.openflow.protocol.match.Match;
36import org.projectfloodlight.openflow.protocol.match.MatchField;
37import org.projectfloodlight.openflow.types.IPv4Address;
38import org.slf4j.Logger;
39
alshabib19fdc122014-10-03 11:38:19 -070040import com.google.common.collect.Lists;
41
alshabib1c319ff2014-10-04 20:29:09 -070042public class FlowEntryBuilder {
alshabib6b5cfec2014-09-18 17:42:18 -070043 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
alshabib19fdc122014-10-03 11:38:19 -070053 private final boolean addedRule;
alshabib6b5cfec2014-09-18 17:42:18 -070054
55
alshabib1c319ff2014-10-04 20:29:09 -070056 public FlowEntryBuilder(Dpid dpid, OFFlowStatsEntry entry) {
alshabib6b5cfec2014-09-18 17:42:18 -070057 this.stat = entry;
58 this.match = entry.getMatch();
alshabib19fdc122014-10-03 11:38:19 -070059 this.actions = getActions(entry);
alshabib6b5cfec2014-09-18 17:42:18 -070060 this.dpid = dpid;
alshabib219ebaa2014-09-22 15:41:24 -070061 this.removed = null;
alshabib19fdc122014-10-03 11:38:19 -070062 this.addedRule = true;
alshabib6b5cfec2014-09-18 17:42:18 -070063 }
64
alshabib1c319ff2014-10-04 20:29:09 -070065 public FlowEntryBuilder(Dpid dpid, OFFlowRemoved removed) {
alshabib6b5cfec2014-09-18 17:42:18 -070066 this.match = removed.getMatch();
67 this.removed = removed;
68
69 this.dpid = dpid;
70 this.actions = null;
71 this.stat = null;
alshabib19fdc122014-10-03 11:38:19 -070072 this.addedRule = false;
alshabib6b5cfec2014-09-18 17:42:18 -070073
74 }
75
alshabib1c319ff2014-10-04 20:29:09 -070076 public FlowEntry build() {
alshabib19fdc122014-10-03 11:38:19 -070077 if (addedRule) {
alshabib1c319ff2014-10-04 20:29:09 -070078 FlowRule rule = new DefaultFlowRule(DeviceId.deviceId(Dpid.uri(dpid)),
alshabib6b5cfec2014-09-18 17:42:18 -070079 buildSelector(), buildTreatment(), stat.getPriority(),
alshabiba0e04982014-10-03 13:03:19 -070080 stat.getCookie().getValue(), stat.getIdleTimeout());
alshabib1c319ff2014-10-04 20:29:09 -070081 return new DefaultFlowEntry(rule, FlowEntryState.ADDED,
82 stat.getDurationSec(), stat.getPacketCount().getValue(),
83 stat.getByteCount().getValue());
84
alshabib6b5cfec2014-09-18 17:42:18 -070085 } else {
alshabib1c319ff2014-10-04 20:29:09 -070086 FlowRule rule = new DefaultFlowRule(DeviceId.deviceId(Dpid.uri(dpid)),
alshabib6b5cfec2014-09-18 17:42:18 -070087 buildSelector(), null, removed.getPriority(),
alshabib1c319ff2014-10-04 20:29:09 -070088 removed.getCookie().getValue(), removed.getIdleTimeout());
89 return new DefaultFlowEntry(rule, FlowEntryState.REMOVED, removed.getDurationSec(),
90 removed.getPacketCount().getValue(), removed.getByteCount().getValue());
alshabib6b5cfec2014-09-18 17:42:18 -070091 }
92 }
93
alshabib19fdc122014-10-03 11:38:19 -070094 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 }
alshabib6b5cfec2014-09-18 17:42:18 -0700114
115 private TrafficTreatment buildTreatment() {
tom9a693fd2014-10-03 11:32:19 -0700116 TrafficTreatment.Builder builder = DefaultTrafficTreatment.builder();
alshabib6b5cfec2014-09-18 17:42:18 -0700117 // If this is a drop rule
118 if (actions.size() == 0) {
alshabib010c31d2014-09-26 10:01:12 -0700119 builder.drop();
alshabib6b5cfec2014-09-18 17:42:18 -0700120 return builder.build();
121 }
122 for (OFAction act : actions) {
123 switch (act.getType()) {
124 case OUTPUT:
125 OFActionOutput out = (OFActionOutput) act;
alshabib010c31d2014-09-26 10:01:12 -0700126 builder.setOutput(
127 PortNumber.portNumber(out.getPort().getPortNumber()));
alshabib6b5cfec2014-09-18 17:42:18 -0700128 break;
129 case SET_VLAN_VID:
alshabib010c31d2014-09-26 10:01:12 -0700130 OFActionSetVlanVid vlan = (OFActionSetVlanVid) act;
131 builder.setVlanId(VlanId.vlanId(vlan.getVlanVid().getVlan()));
132 break;
133 case SET_VLAN_PCP:
alshabib6b5cfec2014-09-18 17:42:18 -0700134 OFActionSetVlanPcp pcp = (OFActionSetVlanPcp) act;
alshabib010c31d2014-09-26 10:01:12 -0700135 builder.setVlanId(VlanId.vlanId(pcp.getVlanPcp().getValue()));
alshabib6b5cfec2014-09-18 17:42:18 -0700136 break;
137 case SET_DL_DST:
138 OFActionSetDlDst dldst = (OFActionSetDlDst) act;
alshabib010c31d2014-09-26 10:01:12 -0700139 builder.setEthDst(
140 MacAddress.valueOf(dldst.getDlAddr().getLong()));
alshabib6b5cfec2014-09-18 17:42:18 -0700141 break;
142 case SET_DL_SRC:
143 OFActionSetDlSrc dlsrc = (OFActionSetDlSrc) act;
alshabib010c31d2014-09-26 10:01:12 -0700144 builder.setEthSrc(
145 MacAddress.valueOf(dlsrc.getDlAddr().getLong()));
alshabib6b5cfec2014-09-18 17:42:18 -0700146
147 break;
148 case SET_NW_DST:
149 OFActionSetNwDst nwdst = (OFActionSetNwDst) act;
150 IPv4Address di = nwdst.getNwAddr();
151 if (di.isCidrMask()) {
alshabib010c31d2014-09-26 10:01:12 -0700152 builder.setIpDst(IpPrefix.valueOf(di.getInt(),
153 di.asCidrMaskLength()));
alshabib6b5cfec2014-09-18 17:42:18 -0700154 } else {
alshabib010c31d2014-09-26 10:01:12 -0700155 builder.setIpDst(IpPrefix.valueOf(di.getInt()));
alshabib6b5cfec2014-09-18 17:42:18 -0700156 }
157 break;
158 case SET_NW_SRC:
159 OFActionSetNwSrc nwsrc = (OFActionSetNwSrc) act;
160 IPv4Address si = nwsrc.getNwAddr();
161 if (si.isCidrMask()) {
alshabib010c31d2014-09-26 10:01:12 -0700162 builder.setIpSrc(IpPrefix.valueOf(si.getInt(),
163 si.asCidrMaskLength()));
alshabib6b5cfec2014-09-18 17:42:18 -0700164 } else {
alshabib010c31d2014-09-26 10:01:12 -0700165 builder.setIpSrc(IpPrefix.valueOf(si.getInt()));
alshabib6b5cfec2014-09-18 17:42:18 -0700166 }
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() {
tom9a693fd2014-10-03 11:32:19 -0700201 TrafficSelector.Builder builder = DefaultTrafficSelector.builder();
alshabib6b5cfec2014-09-18 17:42:18 -0700202 for (MatchField<?> field : match.getMatchFields()) {
203 switch (field.id) {
204 case IN_PORT:
alshabib010c31d2014-09-26 10:01:12 -0700205 builder.matchInport(PortNumber
206 .portNumber(match.get(MatchField.IN_PORT).getPortNumber()));
alshabib6b5cfec2014-09-18 17:42:18 -0700207 break;
208 case ETH_SRC:
209 MacAddress sMac = MacAddress.valueOf(match.get(MatchField.ETH_SRC).getLong());
alshabib010c31d2014-09-26 10:01:12 -0700210 builder.matchEthSrc(sMac);
alshabib6b5cfec2014-09-18 17:42:18 -0700211 break;
212 case ETH_DST:
213 MacAddress dMac = MacAddress.valueOf(match.get(MatchField.ETH_DST).getLong());
alshabib010c31d2014-09-26 10:01:12 -0700214 builder.matchEthDst(dMac);
alshabib6b5cfec2014-09-18 17:42:18 -0700215 break;
216 case ETH_TYPE:
217 int ethType = match.get(MatchField.ETH_TYPE).getValue();
alshabib010c31d2014-09-26 10:01:12 -0700218 builder.matchEthType((short) ethType);
alshabib6b5cfec2014-09-18 17:42:18 -0700219 break;
220 case IPV4_DST:
221 IPv4Address di = match.get(MatchField.IPV4_DST);
Ayaka Koshibe1d56fe42014-09-19 16:51:58 -0700222 IpPrefix dip;
alshabib6b5cfec2014-09-18 17:42:18 -0700223 if (di.isCidrMask()) {
Ayaka Koshibe1d56fe42014-09-19 16:51:58 -0700224 dip = IpPrefix.valueOf(di.getInt(), di.asCidrMaskLength());
alshabib6b5cfec2014-09-18 17:42:18 -0700225 } else {
Jonathan Hart34bc6142014-10-17 11:00:43 -0700226 dip = IpPrefix.valueOf(di.getInt(), IpPrefix.MAX_INET_MASK);
alshabib6b5cfec2014-09-18 17:42:18 -0700227 }
alshabib010c31d2014-09-26 10:01:12 -0700228 builder.matchIPDst(dip);
alshabib6b5cfec2014-09-18 17:42:18 -0700229 break;
230 case IPV4_SRC:
231 IPv4Address si = match.get(MatchField.IPV4_SRC);
Ayaka Koshibe1d56fe42014-09-19 16:51:58 -0700232 IpPrefix sip;
alshabib6b5cfec2014-09-18 17:42:18 -0700233 if (si.isCidrMask()) {
Ayaka Koshibe1d56fe42014-09-19 16:51:58 -0700234 sip = IpPrefix.valueOf(si.getInt(), si.asCidrMaskLength());
alshabib6b5cfec2014-09-18 17:42:18 -0700235 } else {
Jonathan Hart34bc6142014-10-17 11:00:43 -0700236 sip = IpPrefix.valueOf(si.getInt(), IpPrefix.MAX_INET_MASK);
alshabib6b5cfec2014-09-18 17:42:18 -0700237 }
alshabib010c31d2014-09-26 10:01:12 -0700238 builder.matchIPSrc(sip);
alshabib6b5cfec2014-09-18 17:42:18 -0700239 break;
240 case IP_PROTO:
241 short proto = match.get(MatchField.IP_PROTO).getIpProtocolNumber();
alshabib010c31d2014-09-26 10:01:12 -0700242 builder.matchIPProtocol((byte) proto);
alshabib6b5cfec2014-09-18 17:42:18 -0700243 break;
244 case VLAN_PCP:
245 byte vlanPcp = match.get(MatchField.VLAN_PCP).getValue();
alshabib010c31d2014-09-26 10:01:12 -0700246 builder.matchVlanPcp(vlanPcp);
alshabib6b5cfec2014-09-18 17:42:18 -0700247 break;
248 case VLAN_VID:
249 VlanId vlanId = VlanId.vlanId(match.get(MatchField.VLAN_VID).getVlan());
alshabib010c31d2014-09-26 10:01:12 -0700250 builder.matchVlanId(vlanId);
alshabib6b5cfec2014-09-18 17:42:18 -0700251 break;
Jonathan Hart34bc6142014-10-17 11:00:43 -0700252 case TCP_DST:
253 builder.matchTcpDst((short) match.get(MatchField.TCP_DST).getPort());
254 break;
255 case TCP_SRC:
256 builder.matchTcpSrc((short) match.get(MatchField.TCP_SRC).getPort());
257 break;
alshabib6b5cfec2014-09-18 17:42:18 -0700258 case ARP_OP:
259 case ARP_SHA:
260 case ARP_SPA:
261 case ARP_THA:
262 case ARP_TPA:
263 case ICMPV4_CODE:
264 case ICMPV4_TYPE:
265 case ICMPV6_CODE:
266 case ICMPV6_TYPE:
267 case IN_PHY_PORT:
268 case IPV6_DST:
269 case IPV6_FLABEL:
270 case IPV6_ND_SLL:
271 case IPV6_ND_TARGET:
272 case IPV6_ND_TLL:
273 case IPV6_SRC:
274 case IP_DSCP:
275 case IP_ECN:
276 case METADATA:
277 case MPLS_LABEL:
278 case MPLS_TC:
279 case SCTP_DST:
280 case SCTP_SRC:
alshabib6b5cfec2014-09-18 17:42:18 -0700281 case TUNNEL_ID:
282 case UDP_DST:
283 case UDP_SRC:
284 default:
285 log.warn("Match type {} not yet implemented.", field.id);
286
287
288 }
289 }
290 return builder.build();
291 }
292
293}