简述
监听Packetin和flow_removed消息。
如果是packetin,建立packetout从目的端口发送出去,并且写一个流表,匹配这个packetin的要求。
如果是flow_removed,从本地的映射表中删除,并写一个flowmod删除掉交换机上的流表。
涉及方法
Receive:
processPacketInMessage或processFlowRemovedMessage
processPacketInMessage:
writePacketOutForPacketIn写packetout;
writeFlowMod写正向和逆向的流表。
processFlowRemovedMessage:
removeFromPortMap本地删除映射;
writeFlowMod写删除消息。
接口
implements IFloodlightModule, IOFMessageListener
数据结构
protected Map<IOFSwitch, Map<MacAddress, OFPort>> macToSwitchPortMap;
MAC到MAC的防火墙映射:
protected Map<MacAddress, List<MacAddress>> firewallMap;
每个交换机存储最多的MAC数:
MAX_MACS_PER_SWITCH
将MAC加入某个交换机的映射表
protected void addToPortMap(IOFSwitch sw, MacAddress mac, OFPort portVal) {
Map<MacAddress, OFPort> swMap = macToSwitchPortMap.get(sw);
if (swMap == null) {
swMap = Collections.synchronizedMap(new LRULinkedHashMap<MacAddress, OFPort>(MAX_MACS_PER_SWITCH));
macToSwitchPortMap.put(sw, swMap);
}
swMap.put(mac, portVal);
}
从某个交换机中清除某个MAC表项:
protected void removeFromPortMap(IOFSwitch sw, MacAddress mac) {
Map<MacAddress, OFPort> swMap = macToSwitchPortMap.get(sw);
if (swMap != null) {
swMap.remove(mac);
}
}
getFromPortMap,clearLearnedTable,clearLearnedTable(IOFSwitch sw),writePacketOutForPacketIn略。
写一个flowmod(流表)到交换机
private void writeFlowMod(IOFSwitch sw, OFFlowModCommand command, OFBufferId bufferId,Match match, List<OFAction> al, OFPort outPort, short IdleTimeout, short HardTimeout) {
建立一个builder:
OFFlowMod.Builder fmb;
if (command == OFFlowModCommand.DELETE) { //build
fmb = sw.getOFFactory().buildFlowDelete();
} else {
fmb = sw.getOFFactory().buildFlowAdd();
}
各种设置:
fmb.setMatch(match);
fmb.setCookie((U64.of(Example1a.PROJECT2_COOKIE)));
fmb.setIdleTimeout(IdleTimeout);
fmb.setHardTimeout(HardTimeout);
fmb.setPriority(FLOWMOD_PRIORITY);
fmb.setBufferId(bufferId);
fmb.setOutPort((command == OFFlowModCommand.DELETE) ? OFPort.ANY : outPort);
Set<OFFlowModFlags> sfmf = new HashSet<OFFlowModFlags>();
if (command != OFFlowModCommand.DELETE) {
sfmf.add(OFFlowModFlags.SEND_FLOW_REM);
}
fmb.setFlags(sfmf);
fmb.setActions(al);
最后写:
sw.write(fmb.build());
处理packetin
private Command processPacketInMessage(IOFSwitch sw, OFPacketIn pi, FloodlightContext cntx) {
- 输出端口没有则泛洪;
- 输出端口等于输入端口则忽略;
- 正常情况:
建立OFPacketOut.builder pob,建立从pi的源端口到目的端口的packetout,直接write。
建立match.builder mb,匹配输入端口、源目MAC:
Match.Builder mb = sw.getOFFactory().buildMatch();
mb.setExact(MatchField.IN_PORT, inPort)
.setExact(MatchField.ETH_SRC, sourceMac)
.setExact(MatchField.ETH_DST, destMac);
Match match = mb.build();
创建动作列表,来把包转发到学到的输出端口:
List<OFAction> al = new ArrayList<OFAction>();
al.add(sw.getOFFactory().actions().buildOutput().setPort(outPort).setMaxLen(0xffFFffFF).build());
最后调用writeFlowMod写流表:
FLOWMOD_PRIORITY = 100;
this.writeFlowMod(sw, OFFlowModCommand.ADD, OFBufferId.NO_BUFFER, match, al, outPort, FLOWMOD_DEFAULT_IDLE_TIMEOUT, FLOWMOD_DEFAULT_HARD_TIMEOUT);
创建逆向的流表:
匹配:
Match.Builder mb1 = sw.getOFFactory().buildMatch();
mb1.setExact(MatchField.IN_PORT, outPort)
.setExact(MatchField.ETH_SRC, destMac)
.setExact(MatchField.ETH_DST, sourceMac);
Match match1 = mb1.build();
动作列表:
List<OFAction> al1 = new ArrayList<OFAction>();
al1.add(sw.getOFFactory().actions().buildOutput().setPort(inPort).setMaxLen(0xffFFffFF).build());
写流表:
FLOWMOD_PRIORITY = 100;
this.writeFlowMod(sw, OFFlowModCommand.ADD, OFBufferId.NO_BUFFER, match1, al1, inPort, FLOWMOD_DEFAULT_IDLE_TIMEOUT, FLOWMOD_DEFAULT_HARD_TIMEOUT);
处理一个流移除消息,会删除学到的MAC映射:
private Command processFlowRemovedMessage(IOFSwitch sw, OFFlowRemoved flowRemovedMessage)
从flowRemovedMessage中学到match:
Match match = flowRemovedMessage.getMatch();
从match中学到源目MAC:
MacAddress sourceMac = match.get(MatchField.ETH_SRC);
MacAddress destMac = match.get(MatchField.ETH_DST);
调用removeFromPortMap从macToSwitchPortMap中删除:
this.removeFromPortMap(sw, destMac);
从交换机中删除流表:
匹配:
Match.Builder mb = sw.getOFFactory().buildMatch();
mb.setExact(MatchField.ETH_SRC, destMac)
.setExact(MatchField.ETH_DST, sourceMac);
动作列表:
List<OFAction> al = new ArrayList<OFAction>();
al.add(sw.getOFFactory().actions().buildOutput().setPort(match.get(MatchField.IN_PORT)).setMaxLen(0xffFFffFF).build());
写删除流表:
this.writeFlowMod(sw, OFFlowModCommand.DELETE, OFBufferId.NO_BUFFER, mb.build(), al,match.get(MatchField.IN_PORT), FLOWMOD_DEFAULT_IDLE_TIMEOUT, FLOWMOD_DEFAULT_HARD_TIMEOUT);
Receive处理packetin和flowremoved两种消息:
case PACKET_IN:
return this.processPacketInMessage(sw, (OFPacketIn) msg, cntx);
case FLOW_REMOVED:
return this.processFlowRemovedMessage(sw, (OFFlowRemoved) msg);