0

因此,我正在使用交换机 ID 为两个交换机创建嵌套哈希映射,然后输入源 Mac 地址和端口。

例如 switch1 应该包含它自己的映射,switch2 也应该包含它自己的映射,并且两个开关显然应该相互通信,所以我将 HashMap 设置如下:

HashMap<String, HashMap<Long, Short>> map = new HashMap<String, HashMap<Long,Short>>();

if(sw.getId() == 1){
        switchMap.put("1", new HashMap<Long, Short>());
        switchMap.get("1").put(sourceMac, (short) pi.getInPort());
}
else if(sw.getId() == 2){
        switchMap.put("2", new HashMap<Long, Short>());
        switchMap.get("2").put(sourceMac, (short) pi.getInPort());
}

现在,我想做的是检查每个交换机的密钥(1 或 2),然后在检查给定的 destinationMac 时检查每个交换机是否具有正确的 sourceMac 和 port#:

Long destinationMac = Ethernet.toLong(match.getDataLayerDestination());

if (switchMap.containsKey("1") && switchMap.containsValue(destinationMac)) {    
    /* Now we can retrieve the port number. This will be the port that we need to send the
    packet over to reach the host to which that MAC address belongs. */
    short destinationPort = (short) switchMap.get("1").get(destinationMac);
    /*Write the packet to a port, use the destination port we have just found.*/
    installFlowMod(sw, pi, match, destinationPort, 50, 100, cntx);
} 
else if (switchMap.containsKey("2") && switchMap.containsValue(destinationMac)) {   
    /* Now we can retrieve the port number. This will be the port that we need to send the
    packet over to reach the host to which that MAC address belongs. */
    short destinationPort = (short) switchMap.get("2").get(destinationMac)
    /*Write the packet to a port, use the destination port we have just found.*/
    installFlowMod(sw, pi, match, destinationPort, 50, 100, cntx);
}
else {
    log.debug("Destination MAC address unknown: flooding");
    writePacketToPort(sw, pi, OFPort.OFPP_FLOOD.getValue(), cntx);
}

当我运行代码并尝试从 h1(switch1) ping 到 h3(switch2) 时,我收到了请求,但我仍然收到错误消息"Destination MAC address unknown: flooding"

我的问题是,我是否正确地从嵌套的 HashMap 中获取值?还是我的逻辑完全搞砸了?

4

2 回答 2

3
for(String s: map.keySet()){
    HashMap<Long, Short> switchMap =map.get(s);
    if(switchMap.containsValue(destinationMac)){ 
        return switchMap.get(destinationMac);
    }
}
于 2015-10-24T15:10:17.377 回答
1

您检查目标地址是否存在的方式是错误的。它应该是这样的:

Short portS1 = switchMap.get("1").get(destinationMac)
if (portS1 != null) {
  installFlowMod(sw, pi, match, portS1, 50, 100, cntx);
}

Short portS2 = switchMap.get("1").get(destinationMac)
if (portS2 != null) {
  installFlowMod(sw, pi, match, portS2, 50, 100, cntx);
}

为此,您必须使用空的Map<Long, Short>.

一个更通用的方法是:

for (Map.Entry<String, Map<Long, Short>> e: switchMap.entrySet()) {
  Short port = e.getValue().get(destinationMac);
  if (port != null) installFlowMod(sw, pi, match, port, 50, 100, cntx); 
}

这样做你甚至不需要预先初始化 switchMap。

于 2015-10-24T13:53:40.910 回答