blob: efe436f4eda3ea28eeeb58d4e7817849460fa079 [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)
80 .setMatch(match)
81 .setFlags(Collections.singleton(OFFlowModFlags.SEND_FLOW_REM))
alshabibeec3a062014-09-17 18:01:26 -070082 .setPriority(priority)
83 .build();
84
85 return fm;
86
87 }
88
alshabib219ebaa2014-09-22 15:41:24 -070089 public OFFlowMod buildFlowDel() {
90 Match match = buildMatch();
91 List<OFAction> actions = buildActions();
92
93 OFFlowMod fm = factory.buildFlowDelete()
94 .setCookie(U64.of(cookie.value()))
95 .setBufferId(OFBufferId.NO_BUFFER)
alshabib4906fab2014-09-29 23:58:12 -070096 //.setActions(actions)
alshabib219ebaa2014-09-22 15:41:24 -070097 .setMatch(match)
98 .setFlags(Collections.singleton(OFFlowModFlags.SEND_FLOW_REM))
alshabib219ebaa2014-09-22 15:41:24 -070099 .setPriority(priority)
100 .build();
101
102 return fm;
103 }
104
alshabibeec3a062014-09-17 18:01:26 -0700105 private List<OFAction> buildActions() {
106 List<OFAction> acts = new LinkedList<>();
alshabib4906fab2014-09-29 23:58:12 -0700107 if (treatment == null) {
108 return acts;
109 }
alshabibeec3a062014-09-17 18:01:26 -0700110 for (Instruction i : treatment.instructions()) {
111 switch (i.type()) {
112 case DROP:
113 log.warn("Saw drop action; assigning drop action");
114 return new LinkedList<>();
115 case L2MODIFICATION:
116 acts.add(buildL2Modification(i));
alshabibf410eee2014-09-22 18:17:45 -0700117 break;
alshabibeec3a062014-09-17 18:01:26 -0700118 case L3MODIFICATION:
119 acts.add(buildL3Modification(i));
alshabibf410eee2014-09-22 18:17:45 -0700120 break;
alshabibeec3a062014-09-17 18:01:26 -0700121 case OUTPUT:
122 OutputInstruction out = (OutputInstruction) i;
123 acts.add(factory.actions().buildOutput().setPort(
124 OFPort.of((int) out.port().toLong())).build());
125 break;
126 case GROUP:
127 default:
128 log.warn("Instruction type {} not yet implemented.", i.type());
129 }
130 }
131
132 return acts;
133 }
134
135 private OFAction buildL3Modification(Instruction i) {
136 L3ModificationInstruction l3m = (L3ModificationInstruction) i;
137 ModIPInstruction ip;
138 switch (l3m.subtype()) {
alshabib99b8fdc2014-09-25 14:30:22 -0700139 case IP_DST:
alshabibeec3a062014-09-17 18:01:26 -0700140 ip = (ModIPInstruction) i;
141 return factory.actions().setNwDst(IPv4Address.of(ip.ip().toInt()));
alshabib99b8fdc2014-09-25 14:30:22 -0700142 case IP_SRC:
alshabibeec3a062014-09-17 18:01:26 -0700143 ip = (ModIPInstruction) i;
144 return factory.actions().setNwSrc(IPv4Address.of(ip.ip().toInt()));
145 default:
146 log.warn("Unimplemented action type {}.", l3m.subtype());
147 break;
148 }
149 return null;
150 }
151
152 private OFAction buildL2Modification(Instruction i) {
153 L2ModificationInstruction l2m = (L2ModificationInstruction) i;
154 ModEtherInstruction eth;
155 switch (l2m.subtype()) {
alshabib99b8fdc2014-09-25 14:30:22 -0700156 case ETH_DST:
alshabibeec3a062014-09-17 18:01:26 -0700157 eth = (ModEtherInstruction) l2m;
158 return factory.actions().setDlDst(MacAddress.of(eth.mac().toLong()));
alshabib99b8fdc2014-09-25 14:30:22 -0700159 case ETH_SRC:
alshabibeec3a062014-09-17 18:01:26 -0700160 eth = (ModEtherInstruction) l2m;
161 return factory.actions().setDlSrc(MacAddress.of(eth.mac().toLong()));
162 case VLAN_ID:
163 ModVlanIdInstruction vlanId = (ModVlanIdInstruction) l2m;
164 return factory.actions().setVlanVid(VlanVid.ofVlan(vlanId.vlanId.toShort()));
165 case VLAN_PCP:
166 ModVlanPcpInstruction vlanPcp = (ModVlanPcpInstruction) l2m;
167 return factory.actions().setVlanPcp(VlanPcp.of(vlanPcp.vlanPcp()));
168 default:
169 log.warn("Unimplemented action type {}.", l2m.subtype());
170 break;
171 }
172 return null;
173 }
174
175 private Match buildMatch() {
176 Match.Builder mBuilder = factory.buildMatch();
177 EthCriterion eth;
178 IPCriterion ip;
179 for (Criterion c : selector.criteria()) {
180 switch (c.type()) {
181 case IN_PORT:
182 PortCriterion inport = (PortCriterion) c;
183 mBuilder.setExact(MatchField.IN_PORT, OFPort.of((int) inport.port().toLong()));
184 break;
185 case ETH_SRC:
186 eth = (EthCriterion) c;
187 mBuilder.setExact(MatchField.ETH_SRC, MacAddress.of(eth.mac().toLong()));
188 break;
189 case ETH_DST:
190 eth = (EthCriterion) c;
191 mBuilder.setExact(MatchField.ETH_DST, MacAddress.of(eth.mac().toLong()));
192 break;
193 case ETH_TYPE:
194 EthTypeCriterion ethType = (EthTypeCriterion) c;
195 mBuilder.setExact(MatchField.ETH_TYPE, EthType.of(ethType.ethType()));
196 break;
197 case IPV4_DST:
198 ip = (IPCriterion) c;
199 if (ip.ip().isMasked()) {
200 Masked<IPv4Address> maskedIp = Masked.of(IPv4Address.of(ip.ip().toInt()),
201 IPv4Address.of(ip.ip().netmask().toInt()));
202 mBuilder.setMasked(MatchField.IPV4_DST, maskedIp);
203 } else {
204 mBuilder.setExact(MatchField.IPV4_DST, IPv4Address.of(ip.ip().toInt()));
205 }
206 break;
207 case IPV4_SRC:
208 ip = (IPCriterion) c;
209 if (ip.ip().isMasked()) {
210 Masked<IPv4Address> maskedIp = Masked.of(IPv4Address.of(ip.ip().toInt()),
211 IPv4Address.of(ip.ip().netmask().toInt()));
212 mBuilder.setMasked(MatchField.IPV4_SRC, maskedIp);
213 } else {
214 mBuilder.setExact(MatchField.IPV4_SRC, IPv4Address.of(ip.ip().toInt()));
215 }
216 break;
217 case IP_PROTO:
218 IPProtocolCriterion p = (IPProtocolCriterion) c;
219 mBuilder.setExact(MatchField.IP_PROTO, IpProtocol.of(p.protocol()));
220 break;
221 case VLAN_PCP:
222 VlanPcpCriterion vpcp = (VlanPcpCriterion) c;
223 mBuilder.setExact(MatchField.VLAN_PCP, VlanPcp.of(vpcp.priority()));
224 break;
225 case VLAN_VID:
226 VlanIdCriterion vid = (VlanIdCriterion) c;
227 mBuilder.setExact(MatchField.VLAN_VID,
228 OFVlanVidMatch.ofVlanVid(VlanVid.ofVlan(vid.vlanId().toShort())));
229 break;
230 case ARP_OP:
231 case ARP_SHA:
232 case ARP_SPA:
233 case ARP_THA:
234 case ARP_TPA:
235 case ICMPV4_CODE:
236 case ICMPV4_TYPE:
237 case ICMPV6_CODE:
238 case ICMPV6_TYPE:
239 case IN_PHY_PORT:
240 case IPV6_DST:
241 case IPV6_EXTHDR:
242 case IPV6_FLABEL:
243 case IPV6_ND_SLL:
244 case IPV6_ND_TARGET:
245 case IPV6_ND_TLL:
246 case IPV6_SRC:
247 case IP_DSCP:
248 case IP_ECN:
249 case METADATA:
250 case MPLS_BOS:
251 case MPLS_LABEL:
252 case MPLS_TC:
253 case PBB_ISID:
254 case SCTP_DST:
255 case SCTP_SRC:
256 case TCP_DST:
257 case TCP_SRC:
258 case TUNNEL_ID:
259 case UDP_DST:
260 case UDP_SRC:
261 default:
262 log.warn("Match type {} not yet implemented.", c.type());
263 }
264 }
265 return mBuilder.build();
266 }
267
alshabib219ebaa2014-09-22 15:41:24 -0700268
269
alshabibeec3a062014-09-17 18:01:26 -0700270}