blob: fa0b7e695b8835030ed2de1a8ed2a74aaaf78019 [file] [log] [blame]
sanghoshin94872a12015-10-16 18:04:34 +09001/*
2* Copyright 2015 Open Networking Laboratory
3*
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
7*
8* http://www.apache.org/licenses/LICENSE-2.0
9*
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.
15*/
16
17package org.onosproject.openstackswitching;
18
19import org.onlab.packet.Ethernet;
20import org.onlab.packet.IPv4;
21import org.onlab.packet.Ip4Address;
22import org.onlab.packet.Ip4Prefix;
23import org.onlab.packet.MacAddress;
24import org.onlab.packet.TpPort;
25import org.onosproject.core.ApplicationId;
26import org.onosproject.net.DeviceId;
27import org.onosproject.net.Port;
28import org.onosproject.net.PortNumber;
29import org.onosproject.net.flow.DefaultTrafficSelector;
30import org.onosproject.net.flow.DefaultTrafficTreatment;
31import org.onosproject.net.flow.TrafficSelector;
32import org.onosproject.net.flow.TrafficTreatment;
33import org.onosproject.net.flowobjective.DefaultForwardingObjective;
34import org.onosproject.net.flowobjective.FlowObjectiveService;
35import org.onosproject.net.flowobjective.ForwardingObjective;
36import org.slf4j.Logger;
37import org.slf4j.LoggerFactory;
38
39/**
sanghoshin46297d22015-11-03 17:51:24 +090040 * Populates switching flow rules.
sanghoshin94872a12015-10-16 18:04:34 +090041 */
42public class OpenstackSwitchingRulePopulator {
43
44 private static Logger log = LoggerFactory
45 .getLogger(OpenstackSwitchingRulePopulator.class);
46
47 private FlowObjectiveService flowObjectiveService;
48 private ApplicationId appId;
49
50 /**
Ray Milkey87b16b02015-10-30 11:50:00 -070051 * Creates OpenstackSwitchingRulPopulator.
52 *
53 * @param appId application id
sanghoshin94872a12015-10-16 18:04:34 +090054 * @param flowObjectiveService FlowObjectiveService reference
55 */
56 public OpenstackSwitchingRulePopulator(ApplicationId appId,
57 FlowObjectiveService flowObjectiveService) {
58 this.flowObjectiveService = flowObjectiveService;
59 this.appId = appId;
60 }
61
62 /**
63 * Populates flows rules for forwarding packets to and from VMs.
64 *
Ray Milkey87b16b02015-10-30 11:50:00 -070065 * @param ip v4 IP Address
66 * @param id device ID
67 * @param port port
68 * @param cidr v4 IP prefix
sanghoshin94872a12015-10-16 18:04:34 +090069 * @return true if it succeeds to populate rules, false otherwise.
70 */
71 public boolean populateForwardingRule(Ip4Address ip, DeviceId id, Port port, Ip4Prefix cidr) {
72
73
74 setFlowRuleForVMsInSameCnode(ip, id, port, cidr);
75
76 return true;
77 }
78
79 /**
80 * Populates the common flows rules for all VMs.
81 *
82 * - Send ARP packets to the controller
83 * - Send DHCP packets to the controller
84 *
85 * @param id Device ID to populates rules to
86 */
87 public void populateDefaultRules(DeviceId id) {
88
sanghoshin94872a12015-10-16 18:04:34 +090089 setFlowRuleForArp(id);
90
91 log.warn("Default rule has been set");
92 }
93
94 /**
95 * Populates the forwarding rules for VMs with the same VNI but in other Code.
96 *
97 * @param vni VNI for the networks
98 * @param id device ID to populates the flow rules
99 * @param hostIp host IP address of the VM
100 * @param vmIp fixed IP address for the VM
Ray Milkey87b16b02015-10-30 11:50:00 -0700101 * @param vmMac MAC address for the VM
102 * @param tunnelPort tunnel port number for the VM
sanghoshin94872a12015-10-16 18:04:34 +0900103 * @param idx device ID for OVS of the other VM
104 * @param hostIpx host IP address of the other VM
105 * @param vmIpx fixed IP address of the other VM
Ray Milkey87b16b02015-10-30 11:50:00 -0700106 * @param vmMacx MAC address for the other VM
107 * @param tunnelPortx x tunnel port number for other VM
sanghoshin94872a12015-10-16 18:04:34 +0900108 */
109 public void populateForwardingRuleForOtherCnode(String vni, DeviceId id, Ip4Address hostIp,
110 Ip4Address vmIp, MacAddress vmMac, PortNumber tunnelPort,
111 DeviceId idx, Ip4Address hostIpx,
112 Ip4Address vmIpx, MacAddress vmMacx, PortNumber tunnelPortx) {
113 setVxLanFlowRule(vni, id, hostIp, vmIp, vmMac, tunnelPort);
114 setVxLanFlowRule(vni, idx, hostIpx, vmIpx, vmMacx, tunnelPortx);
115 }
116
117 /**
118 * Populates the flow rules for DHCP packets from VMs.
119 *
120 * @param id device ID to set the rules
121 */
Jonathan Hartb35540a2015-11-17 09:30:56 -0800122 private void setFlowRuleForDhcp(DeviceId id) {
sanghoshin94872a12015-10-16 18:04:34 +0900123 TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder();
124 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
125
126 sBuilder.matchEthType(Ethernet.TYPE_IPV4)
127 .matchIPProtocol(IPv4.PROTOCOL_UDP)
128 .matchUdpDst(TpPort.tpPort(OpenstackSwitchingManager.DHCP_PORT));
129 tBuilder.setOutput(PortNumber.CONTROLLER);
130
131 ForwardingObjective fo = DefaultForwardingObjective.builder()
132 .withSelector(sBuilder.build())
133 .withTreatment(tBuilder.build())
134 .withPriority(5000)
135 .withFlag(ForwardingObjective.Flag.VERSATILE)
136 .fromApp(appId)
137 .add();
138
139 flowObjectiveService.forward(id, fo);
140 }
141
142 /**
143 * Populates the flow rules for ARP packets from VMs.
144 *
145 * @param id device ID to put rules.
146 */
147 private void setFlowRuleForArp(DeviceId id) {
148 TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder();
149 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
150
151 sBuilder.matchEthType(Ethernet.TYPE_ARP);
152 tBuilder.setOutput(PortNumber.CONTROLLER);
153
154 ForwardingObjective fo = DefaultForwardingObjective.builder()
155 .withSelector(sBuilder.build())
156 .withTreatment(tBuilder.build())
157 .withPriority(5000)
158 .withFlag(ForwardingObjective.Flag.VERSATILE)
159 .fromApp(appId)
160 .add();
161
162 flowObjectiveService.forward(id, fo);
163 }
164
165 /**
166 * Sets the flow rules for traffic between VMs in the same Cnode.
167 *
168 * @param ip4Address VM IP address
169 * @param id device ID to put rules
170 * @param port VM port
171 * @param cidr subnet info of the VMs
172 */
173 private void setFlowRuleForVMsInSameCnode(Ip4Address ip4Address, DeviceId id,
174 Port port, Ip4Prefix cidr) {
175 TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder();
176 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
177
178 sBuilder.matchEthType(Ethernet.TYPE_IPV4)
179 .matchIPDst(ip4Address.toIpPrefix())
180 .matchIPSrc(cidr);
181 tBuilder.setOutput(port.number());
182
183 ForwardingObjective fo = DefaultForwardingObjective.builder()
184 .withSelector(sBuilder.build())
185 .withTreatment(tBuilder.build())
186 .withPriority(5000)
187 .withFlag(ForwardingObjective.Flag.VERSATILE)
188 .fromApp(appId)
189 .add();
190
191 flowObjectiveService.forward(id, fo);
192 }
193
194 /**
195 * Sets the flow rules between traffic from VMs in different Cnode.
196 *
197 * @param vni VNI
198 * @param id device ID
199 * @param hostIp host IP of the VM
200 * @param vmIp fixed IP of the VM
201 * @param vmMac MAC address of the VM
202 * @param tunnelPort tunnel port to forward traffic to
203 */
204 private void setVxLanFlowRule(String vni, DeviceId id, Ip4Address hostIp,
205 Ip4Address vmIp, MacAddress vmMac, PortNumber tunnelPort) {
206 TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder();
207 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
208
209 sBuilder.matchEthType(Ethernet.TYPE_IPV4)
210 .matchIPDst(vmIp.toIpPrefix());
211 tBuilder.setTunnelId(Long.parseLong(vni))
212 //.setTunnelDst() <- for Nicira ext
213 //.setEthDst(vmMac)
214 .setOutput(tunnelPort);
215
216 ForwardingObjective fo = DefaultForwardingObjective.builder()
217 .withSelector(sBuilder.build())
218 .withTreatment(tBuilder.build())
219 .withPriority(5000)
220 .withFlag(ForwardingObjective.Flag.VERSATILE)
221 .fromApp(appId)
222 .add();
223
224 flowObjectiveService.forward(id, fo);
225 }
226}