blob: 2a2111c80a8179dbed44c8629dfe44876cb838a8 [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07002 * Copyright 2014 Open Networking Laboratory
Thomas Vachuska781d18b2014-10-27 10:31:25 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * 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 Vachuska781d18b2014-10-27 10:31:25 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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 Vachuska781d18b2014-10-27 10:31:25 -070015 */
alshabib6b5cfec2014-09-18 17:42:18 -070016package org.onlab.onos.provider.of.flow.impl;
17
18import static org.slf4j.LoggerFactory.getLogger;
19
20import java.util.List;
21
22import org.onlab.onos.net.DeviceId;
23import org.onlab.onos.net.PortNumber;
alshabib1c319ff2014-10-04 20:29:09 -070024import org.onlab.onos.net.flow.DefaultFlowEntry;
alshabib6b5cfec2014-09-18 17:42:18 -070025import org.onlab.onos.net.flow.DefaultFlowRule;
26import org.onlab.onos.net.flow.DefaultTrafficSelector;
27import org.onlab.onos.net.flow.DefaultTrafficTreatment;
alshabib1c319ff2014-10-04 20:29:09 -070028import org.onlab.onos.net.flow.FlowEntry;
29import org.onlab.onos.net.flow.FlowEntry.FlowEntryState;
alshabib6b5cfec2014-09-18 17:42:18 -070030import org.onlab.onos.net.flow.FlowRule;
31import org.onlab.onos.net.flow.TrafficSelector;
32import org.onlab.onos.net.flow.TrafficTreatment;
alshabib6b5cfec2014-09-18 17:42:18 -070033import org.onlab.onos.openflow.controller.Dpid;
Ayaka Koshibe1d56fe42014-09-19 16:51:58 -070034import org.onlab.packet.IpPrefix;
alshabib6b5cfec2014-09-18 17:42:18 -070035import org.onlab.packet.MacAddress;
36import org.onlab.packet.VlanId;
37import org.projectfloodlight.openflow.protocol.OFFlowRemoved;
38import org.projectfloodlight.openflow.protocol.OFFlowStatsEntry;
alshabib19fdc122014-10-03 11:38:19 -070039import org.projectfloodlight.openflow.protocol.OFInstructionType;
alshabib6b5cfec2014-09-18 17:42:18 -070040import org.projectfloodlight.openflow.protocol.action.OFAction;
Marc De Leenheer49087752014-10-23 13:54:09 -070041import org.projectfloodlight.openflow.protocol.action.OFActionCircuit;
42import org.projectfloodlight.openflow.protocol.action.OFActionExperimenter;
alshabib6b5cfec2014-09-18 17:42:18 -070043import org.projectfloodlight.openflow.protocol.action.OFActionOutput;
44import org.projectfloodlight.openflow.protocol.action.OFActionSetDlDst;
45import org.projectfloodlight.openflow.protocol.action.OFActionSetDlSrc;
46import org.projectfloodlight.openflow.protocol.action.OFActionSetNwDst;
47import org.projectfloodlight.openflow.protocol.action.OFActionSetNwSrc;
48import org.projectfloodlight.openflow.protocol.action.OFActionSetVlanPcp;
49import org.projectfloodlight.openflow.protocol.action.OFActionSetVlanVid;
alshabib19fdc122014-10-03 11:38:19 -070050import org.projectfloodlight.openflow.protocol.instruction.OFInstruction;
Jonathan Hart86e59352014-10-22 10:42:16 -070051import org.projectfloodlight.openflow.protocol.instruction.OFInstructionWriteActions;
alshabib6b5cfec2014-09-18 17:42:18 -070052import org.projectfloodlight.openflow.protocol.match.Match;
53import org.projectfloodlight.openflow.protocol.match.MatchField;
Marc De Leenheer49087752014-10-23 13:54:09 -070054import org.projectfloodlight.openflow.protocol.oxm.OFOxmOchSigidBasic;
alshabib6b5cfec2014-09-18 17:42:18 -070055import org.projectfloodlight.openflow.types.IPv4Address;
Jonathan Hart0e12fad2014-10-17 14:54:58 -070056import org.projectfloodlight.openflow.types.Masked;
alshabib6b5cfec2014-09-18 17:42:18 -070057import org.slf4j.Logger;
58
alshabib19fdc122014-10-03 11:38:19 -070059import com.google.common.collect.Lists;
60
alshabib1c319ff2014-10-04 20:29:09 -070061public class FlowEntryBuilder {
alshabib6b5cfec2014-09-18 17:42:18 -070062 private final Logger log = getLogger(getClass());
63
64 private final OFFlowStatsEntry stat;
65 private final OFFlowRemoved removed;
66
67 private final Match match;
68 private final List<OFAction> actions;
69
70 private final Dpid dpid;
71
alshabib19fdc122014-10-03 11:38:19 -070072 private final boolean addedRule;
alshabib6b5cfec2014-09-18 17:42:18 -070073
74
alshabib1c319ff2014-10-04 20:29:09 -070075 public FlowEntryBuilder(Dpid dpid, OFFlowStatsEntry entry) {
alshabib6b5cfec2014-09-18 17:42:18 -070076 this.stat = entry;
77 this.match = entry.getMatch();
alshabib19fdc122014-10-03 11:38:19 -070078 this.actions = getActions(entry);
alshabib6b5cfec2014-09-18 17:42:18 -070079 this.dpid = dpid;
alshabib219ebaa2014-09-22 15:41:24 -070080 this.removed = null;
alshabib19fdc122014-10-03 11:38:19 -070081 this.addedRule = true;
alshabib6b5cfec2014-09-18 17:42:18 -070082 }
83
alshabib1c319ff2014-10-04 20:29:09 -070084 public FlowEntryBuilder(Dpid dpid, OFFlowRemoved removed) {
alshabib6b5cfec2014-09-18 17:42:18 -070085 this.match = removed.getMatch();
86 this.removed = removed;
87
88 this.dpid = dpid;
89 this.actions = null;
90 this.stat = null;
alshabib19fdc122014-10-03 11:38:19 -070091 this.addedRule = false;
alshabib6b5cfec2014-09-18 17:42:18 -070092
93 }
94
alshabib1c319ff2014-10-04 20:29:09 -070095 public FlowEntry build() {
alshabib19fdc122014-10-03 11:38:19 -070096 if (addedRule) {
alshabib1c319ff2014-10-04 20:29:09 -070097 FlowRule rule = new DefaultFlowRule(DeviceId.deviceId(Dpid.uri(dpid)),
alshabib6b5cfec2014-09-18 17:42:18 -070098 buildSelector(), buildTreatment(), stat.getPriority(),
Jonathan Hartbc4a7932014-10-21 11:46:00 -070099 stat.getCookie().getValue(), stat.getIdleTimeout(), false);
alshabib1c319ff2014-10-04 20:29:09 -0700100 return new DefaultFlowEntry(rule, FlowEntryState.ADDED,
101 stat.getDurationSec(), stat.getPacketCount().getValue(),
102 stat.getByteCount().getValue());
103
alshabib6b5cfec2014-09-18 17:42:18 -0700104 } else {
alshabib1c319ff2014-10-04 20:29:09 -0700105 FlowRule rule = new DefaultFlowRule(DeviceId.deviceId(Dpid.uri(dpid)),
alshabib6b5cfec2014-09-18 17:42:18 -0700106 buildSelector(), null, removed.getPriority(),
Jonathan Hartbc4a7932014-10-21 11:46:00 -0700107 removed.getCookie().getValue(), removed.getIdleTimeout(), false);
alshabib1c319ff2014-10-04 20:29:09 -0700108 return new DefaultFlowEntry(rule, FlowEntryState.REMOVED, removed.getDurationSec(),
109 removed.getPacketCount().getValue(), removed.getByteCount().getValue());
alshabib6b5cfec2014-09-18 17:42:18 -0700110 }
111 }
112
alshabib19fdc122014-10-03 11:38:19 -0700113 private List<OFAction> getActions(OFFlowStatsEntry entry) {
114 switch (entry.getVersion()) {
115 case OF_10:
116 return entry.getActions();
117 case OF_11:
118 case OF_12:
119 case OF_13:
120 List<OFInstruction> ins = entry.getInstructions();
121 for (OFInstruction in : ins) {
Jonathan Hart86e59352014-10-22 10:42:16 -0700122 if (in.getType().equals(OFInstructionType.WRITE_ACTIONS)) {
123 OFInstructionWriteActions apply = (OFInstructionWriteActions) in;
alshabib19fdc122014-10-03 11:38:19 -0700124 return apply.getActions();
125 }
126 }
127 return Lists.newLinkedList();
128 default:
129 log.warn("Unknown OF version {}", entry.getVersion());
130 }
131 return Lists.newLinkedList();
132 }
alshabib6b5cfec2014-09-18 17:42:18 -0700133
134 private TrafficTreatment buildTreatment() {
tom9a693fd2014-10-03 11:32:19 -0700135 TrafficTreatment.Builder builder = DefaultTrafficTreatment.builder();
alshabib6b5cfec2014-09-18 17:42:18 -0700136 // If this is a drop rule
137 if (actions.size() == 0) {
alshabib010c31d2014-09-26 10:01:12 -0700138 builder.drop();
alshabib6b5cfec2014-09-18 17:42:18 -0700139 return builder.build();
140 }
141 for (OFAction act : actions) {
142 switch (act.getType()) {
143 case OUTPUT:
144 OFActionOutput out = (OFActionOutput) act;
alshabib010c31d2014-09-26 10:01:12 -0700145 builder.setOutput(
146 PortNumber.portNumber(out.getPort().getPortNumber()));
alshabib6b5cfec2014-09-18 17:42:18 -0700147 break;
148 case SET_VLAN_VID:
alshabib010c31d2014-09-26 10:01:12 -0700149 OFActionSetVlanVid vlan = (OFActionSetVlanVid) act;
150 builder.setVlanId(VlanId.vlanId(vlan.getVlanVid().getVlan()));
151 break;
152 case SET_VLAN_PCP:
alshabib6b5cfec2014-09-18 17:42:18 -0700153 OFActionSetVlanPcp pcp = (OFActionSetVlanPcp) act;
alshabib010c31d2014-09-26 10:01:12 -0700154 builder.setVlanId(VlanId.vlanId(pcp.getVlanPcp().getValue()));
alshabib6b5cfec2014-09-18 17:42:18 -0700155 break;
156 case SET_DL_DST:
157 OFActionSetDlDst dldst = (OFActionSetDlDst) act;
alshabib010c31d2014-09-26 10:01:12 -0700158 builder.setEthDst(
159 MacAddress.valueOf(dldst.getDlAddr().getLong()));
alshabib6b5cfec2014-09-18 17:42:18 -0700160 break;
161 case SET_DL_SRC:
162 OFActionSetDlSrc dlsrc = (OFActionSetDlSrc) act;
alshabib010c31d2014-09-26 10:01:12 -0700163 builder.setEthSrc(
164 MacAddress.valueOf(dlsrc.getDlAddr().getLong()));
alshabib6b5cfec2014-09-18 17:42:18 -0700165
166 break;
167 case SET_NW_DST:
168 OFActionSetNwDst nwdst = (OFActionSetNwDst) act;
169 IPv4Address di = nwdst.getNwAddr();
170 if (di.isCidrMask()) {
alshabib010c31d2014-09-26 10:01:12 -0700171 builder.setIpDst(IpPrefix.valueOf(di.getInt(),
172 di.asCidrMaskLength()));
alshabib6b5cfec2014-09-18 17:42:18 -0700173 } else {
alshabib010c31d2014-09-26 10:01:12 -0700174 builder.setIpDst(IpPrefix.valueOf(di.getInt()));
alshabib6b5cfec2014-09-18 17:42:18 -0700175 }
176 break;
177 case SET_NW_SRC:
178 OFActionSetNwSrc nwsrc = (OFActionSetNwSrc) act;
179 IPv4Address si = nwsrc.getNwAddr();
180 if (si.isCidrMask()) {
alshabib010c31d2014-09-26 10:01:12 -0700181 builder.setIpSrc(IpPrefix.valueOf(si.getInt(),
182 si.asCidrMaskLength()));
alshabib6b5cfec2014-09-18 17:42:18 -0700183 } else {
alshabib010c31d2014-09-26 10:01:12 -0700184 builder.setIpSrc(IpPrefix.valueOf(si.getInt()));
alshabib6b5cfec2014-09-18 17:42:18 -0700185 }
186 break;
Marc De Leenheer49087752014-10-23 13:54:09 -0700187 case EXPERIMENTER:
188 OFActionExperimenter exp = (OFActionExperimenter) act;
189 if (exp.getExperimenter() == 0x80005A06) {
190 OFActionCircuit ct = (OFActionCircuit) exp;
191 builder.setLambda(((OFOxmOchSigidBasic) ct.getField()).getValue().getChannelNumber());
192 } else {
193 log.warn("Unsupported OFActionExperimenter {}", exp.getExperimenter());
194 }
195 break;
alshabib6b5cfec2014-09-18 17:42:18 -0700196 case SET_TP_DST:
197 case SET_TP_SRC:
198 case POP_MPLS:
199 case POP_PBB:
200 case POP_VLAN:
201 case PUSH_MPLS:
202 case PUSH_PBB:
203 case PUSH_VLAN:
204 case SET_FIELD:
205 case SET_MPLS_LABEL:
206 case SET_MPLS_TC:
207 case SET_MPLS_TTL:
208 case SET_NW_ECN:
209 case SET_NW_TOS:
210 case SET_NW_TTL:
211 case SET_QUEUE:
212 case STRIP_VLAN:
213 case COPY_TTL_IN:
214 case COPY_TTL_OUT:
215 case DEC_MPLS_TTL:
216 case DEC_NW_TTL:
217 case ENQUEUE:
Marc De Leenheer49087752014-10-23 13:54:09 -0700218
alshabib6b5cfec2014-09-18 17:42:18 -0700219 case GROUP:
220 default:
221 log.warn("Action type {} not yet implemented.", act.getType());
222 }
223 }
224
225 return builder.build();
226 }
227
228 private TrafficSelector buildSelector() {
tom9a693fd2014-10-03 11:32:19 -0700229 TrafficSelector.Builder builder = DefaultTrafficSelector.builder();
alshabib6b5cfec2014-09-18 17:42:18 -0700230 for (MatchField<?> field : match.getMatchFields()) {
231 switch (field.id) {
232 case IN_PORT:
alshabib010c31d2014-09-26 10:01:12 -0700233 builder.matchInport(PortNumber
234 .portNumber(match.get(MatchField.IN_PORT).getPortNumber()));
alshabib6b5cfec2014-09-18 17:42:18 -0700235 break;
236 case ETH_SRC:
237 MacAddress sMac = MacAddress.valueOf(match.get(MatchField.ETH_SRC).getLong());
alshabib010c31d2014-09-26 10:01:12 -0700238 builder.matchEthSrc(sMac);
alshabib6b5cfec2014-09-18 17:42:18 -0700239 break;
240 case ETH_DST:
241 MacAddress dMac = MacAddress.valueOf(match.get(MatchField.ETH_DST).getLong());
alshabib010c31d2014-09-26 10:01:12 -0700242 builder.matchEthDst(dMac);
alshabib6b5cfec2014-09-18 17:42:18 -0700243 break;
244 case ETH_TYPE:
245 int ethType = match.get(MatchField.ETH_TYPE).getValue();
alshabib010c31d2014-09-26 10:01:12 -0700246 builder.matchEthType((short) ethType);
alshabib6b5cfec2014-09-18 17:42:18 -0700247 break;
248 case IPV4_DST:
Ayaka Koshibe1d56fe42014-09-19 16:51:58 -0700249 IpPrefix dip;
Jonathan Hart0e12fad2014-10-17 14:54:58 -0700250 if (match.isPartiallyMasked(MatchField.IPV4_DST)) {
251 Masked<IPv4Address> maskedIp = match.getMasked(MatchField.IPV4_DST);
252
253 dip = IpPrefix.valueOf(
254 maskedIp.getValue().getInt(),
255 maskedIp.getMask().asCidrMaskLength());
alshabib6b5cfec2014-09-18 17:42:18 -0700256 } else {
Jonathan Hart0e12fad2014-10-17 14:54:58 -0700257 dip = IpPrefix.valueOf(
258 match.get(MatchField.IPV4_DST).getInt(),
259 IpPrefix.MAX_INET_MASK);
alshabib6b5cfec2014-09-18 17:42:18 -0700260 }
Jonathan Hart0e12fad2014-10-17 14:54:58 -0700261
alshabib010c31d2014-09-26 10:01:12 -0700262 builder.matchIPDst(dip);
alshabib6b5cfec2014-09-18 17:42:18 -0700263 break;
264 case IPV4_SRC:
Ayaka Koshibe1d56fe42014-09-19 16:51:58 -0700265 IpPrefix sip;
Jonathan Hart0e12fad2014-10-17 14:54:58 -0700266 if (match.isPartiallyMasked(MatchField.IPV4_SRC)) {
267 Masked<IPv4Address> maskedIp = match.getMasked(MatchField.IPV4_SRC);
268
269 sip = IpPrefix.valueOf(
270 maskedIp.getValue().getInt(),
271 maskedIp.getMask().asCidrMaskLength());
alshabib6b5cfec2014-09-18 17:42:18 -0700272 } else {
Jonathan Hart0e12fad2014-10-17 14:54:58 -0700273 sip = IpPrefix.valueOf(
274 match.get(MatchField.IPV4_SRC).getInt(),
275 IpPrefix.MAX_INET_MASK);
alshabib6b5cfec2014-09-18 17:42:18 -0700276 }
Jonathan Hart0e12fad2014-10-17 14:54:58 -0700277
alshabib010c31d2014-09-26 10:01:12 -0700278 builder.matchIPSrc(sip);
alshabib6b5cfec2014-09-18 17:42:18 -0700279 break;
280 case IP_PROTO:
281 short proto = match.get(MatchField.IP_PROTO).getIpProtocolNumber();
alshabib010c31d2014-09-26 10:01:12 -0700282 builder.matchIPProtocol((byte) proto);
alshabib6b5cfec2014-09-18 17:42:18 -0700283 break;
284 case VLAN_PCP:
285 byte vlanPcp = match.get(MatchField.VLAN_PCP).getValue();
alshabib010c31d2014-09-26 10:01:12 -0700286 builder.matchVlanPcp(vlanPcp);
alshabib6b5cfec2014-09-18 17:42:18 -0700287 break;
288 case VLAN_VID:
289 VlanId vlanId = VlanId.vlanId(match.get(MatchField.VLAN_VID).getVlan());
alshabib010c31d2014-09-26 10:01:12 -0700290 builder.matchVlanId(vlanId);
alshabib6b5cfec2014-09-18 17:42:18 -0700291 break;
Jonathan Hart34bc6142014-10-17 11:00:43 -0700292 case TCP_DST:
293 builder.matchTcpDst((short) match.get(MatchField.TCP_DST).getPort());
294 break;
295 case TCP_SRC:
296 builder.matchTcpSrc((short) match.get(MatchField.TCP_SRC).getPort());
297 break;
Marc De Leenheer49087752014-10-23 13:54:09 -0700298 case OCH_SIGID:
299 builder.matchLambda(match.get(MatchField.OCH_SIGID).getChannelNumber());
300 break;
301 case OCH_SIGTYPE_BASIC:
alshabib6b5cfec2014-09-18 17:42:18 -0700302 case ARP_OP:
303 case ARP_SHA:
304 case ARP_SPA:
305 case ARP_THA:
306 case ARP_TPA:
307 case ICMPV4_CODE:
308 case ICMPV4_TYPE:
309 case ICMPV6_CODE:
310 case ICMPV6_TYPE:
311 case IN_PHY_PORT:
312 case IPV6_DST:
313 case IPV6_FLABEL:
314 case IPV6_ND_SLL:
315 case IPV6_ND_TARGET:
316 case IPV6_ND_TLL:
317 case IPV6_SRC:
318 case IP_DSCP:
319 case IP_ECN:
320 case METADATA:
321 case MPLS_LABEL:
322 case MPLS_TC:
323 case SCTP_DST:
324 case SCTP_SRC:
alshabib6b5cfec2014-09-18 17:42:18 -0700325 case TUNNEL_ID:
326 case UDP_DST:
327 case UDP_SRC:
328 default:
329 log.warn("Match type {} not yet implemented.", field.id);
330
331
332 }
333 }
334 return builder.build();
335 }
336
337}