blob: 1d9a2fcf41a1222ec4ebbf4dd1d57c3a6d897458 [file] [log] [blame]
Pier Ventre10bd8d12016-11-26 21:05:22 -08001/*
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
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
76 */
77 protected void getSenderInfo(byte[] mac,
78 byte[] ip,
79 DeviceId deviceId,
80 IpAddress targetAddress) {
81 byte[] senderMacAddress;
82 byte[] senderIpAddress;
83 try {
84 senderMacAddress = config.getDeviceMac(deviceId).toBytes();
Pier Ventre968da122016-12-09 17:26:04 -080085 if (targetAddress.isIp4()) {
86 senderIpAddress = config.getRouterIpAddressForASubnetHost(targetAddress.getIp4Address())
87 .toOctets();
88 } else {
89 senderIpAddress = config.getRouterIpAddressForASubnetHost(targetAddress.getIp6Address())
90 .toOctets();
91 }
Pier Ventre735b8c82016-12-02 08:16:05 -080092 } catch (DeviceConfigNotFoundException e) {
93 log.warn(e.getMessage() + " Aborting sendArpRequest.");
94 return;
95 }
Pier Ventre735b8c82016-12-02 08:16:05 -080096 System.arraycopy(senderMacAddress, 0, mac, 0, senderMacAddress.length);
97 System.arraycopy(senderIpAddress, 0, ip, 0, senderIpAddress.length);
98 }
99
100 /**
101 * Utility to send a ND reply using the supplied information.
102 *
103 * @param pkt the request
104 * @param targetMac the target mac
105 * @param hostService the host service
106 */
107 protected void sendResponse(NeighbourMessageContext pkt, MacAddress targetMac, HostService hostService) {
108 HostId dstId = HostId.hostId(pkt.srcMac(), pkt.vlan());
109 Host dst = hostService.getHost(dstId);
110 if (dst == null) {
111 log.warn("Cannot send {} response to host {} - does not exist in the store",
112 pkt.protocol(), dstId);
113 return;
114 }
115 pkt.reply(targetMac);
116 }
117
118 /**
119 * Flood to all ports in the same subnet.
120 *
121 * @param packet packet to be flooded
122 * @param inPort where the packet comes from
123 * @param targetAddress the target address
124 */
125 protected void flood(Ethernet packet, ConnectPoint inPort, IpAddress targetAddress) {
126 try {
127 srManager.deviceConfiguration
128 .getSubnetPortsMap(inPort.deviceId()).forEach((subnet, ports) -> {
129 if (subnet.contains(targetAddress)) {
130 ports.stream()
131 .filter(port -> port != inPort.port())
132 .forEach(port -> {
133 forward(packet, new ConnectPoint(inPort.deviceId(), port));
134 });
Pier Ventre10bd8d12016-11-26 21:05:22 -0800135 }
Pier Ventre735b8c82016-12-02 08:16:05 -0800136 });
137 } catch (DeviceConfigNotFoundException e) {
138 log.warn(e.getMessage()
139 + " Cannot flood in subnet as device config not available"
140 + " for device: " + inPort.deviceId());
Pier Ventre10bd8d12016-11-26 21:05:22 -0800141 }
142 }
143
Pier Ventre735b8c82016-12-02 08:16:05 -0800144 /*
145 * Floods only on the port which have been configured with the subnet
146 * of the target address. The in port is excluded.
147 *
148 * @param pkt the ndp/arp packet and context information
149 */
150 protected void flood(NeighbourMessageContext pkt) {
151 try {
152 srManager.deviceConfiguration
153 .getSubnetPortsMap(pkt.inPort().deviceId()).forEach((subnet, ports) -> {
154 if (subnet.contains(pkt.target())) {
155 ports.stream()
156 .filter(port -> port != pkt.inPort().port())
157 .forEach(port -> {
158 ConnectPoint outPoint = new ConnectPoint(
159 pkt.inPort().deviceId(),
160 port
161 );
162 pkt.forward(outPoint);
163 });
164 }
165 });
166 } catch (DeviceConfigNotFoundException e) {
167 log.warn(e.getMessage()
168 + " Cannot flood in subnet as device config not available"
169 + " for device: " + pkt.inPort().deviceId());
170 }
171 }
172
173 /**
174 * Packet out to given port.
175 *
176 * Note: In current implementation, we expect all communication with
177 * end hosts within a subnet to be untagged.
178 * <p>
179 * For those pipelines that internally assigns a VLAN, the VLAN tag will be
180 * removed before egress.
181 * <p>
182 * For those pipelines that do not assign internal VLAN, the packet remains
183 * untagged.
184 *
185 * @param packet packet to be forwarded
186 * @param outPort where the packet should be forwarded
187 */
188 private void forward(Ethernet packet, ConnectPoint outPort) {
189 ByteBuffer buf = ByteBuffer.wrap(packet.serialize());
190
191 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
192 tbuilder.setOutput(outPort.port());
193 srManager.packetService.emit(new DefaultOutboundPacket(outPort.deviceId(),
194 tbuilder.build(), buf));
195 }
Pier Ventre10bd8d12016-11-26 21:05:22 -0800196
197}