blob: bb881d21c385ea5f60034a6588f403a9193f5fa0 [file] [log] [blame]
alshabibeec3a062014-09-17 18:01:26 -07001package org.onlab.onos.provider.of.flow.impl;
2
3import static org.slf4j.LoggerFactory.getLogger;
4
5import java.util.Collections;
6import java.util.LinkedList;
7import java.util.List;
8
alshabib6b5cfec2014-09-18 17:42:18 -07009import org.onlab.onos.net.flow.FlowId;
alshabibeec3a062014-09-17 18:01:26 -070010import org.onlab.onos.net.flow.FlowRule;
11import org.onlab.onos.net.flow.TrafficSelector;
12import org.onlab.onos.net.flow.TrafficTreatment;
13import org.onlab.onos.net.flow.criteria.Criteria.EthCriterion;
14import org.onlab.onos.net.flow.criteria.Criteria.EthTypeCriterion;
15import org.onlab.onos.net.flow.criteria.Criteria.IPCriterion;
16import org.onlab.onos.net.flow.criteria.Criteria.IPProtocolCriterion;
Marc De Leenheer49087752014-10-23 13:54:09 -070017import org.onlab.onos.net.flow.criteria.Criteria.LambdaCriterion;
alshabibeec3a062014-09-17 18:01:26 -070018import org.onlab.onos.net.flow.criteria.Criteria.PortCriterion;
Jonathan Hart34bc6142014-10-17 11:00:43 -070019import org.onlab.onos.net.flow.criteria.Criteria.TcpPortCriterion;
alshabibeec3a062014-09-17 18:01:26 -070020import org.onlab.onos.net.flow.criteria.Criteria.VlanIdCriterion;
21import org.onlab.onos.net.flow.criteria.Criteria.VlanPcpCriterion;
22import org.onlab.onos.net.flow.criteria.Criterion;
23import org.onlab.onos.net.flow.instructions.Instruction;
24import org.onlab.onos.net.flow.instructions.Instructions.OutputInstruction;
Marc De Leenheer49087752014-10-23 13:54:09 -070025import org.onlab.onos.net.flow.instructions.L0ModificationInstruction;
26import org.onlab.onos.net.flow.instructions.L0ModificationInstruction.ModLambdaInstruction;
alshabibeec3a062014-09-17 18:01:26 -070027import org.onlab.onos.net.flow.instructions.L2ModificationInstruction;
28import org.onlab.onos.net.flow.instructions.L2ModificationInstruction.ModEtherInstruction;
29import org.onlab.onos.net.flow.instructions.L2ModificationInstruction.ModVlanIdInstruction;
30import org.onlab.onos.net.flow.instructions.L2ModificationInstruction.ModVlanPcpInstruction;
31import org.onlab.onos.net.flow.instructions.L3ModificationInstruction;
32import org.onlab.onos.net.flow.instructions.L3ModificationInstruction.ModIPInstruction;
33import org.projectfloodlight.openflow.protocol.OFFactory;
alshabib193525b2014-10-08 18:58:03 -070034import org.projectfloodlight.openflow.protocol.OFFlowAdd;
35import org.projectfloodlight.openflow.protocol.OFFlowDelete;
alshabibeec3a062014-09-17 18:01:26 -070036import org.projectfloodlight.openflow.protocol.OFFlowMod;
37import org.projectfloodlight.openflow.protocol.OFFlowModFlags;
38import org.projectfloodlight.openflow.protocol.action.OFAction;
39import org.projectfloodlight.openflow.protocol.match.Match;
40import org.projectfloodlight.openflow.protocol.match.MatchField;
Marc De Leenheer49087752014-10-23 13:54:09 -070041import org.projectfloodlight.openflow.types.CircuitSignalID;
alshabibeec3a062014-09-17 18:01:26 -070042import org.projectfloodlight.openflow.types.EthType;
43import org.projectfloodlight.openflow.types.IPv4Address;
44import org.projectfloodlight.openflow.types.IpProtocol;
45import org.projectfloodlight.openflow.types.MacAddress;
46import org.projectfloodlight.openflow.types.Masked;
47import org.projectfloodlight.openflow.types.OFBufferId;
48import org.projectfloodlight.openflow.types.OFPort;
49import org.projectfloodlight.openflow.types.OFVlanVidMatch;
Jonathan Hart34bc6142014-10-17 11:00:43 -070050import org.projectfloodlight.openflow.types.TransportPort;
alshabib6b5cfec2014-09-18 17:42:18 -070051import org.projectfloodlight.openflow.types.U64;
alshabibeec3a062014-09-17 18:01:26 -070052import org.projectfloodlight.openflow.types.VlanPcp;
53import org.projectfloodlight.openflow.types.VlanVid;
54import org.slf4j.Logger;
55
56
57public class FlowModBuilder {
58
59 private final Logger log = getLogger(getClass());
60
61 private final OFFactory factory;
62 private final TrafficTreatment treatment;
63 private final TrafficSelector selector;
64
65 private final int priority;
66
alshabib6b5cfec2014-09-18 17:42:18 -070067 private final FlowId cookie;
68
alshabibeec3a062014-09-17 18:01:26 -070069
70
71 public FlowModBuilder(FlowRule flowRule, OFFactory factory) {
72 this.factory = factory;
73 this.treatment = flowRule.treatment();
74 this.selector = flowRule.selector();
75 this.priority = flowRule.priority();
alshabib6b5cfec2014-09-18 17:42:18 -070076 this.cookie = flowRule.id();
alshabibeec3a062014-09-17 18:01:26 -070077 }
78
alshabib193525b2014-10-08 18:58:03 -070079 public OFFlowAdd buildFlowAdd() {
alshabibeec3a062014-09-17 18:01:26 -070080 Match match = buildMatch();
81 List<OFAction> actions = buildActions();
82
83 //TODO: what to do without bufferid? do we assume that there will be a pktout as well?
alshabib193525b2014-10-08 18:58:03 -070084 OFFlowAdd fm = factory.buildFlowAdd()
85 .setXid(cookie.value())
alshabib6b5cfec2014-09-18 17:42:18 -070086 .setCookie(U64.of(cookie.value()))
alshabibeec3a062014-09-17 18:01:26 -070087 .setBufferId(OFBufferId.NO_BUFFER)
88 .setActions(actions)
89 .setMatch(match)
90 .setFlags(Collections.singleton(OFFlowModFlags.SEND_FLOW_REM))
alshabibeec3a062014-09-17 18:01:26 -070091 .setPriority(priority)
92 .build();
93
94 return fm;
95
96 }
97
alshabib902d41b2014-10-07 16:52:05 -070098 public OFFlowMod buildFlowMod() {
99 Match match = buildMatch();
100 List<OFAction> actions = buildActions();
101
102 //TODO: what to do without bufferid? do we assume that there will be a pktout as well?
103 OFFlowMod fm = factory.buildFlowModify()
alshabib193525b2014-10-08 18:58:03 -0700104 .setXid(cookie.value())
alshabib902d41b2014-10-07 16:52:05 -0700105 .setCookie(U64.of(cookie.value()))
106 .setBufferId(OFBufferId.NO_BUFFER)
107 .setActions(actions)
108 .setMatch(match)
109 .setFlags(Collections.singleton(OFFlowModFlags.SEND_FLOW_REM))
110 .setPriority(priority)
111 .build();
112
113 return fm;
114
115 }
116
alshabib193525b2014-10-08 18:58:03 -0700117 public OFFlowDelete buildFlowDel() {
alshabib219ebaa2014-09-22 15:41:24 -0700118 Match match = buildMatch();
119 List<OFAction> actions = buildActions();
120
alshabib193525b2014-10-08 18:58:03 -0700121 OFFlowDelete fm = factory.buildFlowDelete()
122 .setXid(cookie.value())
alshabib219ebaa2014-09-22 15:41:24 -0700123 .setCookie(U64.of(cookie.value()))
124 .setBufferId(OFBufferId.NO_BUFFER)
alshabib7814e9f2014-09-30 11:52:12 -0700125 .setActions(actions)
alshabib219ebaa2014-09-22 15:41:24 -0700126 .setMatch(match)
127 .setFlags(Collections.singleton(OFFlowModFlags.SEND_FLOW_REM))
alshabib219ebaa2014-09-22 15:41:24 -0700128 .setPriority(priority)
129 .build();
130
131 return fm;
132 }
133
alshabibeec3a062014-09-17 18:01:26 -0700134 private List<OFAction> buildActions() {
135 List<OFAction> acts = new LinkedList<>();
alshabib4906fab2014-09-29 23:58:12 -0700136 if (treatment == null) {
137 return acts;
138 }
alshabibeec3a062014-09-17 18:01:26 -0700139 for (Instruction i : treatment.instructions()) {
140 switch (i.type()) {
141 case DROP:
142 log.warn("Saw drop action; assigning drop action");
143 return new LinkedList<>();
Marc De Leenheer49087752014-10-23 13:54:09 -0700144 case L0MODIFICATION:
145 acts.add(buildL0Modification(i));
alshabibeec3a062014-09-17 18:01:26 -0700146 case L2MODIFICATION:
147 acts.add(buildL2Modification(i));
alshabibf410eee2014-09-22 18:17:45 -0700148 break;
alshabibeec3a062014-09-17 18:01:26 -0700149 case L3MODIFICATION:
150 acts.add(buildL3Modification(i));
alshabibf410eee2014-09-22 18:17:45 -0700151 break;
alshabibeec3a062014-09-17 18:01:26 -0700152 case OUTPUT:
153 OutputInstruction out = (OutputInstruction) i;
154 acts.add(factory.actions().buildOutput().setPort(
155 OFPort.of((int) out.port().toLong())).build());
156 break;
157 case GROUP:
158 default:
159 log.warn("Instruction type {} not yet implemented.", i.type());
160 }
161 }
162
163 return acts;
164 }
165
Marc De Leenheer49087752014-10-23 13:54:09 -0700166 private OFAction buildL0Modification(Instruction i) {
167 L0ModificationInstruction l0m = (L0ModificationInstruction) i;
168 switch (l0m.subtype()) {
169 case LAMBDA:
170 ModLambdaInstruction ml = (ModLambdaInstruction) i;
171 return factory.actions().circuit(factory.oxms().ochSigidBasic(
172 new CircuitSignalID((byte) 1, (byte) 2, ml.lambda(), (short) 1)));
173 default:
174 log.warn("Unimplemented action type {}.", l0m.subtype());
175 break;
176 }
177 return null;
178 }
179
alshabibeec3a062014-09-17 18:01:26 -0700180 private OFAction buildL3Modification(Instruction i) {
181 L3ModificationInstruction l3m = (L3ModificationInstruction) i;
182 ModIPInstruction ip;
183 switch (l3m.subtype()) {
alshabib99b8fdc2014-09-25 14:30:22 -0700184 case IP_DST:
alshabibeec3a062014-09-17 18:01:26 -0700185 ip = (ModIPInstruction) i;
Jonathan Hartbcae7bd2014-10-16 10:24:41 -0700186 return factory.actions().setNwDst(IPv4Address.of(ip.ip().toInt()));
alshabib99b8fdc2014-09-25 14:30:22 -0700187 case IP_SRC:
alshabibeec3a062014-09-17 18:01:26 -0700188 ip = (ModIPInstruction) i;
Jonathan Hartbcae7bd2014-10-16 10:24:41 -0700189 return factory.actions().setNwSrc(IPv4Address.of(ip.ip().toInt()));
alshabibeec3a062014-09-17 18:01:26 -0700190 default:
191 log.warn("Unimplemented action type {}.", l3m.subtype());
192 break;
193 }
194 return null;
195 }
196
197 private OFAction buildL2Modification(Instruction i) {
198 L2ModificationInstruction l2m = (L2ModificationInstruction) i;
199 ModEtherInstruction eth;
200 switch (l2m.subtype()) {
alshabib99b8fdc2014-09-25 14:30:22 -0700201 case ETH_DST:
alshabibeec3a062014-09-17 18:01:26 -0700202 eth = (ModEtherInstruction) l2m;
203 return factory.actions().setDlDst(MacAddress.of(eth.mac().toLong()));
alshabib99b8fdc2014-09-25 14:30:22 -0700204 case ETH_SRC:
alshabibeec3a062014-09-17 18:01:26 -0700205 eth = (ModEtherInstruction) l2m;
206 return factory.actions().setDlSrc(MacAddress.of(eth.mac().toLong()));
207 case VLAN_ID:
208 ModVlanIdInstruction vlanId = (ModVlanIdInstruction) l2m;
209 return factory.actions().setVlanVid(VlanVid.ofVlan(vlanId.vlanId.toShort()));
210 case VLAN_PCP:
211 ModVlanPcpInstruction vlanPcp = (ModVlanPcpInstruction) l2m;
212 return factory.actions().setVlanPcp(VlanPcp.of(vlanPcp.vlanPcp()));
213 default:
214 log.warn("Unimplemented action type {}.", l2m.subtype());
215 break;
216 }
217 return null;
218 }
219
220 private Match buildMatch() {
221 Match.Builder mBuilder = factory.buildMatch();
222 EthCriterion eth;
223 IPCriterion ip;
Jonathan Hart34bc6142014-10-17 11:00:43 -0700224 TcpPortCriterion tp;
alshabibeec3a062014-09-17 18:01:26 -0700225 for (Criterion c : selector.criteria()) {
226 switch (c.type()) {
227 case IN_PORT:
228 PortCriterion inport = (PortCriterion) c;
229 mBuilder.setExact(MatchField.IN_PORT, OFPort.of((int) inport.port().toLong()));
230 break;
231 case ETH_SRC:
232 eth = (EthCriterion) c;
233 mBuilder.setExact(MatchField.ETH_SRC, MacAddress.of(eth.mac().toLong()));
234 break;
235 case ETH_DST:
236 eth = (EthCriterion) c;
237 mBuilder.setExact(MatchField.ETH_DST, MacAddress.of(eth.mac().toLong()));
238 break;
239 case ETH_TYPE:
240 EthTypeCriterion ethType = (EthTypeCriterion) c;
241 mBuilder.setExact(MatchField.ETH_TYPE, EthType.of(ethType.ethType()));
242 break;
243 case IPV4_DST:
244 ip = (IPCriterion) c;
245 if (ip.ip().isMasked()) {
Jonathan Hartbcae7bd2014-10-16 10:24:41 -0700246 Masked<IPv4Address> maskedIp = Masked.of(IPv4Address.of(ip.ip().toInt()),
247 IPv4Address.of(ip.ip().netmask().toInt()));
alshabibeec3a062014-09-17 18:01:26 -0700248 mBuilder.setMasked(MatchField.IPV4_DST, maskedIp);
249 } else {
Jonathan Hartbcae7bd2014-10-16 10:24:41 -0700250 mBuilder.setExact(MatchField.IPV4_DST, IPv4Address.of(ip.ip().toInt()));
alshabibeec3a062014-09-17 18:01:26 -0700251 }
252 break;
253 case IPV4_SRC:
254 ip = (IPCriterion) c;
255 if (ip.ip().isMasked()) {
Jonathan Hartbcae7bd2014-10-16 10:24:41 -0700256 Masked<IPv4Address> maskedIp = Masked.of(IPv4Address.of(ip.ip().toInt()),
257 IPv4Address.of(ip.ip().netmask().toInt()));
alshabibeec3a062014-09-17 18:01:26 -0700258 mBuilder.setMasked(MatchField.IPV4_SRC, maskedIp);
259 } else {
Jonathan Hartbcae7bd2014-10-16 10:24:41 -0700260 mBuilder.setExact(MatchField.IPV4_SRC, IPv4Address.of(ip.ip().toInt()));
alshabibeec3a062014-09-17 18:01:26 -0700261 }
262 break;
263 case IP_PROTO:
264 IPProtocolCriterion p = (IPProtocolCriterion) c;
265 mBuilder.setExact(MatchField.IP_PROTO, IpProtocol.of(p.protocol()));
266 break;
267 case VLAN_PCP:
268 VlanPcpCriterion vpcp = (VlanPcpCriterion) c;
269 mBuilder.setExact(MatchField.VLAN_PCP, VlanPcp.of(vpcp.priority()));
270 break;
271 case VLAN_VID:
272 VlanIdCriterion vid = (VlanIdCriterion) c;
273 mBuilder.setExact(MatchField.VLAN_VID,
274 OFVlanVidMatch.ofVlanVid(VlanVid.ofVlan(vid.vlanId().toShort())));
275 break;
Jonathan Hart34bc6142014-10-17 11:00:43 -0700276 case TCP_DST:
277 tp = (TcpPortCriterion) c;
278 mBuilder.setExact(MatchField.TCP_DST, TransportPort.of(tp.tcpPort()));
279 break;
280 case TCP_SRC:
281 tp = (TcpPortCriterion) c;
282 mBuilder.setExact(MatchField.TCP_SRC, TransportPort.of(tp.tcpPort()));
283 break;
Marc De Leenheer49087752014-10-23 13:54:09 -0700284 case OCH_SIGID:
285 LambdaCriterion lc = (LambdaCriterion) c;
286 mBuilder.setExact(MatchField.OCH_SIGID,
287 new CircuitSignalID((byte) 1, (byte) 2, lc.lambda(), (short) 1));
288 break;
alshabibeec3a062014-09-17 18:01:26 -0700289 case ARP_OP:
290 case ARP_SHA:
291 case ARP_SPA:
292 case ARP_THA:
293 case ARP_TPA:
294 case ICMPV4_CODE:
295 case ICMPV4_TYPE:
296 case ICMPV6_CODE:
297 case ICMPV6_TYPE:
298 case IN_PHY_PORT:
299 case IPV6_DST:
300 case IPV6_EXTHDR:
301 case IPV6_FLABEL:
302 case IPV6_ND_SLL:
303 case IPV6_ND_TARGET:
304 case IPV6_ND_TLL:
305 case IPV6_SRC:
306 case IP_DSCP:
307 case IP_ECN:
308 case METADATA:
309 case MPLS_BOS:
310 case MPLS_LABEL:
311 case MPLS_TC:
312 case PBB_ISID:
313 case SCTP_DST:
314 case SCTP_SRC:
alshabibeec3a062014-09-17 18:01:26 -0700315 case TUNNEL_ID:
316 case UDP_DST:
317 case UDP_SRC:
318 default:
319 log.warn("Match type {} not yet implemented.", c.type());
320 }
321 }
322 return mBuilder.build();
323 }
324
alshabib219ebaa2014-09-22 15:41:24 -0700325
326
alshabibeec3a062014-09-17 18:01:26 -0700327}