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