Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 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 | package org.onosproject.openstacknetworking.impl; |
| 17 | |
| 18 | import com.google.common.base.Strings; |
Daniel Park | d9d4c29 | 2018-06-26 20:33:58 +0900 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableList; |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 20 | import com.google.common.collect.Lists; |
| 21 | import org.apache.felix.scr.annotations.Activate; |
| 22 | import org.apache.felix.scr.annotations.Component; |
| 23 | import org.apache.felix.scr.annotations.Deactivate; |
| 24 | import org.apache.felix.scr.annotations.Modified; |
| 25 | import org.apache.felix.scr.annotations.Property; |
| 26 | import org.apache.felix.scr.annotations.Reference; |
| 27 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 28 | import org.onlab.packet.DHCP; |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 29 | import org.onlab.packet.Ethernet; |
| 30 | import org.onlab.packet.IPv4; |
| 31 | import org.onlab.packet.Ip4Address; |
Daniel Park | 4d42100 | 2018-07-27 23:36:57 +0900 | [diff] [blame] | 32 | import org.onlab.packet.IpAddress; |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 33 | import org.onlab.packet.IpPrefix; |
| 34 | import org.onlab.packet.MacAddress; |
| 35 | import org.onlab.packet.TpPort; |
| 36 | import org.onlab.packet.UDP; |
daniel park | 796c2eb | 2018-03-22 17:01:51 +0900 | [diff] [blame] | 37 | import org.onlab.packet.dhcp.DhcpOption; |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 38 | import org.onlab.util.Tools; |
| 39 | import org.onosproject.cfg.ComponentConfigService; |
daniel park | 15506e8 | 2018-04-04 18:52:16 +0900 | [diff] [blame] | 40 | import org.onosproject.cluster.ClusterService; |
| 41 | import org.onosproject.cluster.LeadershipService; |
| 42 | import org.onosproject.cluster.NodeId; |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 43 | import org.onosproject.core.ApplicationId; |
| 44 | import org.onosproject.core.CoreService; |
| 45 | import org.onosproject.net.ConnectPoint; |
| 46 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 47 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 48 | import org.onosproject.net.flow.TrafficSelector; |
| 49 | import org.onosproject.net.flow.TrafficTreatment; |
| 50 | import org.onosproject.net.packet.DefaultOutboundPacket; |
| 51 | import org.onosproject.net.packet.PacketContext; |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 52 | import org.onosproject.net.packet.PacketProcessor; |
| 53 | import org.onosproject.net.packet.PacketService; |
| 54 | import org.onosproject.openstacknetworking.api.Constants; |
| 55 | import org.onosproject.openstacknetworking.api.InstancePort; |
| 56 | import org.onosproject.openstacknetworking.api.InstancePortService; |
daniel park | 796c2eb | 2018-03-22 17:01:51 +0900 | [diff] [blame] | 57 | import org.onosproject.openstacknetworking.api.OpenstackFlowRuleService; |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 58 | import org.onosproject.openstacknetworking.api.OpenstackNetworkService; |
daniel park | 15506e8 | 2018-04-04 18:52:16 +0900 | [diff] [blame] | 59 | import org.onosproject.openstacknode.api.OpenstackNode; |
| 60 | import org.onosproject.openstacknode.api.OpenstackNodeEvent; |
| 61 | import org.onosproject.openstacknode.api.OpenstackNodeListener; |
daniel park | 796c2eb | 2018-03-22 17:01:51 +0900 | [diff] [blame] | 62 | import org.onosproject.openstacknode.api.OpenstackNodeService; |
Daniel Park | d9d4c29 | 2018-06-26 20:33:58 +0900 | [diff] [blame] | 63 | import org.openstack4j.model.network.HostRoute; |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 64 | import org.openstack4j.model.network.IP; |
Daniel Park | 468e785 | 2018-07-28 00:38:45 +0900 | [diff] [blame] | 65 | import org.openstack4j.model.network.Network; |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 66 | import org.openstack4j.model.network.Port; |
| 67 | import org.openstack4j.model.network.Subnet; |
| 68 | import org.osgi.service.component.ComponentContext; |
| 69 | import org.slf4j.Logger; |
| 70 | |
| 71 | import java.nio.ByteBuffer; |
| 72 | import java.util.Dictionary; |
| 73 | import java.util.List; |
daniel park | 15506e8 | 2018-04-04 18:52:16 +0900 | [diff] [blame] | 74 | import java.util.Objects; |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 75 | |
Daniel Park | f8e422d | 2018-07-30 14:14:37 +0900 | [diff] [blame] | 76 | import static com.google.common.base.Preconditions.checkNotNull; |
daniel park | 796c2eb | 2018-03-22 17:01:51 +0900 | [diff] [blame] | 77 | import static org.onlab.packet.DHCP.DHCPOptionCode.OptionCode_BroadcastAddress; |
Daniel Park | d9d4c29 | 2018-06-26 20:33:58 +0900 | [diff] [blame] | 78 | import static org.onlab.packet.DHCP.DHCPOptionCode.OptionCode_Classless_Static_Route; |
daniel park | 796c2eb | 2018-03-22 17:01:51 +0900 | [diff] [blame] | 79 | import static org.onlab.packet.DHCP.DHCPOptionCode.OptionCode_DHCPServerIp; |
| 80 | import static org.onlab.packet.DHCP.DHCPOptionCode.OptionCode_DomainServer; |
| 81 | import static org.onlab.packet.DHCP.DHCPOptionCode.OptionCode_END; |
| 82 | import static org.onlab.packet.DHCP.DHCPOptionCode.OptionCode_LeaseTime; |
| 83 | import static org.onlab.packet.DHCP.DHCPOptionCode.OptionCode_MessageType; |
Daniel Park | 48f1033 | 2018-08-02 10:57:27 +0900 | [diff] [blame] | 84 | import static org.onlab.packet.DHCP.DHCPOptionCode.OptionCode_RouterAddress; |
daniel park | 796c2eb | 2018-03-22 17:01:51 +0900 | [diff] [blame] | 85 | import static org.onlab.packet.DHCP.DHCPOptionCode.OptionCode_SubnetMask; |
Yi Tseng | c7403c2 | 2017-06-19 16:23:22 -0700 | [diff] [blame] | 86 | import static org.onlab.packet.DHCP.MsgType.DHCPACK; |
| 87 | import static org.onlab.packet.DHCP.MsgType.DHCPOFFER; |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 88 | import static org.onosproject.openstacknetworking.api.Constants.DEFAULT_GATEWAY_MAC_STR; |
Jian Li | 70a2c3f | 2018-04-13 17:26:31 +0900 | [diff] [blame] | 89 | import static org.onosproject.openstacknetworking.api.Constants.DHCP_ARP_TABLE; |
daniel park | 796c2eb | 2018-03-22 17:01:51 +0900 | [diff] [blame] | 90 | import static org.onosproject.openstacknetworking.api.Constants.PRIORITY_DHCP_RULE; |
Jian Li | 51b844c | 2018-05-31 10:59:03 +0900 | [diff] [blame] | 91 | import static org.onosproject.openstacknode.api.OpenstackNode.NodeType.COMPUTE; |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 92 | import static org.slf4j.LoggerFactory.getLogger; |
| 93 | |
| 94 | /** |
| 95 | * Handles DHCP requests for the virtual instances. |
| 96 | */ |
| 97 | @Component(immediate = true) |
| 98 | public class OpenstackSwitchingDhcpHandler { |
| 99 | protected final Logger log = getLogger(getClass()); |
| 100 | |
| 101 | private static final String DHCP_SERVER_MAC = "dhcpServerMac"; |
Daniel Park | 4d42100 | 2018-07-27 23:36:57 +0900 | [diff] [blame] | 102 | private static final Ip4Address DEFAULT_PRIMARY_DNS = Ip4Address.valueOf("8.8.8.8"); |
| 103 | private static final Ip4Address DEFAULT_SECONDARY_DNS = Ip4Address.valueOf("8.8.4.4"); |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 104 | private static final byte PACKET_TTL = (byte) 127; |
| 105 | // TODO add MTU, static route option codes to ONOS DHCP and remove here |
| 106 | private static final byte DHCP_OPTION_MTU = (byte) 26; |
| 107 | private static final byte[] DHCP_DATA_LEASE_INFINITE = |
| 108 | ByteBuffer.allocate(4).putInt(-1).array(); |
Jian Li | fb00549 | 2018-03-02 10:50:15 +0900 | [diff] [blame] | 109 | // we are using 1450 as a default DHCP MTU value |
| 110 | private static final int DHCP_DATA_MTU_DEFAULT = 1450; |
Daniel Park | d9d4c29 | 2018-06-26 20:33:58 +0900 | [diff] [blame] | 111 | private static final int OCTET_BIT_LENGTH = 8; |
| 112 | private static final int V4_BYTE_SIZE = 4; |
Daniel Park | f8e422d | 2018-07-30 14:14:37 +0900 | [diff] [blame] | 113 | private static final int V4_CIDR_LOWER_BOUND = -1; |
Daniel Park | d9d4c29 | 2018-06-26 20:33:58 +0900 | [diff] [blame] | 114 | private static final int V4_CIDR_UPPER_BOUND = 33; |
| 115 | private static final int PADDING_SIZE = 4; |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 116 | |
| 117 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 118 | protected CoreService coreService; |
| 119 | |
| 120 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 121 | protected ComponentConfigService configService; |
| 122 | |
| 123 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 124 | protected PacketService packetService; |
| 125 | |
| 126 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 127 | protected InstancePortService instancePortService; |
| 128 | |
| 129 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 130 | protected OpenstackNetworkService osNetworkService; |
| 131 | |
daniel park | 796c2eb | 2018-03-22 17:01:51 +0900 | [diff] [blame] | 132 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 133 | protected OpenstackNodeService osNodeService; |
| 134 | |
| 135 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 136 | protected OpenstackFlowRuleService osFlowRuleService; |
| 137 | |
daniel park | 15506e8 | 2018-04-04 18:52:16 +0900 | [diff] [blame] | 138 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 139 | protected ClusterService clusterService; |
| 140 | |
| 141 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 142 | protected LeadershipService leadershipService; |
| 143 | |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 144 | @Property(name = DHCP_SERVER_MAC, value = DEFAULT_GATEWAY_MAC_STR, |
| 145 | label = "Fake MAC address for virtual network subnet gateway") |
| 146 | private String dhcpServerMac = DEFAULT_GATEWAY_MAC_STR; |
| 147 | |
Jian Li | fb00549 | 2018-03-02 10:50:15 +0900 | [diff] [blame] | 148 | private int dhcpDataMtu = DHCP_DATA_MTU_DEFAULT; |
| 149 | |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 150 | private final PacketProcessor packetProcessor = new InternalPacketProcessor(); |
daniel park | 15506e8 | 2018-04-04 18:52:16 +0900 | [diff] [blame] | 151 | private final OpenstackNodeListener osNodeListener = new InternalNodeEventListener(); |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 152 | |
| 153 | private ApplicationId appId; |
daniel park | 15506e8 | 2018-04-04 18:52:16 +0900 | [diff] [blame] | 154 | private NodeId localNodeId; |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 155 | |
| 156 | @Activate |
| 157 | protected void activate() { |
| 158 | appId = coreService.registerApplication(Constants.OPENSTACK_NETWORKING_APP_ID); |
daniel park | 15506e8 | 2018-04-04 18:52:16 +0900 | [diff] [blame] | 159 | localNodeId = clusterService.getLocalNode().id(); |
| 160 | osNodeService.addListener(osNodeListener); |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 161 | configService.registerProperties(getClass()); |
| 162 | packetService.addProcessor(packetProcessor, PacketProcessor.director(0)); |
daniel park | 15506e8 | 2018-04-04 18:52:16 +0900 | [diff] [blame] | 163 | leadershipService.runForLeadership(appId.name()); |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 164 | |
| 165 | log.info("Started"); |
| 166 | } |
| 167 | |
| 168 | @Deactivate |
| 169 | protected void deactivate() { |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 170 | packetService.removeProcessor(packetProcessor); |
daniel park | 15506e8 | 2018-04-04 18:52:16 +0900 | [diff] [blame] | 171 | osNodeService.removeListener(osNodeListener); |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 172 | configService.unregisterProperties(getClass(), false); |
daniel park | 15506e8 | 2018-04-04 18:52:16 +0900 | [diff] [blame] | 173 | leadershipService.withdraw(appId.name()); |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 174 | |
| 175 | log.info("Stopped"); |
| 176 | } |
| 177 | |
| 178 | @Modified |
| 179 | protected void modified(ComponentContext context) { |
| 180 | Dictionary<?, ?> properties = context.getProperties(); |
| 181 | String updatedMac; |
| 182 | |
| 183 | updatedMac = Tools.get(properties, DHCP_SERVER_MAC); |
Jian Li | fb00549 | 2018-03-02 10:50:15 +0900 | [diff] [blame] | 184 | |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 185 | if (!Strings.isNullOrEmpty(updatedMac) && !updatedMac.equals(dhcpServerMac)) { |
| 186 | dhcpServerMac = updatedMac; |
| 187 | } |
| 188 | |
| 189 | log.info("Modified"); |
| 190 | } |
| 191 | |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 192 | private class InternalPacketProcessor implements PacketProcessor { |
| 193 | |
| 194 | @Override |
| 195 | public void process(PacketContext context) { |
| 196 | if (context.isHandled()) { |
| 197 | return; |
| 198 | } |
| 199 | |
| 200 | Ethernet ethPacket = context.inPacket().parsed(); |
| 201 | if (ethPacket == null || ethPacket.getEtherType() != Ethernet.TYPE_IPV4) { |
| 202 | return; |
| 203 | } |
| 204 | IPv4 ipv4Packet = (IPv4) ethPacket.getPayload(); |
| 205 | if (ipv4Packet.getProtocol() != IPv4.PROTOCOL_UDP) { |
| 206 | return; |
| 207 | } |
| 208 | UDP udpPacket = (UDP) ipv4Packet.getPayload(); |
| 209 | if (udpPacket.getDestinationPort() != UDP.DHCP_SERVER_PORT || |
| 210 | udpPacket.getSourcePort() != UDP.DHCP_CLIENT_PORT) { |
| 211 | return; |
| 212 | } |
| 213 | |
| 214 | DHCP dhcpPacket = (DHCP) udpPacket.getPayload(); |
| 215 | processDhcp(context, dhcpPacket); |
| 216 | } |
| 217 | |
| 218 | private void processDhcp(PacketContext context, DHCP dhcpPacket) { |
| 219 | if (dhcpPacket == null) { |
| 220 | log.trace("DHCP packet without payload received, do nothing"); |
| 221 | return; |
| 222 | } |
| 223 | |
Yi Tseng | c7403c2 | 2017-06-19 16:23:22 -0700 | [diff] [blame] | 224 | DHCP.MsgType inPacketType = getPacketType(dhcpPacket); |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 225 | if (inPacketType == null || dhcpPacket.getClientHardwareAddress() == null) { |
| 226 | log.trace("Malformed DHCP packet received, ignore it"); |
| 227 | return; |
| 228 | } |
| 229 | |
| 230 | MacAddress clientMac = MacAddress.valueOf(dhcpPacket.getClientHardwareAddress()); |
| 231 | InstancePort reqInstPort = instancePortService.instancePort(clientMac); |
| 232 | if (reqInstPort == null) { |
| 233 | log.trace("Failed to find host(MAC:{})", clientMac); |
| 234 | return; |
| 235 | } |
| 236 | Ethernet ethPacket = context.inPacket().parsed(); |
| 237 | switch (inPacketType) { |
| 238 | case DHCPDISCOVER: |
| 239 | log.trace("DHCP DISCOVER received from {}", clientMac); |
| 240 | Ethernet discoverReply = buildReply( |
| 241 | ethPacket, |
| 242 | (byte) DHCPOFFER.getValue(), |
| 243 | reqInstPort); |
| 244 | sendReply(context, discoverReply); |
| 245 | log.trace("DHCP OFFER({}) is sent for {}", |
daniel park | 15506e8 | 2018-04-04 18:52:16 +0900 | [diff] [blame] | 246 | reqInstPort.ipAddress(), clientMac); |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 247 | break; |
| 248 | case DHCPREQUEST: |
| 249 | log.trace("DHCP REQUEST received from {}", clientMac); |
| 250 | Ethernet requestReply = buildReply( |
| 251 | ethPacket, |
| 252 | (byte) DHCPACK.getValue(), |
| 253 | reqInstPort); |
| 254 | sendReply(context, requestReply); |
| 255 | log.trace("DHCP ACK({}) is sent for {}", |
daniel park | 15506e8 | 2018-04-04 18:52:16 +0900 | [diff] [blame] | 256 | reqInstPort.ipAddress(), clientMac); |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 257 | break; |
| 258 | case DHCPRELEASE: |
| 259 | log.trace("DHCP RELEASE received from {}", clientMac); |
| 260 | // do nothing |
| 261 | break; |
| 262 | default: |
| 263 | break; |
| 264 | } |
| 265 | } |
| 266 | |
Yi Tseng | c7403c2 | 2017-06-19 16:23:22 -0700 | [diff] [blame] | 267 | private DHCP.MsgType getPacketType(DHCP dhcpPacket) { |
| 268 | DhcpOption optType = dhcpPacket.getOption(OptionCode_MessageType); |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 269 | if (optType == null) { |
| 270 | log.trace("DHCP packet with no message type, ignore it"); |
| 271 | return null; |
| 272 | } |
| 273 | |
Yi Tseng | c7403c2 | 2017-06-19 16:23:22 -0700 | [diff] [blame] | 274 | DHCP.MsgType inPacketType = DHCP.MsgType.getType(optType.getData()[0]); |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 275 | if (inPacketType == null) { |
| 276 | log.trace("DHCP packet with no packet type, ignore it"); |
| 277 | } |
| 278 | return inPacketType; |
| 279 | } |
| 280 | |
| 281 | private Ethernet buildReply(Ethernet ethRequest, byte packetType, |
| 282 | InstancePort reqInstPort) { |
Daniel Park | e0945c1 | 2018-08-28 17:22:25 +0900 | [diff] [blame] | 283 | log.trace("Build for DHCP reply msg for instance port {}", reqInstPort.toString()); |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 284 | Port osPort = osNetworkService.port(reqInstPort.portId()); |
Daniel Park | e0945c1 | 2018-08-28 17:22:25 +0900 | [diff] [blame] | 285 | if (osPort == null) { |
| 286 | log.error("Failed to retrieve openstack port information for instance port {}", |
| 287 | reqInstPort.toString()); |
Ray Milkey | 9281593 | 2018-08-28 10:32:17 -0700 | [diff] [blame] | 288 | return null; |
Daniel Park | e0945c1 | 2018-08-28 17:22:25 +0900 | [diff] [blame] | 289 | } |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 290 | // pick one IP address to make a reply |
| 291 | IP fixedIp = osPort.getFixedIps().stream().findFirst().get(); |
| 292 | Subnet osSubnet = osNetworkService.subnet(fixedIp.getSubnetId()); |
| 293 | |
| 294 | Ethernet ethReply = new Ethernet(); |
| 295 | ethReply.setSourceMACAddress(dhcpServerMac); |
| 296 | ethReply.setDestinationMACAddress(ethRequest.getSourceMAC()); |
| 297 | ethReply.setEtherType(Ethernet.TYPE_IPV4); |
| 298 | |
| 299 | IPv4 ipv4Request = (IPv4) ethRequest.getPayload(); |
| 300 | IPv4 ipv4Reply = new IPv4(); |
daniel park | 15506e8 | 2018-04-04 18:52:16 +0900 | [diff] [blame] | 301 | |
| 302 | ipv4Reply.setSourceAddress(clusterService.getLocalNode().ip().getIp4Address().toString()); |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 303 | ipv4Reply.setDestinationAddress(reqInstPort.ipAddress().getIp4Address().toInt()); |
| 304 | ipv4Reply.setTtl(PACKET_TTL); |
| 305 | |
| 306 | UDP udpRequest = (UDP) ipv4Request.getPayload(); |
| 307 | UDP udpReply = new UDP(); |
| 308 | udpReply.setSourcePort((byte) UDP.DHCP_SERVER_PORT); |
| 309 | udpReply.setDestinationPort((byte) UDP.DHCP_CLIENT_PORT); |
| 310 | |
| 311 | DHCP dhcpRequest = (DHCP) udpRequest.getPayload(); |
| 312 | DHCP dhcpReply = buildDhcpReply( |
| 313 | dhcpRequest, |
| 314 | packetType, |
| 315 | reqInstPort.ipAddress().getIp4Address(), |
| 316 | osSubnet); |
| 317 | |
| 318 | udpReply.setPayload(dhcpReply); |
| 319 | ipv4Reply.setPayload(udpReply); |
| 320 | ethReply.setPayload(ipv4Reply); |
| 321 | |
| 322 | return ethReply; |
| 323 | } |
| 324 | |
| 325 | private void sendReply(PacketContext context, Ethernet ethReply) { |
| 326 | if (ethReply == null) { |
| 327 | return; |
| 328 | } |
| 329 | ConnectPoint srcPoint = context.inPacket().receivedFrom(); |
| 330 | TrafficTreatment treatment = DefaultTrafficTreatment |
| 331 | .builder() |
| 332 | .setOutput(srcPoint.port()) |
| 333 | .build(); |
| 334 | |
| 335 | packetService.emit(new DefaultOutboundPacket( |
| 336 | srcPoint.deviceId(), |
| 337 | treatment, |
| 338 | ByteBuffer.wrap(ethReply.serialize()))); |
| 339 | context.block(); |
| 340 | } |
| 341 | |
| 342 | private DHCP buildDhcpReply(DHCP request, byte msgType, Ip4Address yourIp, |
| 343 | Subnet osSubnet) { |
daniel park | 15506e8 | 2018-04-04 18:52:16 +0900 | [diff] [blame] | 344 | Ip4Address gatewayIp = clusterService.getLocalNode().ip().getIp4Address(); |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 345 | int subnetPrefixLen = IpPrefix.valueOf(osSubnet.getCidr()).prefixLength(); |
| 346 | |
| 347 | DHCP dhcpReply = new DHCP(); |
| 348 | dhcpReply.setOpCode(DHCP.OPCODE_REPLY); |
| 349 | dhcpReply.setHardwareType(DHCP.HWTYPE_ETHERNET); |
| 350 | dhcpReply.setHardwareAddressLength((byte) 6); |
| 351 | dhcpReply.setTransactionId(request.getTransactionId()); |
| 352 | dhcpReply.setFlags(request.getFlags()); |
| 353 | dhcpReply.setYourIPAddress(yourIp.toInt()); |
| 354 | dhcpReply.setServerIPAddress(gatewayIp.toInt()); |
| 355 | dhcpReply.setClientHardwareAddress(request.getClientHardwareAddress()); |
| 356 | |
Yi Tseng | c7403c2 | 2017-06-19 16:23:22 -0700 | [diff] [blame] | 357 | List<DhcpOption> options = Lists.newArrayList(); |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 358 | // message type |
Yi Tseng | c7403c2 | 2017-06-19 16:23:22 -0700 | [diff] [blame] | 359 | DhcpOption option = new DhcpOption(); |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 360 | option.setCode(OptionCode_MessageType.getValue()); |
| 361 | option.setLength((byte) 1); |
| 362 | byte[] optionData = {msgType}; |
| 363 | option.setData(optionData); |
| 364 | options.add(option); |
| 365 | |
| 366 | // server identifier |
Yi Tseng | c7403c2 | 2017-06-19 16:23:22 -0700 | [diff] [blame] | 367 | option = new DhcpOption(); |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 368 | option.setCode(OptionCode_DHCPServerIp.getValue()); |
| 369 | option.setLength((byte) 4); |
| 370 | option.setData(gatewayIp.toOctets()); |
| 371 | options.add(option); |
| 372 | |
| 373 | // lease time |
Yi Tseng | c7403c2 | 2017-06-19 16:23:22 -0700 | [diff] [blame] | 374 | option = new DhcpOption(); |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 375 | option.setCode(OptionCode_LeaseTime.getValue()); |
| 376 | option.setLength((byte) 4); |
| 377 | option.setData(DHCP_DATA_LEASE_INFINITE); |
| 378 | options.add(option); |
| 379 | |
| 380 | // subnet mask |
| 381 | Ip4Address subnetMask = Ip4Address.makeMaskPrefix(subnetPrefixLen); |
Yi Tseng | c7403c2 | 2017-06-19 16:23:22 -0700 | [diff] [blame] | 382 | option = new DhcpOption(); |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 383 | option.setCode(OptionCode_SubnetMask.getValue()); |
| 384 | option.setLength((byte) 4); |
| 385 | option.setData(subnetMask.toOctets()); |
| 386 | options.add(option); |
| 387 | |
| 388 | // broadcast address |
| 389 | Ip4Address broadcast = Ip4Address.makeMaskedAddress(yourIp, subnetPrefixLen); |
Yi Tseng | c7403c2 | 2017-06-19 16:23:22 -0700 | [diff] [blame] | 390 | option = new DhcpOption(); |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 391 | option.setCode(OptionCode_BroadcastAddress.getValue()); |
| 392 | option.setLength((byte) 4); |
| 393 | option.setData(broadcast.toOctets()); |
| 394 | options.add(option); |
| 395 | |
| 396 | // domain server |
Yi Tseng | c7403c2 | 2017-06-19 16:23:22 -0700 | [diff] [blame] | 397 | option = new DhcpOption(); |
Daniel Park | 4d42100 | 2018-07-27 23:36:57 +0900 | [diff] [blame] | 398 | |
| 399 | List<String> dnsServers = osSubnet.getDnsNames(); |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 400 | option.setCode(OptionCode_DomainServer.getValue()); |
Daniel Park | 4d42100 | 2018-07-27 23:36:57 +0900 | [diff] [blame] | 401 | |
| 402 | if (dnsServers.isEmpty()) { |
| 403 | option.setLength((byte) 8); |
| 404 | ByteBuffer dnsByteBuf = ByteBuffer.allocate(8); |
| 405 | dnsByteBuf.put(DEFAULT_PRIMARY_DNS.toOctets()); |
| 406 | dnsByteBuf.put(DEFAULT_SECONDARY_DNS.toOctets()); |
| 407 | |
| 408 | option.setData(dnsByteBuf.array()); |
| 409 | } else { |
| 410 | int dnsLength = 4 * dnsServers.size(); |
| 411 | |
| 412 | option.setLength((byte) dnsLength); |
| 413 | |
| 414 | ByteBuffer dnsByteBuf = ByteBuffer.allocate(8); |
| 415 | |
| 416 | for (int i = 0; i < dnsServers.size(); i++) { |
| 417 | dnsByteBuf.put(IpAddress.valueOf(dnsServers.get(i)).toOctets()); |
| 418 | } |
| 419 | option.setData(dnsByteBuf.array()); |
| 420 | } |
| 421 | |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 422 | options.add(option); |
| 423 | |
Yi Tseng | c7403c2 | 2017-06-19 16:23:22 -0700 | [diff] [blame] | 424 | option = new DhcpOption(); |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 425 | option.setCode(DHCP_OPTION_MTU); |
| 426 | option.setLength((byte) 2); |
Daniel Park | 468e785 | 2018-07-28 00:38:45 +0900 | [diff] [blame] | 427 | Network osNetwork = osNetworkService.network(osSubnet.getNetworkId()); |
| 428 | checkNotNull(osNetwork); |
| 429 | checkNotNull(osNetwork.getMTU()); |
| 430 | |
| 431 | option.setData(ByteBuffer.allocate(2).putShort(osNetwork.getMTU().shortValue()).array()); |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 432 | options.add(option); |
| 433 | |
Daniel Park | d9d4c29 | 2018-06-26 20:33:58 +0900 | [diff] [blame] | 434 | // classless static route |
| 435 | if (!osSubnet.getHostRoutes().isEmpty()) { |
| 436 | option = new DhcpOption(); |
| 437 | option.setCode(OptionCode_Classless_Static_Route.getValue()); |
| 438 | |
| 439 | int hostRoutesSize = hostRoutesSize(ImmutableList.copyOf(osSubnet.getHostRoutes())); |
| 440 | if (hostRoutesSize == 0) { |
| 441 | throw new IllegalArgumentException("Illegal CIDR hostRoutesSize value!"); |
| 442 | } |
| 443 | |
| 444 | log.trace("hostRouteSize: {}", hostRoutesSize); |
| 445 | |
| 446 | option.setLength((byte) hostRoutesSize); |
| 447 | ByteBuffer hostRouteByteBuf = ByteBuffer.allocate(hostRoutesSize); |
| 448 | |
| 449 | osSubnet.getHostRoutes().forEach(h -> { |
| 450 | log.debug("processing host route information: {}", h.toString()); |
| 451 | |
| 452 | IpPrefix ipPrefix = IpPrefix.valueOf(h.getDestination()); |
| 453 | |
| 454 | hostRouteByteBuf.put(bytesDestinationDescriptor(ipPrefix)); |
| 455 | |
| 456 | hostRouteByteBuf.put(Ip4Address.valueOf(h.getNexthop()).toOctets()); |
| 457 | }); |
| 458 | |
| 459 | option.setData(hostRouteByteBuf.array()); |
| 460 | |
| 461 | options.add(option); |
| 462 | } |
| 463 | |
Daniel Park | 48f1033 | 2018-08-02 10:57:27 +0900 | [diff] [blame] | 464 | // Sets the default router address up. |
| 465 | // Performs only if the gateway is set in subnet. |
| 466 | if (!Strings.isNullOrEmpty(osSubnet.getGateway())) { |
| 467 | option = new DhcpOption(); |
| 468 | option.setCode(OptionCode_RouterAddress.getValue()); |
| 469 | option.setLength((byte) 4); |
| 470 | option.setData(Ip4Address.valueOf(osSubnet.getGateway()).toOctets()); |
| 471 | options.add(option); |
| 472 | } |
| 473 | |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 474 | // end option |
Yi Tseng | c7403c2 | 2017-06-19 16:23:22 -0700 | [diff] [blame] | 475 | option = new DhcpOption(); |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 476 | option.setCode(OptionCode_END.getValue()); |
| 477 | option.setLength((byte) 1); |
| 478 | options.add(option); |
| 479 | |
| 480 | dhcpReply.setOptions(options); |
| 481 | return dhcpReply; |
| 482 | } |
Daniel Park | d9d4c29 | 2018-06-26 20:33:58 +0900 | [diff] [blame] | 483 | |
| 484 | private int hostRoutesSize(List<HostRoute> hostRoutes) { |
| 485 | int size = 0; |
| 486 | int preFixLen; |
| 487 | |
| 488 | for (HostRoute h : hostRoutes) { |
| 489 | preFixLen = IpPrefix.valueOf(h.getDestination()).prefixLength(); |
| 490 | if (Math.max(V4_CIDR_LOWER_BOUND, preFixLen) == V4_CIDR_LOWER_BOUND || |
| 491 | Math.min(preFixLen, V4_CIDR_UPPER_BOUND) == V4_CIDR_UPPER_BOUND) { |
| 492 | throw new IllegalArgumentException("Illegal CIDR length value!"); |
| 493 | } |
| 494 | |
Daniel Park | f8e422d | 2018-07-30 14:14:37 +0900 | [diff] [blame] | 495 | for (int i = 0; i <= V4_BYTE_SIZE; i++) { |
Daniel Park | d9d4c29 | 2018-06-26 20:33:58 +0900 | [diff] [blame] | 496 | if (preFixLen == Math.min(preFixLen, i * OCTET_BIT_LENGTH)) { |
| 497 | size = size + i + 1 + PADDING_SIZE; |
| 498 | break; |
| 499 | } |
| 500 | } |
| 501 | } |
| 502 | return size; |
| 503 | } |
| 504 | |
| 505 | private byte[] bytesDestinationDescriptor(IpPrefix ipPrefix) { |
| 506 | ByteBuffer byteBuffer; |
| 507 | int prefixLen = ipPrefix.prefixLength(); |
| 508 | |
| 509 | // retrieve ipPrefix to the destination descriptor format |
| 510 | // ex) 10.1.1.0/24 -> [10,1,1,0] |
| 511 | String[] ipPrefixString = ipPrefix.getIp4Prefix().toString() |
| 512 | .split("/")[0] |
| 513 | .split("\\."); |
| 514 | |
Daniel Park | f8e422d | 2018-07-30 14:14:37 +0900 | [diff] [blame] | 515 | // retrieve destination descriptor and put this to bytebuffer according to RFC 3442 |
| 516 | // ex) 0.0.0.0/0 -> 0 |
Daniel Park | d9d4c29 | 2018-06-26 20:33:58 +0900 | [diff] [blame] | 517 | // ex) 10.0.0.0/8 -> 8.10 |
| 518 | // ex) 10.17.0.0/16 -> 16.10.17 |
| 519 | // ex) 10.27.129.0/24 -> 24.10.27.129 |
| 520 | // ex) 10.229.0.128/25 -> 25.10.229.0.128 |
Daniel Park | f8e422d | 2018-07-30 14:14:37 +0900 | [diff] [blame] | 521 | for (int i = 0; i <= V4_BYTE_SIZE; i++) { |
Daniel Park | d9d4c29 | 2018-06-26 20:33:58 +0900 | [diff] [blame] | 522 | if (prefixLen == Math.min(prefixLen, i * OCTET_BIT_LENGTH)) { |
| 523 | byteBuffer = ByteBuffer.allocate(i + 1); |
| 524 | byteBuffer.put((byte) prefixLen); |
| 525 | |
| 526 | for (int j = 0; j < i; j++) { |
| 527 | byteBuffer.put((byte) Integer.parseInt(ipPrefixString[j])); |
| 528 | } |
| 529 | return byteBuffer.array(); |
| 530 | } |
| 531 | } |
| 532 | |
| 533 | return null; |
| 534 | } |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 535 | } |
daniel park | 15506e8 | 2018-04-04 18:52:16 +0900 | [diff] [blame] | 536 | |
| 537 | private class InternalNodeEventListener implements OpenstackNodeListener { |
| 538 | @Override |
| 539 | public boolean isRelevant(OpenstackNodeEvent event) { |
| 540 | // do not allow to proceed without leadership |
| 541 | NodeId leader = leadershipService.getLeader(appId.name()); |
Jian Li | 51b844c | 2018-05-31 10:59:03 +0900 | [diff] [blame] | 542 | return Objects.equals(localNodeId, leader) && event.subject().type() == COMPUTE; |
daniel park | 15506e8 | 2018-04-04 18:52:16 +0900 | [diff] [blame] | 543 | } |
| 544 | |
| 545 | @Override |
| 546 | public void event(OpenstackNodeEvent event) { |
| 547 | OpenstackNode osNode = event.subject(); |
| 548 | switch (event.type()) { |
| 549 | case OPENSTACK_NODE_COMPLETE: |
| 550 | setDhcpRule(osNode, true); |
| 551 | break; |
| 552 | case OPENSTACK_NODE_INCOMPLETE: |
| 553 | setDhcpRule(osNode, false); |
| 554 | break; |
| 555 | case OPENSTACK_NODE_CREATED: |
| 556 | case OPENSTACK_NODE_UPDATED: |
| 557 | case OPENSTACK_NODE_REMOVED: |
| 558 | default: |
| 559 | break; |
| 560 | } |
| 561 | } |
| 562 | |
| 563 | private void setDhcpRule(OpenstackNode openstackNode, boolean install) { |
daniel park | 15506e8 | 2018-04-04 18:52:16 +0900 | [diff] [blame] | 564 | TrafficSelector selector = DefaultTrafficSelector.builder() |
| 565 | .matchEthType(Ethernet.TYPE_IPV4) |
| 566 | .matchIPProtocol(IPv4.PROTOCOL_UDP) |
| 567 | .matchUdpDst(TpPort.tpPort(UDP.DHCP_SERVER_PORT)) |
| 568 | .matchUdpSrc(TpPort.tpPort(UDP.DHCP_CLIENT_PORT)) |
| 569 | .build(); |
| 570 | |
| 571 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 572 | .punt() |
| 573 | .build(); |
| 574 | |
| 575 | osFlowRuleService.setRule( |
| 576 | appId, |
| 577 | openstackNode.intgBridge(), |
| 578 | selector, |
| 579 | treatment, |
| 580 | PRIORITY_DHCP_RULE, |
Jian Li | 70a2c3f | 2018-04-13 17:26:31 +0900 | [diff] [blame] | 581 | DHCP_ARP_TABLE, |
daniel park | 15506e8 | 2018-04-04 18:52:16 +0900 | [diff] [blame] | 582 | install); |
| 583 | } |
| 584 | } |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 585 | } |