Feat: done

This commit is contained in:
2025-10-08 19:44:08 +08:00
commit 2947f0c6b7
10 changed files with 716 additions and 0 deletions

View File

@@ -0,0 +1,92 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright 2025 Open Networking Foundation
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.onosproject</groupId>
<artifactId>onos-dependencies</artifactId>
<version>2.7.0</version>
</parent>
<groupId>nycu.winlab</groupId>
<artifactId>ProxyArp</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>bundle</packaging>
<description>ONOS OSGi bundle archetype</description>
<url>http://onosproject.org</url>
<properties>
<onos.app.name>nycu.winlab.ProxyArp</onos.app.name>
<onos.app.title>Proxy Arp App</onos.app.title>
<onos.app.origin>Winlab, NCTU</onos.app.origin>
<onos.app.category>default</onos.app.category>
<onos.app.url>http://onosproject.org</onos.app.url>
<onos.app.readme>ONOS OSGi bundle archetype.</onos.app.readme>
</properties>
<dependencies>
<dependency>
<groupId>org.onosproject</groupId>
<artifactId>onos-api</artifactId>
<version>${onos.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.onosproject</groupId>
<artifactId>onlab-osgi</artifactId>
<version>${onos.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.onosproject</groupId>
<artifactId>onlab-misc</artifactId>
<version>${onos.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.onosproject</groupId>
<artifactId>onos-api</artifactId>
<version>${onos.version}</version>
<scope>test</scope>
<classifier>tests</classifier>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.onosproject</groupId>
<artifactId>onos-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<!-- Uncomment to disable checkstyle validation -->
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@@ -0,0 +1,171 @@
/*
* Copyright 2025-present Open Networking Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package nycu.winlab;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Deactivate;
import org.osgi.service.component.annotations.Reference;
import org.osgi.service.component.annotations.ReferenceCardinality;
import org.onosproject.core.ApplicationId;
import org.onosproject.core.CoreService;
import org.onosproject.net.packet.PacketContext;
import org.onosproject.net.packet.PacketProcessor;
import org.onosproject.net.packet.PacketService;
import org.onosproject.net.packet.InboundPacket;
import org.onosproject.net.packet.OutboundPacket;
import org.onosproject.net.packet.DefaultOutboundPacket;
import org.onosproject.net.packet.PacketPriority;
import org.onosproject.net.edge.EdgePortService;
import org.onosproject.net.ConnectPoint;
import org.onosproject.net.flow.DefaultTrafficSelector;
import org.onosproject.net.flow.DefaultTrafficTreatment;
import org.onosproject.net.flow.TrafficTreatment;
import org.onosproject.net.flow.TrafficSelector;
import org.onlab.packet.Ethernet;
import org.onlab.packet.ARP;
import org.onlab.packet.Ip4Address;
import org.onlab.packet.MacAddress;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.nio.ByteBuffer;
/**
* Skeletal ONOS application component.
*/
@Component(immediate = true)
public class AppComponent {
private final Logger log = LoggerFactory.getLogger(getClass());
@Reference(cardinality = ReferenceCardinality.MANDATORY)
private CoreService coreService;
@Reference(cardinality = ReferenceCardinality.MANDATORY)
private PacketService packetService;
@Reference(cardinality = ReferenceCardinality.MANDATORY)
private EdgePortService edgePortService;
private ApplicationId appId;
private final PacketProcessor processor = new ProxyArpProcessor();
private final Map<Ip4Address, MacAddress> arpTable = new ConcurrentHashMap<>();
private final Map<MacAddress, ConnectPoint> macTable = new ConcurrentHashMap<>();
private TrafficSelector arpSelector;
@Activate
protected void activate() {
arpSelector = DefaultTrafficSelector.builder().
matchEthType(Ethernet.TYPE_ARP).
build();
appId = coreService.registerApplication("nycu.winlab.ProxyArp");
packetService.addProcessor(processor, PacketProcessor.director(3));
packetService.requestPackets(arpSelector, PacketPriority.REACTIVE, appId);
log.info("Started");
}
@Deactivate
protected void deactivate() {
packetService.cancelPackets(arpSelector, PacketPriority.REACTIVE, appId);
packetService.removeProcessor(processor);
log.info("Stopped");
}
private class ProxyArpProcessor implements PacketProcessor {
private void sendPacket(Ethernet ethPacket, ConnectPoint outPort) {
TrafficTreatment treatment = DefaultTrafficTreatment.builder().
setOutput(outPort.port()).
build();
OutboundPacket outPacket = new DefaultOutboundPacket(
outPort.deviceId(),
treatment,
ByteBuffer.wrap(ethPacket.serialize())
);
packetService.emit(outPacket);
}
@Override
public void process(PacketContext context) {
if (context.isHandled()) {
return;
}
InboundPacket packet = context.inPacket();
Ethernet ethPacket = packet.parsed();
if (ethPacket == null) {
return;
}
ARP arpPacket = (ARP) ethPacket.getPayload();
if (arpPacket.getProtocolType() != ARP.PROTO_TYPE_IP) {
return;
}
ConnectPoint inPort = packet.receivedFrom();
MacAddress srcMac = ethPacket.getSourceMAC();
Ip4Address srcIp = Ip4Address.valueOf(arpPacket.
getSenderProtocolAddress());
Ip4Address dstIp = Ip4Address.valueOf(arpPacket.
getTargetProtocolAddress());
macTable.putIfAbsent(srcMac, inPort);
arpTable.putIfAbsent(srcIp, srcMac);
if (arpPacket.getOpCode() == ARP.OP_REQUEST) {
MacAddress dstMac = arpTable.get(dstIp);
if (dstMac == null) {
for (ConnectPoint cp: edgePortService.getEdgePoints()) {
if (!cp.equals(inPort)) {
sendPacket(ethPacket, cp);
}
}
log.info("TABLE MISS. Send request to edge ports");
} else {
Ethernet arpReply = ARP.buildArpReply(dstIp, dstMac, ethPacket);
sendPacket(arpReply, inPort);
log.info("TABLE HIT. Requested MAC = {}", dstMac);
}
}
if (arpPacket.getOpCode() == ARP.OP_REPLY) {
arpTable.put(srcIp, srcMac);
macTable.put(srcMac, inPort);
MacAddress dstMac = arpTable.get(dstIp);
ConnectPoint outPort = macTable.get(dstMac);
if (outPort != null) {
sendPacket(ethPacket, outPort);
}
}
}
}
}

View File

@@ -0,0 +1,20 @@
/*
* Copyright 2025-present Open Networking Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* ONOS application archetype.
*/
package nycu.winlab.ProxyArp;

View File

@@ -0,0 +1,46 @@
/*
* Copyright 2025-present Open Networking Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package nycu.winlab;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.onosproject.cfg.ComponentConfigAdapter;
/**
* Set of tests of the ONOS application component.
*/
public class AppComponentTest {
private AppComponent component;
@Before
public void setUp() {
component = new AppComponent();
component.activate();
}
@After
public void tearDown() {
component.deactivate();
}
@Test
public void basics() {
}
}