Example #1
0
func (manager *LinkManager) netlinkMessageReceived(msg *netlink.Message) {
	switch msg.Header.MessageType() {
	case rtnetlink.RTM_NEWLINK, rtnetlink.RTM_GETLINK:
		rtmsg, err := link.ParseMessage(*msg)
		if err != nil {
			log.Printf("Can't parse link message: %v", err)
			break
		}
		hdr, _ := rtmsg.Header.(*link.Header)
		ifIndex := hdr.InterfaceIndex()
		link := manager.links[ifIndex]
		if link == nil {
			link := &LinkHandler{cache: rtmsg, addresses: make([]*net.IP, 0, 5)}
			manager.links[hdr.InterfaceIndex()] = link
			manager.interfaceChanged(rtmsg, link, true)
		} else {
			link.cache = rtmsg
			manager.interfaceChanged(rtmsg, link, false)
		}
	case rtnetlink.RTM_DELLINK:
		manager.linkRemoved(msg)
	case rtnetlink.RTM_NEWADDR, rtnetlink.RTM_GETADDR:
		manager.addrReceived(msg)
	case rtnetlink.RTM_DELADDR:
		manager.addressRemoved(msg)
	case rtnetlink.RTM_NEWROUTE, rtnetlink.RTM_GETROUTE:
		manager.routeAdded(msg)
	case netlink.NLMSG_UNSPECIFIED:
		log.Printf("Received unspecified message. Header: %v Body %v", msg.Header, msg.Body)
	case netlink.NLMSG_DONE:
		// DO nothing with this.
	}
}
Example #2
0
func (manager *LinkManager) linkRemoved(msg *netlink.Message) {
	rtmsg, err := link.ParseMessage(*msg)
	hdr, _ := rtmsg.Header.(*link.Header)
	if err != nil {
		log.Printf("Can't unmarshall rtnetlink message: %v", err)
	}
	link := manager.links[hdr.InterfaceIndex()]
	delete(manager.links, hdr.InterfaceIndex())
	env := manager.createEmitEnv(link)
	manager.upstartController.Emit("net-device-removed", env, false)
}