blob: 2822f0286499860cf4cb4ac95dc0251d69aabba3 [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)
96 .setActions(actions)
97 .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<>();
107 for (Instruction i : treatment.instructions()) {
108 switch (i.type()) {
109 case DROP:
110 log.warn("Saw drop action; assigning drop action");
111 return new LinkedList<>();
112 case L2MODIFICATION:
113 acts.add(buildL2Modification(i));
alshabibf410eee2014-09-22 18:17:45 -0700114 break;
alshabibeec3a062014-09-17 18:01:26 -0700115 case L3MODIFICATION:
116 acts.add(buildL3Modification(i));
alshabibf410eee2014-09-22 18:17:45 -0700117 break;
alshabibeec3a062014-09-17 18:01:26 -0700118 case OUTPUT:
119 OutputInstruction out = (OutputInstruction) i;
120 acts.add(factory.actions().buildOutput().setPort(
121 OFPort.of((int) out.port().toLong())).build());
122 break;
123 case GROUP:
124 default:
125 log.warn("Instruction type {} not yet implemented.", i.type());
126 }
127 }
128
129 return acts;
130 }
131
132 private OFAction buildL3Modification(Instruction i) {
133 L3ModificationInstruction l3m = (L3ModificationInstruction) i;
134 ModIPInstruction ip;
135 switch (l3m.subtype()) {
alshabib99b8fdc2014-09-25 14:30:22 -0700136 case IP_DST:
alshabibeec3a062014-09-17 18:01:26 -0700137 ip = (ModIPInstruction) i;
138 return factory.actions().setNwDst(IPv4Address.of(ip.ip().toInt()));
alshabib99b8fdc2014-09-25 14:30:22 -0700139 case IP_SRC:
alshabibeec3a062014-09-17 18:01:26 -0700140 ip = (ModIPInstruction) i;
141 return factory.actions().setNwSrc(IPv4Address.of(ip.ip().toInt()));
142 default:
143 log.warn("Unimplemented action type {}.", l3m.subtype());
144 break;
145 }
146 return null;
147 }
148
149 private OFAction buildL2Modification(Instruction i) {
150 L2ModificationInstruction l2m = (L2ModificationInstruction) i;
151 ModEtherInstruction eth;
152 switch (l2m.subtype()) {
alshabib99b8fdc2014-09-25 14:30:22 -0700153 case ETH_DST:
alshabibeec3a062014-09-17 18:01:26 -0700154 eth = (ModEtherInstruction) l2m;
155 return factory.actions().setDlDst(MacAddress.of(eth.mac().toLong()));
alshabib99b8fdc2014-09-25 14:30:22 -0700156 case ETH_SRC:
alshabibeec3a062014-09-17 18:01:26 -0700157 eth = (ModEtherInstruction) l2m;
158 return factory.actions().setDlSrc(MacAddress.of(eth.mac().toLong()));
159 case VLAN_ID:
160 ModVlanIdInstruction vlanId = (ModVlanIdInstruction) l2m;
161 return factory.actions().setVlanVid(VlanVid.ofVlan(vlanId.vlanId.toShort()));
162 case VLAN_PCP:
163 ModVlanPcpInstruction vlanPcp = (ModVlanPcpInstruction) l2m;
164 return factory.actions().setVlanPcp(VlanPcp.of(vlanPcp.vlanPcp()));
165 default:
166 log.warn("Unimplemented action type {}.", l2m.subtype());
167 break;
168 }
169 return null;
170 }
171
172 private Match buildMatch() {
173 Match.Builder mBuilder = factory.buildMatch();
174 EthCriterion eth;
175 IPCriterion ip;
176 for (Criterion c : selector.criteria()) {
177 switch (c.type()) {
178 case IN_PORT:
179 PortCriterion inport = (PortCriterion) c;
180 mBuilder.setExact(MatchField.IN_PORT, OFPort.of((int) inport.port().toLong()));
181 break;
182 case ETH_SRC:
183 eth = (EthCriterion) c;
184 mBuilder.setExact(MatchField.ETH_SRC, MacAddress.of(eth.mac().toLong()));
185 break;
186 case ETH_DST:
187 eth = (EthCriterion) c;
188 mBuilder.setExact(MatchField.ETH_DST, MacAddress.of(eth.mac().toLong()));
189 break;
190 case ETH_TYPE:
191 EthTypeCriterion ethType = (EthTypeCriterion) c;
192 mBuilder.setExact(MatchField.ETH_TYPE, EthType.of(ethType.ethType()));
193 break;
194 case IPV4_DST:
195 ip = (IPCriterion) c;
196 if (ip.ip().isMasked()) {
197 Masked<IPv4Address> maskedIp = Masked.of(IPv4Address.of(ip.ip().toInt()),
198 IPv4Address.of(ip.ip().netmask().toInt()));
199 mBuilder.setMasked(MatchField.IPV4_DST, maskedIp);
200 } else {
201 mBuilder.setExact(MatchField.IPV4_DST, IPv4Address.of(ip.ip().toInt()));
202 }
203 break;
204 case IPV4_SRC:
205 ip = (IPCriterion) c;
206 if (ip.ip().isMasked()) {
207 Masked<IPv4Address> maskedIp = Masked.of(IPv4Address.of(ip.ip().toInt()),
208 IPv4Address.of(ip.ip().netmask().toInt()));
209 mBuilder.setMasked(MatchField.IPV4_SRC, maskedIp);
210 } else {
211 mBuilder.setExact(MatchField.IPV4_SRC, IPv4Address.of(ip.ip().toInt()));
212 }
213 break;
214 case IP_PROTO:
215 IPProtocolCriterion p = (IPProtocolCriterion) c;
216 mBuilder.setExact(MatchField.IP_PROTO, IpProtocol.of(p.protocol()));
217 break;
218 case VLAN_PCP:
219 VlanPcpCriterion vpcp = (VlanPcpCriterion) c;
220 mBuilder.setExact(MatchField.VLAN_PCP, VlanPcp.of(vpcp.priority()));
221 break;
222 case VLAN_VID:
223 VlanIdCriterion vid = (VlanIdCriterion) c;
224 mBuilder.setExact(MatchField.VLAN_VID,
225 OFVlanVidMatch.ofVlanVid(VlanVid.ofVlan(vid.vlanId().toShort())));
226 break;
227 case ARP_OP:
228 case ARP_SHA:
229 case ARP_SPA:
230 case ARP_THA:
231 case ARP_TPA:
232 case ICMPV4_CODE:
233 case ICMPV4_TYPE:
234 case ICMPV6_CODE:
235 case ICMPV6_TYPE:
236 case IN_PHY_PORT:
237 case IPV6_DST:
238 case IPV6_EXTHDR:
239 case IPV6_FLABEL:
240 case IPV6_ND_SLL:
241 case IPV6_ND_TARGET:
242 case IPV6_ND_TLL:
243 case IPV6_SRC:
244 case IP_DSCP:
245 case IP_ECN:
246 case METADATA:
247 case MPLS_BOS:
248 case MPLS_LABEL:
249 case MPLS_TC:
250 case PBB_ISID:
251 case SCTP_DST:
252 case SCTP_SRC:
253 case TCP_DST:
254 case TCP_SRC:
255 case TUNNEL_ID:
256 case UDP_DST:
257 case UDP_SRC:
258 default:
259 log.warn("Match type {} not yet implemented.", c.type());
260 }
261 }
262 return mBuilder.build();
263 }
264
alshabib219ebaa2014-09-22 15:41:24 -0700265
266
alshabibeec3a062014-09-17 18:01:26 -0700267}