blob: d7ebbd82ea765641f8189f533b4feff2f5e64cd2 [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;
Pavlin Radoslavovff894a22014-11-05 16:09:35 -080034import org.onlab.packet.Ip4Address;
35import org.onlab.packet.Ip4Prefix;
alshabib6b5cfec2014-09-18 17:42:18 -070036import org.onlab.packet.MacAddress;
37import org.onlab.packet.VlanId;
38import org.projectfloodlight.openflow.protocol.OFFlowRemoved;
39import org.projectfloodlight.openflow.protocol.OFFlowStatsEntry;
alshabib19fdc122014-10-03 11:38:19 -070040import org.projectfloodlight.openflow.protocol.OFInstructionType;
alshabib6b5cfec2014-09-18 17:42:18 -070041import org.projectfloodlight.openflow.protocol.action.OFAction;
Marc De Leenheer49087752014-10-23 13:54:09 -070042import org.projectfloodlight.openflow.protocol.action.OFActionCircuit;
43import org.projectfloodlight.openflow.protocol.action.OFActionExperimenter;
alshabib6b5cfec2014-09-18 17:42:18 -070044import org.projectfloodlight.openflow.protocol.action.OFActionOutput;
45import org.projectfloodlight.openflow.protocol.action.OFActionSetDlDst;
46import org.projectfloodlight.openflow.protocol.action.OFActionSetDlSrc;
47import org.projectfloodlight.openflow.protocol.action.OFActionSetNwDst;
48import org.projectfloodlight.openflow.protocol.action.OFActionSetNwSrc;
49import org.projectfloodlight.openflow.protocol.action.OFActionSetVlanPcp;
50import org.projectfloodlight.openflow.protocol.action.OFActionSetVlanVid;
alshabib19fdc122014-10-03 11:38:19 -070051import org.projectfloodlight.openflow.protocol.instruction.OFInstruction;
Jonathan Hart86e59352014-10-22 10:42:16 -070052import org.projectfloodlight.openflow.protocol.instruction.OFInstructionWriteActions;
alshabib6b5cfec2014-09-18 17:42:18 -070053import org.projectfloodlight.openflow.protocol.match.Match;
54import org.projectfloodlight.openflow.protocol.match.MatchField;
Marc De Leenheer49087752014-10-23 13:54:09 -070055import org.projectfloodlight.openflow.protocol.oxm.OFOxmOchSigidBasic;
alshabib6b5cfec2014-09-18 17:42:18 -070056import org.projectfloodlight.openflow.types.IPv4Address;
Jonathan Hart0e12fad2014-10-17 14:54:58 -070057import org.projectfloodlight.openflow.types.Masked;
alshabib6b5cfec2014-09-18 17:42:18 -070058import org.slf4j.Logger;
59
alshabib19fdc122014-10-03 11:38:19 -070060import com.google.common.collect.Lists;
61
alshabib1c319ff2014-10-04 20:29:09 -070062public class FlowEntryBuilder {
alshabib6b5cfec2014-09-18 17:42:18 -070063 private final Logger log = getLogger(getClass());
64
65 private final OFFlowStatsEntry stat;
66 private final OFFlowRemoved removed;
67
68 private final Match match;
69 private final List<OFAction> actions;
70
71 private final Dpid dpid;
72
alshabib19fdc122014-10-03 11:38:19 -070073 private final boolean addedRule;
alshabib6b5cfec2014-09-18 17:42:18 -070074
75
alshabib1c319ff2014-10-04 20:29:09 -070076 public FlowEntryBuilder(Dpid dpid, OFFlowStatsEntry entry) {
alshabib6b5cfec2014-09-18 17:42:18 -070077 this.stat = entry;
78 this.match = entry.getMatch();
alshabib19fdc122014-10-03 11:38:19 -070079 this.actions = getActions(entry);
alshabib6b5cfec2014-09-18 17:42:18 -070080 this.dpid = dpid;
alshabib219ebaa2014-09-22 15:41:24 -070081 this.removed = null;
alshabib19fdc122014-10-03 11:38:19 -070082 this.addedRule = true;
alshabib6b5cfec2014-09-18 17:42:18 -070083 }
84
alshabib1c319ff2014-10-04 20:29:09 -070085 public FlowEntryBuilder(Dpid dpid, OFFlowRemoved removed) {
alshabib6b5cfec2014-09-18 17:42:18 -070086 this.match = removed.getMatch();
87 this.removed = removed;
88
89 this.dpid = dpid;
90 this.actions = null;
91 this.stat = null;
alshabib19fdc122014-10-03 11:38:19 -070092 this.addedRule = false;
alshabib6b5cfec2014-09-18 17:42:18 -070093
94 }
95
alshabib1c319ff2014-10-04 20:29:09 -070096 public FlowEntry build() {
alshabib19fdc122014-10-03 11:38:19 -070097 if (addedRule) {
alshabib1c319ff2014-10-04 20:29:09 -070098 FlowRule rule = new DefaultFlowRule(DeviceId.deviceId(Dpid.uri(dpid)),
alshabib6b5cfec2014-09-18 17:42:18 -070099 buildSelector(), buildTreatment(), stat.getPriority(),
Jonathan Hartbc4a7932014-10-21 11:46:00 -0700100 stat.getCookie().getValue(), stat.getIdleTimeout(), false);
alshabib1c319ff2014-10-04 20:29:09 -0700101 return new DefaultFlowEntry(rule, FlowEntryState.ADDED,
102 stat.getDurationSec(), stat.getPacketCount().getValue(),
103 stat.getByteCount().getValue());
104
alshabib6b5cfec2014-09-18 17:42:18 -0700105 } else {
alshabib1c319ff2014-10-04 20:29:09 -0700106 FlowRule rule = new DefaultFlowRule(DeviceId.deviceId(Dpid.uri(dpid)),
alshabib6b5cfec2014-09-18 17:42:18 -0700107 buildSelector(), null, removed.getPriority(),
Jonathan Hartbc4a7932014-10-21 11:46:00 -0700108 removed.getCookie().getValue(), removed.getIdleTimeout(), false);
alshabib1c319ff2014-10-04 20:29:09 -0700109 return new DefaultFlowEntry(rule, FlowEntryState.REMOVED, removed.getDurationSec(),
110 removed.getPacketCount().getValue(), removed.getByteCount().getValue());
alshabib6b5cfec2014-09-18 17:42:18 -0700111 }
112 }
113
alshabib19fdc122014-10-03 11:38:19 -0700114 private List<OFAction> getActions(OFFlowStatsEntry entry) {
115 switch (entry.getVersion()) {
116 case OF_10:
117 return entry.getActions();
118 case OF_11:
119 case OF_12:
120 case OF_13:
121 List<OFInstruction> ins = entry.getInstructions();
122 for (OFInstruction in : ins) {
Jonathan Hart86e59352014-10-22 10:42:16 -0700123 if (in.getType().equals(OFInstructionType.WRITE_ACTIONS)) {
124 OFInstructionWriteActions apply = (OFInstructionWriteActions) in;
alshabib19fdc122014-10-03 11:38:19 -0700125 return apply.getActions();
126 }
127 }
128 return Lists.newLinkedList();
129 default:
130 log.warn("Unknown OF version {}", entry.getVersion());
131 }
132 return Lists.newLinkedList();
133 }
alshabib6b5cfec2014-09-18 17:42:18 -0700134
135 private TrafficTreatment buildTreatment() {
tom9a693fd2014-10-03 11:32:19 -0700136 TrafficTreatment.Builder builder = DefaultTrafficTreatment.builder();
alshabib6b5cfec2014-09-18 17:42:18 -0700137 // If this is a drop rule
138 if (actions.size() == 0) {
alshabib010c31d2014-09-26 10:01:12 -0700139 builder.drop();
alshabib6b5cfec2014-09-18 17:42:18 -0700140 return builder.build();
141 }
142 for (OFAction act : actions) {
143 switch (act.getType()) {
144 case OUTPUT:
145 OFActionOutput out = (OFActionOutput) act;
alshabib010c31d2014-09-26 10:01:12 -0700146 builder.setOutput(
147 PortNumber.portNumber(out.getPort().getPortNumber()));
alshabib6b5cfec2014-09-18 17:42:18 -0700148 break;
149 case SET_VLAN_VID:
alshabib010c31d2014-09-26 10:01:12 -0700150 OFActionSetVlanVid vlan = (OFActionSetVlanVid) act;
151 builder.setVlanId(VlanId.vlanId(vlan.getVlanVid().getVlan()));
152 break;
153 case SET_VLAN_PCP:
alshabib6b5cfec2014-09-18 17:42:18 -0700154 OFActionSetVlanPcp pcp = (OFActionSetVlanPcp) act;
alshabib010c31d2014-09-26 10:01:12 -0700155 builder.setVlanId(VlanId.vlanId(pcp.getVlanPcp().getValue()));
alshabib6b5cfec2014-09-18 17:42:18 -0700156 break;
157 case SET_DL_DST:
158 OFActionSetDlDst dldst = (OFActionSetDlDst) act;
alshabib010c31d2014-09-26 10:01:12 -0700159 builder.setEthDst(
160 MacAddress.valueOf(dldst.getDlAddr().getLong()));
alshabib6b5cfec2014-09-18 17:42:18 -0700161 break;
162 case SET_DL_SRC:
163 OFActionSetDlSrc dlsrc = (OFActionSetDlSrc) act;
alshabib010c31d2014-09-26 10:01:12 -0700164 builder.setEthSrc(
165 MacAddress.valueOf(dlsrc.getDlAddr().getLong()));
alshabib6b5cfec2014-09-18 17:42:18 -0700166
167 break;
168 case SET_NW_DST:
169 OFActionSetNwDst nwdst = (OFActionSetNwDst) act;
170 IPv4Address di = nwdst.getNwAddr();
Pavlin Radoslavovff894a22014-11-05 16:09:35 -0800171 builder.setIpDst(Ip4Address.valueOf(di.getInt()));
alshabib6b5cfec2014-09-18 17:42:18 -0700172 break;
173 case SET_NW_SRC:
174 OFActionSetNwSrc nwsrc = (OFActionSetNwSrc) act;
175 IPv4Address si = nwsrc.getNwAddr();
Pavlin Radoslavovff894a22014-11-05 16:09:35 -0800176 builder.setIpSrc(Ip4Address.valueOf(si.getInt()));
alshabib6b5cfec2014-09-18 17:42:18 -0700177 break;
Marc De Leenheer49087752014-10-23 13:54:09 -0700178 case EXPERIMENTER:
179 OFActionExperimenter exp = (OFActionExperimenter) act;
180 if (exp.getExperimenter() == 0x80005A06) {
181 OFActionCircuit ct = (OFActionCircuit) exp;
182 builder.setLambda(((OFOxmOchSigidBasic) ct.getField()).getValue().getChannelNumber());
183 } else {
184 log.warn("Unsupported OFActionExperimenter {}", exp.getExperimenter());
185 }
186 break;
alshabib6b5cfec2014-09-18 17:42:18 -0700187 case SET_TP_DST:
188 case SET_TP_SRC:
189 case POP_MPLS:
190 case POP_PBB:
191 case POP_VLAN:
192 case PUSH_MPLS:
193 case PUSH_PBB:
194 case PUSH_VLAN:
195 case SET_FIELD:
196 case SET_MPLS_LABEL:
197 case SET_MPLS_TC:
198 case SET_MPLS_TTL:
199 case SET_NW_ECN:
200 case SET_NW_TOS:
201 case SET_NW_TTL:
202 case SET_QUEUE:
203 case STRIP_VLAN:
204 case COPY_TTL_IN:
205 case COPY_TTL_OUT:
206 case DEC_MPLS_TTL:
207 case DEC_NW_TTL:
208 case ENQUEUE:
Marc De Leenheer49087752014-10-23 13:54:09 -0700209
alshabib6b5cfec2014-09-18 17:42:18 -0700210 case GROUP:
211 default:
212 log.warn("Action type {} not yet implemented.", act.getType());
213 }
214 }
215
216 return builder.build();
217 }
218
219 private TrafficSelector buildSelector() {
tom9a693fd2014-10-03 11:32:19 -0700220 TrafficSelector.Builder builder = DefaultTrafficSelector.builder();
alshabib6b5cfec2014-09-18 17:42:18 -0700221 for (MatchField<?> field : match.getMatchFields()) {
222 switch (field.id) {
223 case IN_PORT:
alshabib010c31d2014-09-26 10:01:12 -0700224 builder.matchInport(PortNumber
225 .portNumber(match.get(MatchField.IN_PORT).getPortNumber()));
alshabib6b5cfec2014-09-18 17:42:18 -0700226 break;
227 case ETH_SRC:
228 MacAddress sMac = MacAddress.valueOf(match.get(MatchField.ETH_SRC).getLong());
alshabib010c31d2014-09-26 10:01:12 -0700229 builder.matchEthSrc(sMac);
alshabib6b5cfec2014-09-18 17:42:18 -0700230 break;
231 case ETH_DST:
232 MacAddress dMac = MacAddress.valueOf(match.get(MatchField.ETH_DST).getLong());
alshabib010c31d2014-09-26 10:01:12 -0700233 builder.matchEthDst(dMac);
alshabib6b5cfec2014-09-18 17:42:18 -0700234 break;
235 case ETH_TYPE:
236 int ethType = match.get(MatchField.ETH_TYPE).getValue();
alshabib010c31d2014-09-26 10:01:12 -0700237 builder.matchEthType((short) ethType);
alshabib6b5cfec2014-09-18 17:42:18 -0700238 break;
239 case IPV4_DST:
Pavlin Radoslavovff894a22014-11-05 16:09:35 -0800240 Ip4Prefix dip;
Jonathan Hart0e12fad2014-10-17 14:54:58 -0700241 if (match.isPartiallyMasked(MatchField.IPV4_DST)) {
242 Masked<IPv4Address> maskedIp = match.getMasked(MatchField.IPV4_DST);
243
Pavlin Radoslavovff894a22014-11-05 16:09:35 -0800244 dip = Ip4Prefix.valueOf(
Jonathan Hart0e12fad2014-10-17 14:54:58 -0700245 maskedIp.getValue().getInt(),
246 maskedIp.getMask().asCidrMaskLength());
alshabib6b5cfec2014-09-18 17:42:18 -0700247 } else {
Pavlin Radoslavovff894a22014-11-05 16:09:35 -0800248 dip = Ip4Prefix.valueOf(
Jonathan Hart0e12fad2014-10-17 14:54:58 -0700249 match.get(MatchField.IPV4_DST).getInt(),
Pavlin Radoslavovff894a22014-11-05 16:09:35 -0800250 Ip4Prefix.MAX_MASK_LENGTH);
alshabib6b5cfec2014-09-18 17:42:18 -0700251 }
Jonathan Hart0e12fad2014-10-17 14:54:58 -0700252
alshabib010c31d2014-09-26 10:01:12 -0700253 builder.matchIPDst(dip);
alshabib6b5cfec2014-09-18 17:42:18 -0700254 break;
255 case IPV4_SRC:
Pavlin Radoslavovff894a22014-11-05 16:09:35 -0800256 Ip4Prefix sip;
Jonathan Hart0e12fad2014-10-17 14:54:58 -0700257 if (match.isPartiallyMasked(MatchField.IPV4_SRC)) {
258 Masked<IPv4Address> maskedIp = match.getMasked(MatchField.IPV4_SRC);
259
Pavlin Radoslavovff894a22014-11-05 16:09:35 -0800260 sip = Ip4Prefix.valueOf(
Jonathan Hart0e12fad2014-10-17 14:54:58 -0700261 maskedIp.getValue().getInt(),
262 maskedIp.getMask().asCidrMaskLength());
alshabib6b5cfec2014-09-18 17:42:18 -0700263 } else {
Pavlin Radoslavovff894a22014-11-05 16:09:35 -0800264 sip = Ip4Prefix.valueOf(
Jonathan Hart0e12fad2014-10-17 14:54:58 -0700265 match.get(MatchField.IPV4_SRC).getInt(),
Pavlin Radoslavovff894a22014-11-05 16:09:35 -0800266 Ip4Prefix.MAX_MASK_LENGTH);
alshabib6b5cfec2014-09-18 17:42:18 -0700267 }
Jonathan Hart0e12fad2014-10-17 14:54:58 -0700268
alshabib010c31d2014-09-26 10:01:12 -0700269 builder.matchIPSrc(sip);
alshabib6b5cfec2014-09-18 17:42:18 -0700270 break;
271 case IP_PROTO:
272 short proto = match.get(MatchField.IP_PROTO).getIpProtocolNumber();
alshabib010c31d2014-09-26 10:01:12 -0700273 builder.matchIPProtocol((byte) proto);
alshabib6b5cfec2014-09-18 17:42:18 -0700274 break;
275 case VLAN_PCP:
276 byte vlanPcp = match.get(MatchField.VLAN_PCP).getValue();
alshabib010c31d2014-09-26 10:01:12 -0700277 builder.matchVlanPcp(vlanPcp);
alshabib6b5cfec2014-09-18 17:42:18 -0700278 break;
279 case VLAN_VID:
280 VlanId vlanId = VlanId.vlanId(match.get(MatchField.VLAN_VID).getVlan());
alshabib010c31d2014-09-26 10:01:12 -0700281 builder.matchVlanId(vlanId);
alshabib6b5cfec2014-09-18 17:42:18 -0700282 break;
Jonathan Hart34bc6142014-10-17 11:00:43 -0700283 case TCP_DST:
284 builder.matchTcpDst((short) match.get(MatchField.TCP_DST).getPort());
285 break;
286 case TCP_SRC:
287 builder.matchTcpSrc((short) match.get(MatchField.TCP_SRC).getPort());
288 break;
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800289 case MPLS_LABEL:
290 builder.matchMplsLabel((int) match.get(MatchField.MPLS_LABEL)
291 .getValue());
292 break;
Marc De Leenheer49087752014-10-23 13:54:09 -0700293 case OCH_SIGID:
294 builder.matchLambda(match.get(MatchField.OCH_SIGID).getChannelNumber());
295 break;
Praseed Balakrishnan2dd5abd2014-11-03 14:56:28 -0800296 case OCH_SIGTYPE:
297 builder.matchOpticalSignalType(match.get(MatchField
298 .OCH_SIGTYPE).getValue());
299 break;
alshabib6b5cfec2014-09-18 17:42:18 -0700300 case ARP_OP:
301 case ARP_SHA:
302 case ARP_SPA:
303 case ARP_THA:
304 case ARP_TPA:
305 case ICMPV4_CODE:
306 case ICMPV4_TYPE:
307 case ICMPV6_CODE:
308 case ICMPV6_TYPE:
309 case IN_PHY_PORT:
310 case IPV6_DST:
311 case IPV6_FLABEL:
312 case IPV6_ND_SLL:
313 case IPV6_ND_TARGET:
314 case IPV6_ND_TLL:
315 case IPV6_SRC:
316 case IP_DSCP:
317 case IP_ECN:
318 case METADATA:
alshabib6b5cfec2014-09-18 17:42:18 -0700319 case MPLS_TC:
320 case SCTP_DST:
321 case SCTP_SRC:
alshabib6b5cfec2014-09-18 17:42:18 -0700322 case TUNNEL_ID:
323 case UDP_DST:
324 case UDP_SRC:
325 default:
326 log.warn("Match type {} not yet implemented.", field.id);
327
328
329 }
330 }
331 return builder.build();
332 }
333
334}