blob: b3fb0349705be9c206886b4a47ab73ffba749e4d [file] [log] [blame]
Hyunsun Moon44aac662017-02-18 02:07:01 +09001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Hyunsun Moon44aac662017-02-18 02:07:01 +09003 *
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 */
16package org.onosproject.openstacknetworking.impl;
17
18import com.google.common.base.Strings;
Daniel Parkd9d4c292018-06-26 20:33:58 +090019import com.google.common.collect.ImmutableList;
Hyunsun Moon44aac662017-02-18 02:07:01 +090020import com.google.common.collect.Lists;
21import org.apache.felix.scr.annotations.Activate;
22import org.apache.felix.scr.annotations.Component;
23import org.apache.felix.scr.annotations.Deactivate;
24import org.apache.felix.scr.annotations.Modified;
25import org.apache.felix.scr.annotations.Property;
26import org.apache.felix.scr.annotations.Reference;
27import org.apache.felix.scr.annotations.ReferenceCardinality;
28import org.onlab.packet.DHCP;
Hyunsun Moon44aac662017-02-18 02:07:01 +090029import org.onlab.packet.Ethernet;
30import org.onlab.packet.IPv4;
31import org.onlab.packet.Ip4Address;
Daniel Park4d421002018-07-27 23:36:57 +090032import org.onlab.packet.IpAddress;
Hyunsun Moon44aac662017-02-18 02:07:01 +090033import org.onlab.packet.IpPrefix;
34import org.onlab.packet.MacAddress;
35import org.onlab.packet.TpPort;
36import org.onlab.packet.UDP;
daniel park796c2eb2018-03-22 17:01:51 +090037import org.onlab.packet.dhcp.DhcpOption;
Hyunsun Moon44aac662017-02-18 02:07:01 +090038import org.onlab.util.Tools;
39import org.onosproject.cfg.ComponentConfigService;
daniel park15506e82018-04-04 18:52:16 +090040import org.onosproject.cluster.ClusterService;
41import org.onosproject.cluster.LeadershipService;
42import org.onosproject.cluster.NodeId;
Hyunsun Moon44aac662017-02-18 02:07:01 +090043import org.onosproject.core.ApplicationId;
44import org.onosproject.core.CoreService;
45import org.onosproject.net.ConnectPoint;
46import org.onosproject.net.flow.DefaultTrafficSelector;
47import org.onosproject.net.flow.DefaultTrafficTreatment;
48import org.onosproject.net.flow.TrafficSelector;
49import org.onosproject.net.flow.TrafficTreatment;
50import org.onosproject.net.packet.DefaultOutboundPacket;
51import org.onosproject.net.packet.PacketContext;
Hyunsun Moon44aac662017-02-18 02:07:01 +090052import org.onosproject.net.packet.PacketProcessor;
53import org.onosproject.net.packet.PacketService;
54import org.onosproject.openstacknetworking.api.Constants;
55import org.onosproject.openstacknetworking.api.InstancePort;
56import org.onosproject.openstacknetworking.api.InstancePortService;
daniel park796c2eb2018-03-22 17:01:51 +090057import org.onosproject.openstacknetworking.api.OpenstackFlowRuleService;
Hyunsun Moon44aac662017-02-18 02:07:01 +090058import org.onosproject.openstacknetworking.api.OpenstackNetworkService;
daniel park15506e82018-04-04 18:52:16 +090059import org.onosproject.openstacknode.api.OpenstackNode;
60import org.onosproject.openstacknode.api.OpenstackNodeEvent;
61import org.onosproject.openstacknode.api.OpenstackNodeListener;
daniel park796c2eb2018-03-22 17:01:51 +090062import org.onosproject.openstacknode.api.OpenstackNodeService;
Daniel Parkd9d4c292018-06-26 20:33:58 +090063import org.openstack4j.model.network.HostRoute;
Hyunsun Moon44aac662017-02-18 02:07:01 +090064import org.openstack4j.model.network.IP;
Daniel Park468e7852018-07-28 00:38:45 +090065import org.openstack4j.model.network.Network;
Hyunsun Moon44aac662017-02-18 02:07:01 +090066import org.openstack4j.model.network.Port;
67import org.openstack4j.model.network.Subnet;
68import org.osgi.service.component.ComponentContext;
69import org.slf4j.Logger;
70
71import java.nio.ByteBuffer;
72import java.util.Dictionary;
73import java.util.List;
daniel park15506e82018-04-04 18:52:16 +090074import java.util.Objects;
Hyunsun Moon44aac662017-02-18 02:07:01 +090075
Daniel Parkf8e422d2018-07-30 14:14:37 +090076import static com.google.common.base.Preconditions.checkNotNull;
daniel park796c2eb2018-03-22 17:01:51 +090077import static org.onlab.packet.DHCP.DHCPOptionCode.OptionCode_BroadcastAddress;
Daniel Parkd9d4c292018-06-26 20:33:58 +090078import static org.onlab.packet.DHCP.DHCPOptionCode.OptionCode_Classless_Static_Route;
daniel park796c2eb2018-03-22 17:01:51 +090079import static org.onlab.packet.DHCP.DHCPOptionCode.OptionCode_DHCPServerIp;
80import static org.onlab.packet.DHCP.DHCPOptionCode.OptionCode_DomainServer;
81import static org.onlab.packet.DHCP.DHCPOptionCode.OptionCode_END;
82import static org.onlab.packet.DHCP.DHCPOptionCode.OptionCode_LeaseTime;
83import static org.onlab.packet.DHCP.DHCPOptionCode.OptionCode_MessageType;
Daniel Park48f10332018-08-02 10:57:27 +090084import static org.onlab.packet.DHCP.DHCPOptionCode.OptionCode_RouterAddress;
daniel park796c2eb2018-03-22 17:01:51 +090085import static org.onlab.packet.DHCP.DHCPOptionCode.OptionCode_SubnetMask;
Yi Tsengc7403c22017-06-19 16:23:22 -070086import static org.onlab.packet.DHCP.MsgType.DHCPACK;
87import static org.onlab.packet.DHCP.MsgType.DHCPOFFER;
Hyunsun Moon44aac662017-02-18 02:07:01 +090088import static org.onosproject.openstacknetworking.api.Constants.DEFAULT_GATEWAY_MAC_STR;
Jian Li70a2c3f2018-04-13 17:26:31 +090089import static org.onosproject.openstacknetworking.api.Constants.DHCP_ARP_TABLE;
daniel park796c2eb2018-03-22 17:01:51 +090090import static org.onosproject.openstacknetworking.api.Constants.PRIORITY_DHCP_RULE;
Jian Li51b844c2018-05-31 10:59:03 +090091import static org.onosproject.openstacknode.api.OpenstackNode.NodeType.COMPUTE;
Hyunsun Moon44aac662017-02-18 02:07:01 +090092import static org.slf4j.LoggerFactory.getLogger;
93
94/**
95 * Handles DHCP requests for the virtual instances.
96 */
97@Component(immediate = true)
98public class OpenstackSwitchingDhcpHandler {
99 protected final Logger log = getLogger(getClass());
100
101 private static final String DHCP_SERVER_MAC = "dhcpServerMac";
Daniel Park4d421002018-07-27 23:36:57 +0900102 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 Moon44aac662017-02-18 02:07:01 +0900104 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 Lifb005492018-03-02 10:50:15 +0900109 // we are using 1450 as a default DHCP MTU value
110 private static final int DHCP_DATA_MTU_DEFAULT = 1450;
Daniel Parkd9d4c292018-06-26 20:33:58 +0900111 private static final int OCTET_BIT_LENGTH = 8;
112 private static final int V4_BYTE_SIZE = 4;
Daniel Parkf8e422d2018-07-30 14:14:37 +0900113 private static final int V4_CIDR_LOWER_BOUND = -1;
Daniel Parkd9d4c292018-06-26 20:33:58 +0900114 private static final int V4_CIDR_UPPER_BOUND = 33;
115 private static final int PADDING_SIZE = 4;
Hyunsun Moon44aac662017-02-18 02:07:01 +0900116
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 park796c2eb2018-03-22 17:01:51 +0900132 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
133 protected OpenstackNodeService osNodeService;
134
135 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
136 protected OpenstackFlowRuleService osFlowRuleService;
137
daniel park15506e82018-04-04 18:52:16 +0900138 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
139 protected ClusterService clusterService;
140
141 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
142 protected LeadershipService leadershipService;
143
Hyunsun Moon44aac662017-02-18 02:07:01 +0900144 @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 Lifb005492018-03-02 10:50:15 +0900148 private int dhcpDataMtu = DHCP_DATA_MTU_DEFAULT;
149
Hyunsun Moon44aac662017-02-18 02:07:01 +0900150 private final PacketProcessor packetProcessor = new InternalPacketProcessor();
daniel park15506e82018-04-04 18:52:16 +0900151 private final OpenstackNodeListener osNodeListener = new InternalNodeEventListener();
Hyunsun Moon44aac662017-02-18 02:07:01 +0900152
153 private ApplicationId appId;
daniel park15506e82018-04-04 18:52:16 +0900154 private NodeId localNodeId;
Hyunsun Moon44aac662017-02-18 02:07:01 +0900155
156 @Activate
157 protected void activate() {
158 appId = coreService.registerApplication(Constants.OPENSTACK_NETWORKING_APP_ID);
daniel park15506e82018-04-04 18:52:16 +0900159 localNodeId = clusterService.getLocalNode().id();
160 osNodeService.addListener(osNodeListener);
Hyunsun Moon44aac662017-02-18 02:07:01 +0900161 configService.registerProperties(getClass());
162 packetService.addProcessor(packetProcessor, PacketProcessor.director(0));
daniel park15506e82018-04-04 18:52:16 +0900163 leadershipService.runForLeadership(appId.name());
Hyunsun Moon44aac662017-02-18 02:07:01 +0900164
165 log.info("Started");
166 }
167
168 @Deactivate
169 protected void deactivate() {
Hyunsun Moon44aac662017-02-18 02:07:01 +0900170 packetService.removeProcessor(packetProcessor);
daniel park15506e82018-04-04 18:52:16 +0900171 osNodeService.removeListener(osNodeListener);
Hyunsun Moon44aac662017-02-18 02:07:01 +0900172 configService.unregisterProperties(getClass(), false);
daniel park15506e82018-04-04 18:52:16 +0900173 leadershipService.withdraw(appId.name());
Hyunsun Moon44aac662017-02-18 02:07:01 +0900174
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 Lifb005492018-03-02 10:50:15 +0900184
Hyunsun Moon44aac662017-02-18 02:07:01 +0900185 if (!Strings.isNullOrEmpty(updatedMac) && !updatedMac.equals(dhcpServerMac)) {
186 dhcpServerMac = updatedMac;
187 }
188
189 log.info("Modified");
190 }
191
Hyunsun Moon44aac662017-02-18 02:07:01 +0900192 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 Tsengc7403c22017-06-19 16:23:22 -0700224 DHCP.MsgType inPacketType = getPacketType(dhcpPacket);
Hyunsun Moon44aac662017-02-18 02:07:01 +0900225 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 park15506e82018-04-04 18:52:16 +0900246 reqInstPort.ipAddress(), clientMac);
Hyunsun Moon44aac662017-02-18 02:07:01 +0900247 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 park15506e82018-04-04 18:52:16 +0900256 reqInstPort.ipAddress(), clientMac);
Hyunsun Moon44aac662017-02-18 02:07:01 +0900257 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 Tsengc7403c22017-06-19 16:23:22 -0700267 private DHCP.MsgType getPacketType(DHCP dhcpPacket) {
268 DhcpOption optType = dhcpPacket.getOption(OptionCode_MessageType);
Hyunsun Moon44aac662017-02-18 02:07:01 +0900269 if (optType == null) {
270 log.trace("DHCP packet with no message type, ignore it");
271 return null;
272 }
273
Yi Tsengc7403c22017-06-19 16:23:22 -0700274 DHCP.MsgType inPacketType = DHCP.MsgType.getType(optType.getData()[0]);
Hyunsun Moon44aac662017-02-18 02:07:01 +0900275 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 Parke0945c12018-08-28 17:22:25 +0900283 log.trace("Build for DHCP reply msg for instance port {}", reqInstPort.toString());
Hyunsun Moon44aac662017-02-18 02:07:01 +0900284 Port osPort = osNetworkService.port(reqInstPort.portId());
Daniel Parke0945c12018-08-28 17:22:25 +0900285 if (osPort == null) {
286 log.error("Failed to retrieve openstack port information for instance port {}",
287 reqInstPort.toString());
288 }
Hyunsun Moon44aac662017-02-18 02:07:01 +0900289 // pick one IP address to make a reply
290 IP fixedIp = osPort.getFixedIps().stream().findFirst().get();
291 Subnet osSubnet = osNetworkService.subnet(fixedIp.getSubnetId());
292
293 Ethernet ethReply = new Ethernet();
294 ethReply.setSourceMACAddress(dhcpServerMac);
295 ethReply.setDestinationMACAddress(ethRequest.getSourceMAC());
296 ethReply.setEtherType(Ethernet.TYPE_IPV4);
297
298 IPv4 ipv4Request = (IPv4) ethRequest.getPayload();
299 IPv4 ipv4Reply = new IPv4();
daniel park15506e82018-04-04 18:52:16 +0900300
301 ipv4Reply.setSourceAddress(clusterService.getLocalNode().ip().getIp4Address().toString());
Hyunsun Moon44aac662017-02-18 02:07:01 +0900302 ipv4Reply.setDestinationAddress(reqInstPort.ipAddress().getIp4Address().toInt());
303 ipv4Reply.setTtl(PACKET_TTL);
304
305 UDP udpRequest = (UDP) ipv4Request.getPayload();
306 UDP udpReply = new UDP();
307 udpReply.setSourcePort((byte) UDP.DHCP_SERVER_PORT);
308 udpReply.setDestinationPort((byte) UDP.DHCP_CLIENT_PORT);
309
310 DHCP dhcpRequest = (DHCP) udpRequest.getPayload();
311 DHCP dhcpReply = buildDhcpReply(
312 dhcpRequest,
313 packetType,
314 reqInstPort.ipAddress().getIp4Address(),
315 osSubnet);
316
317 udpReply.setPayload(dhcpReply);
318 ipv4Reply.setPayload(udpReply);
319 ethReply.setPayload(ipv4Reply);
320
321 return ethReply;
322 }
323
324 private void sendReply(PacketContext context, Ethernet ethReply) {
325 if (ethReply == null) {
326 return;
327 }
328 ConnectPoint srcPoint = context.inPacket().receivedFrom();
329 TrafficTreatment treatment = DefaultTrafficTreatment
330 .builder()
331 .setOutput(srcPoint.port())
332 .build();
333
334 packetService.emit(new DefaultOutboundPacket(
335 srcPoint.deviceId(),
336 treatment,
337 ByteBuffer.wrap(ethReply.serialize())));
338 context.block();
339 }
340
341 private DHCP buildDhcpReply(DHCP request, byte msgType, Ip4Address yourIp,
342 Subnet osSubnet) {
daniel park15506e82018-04-04 18:52:16 +0900343 Ip4Address gatewayIp = clusterService.getLocalNode().ip().getIp4Address();
Hyunsun Moon44aac662017-02-18 02:07:01 +0900344 int subnetPrefixLen = IpPrefix.valueOf(osSubnet.getCidr()).prefixLength();
345
346 DHCP dhcpReply = new DHCP();
347 dhcpReply.setOpCode(DHCP.OPCODE_REPLY);
348 dhcpReply.setHardwareType(DHCP.HWTYPE_ETHERNET);
349 dhcpReply.setHardwareAddressLength((byte) 6);
350 dhcpReply.setTransactionId(request.getTransactionId());
351 dhcpReply.setFlags(request.getFlags());
352 dhcpReply.setYourIPAddress(yourIp.toInt());
353 dhcpReply.setServerIPAddress(gatewayIp.toInt());
354 dhcpReply.setClientHardwareAddress(request.getClientHardwareAddress());
355
Yi Tsengc7403c22017-06-19 16:23:22 -0700356 List<DhcpOption> options = Lists.newArrayList();
Hyunsun Moon44aac662017-02-18 02:07:01 +0900357 // message type
Yi Tsengc7403c22017-06-19 16:23:22 -0700358 DhcpOption option = new DhcpOption();
Hyunsun Moon44aac662017-02-18 02:07:01 +0900359 option.setCode(OptionCode_MessageType.getValue());
360 option.setLength((byte) 1);
361 byte[] optionData = {msgType};
362 option.setData(optionData);
363 options.add(option);
364
365 // server identifier
Yi Tsengc7403c22017-06-19 16:23:22 -0700366 option = new DhcpOption();
Hyunsun Moon44aac662017-02-18 02:07:01 +0900367 option.setCode(OptionCode_DHCPServerIp.getValue());
368 option.setLength((byte) 4);
369 option.setData(gatewayIp.toOctets());
370 options.add(option);
371
372 // lease time
Yi Tsengc7403c22017-06-19 16:23:22 -0700373 option = new DhcpOption();
Hyunsun Moon44aac662017-02-18 02:07:01 +0900374 option.setCode(OptionCode_LeaseTime.getValue());
375 option.setLength((byte) 4);
376 option.setData(DHCP_DATA_LEASE_INFINITE);
377 options.add(option);
378
379 // subnet mask
380 Ip4Address subnetMask = Ip4Address.makeMaskPrefix(subnetPrefixLen);
Yi Tsengc7403c22017-06-19 16:23:22 -0700381 option = new DhcpOption();
Hyunsun Moon44aac662017-02-18 02:07:01 +0900382 option.setCode(OptionCode_SubnetMask.getValue());
383 option.setLength((byte) 4);
384 option.setData(subnetMask.toOctets());
385 options.add(option);
386
387 // broadcast address
388 Ip4Address broadcast = Ip4Address.makeMaskedAddress(yourIp, subnetPrefixLen);
Yi Tsengc7403c22017-06-19 16:23:22 -0700389 option = new DhcpOption();
Hyunsun Moon44aac662017-02-18 02:07:01 +0900390 option.setCode(OptionCode_BroadcastAddress.getValue());
391 option.setLength((byte) 4);
392 option.setData(broadcast.toOctets());
393 options.add(option);
394
395 // domain server
Yi Tsengc7403c22017-06-19 16:23:22 -0700396 option = new DhcpOption();
Daniel Park4d421002018-07-27 23:36:57 +0900397
398 List<String> dnsServers = osSubnet.getDnsNames();
Hyunsun Moon44aac662017-02-18 02:07:01 +0900399 option.setCode(OptionCode_DomainServer.getValue());
Daniel Park4d421002018-07-27 23:36:57 +0900400
401 if (dnsServers.isEmpty()) {
402 option.setLength((byte) 8);
403 ByteBuffer dnsByteBuf = ByteBuffer.allocate(8);
404 dnsByteBuf.put(DEFAULT_PRIMARY_DNS.toOctets());
405 dnsByteBuf.put(DEFAULT_SECONDARY_DNS.toOctets());
406
407 option.setData(dnsByteBuf.array());
408 } else {
409 int dnsLength = 4 * dnsServers.size();
410
411 option.setLength((byte) dnsLength);
412
413 ByteBuffer dnsByteBuf = ByteBuffer.allocate(8);
414
415 for (int i = 0; i < dnsServers.size(); i++) {
416 dnsByteBuf.put(IpAddress.valueOf(dnsServers.get(i)).toOctets());
417 }
418 option.setData(dnsByteBuf.array());
419 }
420
Hyunsun Moon44aac662017-02-18 02:07:01 +0900421 options.add(option);
422
Yi Tsengc7403c22017-06-19 16:23:22 -0700423 option = new DhcpOption();
Hyunsun Moon44aac662017-02-18 02:07:01 +0900424 option.setCode(DHCP_OPTION_MTU);
425 option.setLength((byte) 2);
Daniel Park468e7852018-07-28 00:38:45 +0900426 Network osNetwork = osNetworkService.network(osSubnet.getNetworkId());
427 checkNotNull(osNetwork);
428 checkNotNull(osNetwork.getMTU());
429
430 option.setData(ByteBuffer.allocate(2).putShort(osNetwork.getMTU().shortValue()).array());
Hyunsun Moon44aac662017-02-18 02:07:01 +0900431 options.add(option);
432
Daniel Parkd9d4c292018-06-26 20:33:58 +0900433 // classless static route
434 if (!osSubnet.getHostRoutes().isEmpty()) {
435 option = new DhcpOption();
436 option.setCode(OptionCode_Classless_Static_Route.getValue());
437
438 int hostRoutesSize = hostRoutesSize(ImmutableList.copyOf(osSubnet.getHostRoutes()));
439 if (hostRoutesSize == 0) {
440 throw new IllegalArgumentException("Illegal CIDR hostRoutesSize value!");
441 }
442
443 log.trace("hostRouteSize: {}", hostRoutesSize);
444
445 option.setLength((byte) hostRoutesSize);
446 ByteBuffer hostRouteByteBuf = ByteBuffer.allocate(hostRoutesSize);
447
448 osSubnet.getHostRoutes().forEach(h -> {
449 log.debug("processing host route information: {}", h.toString());
450
451 IpPrefix ipPrefix = IpPrefix.valueOf(h.getDestination());
452
453 hostRouteByteBuf.put(bytesDestinationDescriptor(ipPrefix));
454
455 hostRouteByteBuf.put(Ip4Address.valueOf(h.getNexthop()).toOctets());
456 });
457
458 option.setData(hostRouteByteBuf.array());
459
460 options.add(option);
461 }
462
Daniel Park48f10332018-08-02 10:57:27 +0900463 // Sets the default router address up.
464 // Performs only if the gateway is set in subnet.
465 if (!Strings.isNullOrEmpty(osSubnet.getGateway())) {
466 option = new DhcpOption();
467 option.setCode(OptionCode_RouterAddress.getValue());
468 option.setLength((byte) 4);
469 option.setData(Ip4Address.valueOf(osSubnet.getGateway()).toOctets());
470 options.add(option);
471 }
472
Hyunsun Moon44aac662017-02-18 02:07:01 +0900473 // end option
Yi Tsengc7403c22017-06-19 16:23:22 -0700474 option = new DhcpOption();
Hyunsun Moon44aac662017-02-18 02:07:01 +0900475 option.setCode(OptionCode_END.getValue());
476 option.setLength((byte) 1);
477 options.add(option);
478
479 dhcpReply.setOptions(options);
480 return dhcpReply;
481 }
Daniel Parkd9d4c292018-06-26 20:33:58 +0900482
483 private int hostRoutesSize(List<HostRoute> hostRoutes) {
484 int size = 0;
485 int preFixLen;
486
487 for (HostRoute h : hostRoutes) {
488 preFixLen = IpPrefix.valueOf(h.getDestination()).prefixLength();
489 if (Math.max(V4_CIDR_LOWER_BOUND, preFixLen) == V4_CIDR_LOWER_BOUND ||
490 Math.min(preFixLen, V4_CIDR_UPPER_BOUND) == V4_CIDR_UPPER_BOUND) {
491 throw new IllegalArgumentException("Illegal CIDR length value!");
492 }
493
Daniel Parkf8e422d2018-07-30 14:14:37 +0900494 for (int i = 0; i <= V4_BYTE_SIZE; i++) {
Daniel Parkd9d4c292018-06-26 20:33:58 +0900495 if (preFixLen == Math.min(preFixLen, i * OCTET_BIT_LENGTH)) {
496 size = size + i + 1 + PADDING_SIZE;
497 break;
498 }
499 }
500 }
501 return size;
502 }
503
504 private byte[] bytesDestinationDescriptor(IpPrefix ipPrefix) {
505 ByteBuffer byteBuffer;
506 int prefixLen = ipPrefix.prefixLength();
507
508 // retrieve ipPrefix to the destination descriptor format
509 // ex) 10.1.1.0/24 -> [10,1,1,0]
510 String[] ipPrefixString = ipPrefix.getIp4Prefix().toString()
511 .split("/")[0]
512 .split("\\.");
513
Daniel Parkf8e422d2018-07-30 14:14:37 +0900514 // retrieve destination descriptor and put this to bytebuffer according to RFC 3442
515 // ex) 0.0.0.0/0 -> 0
Daniel Parkd9d4c292018-06-26 20:33:58 +0900516 // ex) 10.0.0.0/8 -> 8.10
517 // ex) 10.17.0.0/16 -> 16.10.17
518 // ex) 10.27.129.0/24 -> 24.10.27.129
519 // ex) 10.229.0.128/25 -> 25.10.229.0.128
Daniel Parkf8e422d2018-07-30 14:14:37 +0900520 for (int i = 0; i <= V4_BYTE_SIZE; i++) {
Daniel Parkd9d4c292018-06-26 20:33:58 +0900521 if (prefixLen == Math.min(prefixLen, i * OCTET_BIT_LENGTH)) {
522 byteBuffer = ByteBuffer.allocate(i + 1);
523 byteBuffer.put((byte) prefixLen);
524
525 for (int j = 0; j < i; j++) {
526 byteBuffer.put((byte) Integer.parseInt(ipPrefixString[j]));
527 }
528 return byteBuffer.array();
529 }
530 }
531
532 return null;
533 }
Hyunsun Moon44aac662017-02-18 02:07:01 +0900534 }
daniel park15506e82018-04-04 18:52:16 +0900535
536 private class InternalNodeEventListener implements OpenstackNodeListener {
537 @Override
538 public boolean isRelevant(OpenstackNodeEvent event) {
539 // do not allow to proceed without leadership
540 NodeId leader = leadershipService.getLeader(appId.name());
Jian Li51b844c2018-05-31 10:59:03 +0900541 return Objects.equals(localNodeId, leader) && event.subject().type() == COMPUTE;
daniel park15506e82018-04-04 18:52:16 +0900542 }
543
544 @Override
545 public void event(OpenstackNodeEvent event) {
546 OpenstackNode osNode = event.subject();
547 switch (event.type()) {
548 case OPENSTACK_NODE_COMPLETE:
549 setDhcpRule(osNode, true);
550 break;
551 case OPENSTACK_NODE_INCOMPLETE:
552 setDhcpRule(osNode, false);
553 break;
554 case OPENSTACK_NODE_CREATED:
555 case OPENSTACK_NODE_UPDATED:
556 case OPENSTACK_NODE_REMOVED:
557 default:
558 break;
559 }
560 }
561
562 private void setDhcpRule(OpenstackNode openstackNode, boolean install) {
daniel park15506e82018-04-04 18:52:16 +0900563 TrafficSelector selector = DefaultTrafficSelector.builder()
564 .matchEthType(Ethernet.TYPE_IPV4)
565 .matchIPProtocol(IPv4.PROTOCOL_UDP)
566 .matchUdpDst(TpPort.tpPort(UDP.DHCP_SERVER_PORT))
567 .matchUdpSrc(TpPort.tpPort(UDP.DHCP_CLIENT_PORT))
568 .build();
569
570 TrafficTreatment treatment = DefaultTrafficTreatment.builder()
571 .punt()
572 .build();
573
574 osFlowRuleService.setRule(
575 appId,
576 openstackNode.intgBridge(),
577 selector,
578 treatment,
579 PRIORITY_DHCP_RULE,
Jian Li70a2c3f2018-04-13 17:26:31 +0900580 DHCP_ARP_TABLE,
daniel park15506e82018-04-04 18:52:16 +0900581 install);
582 }
583 }
Hyunsun Moon44aac662017-02-18 02:07:01 +0900584}