sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package org.onosproject.openstackswitching; |
| 18 | |
| 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; |
| 38 | |
| 39 | /** |
sanghoshin | 46297d2 | 2015-11-03 17:51:24 +0900 | [diff] [blame^] | 40 | * Populates switching flow rules. |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 41 | */ |
| 42 | public 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 Milkey | 87b16b0 | 2015-10-30 11:50:00 -0700 | [diff] [blame] | 51 | * Creates OpenstackSwitchingRulPopulator. |
| 52 | * |
| 53 | * @param appId application id |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 54 | * @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 Milkey | 87b16b0 | 2015-10-30 11:50:00 -0700 | [diff] [blame] | 65 | * @param ip v4 IP Address |
| 66 | * @param id device ID |
| 67 | * @param port port |
| 68 | * @param cidr v4 IP prefix |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 69 | * @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 | |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 89 | 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 Milkey | 87b16b0 | 2015-10-30 11:50:00 -0700 | [diff] [blame] | 101 | * @param vmMac MAC address for the VM |
| 102 | * @param tunnelPort tunnel port number for the VM |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 103 | * @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 Milkey | 87b16b0 | 2015-10-30 11:50:00 -0700 | [diff] [blame] | 106 | * @param vmMacx MAC address for the other VM |
| 107 | * @param tunnelPortx x tunnel port number for other VM |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 108 | */ |
| 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 Hart | b35540a | 2015-11-17 09:30:56 -0800 | [diff] [blame] | 122 | private void setFlowRuleForDhcp(DeviceId id) { |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 123 | 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 | } |