blob: 51e4a98d459024c395f9f42a6bb095ba74bf8e3d [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;
17import org.onlab.onos.net.flow.criteria.Criteria.PortCriterion;
18import org.onlab.onos.net.flow.criteria.Criteria.VlanIdCriterion;
19import org.onlab.onos.net.flow.criteria.Criteria.VlanPcpCriterion;
20import org.onlab.onos.net.flow.criteria.Criterion;
21import org.onlab.onos.net.flow.instructions.Instruction;
22import org.onlab.onos.net.flow.instructions.Instructions.OutputInstruction;
23import org.onlab.onos.net.flow.instructions.L2ModificationInstruction;
24import org.onlab.onos.net.flow.instructions.L2ModificationInstruction.ModEtherInstruction;
25import org.onlab.onos.net.flow.instructions.L2ModificationInstruction.ModVlanIdInstruction;
26import org.onlab.onos.net.flow.instructions.L2ModificationInstruction.ModVlanPcpInstruction;
27import org.onlab.onos.net.flow.instructions.L3ModificationInstruction;
28import org.onlab.onos.net.flow.instructions.L3ModificationInstruction.ModIPInstruction;
29import org.projectfloodlight.openflow.protocol.OFFactory;
30import org.projectfloodlight.openflow.protocol.OFFlowMod;
31import org.projectfloodlight.openflow.protocol.OFFlowModFlags;
32import org.projectfloodlight.openflow.protocol.action.OFAction;
33import org.projectfloodlight.openflow.protocol.match.Match;
34import org.projectfloodlight.openflow.protocol.match.MatchField;
35import org.projectfloodlight.openflow.types.EthType;
36import org.projectfloodlight.openflow.types.IPv4Address;
37import org.projectfloodlight.openflow.types.IpProtocol;
38import org.projectfloodlight.openflow.types.MacAddress;
39import org.projectfloodlight.openflow.types.Masked;
40import org.projectfloodlight.openflow.types.OFBufferId;
41import org.projectfloodlight.openflow.types.OFPort;
42import org.projectfloodlight.openflow.types.OFVlanVidMatch;
alshabib6b5cfec2014-09-18 17:42:18 -070043import org.projectfloodlight.openflow.types.U64;
alshabibeec3a062014-09-17 18:01:26 -070044import org.projectfloodlight.openflow.types.VlanPcp;
45import org.projectfloodlight.openflow.types.VlanVid;
46import org.slf4j.Logger;
47
48
49public class FlowModBuilder {
50
51 private final Logger log = getLogger(getClass());
52
53 private final OFFactory factory;
54 private final TrafficTreatment treatment;
55 private final TrafficSelector selector;
56
57 private final int priority;
58
alshabib6b5cfec2014-09-18 17:42:18 -070059 private final FlowId cookie;
60
alshabibeec3a062014-09-17 18:01:26 -070061
62
63 public FlowModBuilder(FlowRule flowRule, OFFactory factory) {
64 this.factory = factory;
65 this.treatment = flowRule.treatment();
66 this.selector = flowRule.selector();
67 this.priority = flowRule.priority();
alshabib6b5cfec2014-09-18 17:42:18 -070068 this.cookie = flowRule.id();
alshabibeec3a062014-09-17 18:01:26 -070069 }
70
71 public OFFlowMod buildFlowMod() {
72 Match match = buildMatch();
73 List<OFAction> actions = buildActions();
74
75 //TODO: what to do without bufferid? do we assume that there will be a pktout as well?
76 OFFlowMod fm = factory.buildFlowModify()
alshabib6b5cfec2014-09-18 17:42:18 -070077 .setCookie(U64.of(cookie.value()))
alshabibeec3a062014-09-17 18:01:26 -070078 .setBufferId(OFBufferId.NO_BUFFER)
79 .setActions(actions)
alshabib7814e9f2014-09-30 11:52:12 -070080 .setIdleTimeout(10)
alshabibeec3a062014-09-17 18:01:26 -070081 .setMatch(match)
82 .setFlags(Collections.singleton(OFFlowModFlags.SEND_FLOW_REM))
alshabibeec3a062014-09-17 18:01:26 -070083 .setPriority(priority)
84 .build();
85
86 return fm;
87
88 }
89
alshabib219ebaa2014-09-22 15:41:24 -070090 public OFFlowMod buildFlowDel() {
91 Match match = buildMatch();
92 List<OFAction> actions = buildActions();
93
94 OFFlowMod fm = factory.buildFlowDelete()
95 .setCookie(U64.of(cookie.value()))
96 .setBufferId(OFBufferId.NO_BUFFER)
alshabib7814e9f2014-09-30 11:52:12 -070097 .setActions(actions)
alshabib219ebaa2014-09-22 15:41:24 -070098 .setMatch(match)
99 .setFlags(Collections.singleton(OFFlowModFlags.SEND_FLOW_REM))
alshabib219ebaa2014-09-22 15:41:24 -0700100 .setPriority(priority)
101 .build();
102
103 return fm;
104 }
105
alshabibeec3a062014-09-17 18:01:26 -0700106 private List<OFAction> buildActions() {
107 List<OFAction> acts = new LinkedList<>();
alshabib4906fab2014-09-29 23:58:12 -0700108 if (treatment == null) {
109 return acts;
110 }
alshabibeec3a062014-09-17 18:01:26 -0700111 for (Instruction i : treatment.instructions()) {
112 switch (i.type()) {
113 case DROP:
114 log.warn("Saw drop action; assigning drop action");
115 return new LinkedList<>();
116 case L2MODIFICATION:
117 acts.add(buildL2Modification(i));
alshabibf410eee2014-09-22 18:17:45 -0700118 break;
alshabibeec3a062014-09-17 18:01:26 -0700119 case L3MODIFICATION:
120 acts.add(buildL3Modification(i));
alshabibf410eee2014-09-22 18:17:45 -0700121 break;
alshabibeec3a062014-09-17 18:01:26 -0700122 case OUTPUT:
123 OutputInstruction out = (OutputInstruction) i;
124 acts.add(factory.actions().buildOutput().setPort(
125 OFPort.of((int) out.port().toLong())).build());
126 break;
127 case GROUP:
128 default:
129 log.warn("Instruction type {} not yet implemented.", i.type());
130 }
131 }
132
133 return acts;
134 }
135
136 private OFAction buildL3Modification(Instruction i) {
137 L3ModificationInstruction l3m = (L3ModificationInstruction) i;
138 ModIPInstruction ip;
139 switch (l3m.subtype()) {
alshabib99b8fdc2014-09-25 14:30:22 -0700140 case IP_DST:
alshabibeec3a062014-09-17 18:01:26 -0700141 ip = (ModIPInstruction) i;
142 return factory.actions().setNwDst(IPv4Address.of(ip.ip().toInt()));
alshabib99b8fdc2014-09-25 14:30:22 -0700143 case IP_SRC:
alshabibeec3a062014-09-17 18:01:26 -0700144 ip = (ModIPInstruction) i;
145 return factory.actions().setNwSrc(IPv4Address.of(ip.ip().toInt()));
146 default:
147 log.warn("Unimplemented action type {}.", l3m.subtype());
148 break;
149 }
150 return null;
151 }
152
153 private OFAction buildL2Modification(Instruction i) {
154 L2ModificationInstruction l2m = (L2ModificationInstruction) i;
155 ModEtherInstruction eth;
156 switch (l2m.subtype()) {
alshabib99b8fdc2014-09-25 14:30:22 -0700157 case ETH_DST:
alshabibeec3a062014-09-17 18:01:26 -0700158 eth = (ModEtherInstruction) l2m;
159 return factory.actions().setDlDst(MacAddress.of(eth.mac().toLong()));
alshabib99b8fdc2014-09-25 14:30:22 -0700160 case ETH_SRC:
alshabibeec3a062014-09-17 18:01:26 -0700161 eth = (ModEtherInstruction) l2m;
162 return factory.actions().setDlSrc(MacAddress.of(eth.mac().toLong()));
163 case VLAN_ID:
164 ModVlanIdInstruction vlanId = (ModVlanIdInstruction) l2m;
165 return factory.actions().setVlanVid(VlanVid.ofVlan(vlanId.vlanId.toShort()));
166 case VLAN_PCP:
167 ModVlanPcpInstruction vlanPcp = (ModVlanPcpInstruction) l2m;
168 return factory.actions().setVlanPcp(VlanPcp.of(vlanPcp.vlanPcp()));
169 default:
170 log.warn("Unimplemented action type {}.", l2m.subtype());
171 break;
172 }
173 return null;
174 }
175
176 private Match buildMatch() {
177 Match.Builder mBuilder = factory.buildMatch();
178 EthCriterion eth;
179 IPCriterion ip;
180 for (Criterion c : selector.criteria()) {
181 switch (c.type()) {
182 case IN_PORT:
183 PortCriterion inport = (PortCriterion) c;
184 mBuilder.setExact(MatchField.IN_PORT, OFPort.of((int) inport.port().toLong()));
185 break;
186 case ETH_SRC:
187 eth = (EthCriterion) c;
188 mBuilder.setExact(MatchField.ETH_SRC, MacAddress.of(eth.mac().toLong()));
189 break;
190 case ETH_DST:
191 eth = (EthCriterion) c;
192 mBuilder.setExact(MatchField.ETH_DST, MacAddress.of(eth.mac().toLong()));
193 break;
194 case ETH_TYPE:
195 EthTypeCriterion ethType = (EthTypeCriterion) c;
196 mBuilder.setExact(MatchField.ETH_TYPE, EthType.of(ethType.ethType()));
197 break;
198 case IPV4_DST:
199 ip = (IPCriterion) c;
200 if (ip.ip().isMasked()) {
201 Masked<IPv4Address> maskedIp = Masked.of(IPv4Address.of(ip.ip().toInt()),
202 IPv4Address.of(ip.ip().netmask().toInt()));
203 mBuilder.setMasked(MatchField.IPV4_DST, maskedIp);
204 } else {
205 mBuilder.setExact(MatchField.IPV4_DST, IPv4Address.of(ip.ip().toInt()));
206 }
207 break;
208 case IPV4_SRC:
209 ip = (IPCriterion) c;
210 if (ip.ip().isMasked()) {
211 Masked<IPv4Address> maskedIp = Masked.of(IPv4Address.of(ip.ip().toInt()),
212 IPv4Address.of(ip.ip().netmask().toInt()));
213 mBuilder.setMasked(MatchField.IPV4_SRC, maskedIp);
214 } else {
215 mBuilder.setExact(MatchField.IPV4_SRC, IPv4Address.of(ip.ip().toInt()));
216 }
217 break;
218 case IP_PROTO:
219 IPProtocolCriterion p = (IPProtocolCriterion) c;
220 mBuilder.setExact(MatchField.IP_PROTO, IpProtocol.of(p.protocol()));
221 break;
222 case VLAN_PCP:
223 VlanPcpCriterion vpcp = (VlanPcpCriterion) c;
224 mBuilder.setExact(MatchField.VLAN_PCP, VlanPcp.of(vpcp.priority()));
225 break;
226 case VLAN_VID:
227 VlanIdCriterion vid = (VlanIdCriterion) c;
228 mBuilder.setExact(MatchField.VLAN_VID,
229 OFVlanVidMatch.ofVlanVid(VlanVid.ofVlan(vid.vlanId().toShort())));
230 break;
231 case ARP_OP:
232 case ARP_SHA:
233 case ARP_SPA:
234 case ARP_THA:
235 case ARP_TPA:
236 case ICMPV4_CODE:
237 case ICMPV4_TYPE:
238 case ICMPV6_CODE:
239 case ICMPV6_TYPE:
240 case IN_PHY_PORT:
241 case IPV6_DST:
242 case IPV6_EXTHDR:
243 case IPV6_FLABEL:
244 case IPV6_ND_SLL:
245 case IPV6_ND_TARGET:
246 case IPV6_ND_TLL:
247 case IPV6_SRC:
248 case IP_DSCP:
249 case IP_ECN:
250 case METADATA:
251 case MPLS_BOS:
252 case MPLS_LABEL:
253 case MPLS_TC:
254 case PBB_ISID:
255 case SCTP_DST:
256 case SCTP_SRC:
257 case TCP_DST:
258 case TCP_SRC:
259 case TUNNEL_ID:
260 case UDP_DST:
261 case UDP_SRC:
262 default:
263 log.warn("Match type {} not yet implemented.", c.type());
264 }
265 }
266 return mBuilder.build();
267 }
268
alshabib219ebaa2014-09-22 15:41:24 -0700269
270
alshabibeec3a062014-09-17 18:01:26 -0700271}