2 * Copyright 2015 Open Networking Laboratory
4 * 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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.onosproject.openstackswitching;
19 import org.onlab.packet.Ethernet;
20 import org.onlab.packet.IPv4;
21 import org.onlab.packet.Ip4Address;
22 import org.onlab.packet.Ip4Prefix;
23 import org.onlab.packet.MacAddress;
24 import org.onlab.packet.TpPort;
25 import org.onosproject.core.ApplicationId;
26 import org.onosproject.net.DeviceId;
27 import org.onosproject.net.Port;
28 import org.onosproject.net.PortNumber;
29 import org.onosproject.net.flow.DefaultTrafficSelector;
30 import org.onosproject.net.flow.DefaultTrafficTreatment;
31 import org.onosproject.net.flow.TrafficSelector;
32 import org.onosproject.net.flow.TrafficTreatment;
33 import org.onosproject.net.flowobjective.DefaultForwardingObjective;
34 import org.onosproject.net.flowobjective.FlowObjectiveService;
35 import org.onosproject.net.flowobjective.ForwardingObjective;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
40 * It populates switching flow rules.
43 public class OpenstackSwitchingRulePopulator {
45 private static Logger log = LoggerFactory
46 .getLogger(OpenstackSwitchingRulePopulator.class);
48 private FlowObjectiveService flowObjectiveService;
49 private ApplicationId appId;
52 * Creates OpenstackSwitchingRulPopulator.
54 * @param appId application id
55 * @param flowObjectiveService FlowObjectiveService reference
57 public OpenstackSwitchingRulePopulator(ApplicationId appId,
58 FlowObjectiveService flowObjectiveService) {
59 this.flowObjectiveService = flowObjectiveService;
64 * Populates flows rules for forwarding packets to and from VMs.
66 * @param ip v4 IP Address
69 * @param cidr v4 IP prefix
70 * @return true if it succeeds to populate rules, false otherwise.
72 public boolean populateForwardingRule(Ip4Address ip, DeviceId id, Port port, Ip4Prefix cidr) {
75 setFlowRuleForVMsInSameCnode(ip, id, port, cidr);
81 * Populates the common flows rules for all VMs.
83 * - Send ARP packets to the controller
84 * - Send DHCP packets to the controller
86 * @param id Device ID to populates rules to
88 public void populateDefaultRules(DeviceId id) {
90 //setFlowRuleForDHCP(id);
91 setFlowRuleForArp(id);
93 log.warn("Default rule has been set");
97 * Populates the forwarding rules for VMs with the same VNI but in other Code.
99 * @param vni VNI for the networks
100 * @param id device ID to populates the flow rules
101 * @param hostIp host IP address of the VM
102 * @param vmIp fixed IP address for the VM
103 * @param vmMac MAC address for the VM
104 * @param tunnelPort tunnel port number for the VM
105 * @param idx device ID for OVS of the other VM
106 * @param hostIpx host IP address of the other VM
107 * @param vmIpx fixed IP address of the other VM
108 * @param vmMacx MAC address for the other VM
109 * @param tunnelPortx x tunnel port number for other VM
111 public void populateForwardingRuleForOtherCnode(String vni, DeviceId id, Ip4Address hostIp,
112 Ip4Address vmIp, MacAddress vmMac, PortNumber tunnelPort,
113 DeviceId idx, Ip4Address hostIpx,
114 Ip4Address vmIpx, MacAddress vmMacx, PortNumber tunnelPortx) {
115 setVxLanFlowRule(vni, id, hostIp, vmIp, vmMac, tunnelPort);
116 setVxLanFlowRule(vni, idx, hostIpx, vmIpx, vmMacx, tunnelPortx);
120 * Populates the flow rules for DHCP packets from VMs.
122 * @param id device ID to set the rules
124 private void setFlowRuleForDHCP(DeviceId id) {
125 TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder();
126 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
128 sBuilder.matchEthType(Ethernet.TYPE_IPV4)
129 .matchIPProtocol(IPv4.PROTOCOL_UDP)
130 .matchUdpDst(TpPort.tpPort(OpenstackSwitchingManager.DHCP_PORT));
131 tBuilder.setOutput(PortNumber.CONTROLLER);
133 ForwardingObjective fo = DefaultForwardingObjective.builder()
134 .withSelector(sBuilder.build())
135 .withTreatment(tBuilder.build())
137 .withFlag(ForwardingObjective.Flag.VERSATILE)
141 flowObjectiveService.forward(id, fo);
145 * Populates the flow rules for ARP packets from VMs.
147 * @param id device ID to put rules.
149 private void setFlowRuleForArp(DeviceId id) {
150 TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder();
151 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
153 sBuilder.matchEthType(Ethernet.TYPE_ARP);
154 tBuilder.setOutput(PortNumber.CONTROLLER);
156 ForwardingObjective fo = DefaultForwardingObjective.builder()
157 .withSelector(sBuilder.build())
158 .withTreatment(tBuilder.build())
160 .withFlag(ForwardingObjective.Flag.VERSATILE)
164 flowObjectiveService.forward(id, fo);
168 * Sets the flow rules for traffic between VMs in the same Cnode.
170 * @param ip4Address VM IP address
171 * @param id device ID to put rules
172 * @param port VM port
173 * @param cidr subnet info of the VMs
175 private void setFlowRuleForVMsInSameCnode(Ip4Address ip4Address, DeviceId id,
176 Port port, Ip4Prefix cidr) {
177 TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder();
178 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
180 sBuilder.matchEthType(Ethernet.TYPE_IPV4)
181 .matchIPDst(ip4Address.toIpPrefix())
183 tBuilder.setOutput(port.number());
185 ForwardingObjective fo = DefaultForwardingObjective.builder()
186 .withSelector(sBuilder.build())
187 .withTreatment(tBuilder.build())
189 .withFlag(ForwardingObjective.Flag.VERSATILE)
193 flowObjectiveService.forward(id, fo);
197 * Sets the flow rules between traffic from VMs in different Cnode.
200 * @param id device ID
201 * @param hostIp host IP of the VM
202 * @param vmIp fixed IP of the VM
203 * @param vmMac MAC address of the VM
204 * @param tunnelPort tunnel port to forward traffic to
206 private void setVxLanFlowRule(String vni, DeviceId id, Ip4Address hostIp,
207 Ip4Address vmIp, MacAddress vmMac, PortNumber tunnelPort) {
208 TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder();
209 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
211 sBuilder.matchEthType(Ethernet.TYPE_IPV4)
212 .matchIPDst(vmIp.toIpPrefix());
213 tBuilder.setTunnelId(Long.parseLong(vni))
214 //.setTunnelDst() <- for Nicira ext
216 .setOutput(tunnelPort);
218 ForwardingObjective fo = DefaultForwardingObjective.builder()
219 .withSelector(sBuilder.build())
220 .withTreatment(tBuilder.build())
222 .withFlag(ForwardingObjective.Flag.VERSATILE)
226 flowObjectiveService.forward(id, fo);