private void Initialize(MemoryStream ms, OfpHeader header) { if (!_versionChecked) { _version = header.Version; _versionChecked = true; var reply = new OfpSwitchFeaturesRequest(); _log.Debug("Request for features"); //Console.WriteLine("Request for features"); Write(reply.ToByteArray()); } else if (header.Type == OfpType.OFPT_FEATURES_REPLY) { OfpSwitchFeatures features = new OfpSwitchFeatures(ms, header); _mac = features.DatapathId.GetPhysicalAddress(); _log.Info($"A switch connected - MAC={_mac}"); //Console.WriteLine($"A switch connected - MAC={_mac}"); foreach (var ofpPhyPort in features.Ports) { //Console.WriteLine($"\tPortNum={ofpPhyPort.PortNo} \tPortName={ofpPhyPort.Name} \tPortState={ofpPhyPort.State.ToString()}"); } SwitchFeatures = features; _featureChecked = true; var reply = new OfpSwitchConfig(true); reply.Flags = OfpConfigFlags.OFPC_FRAG_NORMAL; reply.MissSendLen = 65535; Write(reply.ToByteArray()); var reply2 = new OfpSwitchConfigRequest(); Write(reply2.ToByteArray()); } else if (header.Type == OfpType.OFPT_GET_CONFIG_REPLY) { OfpSwitchConfig config = new OfpSwitchConfig(ms, header); _log.Info($"[{_mac}] Config: Flag={config.Flags} MissSendLen={config.MissSendLen}"); //Console.WriteLine($"[{_mac}] Config: Flag={config.Flags} MissSendLen={config.MissSendLen}"); _configChecked = true; return; } else if (header.Type == OfpType.OFPT_ECHO_REQUEST) { OfpEcho echo = new OfpEcho(ms, header); _version = echo.Header.Version; _versionChecked = true; var reply = new OfpEcho(true); reply.Data = echo.Data; Write(reply.ToByteArray()); } else if (header.Type == OfpType.OFPT_ERROR) { OfpErrorMsg error = new OfpErrorMsg(ms, header); _log.Info($"[{_mac}] Error: type={error.Type.ToString()} code={error.GetErrorCode()}"); //Console.WriteLine($"[{_mac}] Error: type={error.Type.ToString()} code={error.GetErrorCode()}"); return; } else { return; } }
private void Features(MemoryStream ms, OfpHeader header) { if (header.Type == OfpType.OFPT_FEATURES_REQUEST) { return; } else { OfpSwitchFeatures features = new OfpSwitchFeatures(ms, header); _controller.PluginSystem.SwitchFeatures(features, this); } }
/// <summary> /// 处理交换机功能 /// </summary> /// <param name="features"></param> /// <param name="handler"></param> /// <returns></returns> public virtual bool SwitchFeatures(OfpSwitchFeatures features, IConnection handler) { foreach (var plugin in Plugins.Values.Where(plugin => plugin.Active)) { try { bool result = plugin.MessageHandler.SwitchFeatures(features, handler); if (result) { break; } } catch (Exception e) { Debug.WriteLine(e); } } return(true); }
public void AddSwitch(OfpSwitchFeatures switchFeatures, IConnection connection) { var name = switchFeatures.DatapathId.ToSwitchString(); var sw = new Switch(switchFeatures, connection); if (!_switches.ContainsKey(name)) { _switches.TryAdd(name, sw); } else { _switches[name] = sw; } if (!Adjacency.ContainsKey(name) || Adjacency[name] == null) { Adjacency[name] = new ConcurrentDictionary <string, Link>(); } _topoVer++; }
public void TestOfpSwitchFeatures() { OfpSwitchFeatures dst; OfpSwitchFeatures src = new OfpSwitchFeatures() { Actions = (OfpActionCapabilities)(1 << 12 - 1), Capabilities = OfpCapabilities.OFPC_TABLE_STATS | OfpCapabilities.OFPC_PORT_STATS, DatapathId = 1234567u, NBuffers = 10, NTables = 1, Ports = new List <OfpPhyPort>() { new OfpPhyPort() { Advertised = OfpPortFeatures.OFPPF_100MB_HD, State = OfpPortState.OFPPS_STP_LISTEN, Curr = OfpPortFeatures.OFPPF_100MB_FD, Peer = OfpPortFeatures.OFPPF_FIBER, Name = "TestPort", HwAddr = new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 }, Supported = OfpPortFeatures.OFPPF_1GB_HD, PortNo = (ushort)OfpPort.OFPP_FLOOD, Config = OfpPortConfig.OFPPC_NO_FLOOD }, new OfpPhyPort() { Advertised = OfpPortFeatures.OFPPF_100MB_HD, State = OfpPortState.OFPPS_STP_LISTEN, Curr = OfpPortFeatures.OFPPF_100MB_FD, Peer = OfpPortFeatures.OFPPF_FIBER, Name = "TestPort2", HwAddr = new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 }, Supported = OfpPortFeatures.OFPPF_1GB_HD, PortNo = (ushort)OfpPort.OFPP_FLOOD, Config = OfpPortConfig.OFPPC_NO_FLOOD } } }; using (MemoryStream ms = new MemoryStream(src.ToByteArray())) { dst = new OfpSwitchFeatures(ms); } Assert.AreEqual(src.Actions, dst.Actions); Assert.AreEqual(src.Capabilities, dst.Capabilities); Assert.AreEqual(src.DatapathId, dst.DatapathId); Assert.AreEqual(src.NBuffers, dst.NBuffers); Assert.AreEqual(src.NTables, dst.NTables); Assert.AreEqual(src.Ports[0].Name, dst.Ports[0].Name); Assert.AreEqual(src.Ports[1].Name, dst.Ports[1].Name); Assert.IsTrue(dst.Capabilities.HasFlag(OfpCapabilities.OFPC_PORT_STATS)); Assert.IsFalse(dst.Capabilities.HasFlag(OfpCapabilities.OFPC_RESERVED)); Assert.IsTrue(dst.Actions.HasFlag(OfpActionCapabilities.OFPAT_ENQUEUE)); }
/// <summary> /// 处理交换机功能 /// </summary> /// <param name="features"></param> /// <param name="handler"></param> /// <returns></returns> public virtual bool SwitchFeatures(OfpSwitchFeatures features, IConnection handler) { return(false); }
public Switch(OfpSwitchFeatures features, IConnection connection) { Features = features; Connection = connection; }