Pier Ventre | b6a7f34 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-present 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.segmentrouting; |
| 18 | |
Pier Ventre | b6b81d5 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 19 | import org.onlab.packet.Ethernet; |
| 20 | import org.onlab.packet.IpAddress; |
| 21 | import org.onlab.packet.MacAddress; |
Pier Ventre | b6a7f34 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 22 | import org.onosproject.incubator.net.neighbour.NeighbourMessageContext; |
Pier Ventre | b6b81d5 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 23 | import org.onosproject.net.ConnectPoint; |
| 24 | import org.onosproject.net.DeviceId; |
| 25 | import org.onosproject.net.Host; |
| 26 | import org.onosproject.net.HostId; |
| 27 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 28 | import org.onosproject.net.flow.TrafficTreatment; |
Pier Ventre | b6a7f34 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 29 | import org.onosproject.net.host.HostService; |
Pier Ventre | b6b81d5 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 30 | import org.onosproject.net.packet.DefaultOutboundPacket; |
| 31 | import org.onosproject.segmentrouting.config.DeviceConfigNotFoundException; |
| 32 | import org.onosproject.segmentrouting.config.DeviceConfiguration; |
Pier Ventre | b6a7f34 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 33 | import org.slf4j.Logger; |
| 34 | import org.slf4j.LoggerFactory; |
| 35 | |
Pier Ventre | b6b81d5 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 36 | import java.nio.ByteBuffer; |
| 37 | |
| 38 | import static com.google.common.base.Preconditions.checkNotNull; |
| 39 | |
Pier Ventre | b6a7f34 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 40 | /** |
Pier Ventre | b6b81d5 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 41 | * This handler provides provides useful functions to the |
| 42 | * neighbour handlers (ARP, NDP). |
Pier Ventre | b6a7f34 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 43 | */ |
Pier Ventre | b6b81d5 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 44 | public class SegmentRoutingNeighbourHandler { |
Pier Ventre | b6a7f34 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 45 | |
| 46 | private static Logger log = LoggerFactory.getLogger(SegmentRoutingNeighbourHandler.class); |
Pier Ventre | b6b81d5 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 47 | |
| 48 | protected SegmentRoutingManager srManager; |
| 49 | protected DeviceConfiguration config; |
Pier Ventre | b6a7f34 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 50 | |
| 51 | /** |
Pier Ventre | b6b81d5 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 52 | * Creates an SegmentRoutingNeighbourHandler object. |
Pier Ventre | b6a7f34 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 53 | * |
Pier Ventre | b6b81d5 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 54 | * @param srManager SegmentRoutingManager object |
Pier Ventre | b6a7f34 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 55 | */ |
Pier Ventre | b6b81d5 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 56 | public SegmentRoutingNeighbourHandler(SegmentRoutingManager srManager) { |
| 57 | this.srManager = srManager; |
| 58 | this.config = checkNotNull(srManager.deviceConfiguration); |
Pier Ventre | b6a7f34 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 59 | } |
| 60 | |
Pier Ventre | b6b81d5 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 61 | /** |
| 62 | * Creates an SegmentRoutingNeighbourHandler object. |
Pier Ventre | b6a7f34 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 63 | */ |
Pier Ventre | b6b81d5 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 64 | public SegmentRoutingNeighbourHandler() { |
| 65 | this.srManager = null; |
| 66 | this.config = null; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Retrieve router (device) info. |
| 71 | * |
| 72 | * @param mac where to copy the mac |
| 73 | * @param ip where to copy the ip |
| 74 | * @param deviceId the device id |
| 75 | * @param targetAddress the target address |
Pier Luigi | 6a83c4a | 2017-01-29 12:38:48 -0800 | [diff] [blame^] | 76 | * @return true if it was possible to get the necessary info. |
| 77 | * False for errors |
Pier Ventre | b6b81d5 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 78 | */ |
Pier Luigi | 6a83c4a | 2017-01-29 12:38:48 -0800 | [diff] [blame^] | 79 | protected boolean getSenderInfo(byte[] mac, |
Pier Ventre | b6b81d5 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 80 | byte[] ip, |
| 81 | DeviceId deviceId, |
| 82 | IpAddress targetAddress) { |
| 83 | byte[] senderMacAddress; |
| 84 | byte[] senderIpAddress; |
Pier Luigi | 6a83c4a | 2017-01-29 12:38:48 -0800 | [diff] [blame^] | 85 | IpAddress sender; |
Pier Ventre | b6b81d5 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 86 | try { |
| 87 | senderMacAddress = config.getDeviceMac(deviceId).toBytes(); |
Pier Ventre | 6b2c1b3 | 2016-12-09 17:26:04 -0800 | [diff] [blame] | 88 | if (targetAddress.isIp4()) { |
Pier Luigi | 6a83c4a | 2017-01-29 12:38:48 -0800 | [diff] [blame^] | 89 | sender = config.getRouterIpAddressForASubnetHost(targetAddress.getIp4Address()); |
| 90 | |
Pier Ventre | 6b2c1b3 | 2016-12-09 17:26:04 -0800 | [diff] [blame] | 91 | } else { |
Pier Luigi | 6a83c4a | 2017-01-29 12:38:48 -0800 | [diff] [blame^] | 92 | sender = config.getRouterIpAddressForASubnetHost(targetAddress.getIp6Address()); |
Pier Ventre | 6b2c1b3 | 2016-12-09 17:26:04 -0800 | [diff] [blame] | 93 | } |
Pier Luigi | 6a83c4a | 2017-01-29 12:38:48 -0800 | [diff] [blame^] | 94 | // If sender is null we abort. |
| 95 | if (sender == null) { |
| 96 | log.warn("Sender ip is null. Aborting getSenderInfo"); |
| 97 | return false; |
| 98 | } |
| 99 | senderIpAddress = sender.toOctets(); |
Pier Ventre | b6b81d5 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 100 | } catch (DeviceConfigNotFoundException e) { |
Pier Luigi | 6a83c4a | 2017-01-29 12:38:48 -0800 | [diff] [blame^] | 101 | log.warn(e.getMessage() + " Aborting getSenderInfo"); |
| 102 | return false; |
Pier Ventre | b6b81d5 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 103 | } |
Pier Ventre | b6b81d5 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 104 | System.arraycopy(senderMacAddress, 0, mac, 0, senderMacAddress.length); |
| 105 | System.arraycopy(senderIpAddress, 0, ip, 0, senderIpAddress.length); |
Pier Luigi | 6a83c4a | 2017-01-29 12:38:48 -0800 | [diff] [blame^] | 106 | return true; |
Pier Ventre | b6b81d5 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Utility to send a ND reply using the supplied information. |
| 111 | * |
| 112 | * @param pkt the request |
| 113 | * @param targetMac the target mac |
| 114 | * @param hostService the host service |
| 115 | */ |
| 116 | protected void sendResponse(NeighbourMessageContext pkt, MacAddress targetMac, HostService hostService) { |
| 117 | HostId dstId = HostId.hostId(pkt.srcMac(), pkt.vlan()); |
| 118 | Host dst = hostService.getHost(dstId); |
| 119 | if (dst == null) { |
| 120 | log.warn("Cannot send {} response to host {} - does not exist in the store", |
| 121 | pkt.protocol(), dstId); |
| 122 | return; |
| 123 | } |
| 124 | pkt.reply(targetMac); |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Flood to all ports in the same subnet. |
| 129 | * |
| 130 | * @param packet packet to be flooded |
| 131 | * @param inPort where the packet comes from |
| 132 | * @param targetAddress the target address |
| 133 | */ |
| 134 | protected void flood(Ethernet packet, ConnectPoint inPort, IpAddress targetAddress) { |
| 135 | try { |
| 136 | srManager.deviceConfiguration |
| 137 | .getSubnetPortsMap(inPort.deviceId()).forEach((subnet, ports) -> { |
| 138 | if (subnet.contains(targetAddress)) { |
| 139 | ports.stream() |
| 140 | .filter(port -> port != inPort.port()) |
| 141 | .forEach(port -> { |
| 142 | forward(packet, new ConnectPoint(inPort.deviceId(), port)); |
| 143 | }); |
Pier Ventre | b6a7f34 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 144 | } |
Pier Ventre | b6b81d5 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 145 | }); |
| 146 | } catch (DeviceConfigNotFoundException e) { |
| 147 | log.warn(e.getMessage() |
| 148 | + " Cannot flood in subnet as device config not available" |
| 149 | + " for device: " + inPort.deviceId()); |
Pier Ventre | b6a7f34 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 150 | } |
| 151 | } |
| 152 | |
Pier Ventre | b6b81d5 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 153 | /* |
| 154 | * Floods only on the port which have been configured with the subnet |
| 155 | * of the target address. The in port is excluded. |
| 156 | * |
| 157 | * @param pkt the ndp/arp packet and context information |
| 158 | */ |
| 159 | protected void flood(NeighbourMessageContext pkt) { |
| 160 | try { |
| 161 | srManager.deviceConfiguration |
| 162 | .getSubnetPortsMap(pkt.inPort().deviceId()).forEach((subnet, ports) -> { |
| 163 | if (subnet.contains(pkt.target())) { |
| 164 | ports.stream() |
| 165 | .filter(port -> port != pkt.inPort().port()) |
| 166 | .forEach(port -> { |
| 167 | ConnectPoint outPoint = new ConnectPoint( |
| 168 | pkt.inPort().deviceId(), |
| 169 | port |
| 170 | ); |
| 171 | pkt.forward(outPoint); |
| 172 | }); |
| 173 | } |
| 174 | }); |
| 175 | } catch (DeviceConfigNotFoundException e) { |
| 176 | log.warn(e.getMessage() |
| 177 | + " Cannot flood in subnet as device config not available" |
| 178 | + " for device: " + pkt.inPort().deviceId()); |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Packet out to given port. |
| 184 | * |
| 185 | * Note: In current implementation, we expect all communication with |
| 186 | * end hosts within a subnet to be untagged. |
| 187 | * <p> |
| 188 | * For those pipelines that internally assigns a VLAN, the VLAN tag will be |
| 189 | * removed before egress. |
| 190 | * <p> |
| 191 | * For those pipelines that do not assign internal VLAN, the packet remains |
| 192 | * untagged. |
| 193 | * |
| 194 | * @param packet packet to be forwarded |
| 195 | * @param outPort where the packet should be forwarded |
| 196 | */ |
| 197 | private void forward(Ethernet packet, ConnectPoint outPort) { |
| 198 | ByteBuffer buf = ByteBuffer.wrap(packet.serialize()); |
| 199 | |
| 200 | TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder(); |
| 201 | tbuilder.setOutput(outPort.port()); |
| 202 | srManager.packetService.emit(new DefaultOutboundPacket(outPort.deviceId(), |
| 203 | tbuilder.build(), buf)); |
| 204 | } |
Pier Ventre | b6a7f34 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 205 | |
| 206 | } |