blob: 2ccb9a685a21fbc2c4292ecb8c6e34c9d4dd9fbf [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 {
Pavlin Radoslavov52307e62014-10-29 15:07:37 -0700174 builder.setIpDst(IpPrefix.valueOf(di.getInt(),
175 IpPrefix.MAX_INET_MASK_LENGTH));
alshabib6b5cfec2014-09-18 17:42:18 -0700176 }
177 break;
178 case SET_NW_SRC:
179 OFActionSetNwSrc nwsrc = (OFActionSetNwSrc) act;
180 IPv4Address si = nwsrc.getNwAddr();
181 if (si.isCidrMask()) {
alshabib010c31d2014-09-26 10:01:12 -0700182 builder.setIpSrc(IpPrefix.valueOf(si.getInt(),
183 si.asCidrMaskLength()));
alshabib6b5cfec2014-09-18 17:42:18 -0700184 } else {
Pavlin Radoslavov52307e62014-10-29 15:07:37 -0700185 builder.setIpSrc(IpPrefix.valueOf(si.getInt(),
186 IpPrefix.MAX_INET_MASK_LENGTH));
alshabib6b5cfec2014-09-18 17:42:18 -0700187 }
188 break;
Marc De Leenheer49087752014-10-23 13:54:09 -0700189 case EXPERIMENTER:
190 OFActionExperimenter exp = (OFActionExperimenter) act;
191 if (exp.getExperimenter() == 0x80005A06) {
192 OFActionCircuit ct = (OFActionCircuit) exp;
193 builder.setLambda(((OFOxmOchSigidBasic) ct.getField()).getValue().getChannelNumber());
194 } else {
195 log.warn("Unsupported OFActionExperimenter {}", exp.getExperimenter());
196 }
197 break;
alshabib6b5cfec2014-09-18 17:42:18 -0700198 case SET_TP_DST:
199 case SET_TP_SRC:
200 case POP_MPLS:
201 case POP_PBB:
202 case POP_VLAN:
203 case PUSH_MPLS:
204 case PUSH_PBB:
205 case PUSH_VLAN:
206 case SET_FIELD:
207 case SET_MPLS_LABEL:
208 case SET_MPLS_TC:
209 case SET_MPLS_TTL:
210 case SET_NW_ECN:
211 case SET_NW_TOS:
212 case SET_NW_TTL:
213 case SET_QUEUE:
214 case STRIP_VLAN:
215 case COPY_TTL_IN:
216 case COPY_TTL_OUT:
217 case DEC_MPLS_TTL:
218 case DEC_NW_TTL:
219 case ENQUEUE:
Marc De Leenheer49087752014-10-23 13:54:09 -0700220
alshabib6b5cfec2014-09-18 17:42:18 -0700221 case GROUP:
222 default:
223 log.warn("Action type {} not yet implemented.", act.getType());
224 }
225 }
226
227 return builder.build();
228 }
229
230 private TrafficSelector buildSelector() {
tom9a693fd2014-10-03 11:32:19 -0700231 TrafficSelector.Builder builder = DefaultTrafficSelector.builder();
alshabib6b5cfec2014-09-18 17:42:18 -0700232 for (MatchField<?> field : match.getMatchFields()) {
233 switch (field.id) {
234 case IN_PORT:
alshabib010c31d2014-09-26 10:01:12 -0700235 builder.matchInport(PortNumber
236 .portNumber(match.get(MatchField.IN_PORT).getPortNumber()));
alshabib6b5cfec2014-09-18 17:42:18 -0700237 break;
238 case ETH_SRC:
239 MacAddress sMac = MacAddress.valueOf(match.get(MatchField.ETH_SRC).getLong());
alshabib010c31d2014-09-26 10:01:12 -0700240 builder.matchEthSrc(sMac);
alshabib6b5cfec2014-09-18 17:42:18 -0700241 break;
242 case ETH_DST:
243 MacAddress dMac = MacAddress.valueOf(match.get(MatchField.ETH_DST).getLong());
alshabib010c31d2014-09-26 10:01:12 -0700244 builder.matchEthDst(dMac);
alshabib6b5cfec2014-09-18 17:42:18 -0700245 break;
246 case ETH_TYPE:
247 int ethType = match.get(MatchField.ETH_TYPE).getValue();
alshabib010c31d2014-09-26 10:01:12 -0700248 builder.matchEthType((short) ethType);
alshabib6b5cfec2014-09-18 17:42:18 -0700249 break;
250 case IPV4_DST:
Ayaka Koshibe1d56fe42014-09-19 16:51:58 -0700251 IpPrefix dip;
Jonathan Hart0e12fad2014-10-17 14:54:58 -0700252 if (match.isPartiallyMasked(MatchField.IPV4_DST)) {
253 Masked<IPv4Address> maskedIp = match.getMasked(MatchField.IPV4_DST);
254
255 dip = IpPrefix.valueOf(
256 maskedIp.getValue().getInt(),
257 maskedIp.getMask().asCidrMaskLength());
alshabib6b5cfec2014-09-18 17:42:18 -0700258 } else {
Jonathan Hart0e12fad2014-10-17 14:54:58 -0700259 dip = IpPrefix.valueOf(
260 match.get(MatchField.IPV4_DST).getInt(),
Pavlin Radoslavov52307e62014-10-29 15:07:37 -0700261 IpPrefix.MAX_INET_MASK_LENGTH);
alshabib6b5cfec2014-09-18 17:42:18 -0700262 }
Jonathan Hart0e12fad2014-10-17 14:54:58 -0700263
alshabib010c31d2014-09-26 10:01:12 -0700264 builder.matchIPDst(dip);
alshabib6b5cfec2014-09-18 17:42:18 -0700265 break;
266 case IPV4_SRC:
Ayaka Koshibe1d56fe42014-09-19 16:51:58 -0700267 IpPrefix sip;
Jonathan Hart0e12fad2014-10-17 14:54:58 -0700268 if (match.isPartiallyMasked(MatchField.IPV4_SRC)) {
269 Masked<IPv4Address> maskedIp = match.getMasked(MatchField.IPV4_SRC);
270
271 sip = IpPrefix.valueOf(
272 maskedIp.getValue().getInt(),
273 maskedIp.getMask().asCidrMaskLength());
alshabib6b5cfec2014-09-18 17:42:18 -0700274 } else {
Jonathan Hart0e12fad2014-10-17 14:54:58 -0700275 sip = IpPrefix.valueOf(
276 match.get(MatchField.IPV4_SRC).getInt(),
Pavlin Radoslavov52307e62014-10-29 15:07:37 -0700277 IpPrefix.MAX_INET_MASK_LENGTH);
alshabib6b5cfec2014-09-18 17:42:18 -0700278 }
Jonathan Hart0e12fad2014-10-17 14:54:58 -0700279
alshabib010c31d2014-09-26 10:01:12 -0700280 builder.matchIPSrc(sip);
alshabib6b5cfec2014-09-18 17:42:18 -0700281 break;
282 case IP_PROTO:
283 short proto = match.get(MatchField.IP_PROTO).getIpProtocolNumber();
alshabib010c31d2014-09-26 10:01:12 -0700284 builder.matchIPProtocol((byte) proto);
alshabib6b5cfec2014-09-18 17:42:18 -0700285 break;
286 case VLAN_PCP:
287 byte vlanPcp = match.get(MatchField.VLAN_PCP).getValue();
alshabib010c31d2014-09-26 10:01:12 -0700288 builder.matchVlanPcp(vlanPcp);
alshabib6b5cfec2014-09-18 17:42:18 -0700289 break;
290 case VLAN_VID:
291 VlanId vlanId = VlanId.vlanId(match.get(MatchField.VLAN_VID).getVlan());
alshabib010c31d2014-09-26 10:01:12 -0700292 builder.matchVlanId(vlanId);
alshabib6b5cfec2014-09-18 17:42:18 -0700293 break;
Jonathan Hart34bc6142014-10-17 11:00:43 -0700294 case TCP_DST:
295 builder.matchTcpDst((short) match.get(MatchField.TCP_DST).getPort());
296 break;
297 case TCP_SRC:
298 builder.matchTcpSrc((short) match.get(MatchField.TCP_SRC).getPort());
299 break;
Marc De Leenheer49087752014-10-23 13:54:09 -0700300 case OCH_SIGID:
301 builder.matchLambda(match.get(MatchField.OCH_SIGID).getChannelNumber());
302 break;
303 case OCH_SIGTYPE_BASIC:
alshabib6b5cfec2014-09-18 17:42:18 -0700304 case ARP_OP:
305 case ARP_SHA:
306 case ARP_SPA:
307 case ARP_THA:
308 case ARP_TPA:
309 case ICMPV4_CODE:
310 case ICMPV4_TYPE:
311 case ICMPV6_CODE:
312 case ICMPV6_TYPE:
313 case IN_PHY_PORT:
314 case IPV6_DST:
315 case IPV6_FLABEL:
316 case IPV6_ND_SLL:
317 case IPV6_ND_TARGET:
318 case IPV6_ND_TLL:
319 case IPV6_SRC:
320 case IP_DSCP:
321 case IP_ECN:
322 case METADATA:
323 case MPLS_LABEL:
324 case MPLS_TC:
325 case SCTP_DST:
326 case SCTP_SRC:
alshabib6b5cfec2014-09-18 17:42:18 -0700327 case TUNNEL_ID:
328 case UDP_DST:
329 case UDP_SRC:
330 default:
331 log.warn("Match type {} not yet implemented.", field.id);
332
333
334 }
335 }
336 return builder.build();
337 }
338
339}