blob: 0215249b7289435143ebc6376a4d61687f81d356 [file] [log] [blame]
Pier Ventre10bd8d12016-11-26 21:05:22 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Pier Ventre10bd8d12016-11-26 21:05:22 -08003 *
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
17package org.onosproject.segmentrouting;
18
Pier Ventre735b8c82016-12-02 08:16:05 -080019import org.onlab.packet.Ethernet;
20import org.onlab.packet.IpAddress;
21import org.onlab.packet.MacAddress;
Pier Ventre10bd8d12016-11-26 21:05:22 -080022import org.onosproject.incubator.net.neighbour.NeighbourMessageContext;
Pier Ventre735b8c82016-12-02 08:16:05 -080023import org.onosproject.net.ConnectPoint;
24import org.onosproject.net.DeviceId;
25import org.onosproject.net.Host;
26import org.onosproject.net.HostId;
27import org.onosproject.net.flow.DefaultTrafficTreatment;
28import org.onosproject.net.flow.TrafficTreatment;
Pier Ventre10bd8d12016-11-26 21:05:22 -080029import org.onosproject.net.host.HostService;
Pier Ventre735b8c82016-12-02 08:16:05 -080030import org.onosproject.net.packet.DefaultOutboundPacket;
31import org.onosproject.segmentrouting.config.DeviceConfigNotFoundException;
32import org.onosproject.segmentrouting.config.DeviceConfiguration;
Pier Ventre10bd8d12016-11-26 21:05:22 -080033import org.slf4j.Logger;
34import org.slf4j.LoggerFactory;
35
Pier Ventre735b8c82016-12-02 08:16:05 -080036import java.nio.ByteBuffer;
37
38import static com.google.common.base.Preconditions.checkNotNull;
39
Pier Ventre10bd8d12016-11-26 21:05:22 -080040/**
Pier Ventre735b8c82016-12-02 08:16:05 -080041 * This handler provides provides useful functions to the
42 * neighbour handlers (ARP, NDP).
Pier Ventre10bd8d12016-11-26 21:05:22 -080043 */
Pier Ventre735b8c82016-12-02 08:16:05 -080044public class SegmentRoutingNeighbourHandler {
Pier Ventre10bd8d12016-11-26 21:05:22 -080045
46 private static Logger log = LoggerFactory.getLogger(SegmentRoutingNeighbourHandler.class);
Pier Ventre735b8c82016-12-02 08:16:05 -080047
48 protected SegmentRoutingManager srManager;
49 protected DeviceConfiguration config;
Pier Ventre10bd8d12016-11-26 21:05:22 -080050
51 /**
Pier Ventre735b8c82016-12-02 08:16:05 -080052 * Creates an SegmentRoutingNeighbourHandler object.
Pier Ventre10bd8d12016-11-26 21:05:22 -080053 *
Pier Ventre735b8c82016-12-02 08:16:05 -080054 * @param srManager SegmentRoutingManager object
Pier Ventre10bd8d12016-11-26 21:05:22 -080055 */
Pier Ventre735b8c82016-12-02 08:16:05 -080056 public SegmentRoutingNeighbourHandler(SegmentRoutingManager srManager) {
57 this.srManager = srManager;
58 this.config = checkNotNull(srManager.deviceConfiguration);
Pier Ventre10bd8d12016-11-26 21:05:22 -080059 }
60
Pier Ventre735b8c82016-12-02 08:16:05 -080061 /**
62 * Creates an SegmentRoutingNeighbourHandler object.
Pier Ventre10bd8d12016-11-26 21:05:22 -080063 */
Pier Ventre735b8c82016-12-02 08:16:05 -080064 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 Luigia905c0c2017-01-29 12:38:48 -080076 * @return true if it was possible to get the necessary info.
77 * False for errors
Pier Ventre735b8c82016-12-02 08:16:05 -080078 */
Pier Luigia905c0c2017-01-29 12:38:48 -080079 protected boolean getSenderInfo(byte[] mac,
Pier Ventre735b8c82016-12-02 08:16:05 -080080 byte[] ip,
81 DeviceId deviceId,
82 IpAddress targetAddress) {
83 byte[] senderMacAddress;
84 byte[] senderIpAddress;
Pier Luigia905c0c2017-01-29 12:38:48 -080085 IpAddress sender;
Pier Ventre735b8c82016-12-02 08:16:05 -080086 try {
87 senderMacAddress = config.getDeviceMac(deviceId).toBytes();
Pier Ventre968da122016-12-09 17:26:04 -080088 if (targetAddress.isIp4()) {
Pier Luigia905c0c2017-01-29 12:38:48 -080089 sender = config.getRouterIpAddressForASubnetHost(targetAddress.getIp4Address());
Pier Ventre968da122016-12-09 17:26:04 -080090 } else {
Pier Luigia905c0c2017-01-29 12:38:48 -080091 sender = config.getRouterIpAddressForASubnetHost(targetAddress.getIp6Address());
Pier Ventre968da122016-12-09 17:26:04 -080092 }
Pier Luigia905c0c2017-01-29 12:38:48 -080093 // If sender is null we abort.
94 if (sender == null) {
95 log.warn("Sender ip is null. Aborting getSenderInfo");
96 return false;
97 }
98 senderIpAddress = sender.toOctets();
Pier Ventre735b8c82016-12-02 08:16:05 -080099 } catch (DeviceConfigNotFoundException e) {
Pier Luigia905c0c2017-01-29 12:38:48 -0800100 log.warn(e.getMessage() + " Aborting getSenderInfo");
101 return false;
Pier Ventre735b8c82016-12-02 08:16:05 -0800102 }
Pier Ventre735b8c82016-12-02 08:16:05 -0800103 System.arraycopy(senderMacAddress, 0, mac, 0, senderMacAddress.length);
104 System.arraycopy(senderIpAddress, 0, ip, 0, senderIpAddress.length);
Pier Luigia905c0c2017-01-29 12:38:48 -0800105 return true;
Pier Ventre735b8c82016-12-02 08:16:05 -0800106 }
107
108 /**
109 * Utility to send a ND reply using the supplied information.
110 *
111 * @param pkt the request
112 * @param targetMac the target mac
113 * @param hostService the host service
114 */
115 protected void sendResponse(NeighbourMessageContext pkt, MacAddress targetMac, HostService hostService) {
116 HostId dstId = HostId.hostId(pkt.srcMac(), pkt.vlan());
117 Host dst = hostService.getHost(dstId);
118 if (dst == null) {
119 log.warn("Cannot send {} response to host {} - does not exist in the store",
120 pkt.protocol(), dstId);
121 return;
122 }
123 pkt.reply(targetMac);
124 }
125
126 /**
127 * Flood to all ports in the same subnet.
128 *
129 * @param packet packet to be flooded
130 * @param inPort where the packet comes from
131 * @param targetAddress the target address
132 */
133 protected void flood(Ethernet packet, ConnectPoint inPort, IpAddress targetAddress) {
134 try {
135 srManager.deviceConfiguration
136 .getSubnetPortsMap(inPort.deviceId()).forEach((subnet, ports) -> {
137 if (subnet.contains(targetAddress)) {
138 ports.stream()
139 .filter(port -> port != inPort.port())
140 .forEach(port -> {
141 forward(packet, new ConnectPoint(inPort.deviceId(), port));
142 });
Pier Ventre10bd8d12016-11-26 21:05:22 -0800143 }
Pier Ventre735b8c82016-12-02 08:16:05 -0800144 });
145 } catch (DeviceConfigNotFoundException e) {
146 log.warn(e.getMessage()
147 + " Cannot flood in subnet as device config not available"
148 + " for device: " + inPort.deviceId());
Pier Ventre10bd8d12016-11-26 21:05:22 -0800149 }
150 }
151
Pier Ventre735b8c82016-12-02 08:16:05 -0800152 /*
153 * Floods only on the port which have been configured with the subnet
154 * of the target address. The in port is excluded.
155 *
156 * @param pkt the ndp/arp packet and context information
157 */
158 protected void flood(NeighbourMessageContext pkt) {
159 try {
160 srManager.deviceConfiguration
161 .getSubnetPortsMap(pkt.inPort().deviceId()).forEach((subnet, ports) -> {
162 if (subnet.contains(pkt.target())) {
163 ports.stream()
164 .filter(port -> port != pkt.inPort().port())
165 .forEach(port -> {
166 ConnectPoint outPoint = new ConnectPoint(
167 pkt.inPort().deviceId(),
168 port
169 );
170 pkt.forward(outPoint);
171 });
172 }
173 });
174 } catch (DeviceConfigNotFoundException e) {
175 log.warn(e.getMessage()
176 + " Cannot flood in subnet as device config not available"
177 + " for device: " + pkt.inPort().deviceId());
178 }
179 }
180
181 /**
182 * Packet out to given port.
183 *
184 * Note: In current implementation, we expect all communication with
185 * end hosts within a subnet to be untagged.
186 * <p>
187 * For those pipelines that internally assigns a VLAN, the VLAN tag will be
188 * removed before egress.
189 * <p>
190 * For those pipelines that do not assign internal VLAN, the packet remains
191 * untagged.
192 *
193 * @param packet packet to be forwarded
194 * @param outPort where the packet should be forwarded
195 */
196 private void forward(Ethernet packet, ConnectPoint outPort) {
197 ByteBuffer buf = ByteBuffer.wrap(packet.serialize());
198
199 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
200 tbuilder.setOutput(outPort.port());
201 srManager.packetService.emit(new DefaultOutboundPacket(outPort.deviceId(),
202 tbuilder.build(), buf));
203 }
Pier Ventre10bd8d12016-11-26 21:05:22 -0800204
205}