Pier Ventre | 10bd8d1 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Pier Ventre | 10bd8d1 | 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 | 735b8c8 | 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 | 800d9d0 | 2018-04-09 09:40:50 -0700 | [diff] [blame] | 22 | import org.onlab.packet.VlanId; |
Ray Milkey | b65d784 | 2017-08-03 16:28:24 -0700 | [diff] [blame] | 23 | import org.onosproject.net.neighbour.NeighbourMessageContext; |
Pier Ventre | 735b8c8 | 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 | 10bd8d1 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 30 | import org.onosproject.net.host.HostService; |
Pier Ventre | 735b8c8 | 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 | 10bd8d1 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 34 | import org.slf4j.Logger; |
| 35 | import org.slf4j.LoggerFactory; |
| 36 | |
Pier Ventre | 735b8c8 | 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 | 10bd8d1 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 41 | /** |
Pier Ventre | 735b8c8 | 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 | 10bd8d1 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 44 | */ |
Pier Ventre | 735b8c8 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 45 | public class SegmentRoutingNeighbourHandler { |
Pier Ventre | 10bd8d1 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 46 | |
| 47 | private static Logger log = LoggerFactory.getLogger(SegmentRoutingNeighbourHandler.class); |
Pier Ventre | 735b8c8 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 48 | |
| 49 | protected SegmentRoutingManager srManager; |
| 50 | protected DeviceConfiguration config; |
Pier Ventre | 10bd8d1 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 51 | |
| 52 | /** |
Pier Ventre | 735b8c8 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 53 | * Creates an SegmentRoutingNeighbourHandler object. |
Pier Ventre | 10bd8d1 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 54 | * |
Pier Ventre | 735b8c8 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 55 | * @param srManager SegmentRoutingManager object |
Pier Ventre | 10bd8d1 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 56 | */ |
Pier Ventre | 735b8c8 | 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 | 10bd8d1 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 60 | } |
| 61 | |
Pier Ventre | 735b8c8 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 62 | /** |
| 63 | * Creates an SegmentRoutingNeighbourHandler object. |
Pier Ventre | 10bd8d1 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 64 | */ |
Pier Ventre | 735b8c8 | 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 | a905c0c | 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 | 735b8c8 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 79 | */ |
Pier Luigi | a905c0c | 2017-01-29 12:38:48 -0800 | [diff] [blame] | 80 | protected boolean getSenderInfo(byte[] mac, |
Pier Ventre | 735b8c8 | 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 | a905c0c | 2017-01-29 12:38:48 -0800 | [diff] [blame] | 86 | IpAddress sender; |
Pier Ventre | 735b8c8 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 87 | try { |
| 88 | senderMacAddress = config.getDeviceMac(deviceId).toBytes(); |
Pier Ventre | 968da12 | 2016-12-09 17:26:04 -0800 | [diff] [blame] | 89 | if (targetAddress.isIp4()) { |
Pier Luigi | a905c0c | 2017-01-29 12:38:48 -0800 | [diff] [blame] | 90 | sender = config.getRouterIpAddressForASubnetHost(targetAddress.getIp4Address()); |
Pier Ventre | 968da12 | 2016-12-09 17:26:04 -0800 | [diff] [blame] | 91 | } else { |
Pier Luigi | a905c0c | 2017-01-29 12:38:48 -0800 | [diff] [blame] | 92 | sender = config.getRouterIpAddressForASubnetHost(targetAddress.getIp6Address()); |
Pier Ventre | 968da12 | 2016-12-09 17:26:04 -0800 | [diff] [blame] | 93 | } |
Pier Luigi | a905c0c | 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 | 735b8c8 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 100 | } catch (DeviceConfigNotFoundException e) { |
Pier Luigi | a905c0c | 2017-01-29 12:38:48 -0800 | [diff] [blame] | 101 | log.warn(e.getMessage() + " Aborting getSenderInfo"); |
| 102 | return false; |
Pier Ventre | 735b8c8 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 103 | } |
Pier Ventre | 735b8c8 | 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 | a905c0c | 2017-01-29 12:38:48 -0800 | [diff] [blame] | 106 | return true; |
Pier Ventre | 735b8c8 | 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) { |
Jonghwan Hyun | 800d9d0 | 2018-04-09 09:40:50 -0700 | [diff] [blame] | 117 | short vlanId = pkt.packet().getQinQVID(); |
| 118 | HostId dstId = HostId.hostId(pkt.srcMac(), vlanId == Ethernet.VLAN_UNTAGGED |
| 119 | ? pkt.vlan() : VlanId.vlanId(vlanId)); |
Pier Ventre | 735b8c8 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 120 | Host dst = hostService.getHost(dstId); |
| 121 | if (dst == null) { |
| 122 | log.warn("Cannot send {} response to host {} - does not exist in the store", |
| 123 | pkt.protocol(), dstId); |
| 124 | return; |
| 125 | } |
| 126 | pkt.reply(targetMac); |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Flood to all ports in the same subnet. |
| 131 | * |
| 132 | * @param packet packet to be flooded |
| 133 | * @param inPort where the packet comes from |
| 134 | * @param targetAddress the target address |
| 135 | */ |
| 136 | protected void flood(Ethernet packet, ConnectPoint inPort, IpAddress targetAddress) { |
| 137 | try { |
| 138 | srManager.deviceConfiguration |
| 139 | .getSubnetPortsMap(inPort.deviceId()).forEach((subnet, ports) -> { |
| 140 | if (subnet.contains(targetAddress)) { |
| 141 | ports.stream() |
| 142 | .filter(port -> port != inPort.port()) |
| 143 | .forEach(port -> { |
| 144 | forward(packet, new ConnectPoint(inPort.deviceId(), port)); |
| 145 | }); |
Pier Ventre | 10bd8d1 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 146 | } |
Pier Ventre | 735b8c8 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 147 | }); |
| 148 | } catch (DeviceConfigNotFoundException e) { |
| 149 | log.warn(e.getMessage() |
| 150 | + " Cannot flood in subnet as device config not available" |
| 151 | + " for device: " + inPort.deviceId()); |
Pier Ventre | 10bd8d1 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 152 | } |
| 153 | } |
| 154 | |
Pier Ventre | 735b8c8 | 2016-12-02 08:16:05 -0800 | [diff] [blame] | 155 | /* |
| 156 | * Floods only on the port which have been configured with the subnet |
| 157 | * of the target address. The in port is excluded. |
| 158 | * |
| 159 | * @param pkt the ndp/arp packet and context information |
| 160 | */ |
| 161 | protected void flood(NeighbourMessageContext pkt) { |
| 162 | try { |
| 163 | srManager.deviceConfiguration |
| 164 | .getSubnetPortsMap(pkt.inPort().deviceId()).forEach((subnet, ports) -> { |
| 165 | if (subnet.contains(pkt.target())) { |
| 166 | ports.stream() |
| 167 | .filter(port -> port != pkt.inPort().port()) |
| 168 | .forEach(port -> { |
| 169 | ConnectPoint outPoint = new ConnectPoint( |
| 170 | pkt.inPort().deviceId(), |
| 171 | port |
| 172 | ); |
| 173 | pkt.forward(outPoint); |
| 174 | }); |
| 175 | } |
| 176 | }); |
| 177 | } catch (DeviceConfigNotFoundException e) { |
| 178 | log.warn(e.getMessage() |
| 179 | + " Cannot flood in subnet as device config not available" |
| 180 | + " for device: " + pkt.inPort().deviceId()); |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * Packet out to given port. |
| 186 | * |
| 187 | * Note: In current implementation, we expect all communication with |
| 188 | * end hosts within a subnet to be untagged. |
| 189 | * <p> |
| 190 | * For those pipelines that internally assigns a VLAN, the VLAN tag will be |
| 191 | * removed before egress. |
| 192 | * <p> |
| 193 | * For those pipelines that do not assign internal VLAN, the packet remains |
| 194 | * untagged. |
| 195 | * |
| 196 | * @param packet packet to be forwarded |
| 197 | * @param outPort where the packet should be forwarded |
| 198 | */ |
| 199 | private void forward(Ethernet packet, ConnectPoint outPort) { |
| 200 | ByteBuffer buf = ByteBuffer.wrap(packet.serialize()); |
| 201 | |
| 202 | TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder(); |
| 203 | tbuilder.setOutput(outPort.port()); |
| 204 | srManager.packetService.emit(new DefaultOutboundPacket(outPort.deviceId(), |
| 205 | tbuilder.build(), buf)); |
| 206 | } |
Pier Ventre | 10bd8d1 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 207 | |
| 208 | } |