From 2281b7a6aeb13bc3b10f1b77fbc7754769911c8f Mon Sep 17 00:00:00 2001 From: Takeshi Date: Tue, 18 Aug 2015 16:54:48 +0800 Subject: Add EventHostAdd event. This event is generated when a new host is added to a switch. Signed-off-by: Takeshi Signed-off-by: FUJITA Tomonori --- ryu/topology/event.py | 14 ++++++++++++++ ryu/topology/switches.py | 15 ++++++++++----- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/ryu/topology/event.py b/ryu/topology/event.py index 83881c00..6afbe78f 100644 --- a/ryu/topology/event.py +++ b/ryu/topology/event.py @@ -149,3 +149,17 @@ class EventHostReply(event.EventReplyBase): def __str__(self): return 'EventHostReply' % \ (self.dst, self.dpid, len(self.hosts)) + + +class EventHostBase(event.EventBase): + def __init__(self, host): + super(EventHostBase, self).__init__() + self.host = host + + def __str__(self): + return '%s<%s>' % (self.__class__.__name__, self.host) + + +class EventHostAdd(EventHostBase): + def __init__(self, host): + super(EventHostAdd, self).__init__(host) diff --git a/ryu/topology/switches.py b/ryu/topology/switches.py index 31b4b0c8..382e5554 100644 --- a/ryu/topology/switches.py +++ b/ryu/topology/switches.py @@ -496,7 +496,8 @@ class Switches(app_manager.RyuApp): _EVENTS = [event.EventSwitchEnter, event.EventSwitchLeave, event.EventPortAdd, event.EventPortDelete, event.EventPortModify, - event.EventLinkAdd, event.EventLinkDelete] + event.EventLinkAdd, event.EventLinkDelete, + event.EventHostAdd] DEFAULT_TTL = 120 # unused. ignored. LLDP_PACKET_LEN = len(LLDPPacket.lldp_packet(0, 0, DONTCARE_STR, 0)) @@ -841,18 +842,22 @@ class Switches(app_manager.RyuApp): host_mac = eth.src host = Host(host_mac, port) - # arp packet, update both location and ip - if eth.ethertype == ether_types.ETH_TYPE_ARP: + if host_mac not in self.hosts: self.hosts.add(host) + ev = event.EventHostAdd(host) + self.send_event_to_observers(ev) + + # arp packet, update ip address + if eth.ethertype == ether_types.ETH_TYPE_ARP: arp_pkt = pkt.get_protocols(arp.arp)[0] self.hosts.update_ip(host, ip_v4=arp_pkt.src_ip) - # ipv4 packet, update ip only + # ipv4 packet, update ipv4 address elif eth.ethertype == ether_types.ETH_TYPE_IP: ipv4_pkt = pkt.get_protocols(ipv4.ipv4)[0] self.hosts.update_ip(host, ip_v4=ipv4_pkt.src) - # ipv6 packet, update ip only + # ipv6 packet, update ipv6 address elif eth.ethertype == ether_types.ETH_TYPE_IPV6: # TODO: need to handle NDP ipv6_pkt = pkt.get_protocols(ipv6.ipv6)[0] -- cgit v1.2.3