-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Tilmann Zäschke
committed
Aug 19, 2024
1 parent
ec1f109
commit 3ae0279
Showing
7 changed files
with
195 additions
and
126 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 0 additions & 103 deletions
103
src/main/java/org/scion/jpan/internal/PathDeduplicator.java
This file was deleted.
Oops, something went wrong.
117 changes: 117 additions & 0 deletions
117
src/main/java/org/scion/jpan/internal/PathDuplicationFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
// Copyright 2024 ETH Zurich | ||
// | ||
// 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 org.scion.jpan.internal; | ||
|
||
import java.nio.ByteBuffer; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import org.scion.jpan.proto.daemon.Daemon; | ||
|
||
class PathDuplicationFilter { | ||
|
||
MultiMap<Integer, Entry> paths = new MultiMap<>(); | ||
|
||
private static class Entry { | ||
Daemon.Path path; | ||
int[] interfaces; | ||
|
||
public Entry(Daemon.Path path, int[] interfaces) { | ||
this.path = path; | ||
this.interfaces = interfaces; | ||
} | ||
|
||
public void update(Daemon.Path path, int[] interfaces) { | ||
this.path = path; | ||
this.interfaces = interfaces; | ||
} | ||
} | ||
|
||
/** | ||
* Add path to list, but avoid duplicates. A path is considered "duplicate" if it uses the same | ||
* sequence of interface IDs. In case of a duplicate, we keep the path with the latest expiration | ||
* date. | ||
* | ||
* @param path New path | ||
*/ | ||
void checkDuplicatePaths(Daemon.Path.Builder path) { | ||
// Create a hash code that uses only interface IDs | ||
int[] interfaces = extractInterfaces(path); | ||
int hash = Arrays.hashCode(interfaces); | ||
if (paths.contains(hash)) { | ||
for (Entry storedPath : paths.get(hash)) { | ||
if (Arrays.equals(interfaces, storedPath.interfaces)) { | ||
// Which one doe we keep? Compare minimum expiration date. | ||
if (path.getExpiration().getSeconds() > storedPath.path.getExpiration().getSeconds()) { | ||
storedPath.update(path.build(), interfaces); | ||
} | ||
return; | ||
} | ||
} | ||
} | ||
// Add new path! | ||
paths.put(hash, new Entry(path.build(), interfaces)); | ||
} | ||
|
||
public List<Daemon.Path> getPaths() { | ||
List<Entry> entries = paths.values(); | ||
List<Daemon.Path> result = new ArrayList<>(entries.size()); | ||
for (Entry entry : entries) { | ||
result.add(entry.path); | ||
} | ||
return result; | ||
} | ||
|
||
/** | ||
* Extract used interfaces. Typically, for detecting duplicates, we could just compare the rwa | ||
* byte[]. This would detect most duplicates. However, in some cases we get two paths that use | ||
* identical interfaces but have different SegmentID, Expiration Date and different "unused" | ||
* interfaces. For example, in the scionproto "default" topology, going from 1-ff00:0:111 to | ||
* 1-ff00:0:112, we end up with two path that look externally like this: [494 > 103]. However, | ||
* internally they look like this: | ||
* | ||
* <p>- segID=9858, timestamp=1723449803, [494, 0, 104, 103] <br> | ||
* - segID=9751, timestamp=1723449803, [494, 0, 105, 103] <br> | ||
* | ||
* <p>The 104 vs 105 interface is not actually used and is an artifact of the path being | ||
* shortened. The following methods considers these cases and only extracts interface IDs of | ||
* interfaces that are actually used. | ||
* | ||
* @param path path | ||
* @return interfaces | ||
*/ | ||
private static int[] extractInterfaces(Daemon.Path.Builder path) { | ||
ByteBuffer raw = path.getRaw().asReadOnlyByteBuffer(); | ||
int[] segLen = PathRawParserLight.getSegments(raw); | ||
int segCount = PathRawParserLight.calcSegmentCount(segLen); | ||
int[] result = new int[PathRawParserLight.extractHopCount(segLen) * 2]; | ||
int offset = 0; | ||
int ifPos = 0; | ||
for (int j = 0; j < segLen.length; j++) { | ||
boolean flagC = PathRawParserLight.extractInfoFlagC(raw, j); | ||
for (int i = offset; i < offset + segLen[j] - 1; i++) { | ||
if (flagC) { | ||
result[ifPos++] = PathRawParserLight.extractHopFieldEgress(raw, segCount, i); | ||
result[ifPos++] = PathRawParserLight.extractHopFieldIngress(raw, segCount, i + 1); | ||
} else { | ||
result[ifPos++] = PathRawParserLight.extractHopFieldIngress(raw, segCount, i); | ||
result[ifPos++] = PathRawParserLight.extractHopFieldEgress(raw, segCount, i + 1); | ||
} | ||
} | ||
offset += segLen[j]; | ||
} | ||
return result; | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
src/main/java/org/scion/jpan/internal/PathRawParserLight.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// Copyright 2023 ETH Zurich | ||
// | ||
// 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 org.scion.jpan.internal; | ||
|
||
import static org.scion.jpan.internal.ByteUtil.readBoolean; | ||
import static org.scion.jpan.internal.ByteUtil.readInt; | ||
|
||
import java.nio.ByteBuffer; | ||
|
||
/** A lightweight stateless parser for raw paths. */ | ||
public class PathRawParserLight { | ||
private static final int PATH_META_LEN = 4; | ||
private static final int PATH_INFO_LEN = 8; | ||
private static final int HOP_FIELD_LEN = 12; | ||
|
||
private PathRawParserLight() {} | ||
|
||
public static int[] getSegments(ByteBuffer data) { | ||
int i0 = data.getInt(); | ||
int[] segLen = new int[3]; | ||
segLen[0] = readInt(i0, 14, 6); | ||
segLen[1] = readInt(i0, 20, 6); | ||
segLen[2] = readInt(i0, 26, 6); | ||
return segLen; | ||
} | ||
|
||
public static boolean extractInfoFlagC(ByteBuffer data, int segID) { | ||
int i0 = data.getInt(PATH_META_LEN + segID * PATH_INFO_LEN); | ||
return readBoolean(i0, 7); | ||
} | ||
|
||
public static int extractHopCount(int[] segLen) { | ||
int nHops = segLen[0] + segLen[1] + segLen[2]; | ||
return nHops - calcSegmentCount(segLen); | ||
} | ||
|
||
public static int extractHopFieldIngress(ByteBuffer data, int segCount, int hopID) { | ||
int hfOffset = PATH_META_LEN + segCount * PATH_INFO_LEN + hopID * HOP_FIELD_LEN; | ||
return ByteUtil.toUnsigned(data.getShort(hfOffset + 2)); | ||
} | ||
|
||
public static int extractHopFieldEgress(ByteBuffer data, int segCount, int hopID) { | ||
int hfOffset = PATH_META_LEN + segCount * PATH_INFO_LEN + hopID * HOP_FIELD_LEN; | ||
return ByteUtil.toUnsigned(data.getShort(hfOffset + 4)); | ||
} | ||
|
||
public static int calcSegmentCount(int[] segLen) { | ||
int nSegmentCount = 1; | ||
nSegmentCount += segLen[1] > 0 ? 1 : 0; | ||
nSegmentCount += segLen[2] > 0 ? 1 : 0; | ||
return nSegmentCount; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters