sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
sangho | 0c2a3da | 2016-02-16 13:39:07 +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.routing; |
| 17 | |
| 18 | import org.onlab.packet.Ethernet; |
| 19 | import org.onlab.packet.IPv4; |
Kyuhwi Choi | e2b37e3 | 2016-02-05 14:04:14 +0900 | [diff] [blame] | 20 | import org.onlab.packet.Ip4Address; |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 21 | import org.onlab.packet.MacAddress; |
| 22 | import org.onlab.packet.TCP; |
| 23 | import org.onlab.packet.UDP; |
| 24 | import org.onosproject.net.DeviceId; |
Kyuhwi Choi | e2b37e3 | 2016-02-05 14:04:14 +0900 | [diff] [blame] | 25 | import org.onosproject.net.Port; |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 26 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 27 | import org.onosproject.net.flow.TrafficTreatment; |
| 28 | import org.onosproject.net.packet.DefaultOutboundPacket; |
| 29 | import org.onosproject.net.packet.InboundPacket; |
| 30 | import org.onosproject.net.packet.PacketContext; |
| 31 | import org.onosproject.net.packet.PacketService; |
sangho | 93447f1 | 2016-02-24 00:33:22 +0900 | [diff] [blame] | 32 | import org.onosproject.openstackinterface.OpenstackInterfaceService; |
sangho | 93447f1 | 2016-02-24 00:33:22 +0900 | [diff] [blame] | 33 | import org.onosproject.openstackinterface.OpenstackPort; |
| 34 | import org.onosproject.openstackinterface.OpenstackRouter; |
Kyuhwi Choi | 92d9ea4 | 2016-06-13 17:28:00 +0900 | [diff] [blame] | 35 | import org.onosproject.scalablegateway.api.GatewayNode; |
| 36 | import org.onosproject.scalablegateway.api.ScalableGatewayService; |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 37 | import org.slf4j.Logger; |
| 38 | import org.slf4j.LoggerFactory; |
| 39 | |
| 40 | import java.nio.ByteBuffer; |
| 41 | |
| 42 | import static com.google.common.base.Preconditions.checkNotNull; |
Kyuhwi Choi | e2b37e3 | 2016-02-05 14:04:14 +0900 | [diff] [blame] | 43 | import static org.onlab.osgi.DefaultServiceDirectory.getService; |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 44 | |
| 45 | |
| 46 | /** |
| 47 | * Handle NAT packet processing for Managing Flow Rules In Openstack Nodes. |
| 48 | */ |
| 49 | public class OpenstackPnatHandler implements Runnable { |
| 50 | |
| 51 | volatile PacketContext context; |
| 52 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 53 | |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 54 | private final OpenstackRoutingRulePopulator rulePopulator; |
| 55 | private final int portNum; |
| 56 | private final OpenstackPort openstackPort; |
Kyuhwi Choi | e2b37e3 | 2016-02-05 14:04:14 +0900 | [diff] [blame] | 57 | private final Port port; |
| 58 | |
| 59 | private static final String DEVICE_OWNER_ROUTER_INTERFACE = "network:router_interface"; |
Kyuhwi Choi | 92d9ea4 | 2016-06-13 17:28:00 +0900 | [diff] [blame] | 60 | private static final String EXTERNAL_PORT_NULL = "There is no external port in this deviceId []"; |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 61 | |
| 62 | OpenstackPnatHandler(OpenstackRoutingRulePopulator rulePopulator, PacketContext context, |
sangho | 6032f34 | 2016-07-07 14:32:03 +0900 | [diff] [blame] | 63 | int portNum, OpenstackPort openstackPort, Port port) { |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 64 | this.rulePopulator = checkNotNull(rulePopulator); |
| 65 | this.context = checkNotNull(context); |
| 66 | this.portNum = checkNotNull(portNum); |
| 67 | this.openstackPort = checkNotNull(openstackPort); |
Kyuhwi Choi | e2b37e3 | 2016-02-05 14:04:14 +0900 | [diff] [blame] | 68 | this.port = checkNotNull(port); |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | @Override |
| 72 | public void run() { |
| 73 | InboundPacket inboundPacket = context.inPacket(); |
| 74 | Ethernet ethernet = checkNotNull(inboundPacket.parsed()); |
| 75 | |
| 76 | //TODO: Considers IPV6 |
| 77 | if (ethernet.getEtherType() != Ethernet.TYPE_IPV4) { |
| 78 | log.warn("Now, we just consider IP version 4"); |
| 79 | return; |
| 80 | } |
| 81 | |
Kyuhwi Choi | e2b37e3 | 2016-02-05 14:04:14 +0900 | [diff] [blame] | 82 | OpenstackRouter router = getOpenstackRouter(openstackPort); |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 83 | |
Kyuhwi Choi | 92d9ea4 | 2016-06-13 17:28:00 +0900 | [diff] [blame] | 84 | MacAddress externalMac = MacAddress.NONE; |
| 85 | MacAddress routerMac = MacAddress.NONE; |
| 86 | |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 87 | rulePopulator.populatePnatFlowRules(inboundPacket, openstackPort, portNum, |
Kyuhwi Choi | 92d9ea4 | 2016-06-13 17:28:00 +0900 | [diff] [blame] | 88 | getExternalIp(router), externalMac, routerMac); |
Kyuhwi Choi | e2b37e3 | 2016-02-05 14:04:14 +0900 | [diff] [blame] | 89 | |
| 90 | packetOut((Ethernet) ethernet.clone(), inboundPacket.receivedFrom().deviceId(), portNum, router); |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 91 | } |
| 92 | |
Kyuhwi Choi | e2b37e3 | 2016-02-05 14:04:14 +0900 | [diff] [blame] | 93 | private OpenstackRouter getOpenstackRouter(OpenstackPort openstackPort) { |
sangho | 93447f1 | 2016-02-24 00:33:22 +0900 | [diff] [blame] | 94 | OpenstackInterfaceService networkingService = getService(OpenstackInterfaceService.class); |
Kyuhwi Choi | e2b37e3 | 2016-02-05 14:04:14 +0900 | [diff] [blame] | 95 | |
| 96 | OpenstackPort port = networkingService.ports() |
| 97 | .stream() |
| 98 | .filter(p -> p.deviceOwner().equals(DEVICE_OWNER_ROUTER_INTERFACE)) |
| 99 | .filter(p -> checkSameSubnet(p, openstackPort)) |
| 100 | .findAny() |
| 101 | .orElse(null); |
| 102 | |
| 103 | return checkNotNull(networkingService.router(port.deviceId())); |
| 104 | } |
| 105 | |
| 106 | private boolean checkSameSubnet(OpenstackPort p, OpenstackPort openstackPort) { |
| 107 | String key1 = checkNotNull(p.fixedIps().keySet().stream().findFirst().orElse(null)).toString(); |
| 108 | String key2 = checkNotNull(openstackPort.fixedIps().keySet().stream().findFirst().orElse(null)).toString(); |
| 109 | return key1.equals(key2) ? true : false; |
| 110 | } |
| 111 | |
| 112 | private Ip4Address getExternalIp(OpenstackRouter router) { |
| 113 | return router.gatewayExternalInfo().externalFixedIps().values().stream().findAny().orElse(null); |
| 114 | } |
| 115 | |
| 116 | private void packetOut(Ethernet ethernet, DeviceId deviceId, int portNum, OpenstackRouter router) { |
| 117 | PacketService packetService = getService(PacketService.class); |
| 118 | |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 119 | IPv4 iPacket = (IPv4) ethernet.getPayload(); |
| 120 | |
| 121 | TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder(); |
| 122 | |
| 123 | switch (iPacket.getProtocol()) { |
| 124 | case IPv4.PROTOCOL_TCP: |
| 125 | TCP tcpPacket = (TCP) iPacket.getPayload(); |
| 126 | tcpPacket.setSourcePort(portNum); |
| 127 | tcpPacket.resetChecksum(); |
| 128 | tcpPacket.setParent(iPacket); |
| 129 | iPacket.setPayload(tcpPacket); |
| 130 | break; |
| 131 | case IPv4.PROTOCOL_UDP: |
| 132 | UDP udpPacket = (UDP) iPacket.getPayload(); |
| 133 | udpPacket.setSourcePort(portNum); |
| 134 | udpPacket.resetChecksum(); |
| 135 | udpPacket.setParent(iPacket); |
| 136 | iPacket.setPayload(udpPacket); |
| 137 | break; |
| 138 | default: |
Kyuhwi Choi | e2b37e3 | 2016-02-05 14:04:14 +0900 | [diff] [blame] | 139 | log.error("Temporally, this method can process UDP and TCP protocol."); |
| 140 | return; |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 141 | } |
| 142 | |
Kyuhwi Choi | e2b37e3 | 2016-02-05 14:04:14 +0900 | [diff] [blame] | 143 | iPacket.setSourceAddress(getExternalIp(router).toString()); |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 144 | iPacket.resetChecksum(); |
Kyuhwi Choi | e2b37e3 | 2016-02-05 14:04:14 +0900 | [diff] [blame] | 145 | iPacket.setParent(ethernet); |
Kyuhwi Choi | ee9e371 | 2016-02-22 22:49:36 +0900 | [diff] [blame] | 146 | ethernet.setPayload(iPacket); |
sangho | 6032f34 | 2016-07-07 14:32:03 +0900 | [diff] [blame] | 147 | |
Kyuhwi Choi | 92d9ea4 | 2016-06-13 17:28:00 +0900 | [diff] [blame] | 148 | ScalableGatewayService gatewayService = getService(ScalableGatewayService.class); |
| 149 | GatewayNode gatewayNode = gatewayService.getGatewayNode(deviceId); |
Hyunsun Moon | f9a16ed | 2016-07-20 21:59:48 -0700 | [diff] [blame^] | 150 | if (gatewayNode.getUplinkIntf() == null) { |
Kyuhwi Choi | 92d9ea4 | 2016-06-13 17:28:00 +0900 | [diff] [blame] | 151 | log.error(EXTERNAL_PORT_NULL, deviceId.toString()); |
| 152 | return; |
| 153 | } |
Hyunsun Moon | f9a16ed | 2016-07-20 21:59:48 -0700 | [diff] [blame^] | 154 | treatment.setOutput(gatewayService.getUplinkPort(deviceId)); |
Kyuhwi Choi | 92d9ea4 | 2016-06-13 17:28:00 +0900 | [diff] [blame] | 155 | |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 156 | ethernet.resetChecksum(); |
| 157 | |
Kyuhwi Choi | e2b37e3 | 2016-02-05 14:04:14 +0900 | [diff] [blame] | 158 | packetService.emit(new DefaultOutboundPacket(deviceId, treatment.build(), |
| 159 | ByteBuffer.wrap(ethernet.serialize()))); |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 160 | } |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 161 | } |