This event is generated when a new host is added to a switch.

Please check attached file for more detail.

-- 
Yi Tseng (a.k.a Takeshi)
Taiwan National Chiao Tung University
Department of Computer Science
W2CNLab

http://blog.takeshi.tw
From f5ec915cf6c7aa3174875647b87e5e5de069fecf Mon Sep 17 00:00:00 2001
From: Takeshi <[email protected]>
Date: Tue, 18 Aug 2015 16:54:48 +0800
Subject: [PATCH 1/2] Add EventHostAdd event.

This event is generated when a new host is added to a switch.

Signed-off-by: Takeshi <[email protected]>
---
 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 83881c0..6afbe78 100644
--- a/ryu/topology/event.py
+++ b/ryu/topology/event.py
@@ -149,3 +149,17 @@ class EventHostReply(event.EventReplyBase):
     def __str__(self):
         return 'EventHostReply<dst=%s, dpid=%s, hosts=%s>' % \
             (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 31b4b0c..382e555 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]
-- 
2.3.2 (Apple Git-55)

------------------------------------------------------------------------------
_______________________________________________
Ryu-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ryu-devel

Reply via email to