sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 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 | 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; |
| 33 | import org.onosproject.openstackinterface.OpenstackNetwork; |
| 34 | import org.onosproject.openstackinterface.OpenstackPort; |
| 35 | import org.onosproject.openstackinterface.OpenstackRouter; |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 36 | import org.slf4j.Logger; |
| 37 | import org.slf4j.LoggerFactory; |
| 38 | |
| 39 | import java.nio.ByteBuffer; |
| 40 | |
| 41 | import static com.google.common.base.Preconditions.checkNotNull; |
Kyuhwi Choi | e2b37e3 | 2016-02-05 14:04:14 +0900 | [diff] [blame] | 42 | import static org.onlab.osgi.DefaultServiceDirectory.getService; |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 43 | |
| 44 | |
| 45 | /** |
| 46 | * Handle NAT packet processing for Managing Flow Rules In Openstack Nodes. |
| 47 | */ |
| 48 | public class OpenstackPnatHandler implements Runnable { |
| 49 | |
| 50 | volatile PacketContext context; |
| 51 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 52 | |
| 53 | protected PacketService packetService; |
| 54 | |
| 55 | private final OpenstackRoutingRulePopulator rulePopulator; |
| 56 | private final int portNum; |
| 57 | private final OpenstackPort openstackPort; |
Kyuhwi Choi | e2b37e3 | 2016-02-05 14:04:14 +0900 | [diff] [blame] | 58 | private final Port port; |
Kyuhwi Choi | ee9e371 | 2016-02-22 22:49:36 +0900 | [diff] [blame^] | 59 | private OpenstackRoutingConfig config; |
Kyuhwi Choi | e2b37e3 | 2016-02-05 14:04:14 +0900 | [diff] [blame] | 60 | |
| 61 | private static final String DEVICE_OWNER_ROUTER_INTERFACE = "network:router_interface"; |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 62 | |
| 63 | OpenstackPnatHandler(OpenstackRoutingRulePopulator rulePopulator, PacketContext context, |
Kyuhwi Choi | ee9e371 | 2016-02-22 22:49:36 +0900 | [diff] [blame^] | 64 | int portNum, OpenstackPort openstackPort, Port port, OpenstackRoutingConfig config) { |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 65 | this.rulePopulator = checkNotNull(rulePopulator); |
| 66 | this.context = checkNotNull(context); |
| 67 | this.portNum = checkNotNull(portNum); |
| 68 | this.openstackPort = checkNotNull(openstackPort); |
Kyuhwi Choi | e2b37e3 | 2016-02-05 14:04:14 +0900 | [diff] [blame] | 69 | this.port = checkNotNull(port); |
Kyuhwi Choi | ee9e371 | 2016-02-22 22:49:36 +0900 | [diff] [blame^] | 70 | this.config = checkNotNull(config); |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | @Override |
| 74 | public void run() { |
| 75 | InboundPacket inboundPacket = context.inPacket(); |
| 76 | Ethernet ethernet = checkNotNull(inboundPacket.parsed()); |
| 77 | |
| 78 | //TODO: Considers IPV6 |
| 79 | if (ethernet.getEtherType() != Ethernet.TYPE_IPV4) { |
| 80 | log.warn("Now, we just consider IP version 4"); |
| 81 | return; |
| 82 | } |
| 83 | |
Kyuhwi Choi | e2b37e3 | 2016-02-05 14:04:14 +0900 | [diff] [blame] | 84 | OpenstackRouter router = getOpenstackRouter(openstackPort); |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 85 | |
| 86 | rulePopulator.populatePnatFlowRules(inboundPacket, openstackPort, portNum, |
Kyuhwi Choi | ee9e371 | 2016-02-22 22:49:36 +0900 | [diff] [blame^] | 87 | getExternalIp(router), MacAddress.valueOf(config.gatewayExternalInterfaceMac()), |
| 88 | MacAddress.valueOf(config.physicalRouterMac())); |
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 | OpenstackNetwork network = networkingService.network(openstackPort.networkId()); |
| 96 | |
| 97 | OpenstackPort port = networkingService.ports() |
| 98 | .stream() |
| 99 | .filter(p -> p.deviceOwner().equals(DEVICE_OWNER_ROUTER_INTERFACE)) |
| 100 | .filter(p -> checkSameSubnet(p, openstackPort)) |
| 101 | .findAny() |
| 102 | .orElse(null); |
| 103 | |
| 104 | return checkNotNull(networkingService.router(port.deviceId())); |
| 105 | } |
| 106 | |
| 107 | private boolean checkSameSubnet(OpenstackPort p, OpenstackPort openstackPort) { |
| 108 | String key1 = checkNotNull(p.fixedIps().keySet().stream().findFirst().orElse(null)).toString(); |
| 109 | String key2 = checkNotNull(openstackPort.fixedIps().keySet().stream().findFirst().orElse(null)).toString(); |
| 110 | return key1.equals(key2) ? true : false; |
| 111 | } |
| 112 | |
| 113 | private Ip4Address getExternalIp(OpenstackRouter router) { |
| 114 | return router.gatewayExternalInfo().externalFixedIps().values().stream().findAny().orElse(null); |
| 115 | } |
| 116 | |
| 117 | private void packetOut(Ethernet ethernet, DeviceId deviceId, int portNum, OpenstackRouter router) { |
| 118 | PacketService packetService = getService(PacketService.class); |
| 119 | |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 120 | IPv4 iPacket = (IPv4) ethernet.getPayload(); |
| 121 | |
| 122 | TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder(); |
| 123 | |
| 124 | switch (iPacket.getProtocol()) { |
| 125 | case IPv4.PROTOCOL_TCP: |
| 126 | TCP tcpPacket = (TCP) iPacket.getPayload(); |
| 127 | tcpPacket.setSourcePort(portNum); |
| 128 | tcpPacket.resetChecksum(); |
| 129 | tcpPacket.setParent(iPacket); |
| 130 | iPacket.setPayload(tcpPacket); |
| 131 | break; |
| 132 | case IPv4.PROTOCOL_UDP: |
| 133 | UDP udpPacket = (UDP) iPacket.getPayload(); |
| 134 | udpPacket.setSourcePort(portNum); |
| 135 | udpPacket.resetChecksum(); |
| 136 | udpPacket.setParent(iPacket); |
| 137 | iPacket.setPayload(udpPacket); |
| 138 | break; |
| 139 | default: |
Kyuhwi Choi | e2b37e3 | 2016-02-05 14:04:14 +0900 | [diff] [blame] | 140 | log.error("Temporally, this method can process UDP and TCP protocol."); |
| 141 | return; |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 142 | } |
| 143 | |
Kyuhwi Choi | e2b37e3 | 2016-02-05 14:04:14 +0900 | [diff] [blame] | 144 | iPacket.setSourceAddress(getExternalIp(router).toString()); |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 145 | iPacket.resetChecksum(); |
Kyuhwi Choi | e2b37e3 | 2016-02-05 14:04:14 +0900 | [diff] [blame] | 146 | iPacket.setParent(ethernet); |
Kyuhwi Choi | ee9e371 | 2016-02-22 22:49:36 +0900 | [diff] [blame^] | 147 | ethernet.setPayload(iPacket); |
| 148 | ethernet.setSourceMACAddress(config.gatewayExternalInterfaceMac()) |
| 149 | .setDestinationMACAddress(config.physicalRouterMac()); |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 150 | ethernet.resetChecksum(); |
| 151 | |
Kyuhwi Choi | e2b37e3 | 2016-02-05 14:04:14 +0900 | [diff] [blame] | 152 | treatment.setOutput(port.number()); |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 153 | |
Kyuhwi Choi | e2b37e3 | 2016-02-05 14:04:14 +0900 | [diff] [blame] | 154 | packetService.emit(new DefaultOutboundPacket(deviceId, treatment.build(), |
| 155 | ByteBuffer.wrap(ethernet.serialize()))); |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 156 | } |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 157 | } |