xref: /illumos-gate/usr/src/cmd/xvm/ipagent/ipagent.ksh (revision 726fad2a65f16c200a03969c29cb5c86c2d427db)
1#!/bin/ksh
2#
3# CDDL HEADER START
4#
5# The contents of this file are subject to the terms of the
6# Common Development and Distribution License (the "License").
7# You may not use this file except in compliance with the License.
8#
9# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10# or http://www.opensolaris.org/os/licensing.
11# See the License for the specific language governing permissions
12# and limitations under the License.
13#
14# When distributing Covered Code, include this CDDL HEADER in each
15# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16# If applicable, add the following below this CDDL HEADER, with the
17# fields enclosed by brackets "[]" replaced with your own identifying
18# information: Portions Copyright [yyyy] [name of copyright owner]
19#
20# CDDL HEADER END
21#
22
23# Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24# Use is subject to license terms.
25
26#
27# xVM PV domU IP address reporting agent. Reports IP address back to dom0.
28#
29
30interval=$1
31
32xs_ipaddr_path="guest/ipv4/0/address"
33xs_link_path="guest/ipv4/default-link"
34link=""
35
36#
37# Look for a valid-seeming address for the given link. Return 0 on success.
38#
39link_to_addr()
40{
41	tmp=`netstat -I $1 -in -f inet | awk '{print $4}' | grep -v Address`;
42	if [ -z "$tmp" ] || [ "$tmp" = "0.0.0.0" ];
43	then
44		addr="(none)";
45		return 1;
46	fi
47
48	addr=$tmp;
49	return 0;
50}
51
52default_link()
53{
54	#
55	# Look in the store for a cached link name.
56	#
57	link=`/usr/lib/xen/bin/xenstore-read $xs_link_path 2>/dev/null`
58	if [ -z "$link" ] || [ "$link" = "(none)" ]
59	then
60		#
61		# If it's not there, try to determine what it is
62		# and add it to the store.
63		determine_default_link
64	fi
65}
66
67#
68# Determine the default link name and update xenstore with the details.
69#
70determine_default_link()
71{
72	link="(none)";
73	#
74	# Choose the first up, non-loopback interface with a valid-looking
75	# IP address.
76	#
77	dladm show-link -p -o link,state | while IFS=: read LINKNAME STATE;
78	do
79		if [ "$STATE" = "up" ];
80		then
81			link_to_addr "$LINKNAME"
82			if [ $? -eq 0 ]; then link=$LINKNAME; break; fi
83		fi
84
85	done
86
87	/usr/lib/xen/bin/xenstore-write $xs_link_path $link
88}
89
90while true; do
91
92	#
93	# Determine the default link in use by this domU.
94	#
95	default_link;
96
97	#
98	# If the link still has a valid-looking IP address, notify dom0 of its
99	# address.
100	#
101	link_to_addr $link
102	if [ $? -ne 0 ]
103	then
104		#
105		# An address could not be determined for the currently cached
106		# default link so determine it again in case it has changed.
107		# We'll still sleep this iteration to rate-limit dladm calls.
108		#
109		determine_default_link;
110	fi
111
112	/usr/lib/xen/bin/xenstore-write $xs_ipaddr_path $addr
113
114	sleep $interval
115done
116
117